nish 0.42.0 → 0.44.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nish",
3
- "version": "0.42.0",
3
+ "version": "0.44.0",
4
4
  "description": "Nish: an ahead-of-time compiler from a static subset of TypeScript to LLVM IR",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -64,15 +64,15 @@
64
64
  "postinstall": "node scripts/postinstall.mjs",
65
65
  "bootstrap": "bash scripts/bootstrap.sh",
66
66
  "compile": "build/nish",
67
- "example": "build/nish examples/add.ts -o build/add.ll && cat build/add.ll",
67
+ "example": "build/nish examples/add.ts --allow-panics -o build/add.ll && cat build/add.ll",
68
68
  "test": "node tests/run.js",
69
- "size-report": "build/nish examples/add.ts -o build/add.ll && bash scripts/size-report.sh build/add.ll examples/main.c",
69
+ "size-report": "build/nish examples/add.ts --allow-panics -o build/add.ll && bash scripts/size-report.sh build/add.ll examples/main.c",
70
70
  "smoke": "bash scripts/smoke.sh",
71
71
  "check": "tsc -p tsconfig.json",
72
72
  "test:update": "UPDATE_GOLDENS=1 node tests/run.js",
73
73
  "test:diff": "node tests/differential/run.js",
74
- "test:nish": "build/nish tests/nish/run.ts -o build/nish-runner.ir/ --link build/nish-runner && build/nish-runner",
75
- "test:cli": "build/nish tests/nish/cli.ts -o build/nish-cli.ir/ --link build/nish-cli && build/nish-cli",
74
+ "test:nish": "bash scripts/nish-harness.sh tests/nish/run.ts build/nish-runner",
75
+ "test:cli": "bash scripts/nish-harness.sh tests/nish/cli.ts build/nish-cli",
76
76
  "test:node": "node tests/differential/unmodified.js",
77
77
  "lint": "node scripts/check-filenames.mjs && node scripts/check-install-scripts.mjs && biome check .",
78
78
  "lint:fix": "biome check --write .",
@@ -114,9 +114,9 @@
114
114
  "typescript": "^5.6.0"
115
115
  },
116
116
  "optionalDependencies": {
117
- "@amritk/nish-aarch64-darwin": "0.42.0",
118
- "@amritk/nish-aarch64-linux": "0.42.0",
119
- "@amritk/nish-x86_64-darwin": "0.42.0",
120
- "@amritk/nish-x86_64-linux": "0.42.0"
117
+ "@amritk/nish-aarch64-darwin": "0.44.0",
118
+ "@amritk/nish-aarch64-linux": "0.44.0",
119
+ "@amritk/nish-x86_64-darwin": "0.44.0",
120
+ "@amritk/nish-x86_64-linux": "0.44.0"
121
121
  }
122
122
  }
package/runtime/core.d.ts CHANGED
@@ -8,6 +8,22 @@ export * from "./core/numbers.js";
8
8
  export * from "./core/result.js";
9
9
  export * from "./core/strings.js";
10
10
 
11
+ // `nish:compact`: what `--emit-ts` writes for a program's `import ... from "nish:compact"`.
12
+
13
+ /**
14
+ * The marker a class implements to store its links as 32-bit offsets in a
15
+ * native build. It has no members, so `implements Compact` adds nothing a
16
+ * TypeScript checker sees.
17
+ */
18
+ export interface Compact {}
19
+ /** The value `core.mjs` exports under the marker's name, so an emitted import of it resolves. */
20
+ export declare const Compact: Compact;
21
+
22
+ /** Reserved for WP48 stage 3, which lets a class refuse the compiler's choice. */
23
+ export interface Wide {}
24
+ /** The value `core.mjs` exports under the reserved marker's name. */
25
+ export declare const Wide: Wide;
26
+
11
27
  // `nish:simd`: what `--emit-ts` writes for a program's `import ... from "nish:simd"`.
12
28
  import type { f32, f64, i32, u16, u32, u8 } from "./core/numbers.js";
13
29
 
package/runtime/core.mjs CHANGED
@@ -84,6 +84,11 @@ const simdLoad = (Lanes) => (xs, i) => {
84
84
  return out;
85
85
  };
86
86
 
87
+ // `nish:compact`: the marker interfaces pick a native layout and mean nothing
88
+ // under Node, where every reference is the engine's own.
89
+ export const Compact = Object.freeze({});
90
+ export const Wide = Object.freeze({});
91
+
87
92
  export const u8x16 = simdMake(Uint8Array);
88
93
  export const u16x8 = simdMake(Uint16Array);
89
94
  export const i32x4 = simdMake(Int32Array);
package/runtime/nish.d.ts CHANGED
@@ -712,8 +712,8 @@ declare function isExecutableSync(path: string): boolean;
712
712
  // refusal reads `` `uncheckedGet` is a `nish:unsafe` function and is called only
713
713
  // through an import of it `` (NL2456). An import may rename (`as`) and cannot be
714
714
  // shadowed; a user function of the same name, with no import, is the user's.
715
- // Every import and call is a recorded fact in `--emit-checked`. Under
716
- // `--deny-panics` or `"nish".noPanic` the import is the opt-in the scope takes.
715
+ // Every import and call is a recorded fact in `--emit-checked`. In
716
+ // the no-panic scope, the default, the import is the opt-in the scope takes.
717
717
 
718
718
  /**
719
719
  * `xs[i]` with no bounds check: undefined behaviour when `i` is out of range,
@@ -726,7 +726,7 @@ declare function isExecutableSync(path: string): boolean;
726
726
  *
727
727
  * To the bounds proof it is the load `xs[i]` is, less the check: it moves no
728
728
  * length, and it records no passed check, so a checked `xs[i]` after it keeps
729
- * its check. Under `--deny-panics` it is an allowed `unchecked` site. Under
729
+ * its check. In the no-panic scope it is an allowed `unchecked` site. Under
730
730
  * Node the runtime shim reads `xs[i]`. Capability: unsafe.
731
731
  *
732
732
  * @param xs the array of numbers to read.
@@ -746,7 +746,7 @@ declare function uncheckedGet<T extends number>(xs: readonly T[], i: i32): T;
746
746
  * The store is a write of the array for every rule that asks, the rule for a
747
747
  * parallel body included. To the bounds proof it is the store `xs[i] = v` is,
748
748
  * less the check, and a resize in the value stored still takes the length away.
749
- * Under `--deny-panics` it is an allowed `unchecked` site. Under Node the
749
+ * In the no-panic scope it is an allowed `unchecked` site. Under Node the
750
750
  * runtime shim stores `xs[i] = v`. Capability: unsafe.
751
751
  *
752
752
  * @param xs the array of numbers to write.
@@ -766,7 +766,7 @@ declare function uncheckedSet<T extends number>(xs: T[], i: i32, v: T): void;
766
766
  *
767
767
  * In a module constant's initialiser the call folds, wrapping, where
768
768
  * `2147483647 + 1` is refused; it is the one call such an initialiser may make.
769
- * It is not a site under `--deny-panics`, as it cannot panic. Under Node the
769
+ * It is not a site in the no-panic scope, as it cannot panic. Under Node the
770
770
  * runtime shim computes `(a + b) | 0` on an `i32` and `BigInt.asIntN(64, ...)`
771
771
  * on the BigInt an `i64` is there. Capability: unsafe.
772
772
  *
@@ -813,7 +813,7 @@ declare function wrappingMul<T extends i32 | i64>(a: T, b: T): T;
813
813
  * The copy is new on every call and is the calling function's allocation, and
814
814
  * it is refused inside a `using a = arena()` block that would release one held
815
815
  * outside it (`` `readCString(p)` may be allocated inside the `using arena()`
816
- * block ``). Under `--deny-panics` its `NULL` test is an `ffi-null` site no
816
+ * block ``). In the no-panic scope its `NULL` test is an `ffi-null` site no
817
817
  * proof removes, so the scope refuses the call (the copy's `oom` beside it is
818
818
  * allowed), and the pointer it reads is `ffi`. It lowers the same
819
819
  * on wasm32, where whether the program links is the `declare function`'s
@@ -842,7 +842,7 @@ declare function readCString(p: CPtr): string;
842
842
  *
843
843
  * A `NULL` `p` is tested before the copy and panics with the `ffi-null` kind,
844
844
  * naming the layout, since a `declare function` declared to answer `CPtr` may
845
- * still answer `NULL`. No proof removes that test, so `--deny-panics` refuses
845
+ * still answer `NULL`. No proof removes that test, so the no-panic scope refuses
846
846
  * the call; `--emit-panics` lists it as an `ffi-null` site, with the copy's
847
847
  * `oom` beside it.
848
848
  *
@@ -1737,9 +1737,14 @@ declare function netWrite2(
1737
1737
  * Shut one or both directions of a connection. Not callable in a `scope()` task or a parallel body.
1738
1738
  * Refused at compile time on wasm32 and the WASI profile (NL2404). Capability: net.
1739
1739
  *
1740
+ * On a connection a `NET_LISTEN_RING` listener accepted, the shutdown goes behind the
1741
+ * bytes already written, so the peer reads them and then the end of the stream; a later
1742
+ * `netWrite` answers -32 once the write side is shut, and `netRead` answers what is queued
1743
+ * and then 0 once the read side is.
1744
+ *
1740
1745
  * @param fd the socket.
1741
1746
  * @param how the read side (0), the write side (1) or both (2).
1742
- * @returns 0, or a negative errno; -22 for any other `how`.
1747
+ * @returns 0, or a negative errno; -22 for any other `how`, -9 for a closed ring connection.
1743
1748
  */
1744
1749
  declare function netShutdown(fd: i32, how: i32): i32;
1745
1750
  /**
@@ -2006,7 +2011,8 @@ declare function netSetOption(fd: i32, option: i32, value: i32): i32;
2006
2011
  * @param fd the socket.
2007
2012
  * @returns the MSS; -1, never an errno, for anything else (a descriptor that is not open, a
2008
2013
  * socket with no peer such as a listener or one never connected, though the kernel answers
2009
- * its default `536` for those, a socket that is not TCP), and always on wasm.
2014
+ * its default `536` for those, a socket that is not TCP, a connection a `NET_LISTEN_RING`
2015
+ * listener accepted, whose ring cannot ask for it), and always on wasm.
2010
2016
  */
2011
2017
  declare function netMss(fd: i32): i32;
2012
2018
  /**
@@ -3246,11 +3252,14 @@ declare module "nish:net" {
3246
3252
  bLen: i32
3247
3253
  ): i32;
3248
3254
  /**
3249
- * Shut one side of a connection, or both.
3255
+ * Shut one side of a connection, or both. On a connection a
3256
+ * `NET_LISTEN_RING` listener accepted, the shutdown goes behind the bytes
3257
+ * already written, so the peer reads them and then the end of the stream.
3250
3258
  *
3251
3259
  * @param fd A connected socket.
3252
3260
  * @param how `0` the read side, `1` the write side, `2` both.
3253
- * @returns `0`, `-22` for any other `how`, or the failure.
3261
+ * @returns `0`, `-22` for any other `how`, `-9` for a closed ring
3262
+ * connection, or the failure.
3254
3263
  */
3255
3264
  export function netShutdown(fd: i32, how: i32): i32;
3256
3265
  /**
@@ -3445,8 +3454,9 @@ declare module "nish:net" {
3445
3454
  * @returns The size, or a plain `-1`, never an errno, for anything else: a
3446
3455
  * descriptor that is not open, a socket with no peer (a listener, or one
3447
3456
  * never connected, though the kernel answers its default `536` for
3448
- * those), a socket that is not TCP, and every call on wasm, where it
3449
- * compiles to the constant with the descriptor still evaluated. It throws
3457
+ * those), a socket that is not TCP, a connection a `NET_LISTEN_RING`
3458
+ * listener accepted, whose ring cannot ask for it, and every call on
3459
+ * wasm, where it compiles to the constant with the descriptor still evaluated. It throws
3450
3460
  * under Node.
3451
3461
  */
3452
3462
  export function netMss(fd: i32): i32;
@@ -3642,7 +3652,7 @@ declare module "nish:net" {
3642
3652
  * `(a - b) | 0` and `Math.imul` on an `i32` and `BigInt.asIntN(64, …)` on an
3643
3653
  * `i64`. A program that stays in range prints the same as natively.
3644
3654
  *
3645
- * **The no-panic scope.** Under `--deny-panics` or `"nish".noPanic`,
3655
+ * **The no-panic scope.** In the scope, every module of the package by default,
3646
3656
  * `uncheckedGet` and `uncheckedSet` are allowed `unchecked` sites, because
3647
3657
  * the import has already said that an index out of range is the author's to
3648
3658
  * rule out. `readCString` and `readStruct` are not: each tests its pointer and
@@ -3742,6 +3752,52 @@ declare module "nish:unsafe" {
3742
3752
  export function cErrno(): i32;
3743
3753
  }
3744
3754
 
3755
+ /**
3756
+ * `nish:compact`: the marker interfaces that pick a class's layout (WP48).
3757
+ * `class R implements Compact` stores every field typed exactly `R` or
3758
+ * `R | null`, in any class, as a 32-bit offset into one reserved address range,
3759
+ * so a tree or list node of an `i32` and two links is 12 bytes, not 24. Locals,
3760
+ * parameters, returns, arrays, maps and other unions stay full pointers.
3761
+ *
3762
+ * A native `--link` build also compacts, with no marker, each class whose
3763
+ * links take at least a quarter off a class made in a loop or recursion
3764
+ * (`--explain-layout` prints each pick and why the others are left alone).
3765
+ * `implements Wide` keeps one class out of that, and `--compact off` keeps
3766
+ * every class, `implements Compact` included, at full pointers.
3767
+ *
3768
+ * A `--emit-header` build keeps a compact class, and every class holding one
3769
+ * of its links, opaque: C holds it as a full `struct X *` handle and links the
3770
+ * range runtime (`scripts/build.sh --arena-range`). The compiler refuses a
3771
+ * compact class where Node or the runtime would read its layout: a build with
3772
+ * `--emit-napi` or `--emit-napi-async` (also beside `--emit-header`), an
3773
+ * N-API export whose signature holds one, a `declare class`, and a class
3774
+ * holding a `Secret`. wasm32, a host `--emit-dts` page module and an ASAN build compile
3775
+ * it uncompressed: a pointer there is already 4 bytes, the page runs on the
3776
+ * wasm runtime, or the range cannot hold the sanitizer's blocks.
3777
+ *
3778
+ * **Under Node.** `runtime/nish.mjs` answers the import with
3779
+ * `runtime/core.mjs`, where both names are empty values: a reference is the
3780
+ * engine's own and the marker changes nothing a program prints.
3781
+ *
3782
+ * Rules: https://github.com/amritk/nish/blob/main/docs/LANGUAGE.md#compact-classes
3783
+ */
3784
+ declare module "nish:compact" {
3785
+ /**
3786
+ * The marker a class implements to store its links as 32-bit offsets. It
3787
+ * has no members, so `implements Compact` adds no field.
3788
+ */
3789
+ export interface Compact {}
3790
+
3791
+ /**
3792
+ * The marker a class implements to keep its links full pointers. The
3793
+ * compiler compacts a class on its own when its rule finds the saving pays
3794
+ * (`--explain-layout` names each pick); `implements Wide` refuses that
3795
+ * pick, the way `--compact off` refuses every one. It has no members, so it
3796
+ * adds no field, and a class cannot write it with `Compact`.
3797
+ */
3798
+ export interface Wide {}
3799
+ }
3800
+
3745
3801
  /**
3746
3802
  * Sixteen-byte vectors of one lane type, and the lane-wise operations on them,
3747
3803
  * each lowered to target-independent LLVM vector IR.
@@ -4999,15 +5055,15 @@ declare module "nish:region" {
4999
5055
  *
5000
5056
  * import { awaitAll, spawn } from "nish:async";
5001
5057
  *
5002
- * async function double(x: i32): Promise<i32> {
5058
+ * const double = async (x: i32): Promise<i32> => {
5003
5059
  * return x * 2;
5004
5060
  * }
5005
5061
  *
5006
- * async function log(x: i32): Promise<void> {
5062
+ * const log = async (x: i32): Promise<void> => {
5007
5063
  * console.log(x);
5008
5064
  * }
5009
5065
  *
5010
- * async function run(): Promise<void> {
5066
+ * const run = async (): Promise<void> => {
5011
5067
  * const [c, d] = await awaitAll(double(1), double(2));
5012
5068
  * spawn(log(c + d));
5013
5069
  * }
@@ -5024,7 +5080,7 @@ declare module "nish:region" {
5024
5080
  *
5025
5081
  * import { readable, sleep } from "nish:async";
5026
5082
  *
5027
- * async function tick(): Promise<void> {
5083
+ * const tick = async (): Promise<void> => {
5028
5084
  * await sleep(10);
5029
5085
  * console.log("tick");
5030
5086
  * }
@@ -5871,6 +5927,22 @@ interface Array<T> {
5871
5927
  * @returns the first such index, or `-1`.
5872
5928
  */
5873
5929
  findIndex(predicate: (value: T, index: number) => boolean): number;
5930
+ /**
5931
+ * Call `callbackfn` with each element in order, as `Array.prototype.forEach`
5932
+ * does, up to the length the array had when the call began: an element the
5933
+ * callback pushes is not visited, and one that shrinks the array ends the
5934
+ * walk at its new length. The call answers `void`, and so does the
5935
+ * callback; what a concise arrow body computes is dropped. The callback takes the element, or the element and its
5936
+ * index; one that declares a third parameter, JavaScript's array, is refused
5937
+ * (NL2698). It is `map`'s callback otherwise: a top-level function or an arrow
5938
+ * written at the call, which may not assign a name of the function around it
5939
+ * (NL2586), so a running total is a `for...of` loop. The call compiles to a
5940
+ * counted loop with the callback inlined (`std/array`'s `forEach`, or
5941
+ * `forEachIndexed` for a two-parameter callback). Capability: none.
5942
+ *
5943
+ * @param callbackfn called once per element, with the element and its index.
5944
+ */
5945
+ forEach(callbackfn: (value: T, index: number) => void): void;
5874
5946
  /**
5875
5947
  * A copy of the elements from `start` up to `end`, in a new array, with
5876
5948
  * string `slice`'s rule: the range must satisfy `0 <= start <= end <=
@@ -5895,7 +5967,7 @@ interface Array<T> {
5895
5967
  * nish_array *`.
5896
5968
  *
5897
5969
  * Everything that reads works (`length`, `a[i]`, `for...of`, `indexOf`, `join`,
5898
- * `map`, `filter`, `some`, `every`, `findIndex`, `slice`, passing it on to
5970
+ * `map`, `filter`, `some`, `every`, `findIndex`, `forEach`, `slice`, passing it on to
5899
5971
  * another `readonly T[]`); every write is refused under its own name, an
5900
5972
  * element store ("Cannot assign to an element of readonly i32[]
5901
5973
  * (declare it i32[] to write through it)"), `push`, `pop`, `set`, `fill` and
@@ -5981,6 +6053,22 @@ interface ReadonlyArray<T> {
5981
6053
  * @returns the first such index, or `-1`.
5982
6054
  */
5983
6055
  findIndex(predicate: (value: T, index: number) => boolean): number;
6056
+ /**
6057
+ * Call `callbackfn` with each element in order, as `Array.prototype.forEach`
6058
+ * does, up to the length the array had when the call began: an element the
6059
+ * callback pushes is not visited, and one that shrinks the array ends the
6060
+ * walk at its new length. The call answers `void`, and so does the
6061
+ * callback; what a concise arrow body computes is dropped. The callback takes the element, or the element and its
6062
+ * index; one that declares a third parameter, JavaScript's array, is refused
6063
+ * (NL2698). It is `map`'s callback otherwise: a top-level function or an arrow
6064
+ * written at the call, which may not assign a name of the function around it
6065
+ * (NL2586), so a running total is a `for...of` loop. The call compiles to a
6066
+ * counted loop with the callback inlined (`std/array`'s `forEach`, or
6067
+ * `forEachIndexed` for a two-parameter callback). Capability: none.
6068
+ *
6069
+ * @param callbackfn called once per element, with the element and its index.
6070
+ */
6071
+ forEach(callbackfn: (value: T, index: number) => void): void;
5984
6072
  /**
5985
6073
  * A copy of the elements from `start` up to `end`, in a new array, with
5986
6074
  * string `slice`'s rule: the range must satisfy `0 <= start <= end <=
@@ -6247,6 +6335,19 @@ interface Set<T> {
6247
6335
  delete(value: T): boolean;
6248
6336
  /** Remove every member. Capability: none. */
6249
6337
  clear(): void;
6338
+ /**
6339
+ * Call `callbackfn` with each member in insertion order, as
6340
+ * `Set.prototype.forEach` does: the `for...of` walk over the set with the
6341
+ * callback inlined (`std/collections`' `forEachElement`), so a member added
6342
+ * during the walk is visited and one deleted before the walk reaches it is
6343
+ * not. The callback takes the member alone; JavaScript passes it again as a
6344
+ * second argument and the set as a third, and a callback that declares a
6345
+ * second parameter is refused (NL2772). The callback rules are those of an
6346
+ * array's `forEach`. Capability: none.
6347
+ *
6348
+ * @param callbackfn called once per member, with the member.
6349
+ */
6350
+ forEach(callbackfn: (value: T) => void): void;
6250
6351
  }
6251
6352
 
6252
6353
  // ---- What this file cannot say ----------------------------------------------
package/runtime/nish.mjs CHANGED
@@ -345,6 +345,8 @@ provide("arena", shim.arena);
345
345
  // `nish:simd` is `runtime/core.mjs`, whose vectors are typed arrays made once
346
346
  // and never written after, so `--emit-ts` output and a program run as it was
347
347
  // written share one implementation.
348
+ // `nish:compact` is `runtime/core.mjs` too: its two marker interfaces pick a
349
+ // native layout and are empty values under Node.
348
350
  const UNSAFE_EXPORTS = ["uncheckedGet", "uncheckedSet", "wrappingAdd", "wrappingSub", "wrappingMul"];
349
351
  const unsafeSource = UNSAFE_EXPORTS.map((name) => `export const ${name} = globalThis.${name};`).join("\n");
350
352
  // `nish:region` has no allocator to swap under Node: a region is an entry in a
@@ -442,7 +444,7 @@ registerHooks({
442
444
  if (specifier === "nish:unsafe") {
443
445
  return { url: `data:text/javascript,${encodeURIComponent(unsafeSource)}`, shortCircuit: true };
444
446
  }
445
- if (specifier === "nish:simd") {
447
+ if (specifier === "nish:simd" || specifier === "nish:compact") {
446
448
  return next(new URL("./core.mjs", import.meta.url).href, context);
447
449
  }
448
450
  if (specifier === "nish:async") {
@@ -216,6 +216,8 @@ void *nish_cross_keep(void *v, uint64_t esize, uint64_t mark) {
216
216
  if (!x) {
217
217
  v = nish_cross_copy(v, esize);
218
218
  } else {
219
+ /* `x->off` needs no realigning under NISH_ARENA_RANGE: only this
220
+ code bumps a transfer block, by `nish_cross_size`'s 8-byte multiples. */
219
221
  uint64_t n = nish_cross_size(v, esize);
220
222
  if (!x->block || x->block->cap - x->off < n) {
221
223
  uint64_t want = x->block && x->block->cap < 32768 ? x->block->cap * 2 : x->block ? 65536 : 4096;
@@ -243,7 +245,14 @@ void *nish_cross_in(void *p, uint64_t esize) {
243
245
  void *v = (void *)((uintptr_t)p & ~(uintptr_t)1);
244
246
  nish_xchunk *c = (nish_xchunk *)v - 1;
245
247
  uint64_t n = c->cap;
248
+ #ifdef NISH_ARENA_RANGE
249
+ /* What `nish_alloc_struct` would have left: it starts at `off` rounded up
250
+ to 8 (runtime.c gives the reason). */
251
+ uint64_t at = (nish_arena.off + 7) & ~(uint64_t)7;
252
+ if (n >= 65536 && (at > nish_arena.cap || n > nish_arena.cap - at)) {
253
+ #else
246
254
  if (n >= 65536 && n > nish_arena.cap - nish_arena.off) {
255
+ #endif
247
256
  nish_cross_link(c);
248
257
  return v;
249
258
  }
@@ -250,10 +250,13 @@ int32_t nish_net_address(nish_array *out, const nish_str *host, int32_t port) {
250
250
  keeps every handle below 2^31; their ids are not reused, so a closed
251
251
  listener's handles answer -9 for good.
252
252
 
253
- Every call that takes a handle and is not one of the slot paths here
254
- (`sendFd`, `netShutdown`, `connectResult`, `peerCredentials`, ...) passes it
255
- to the kernel as a descriptor, which no process holds at 2^20 or above, so
256
- it answers -9 (EBADF) with nothing to check.
253
+ `netShutdown` queues an io_uring SHUTDOWN behind the slot's sends, so the
254
+ peer reads every byte written before it and then the FIN (NURING-39);
255
+ `netMss` answers -1, its "not known", for a slot. Every other call that
256
+ takes a handle and is not one of the slot paths here (`sendFd`,
257
+ `connectResult`, `peerCredentials`, ...) passes it to the kernel as a
258
+ descriptor, which no process holds at 2^20 or above, so it answers -9
259
+ (EBADF) with nothing to check.
257
260
 
258
261
  Readiness keeps the descriptor contract: a read with nothing queued, a write
259
262
  to a full send area and an accept with nothing accepted answer -11, and the
@@ -291,6 +294,7 @@ int32_t nish_uring_sleep_ms(const struct nish_uring_set *s, int32_t timeout_ms);
291
294
  int32_t nish_uring_wait(struct nish_uring_set *s, int32_t timeout_ms, struct nish_uring_event *ev, int32_t cap);
292
295
  int32_t nish_uring_send(struct nish_uring_set *s, int32_t ring, int32_t slot, const uint8_t *p, int32_t n);
293
296
  int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot);
297
+ int32_t nish_uring_shutdown(struct nish_uring_set *s, int32_t ring, int32_t slot, int32_t how);
294
298
  int64_t nish_uring_queued(const struct nish_uring_set *s, int32_t ring, int32_t slot);
295
299
  int32_t nish_uring_full(const struct nish_uring_set *s);
296
300
  int32_t nish_uring_hold(struct nish_uring_set *s, int32_t ring, int32_t slot, int32_t on);
@@ -305,6 +309,9 @@ void nish_uring_destroy(struct nish_uring_set *s);
305
309
  /* What a held slot's queue may still take from the kernel before the cancel
306
310
  lands; past it the connection ends with -105 (ENOBUFS). */
307
311
  #define NR_RECV_MAX (16 * 1024 * 1024)
312
+ /* A slot's first receive queue, doubled as it fills: small enough for
313
+ glibc's thread cache, and freed once drained (NURING-38). */
314
+ #define NR_RECV_FIRST 1024
308
315
  /* The most slots per ring: a table is the kernel's memory, sized up front. */
309
316
  #define NR_SLOTS_MAX 16384
310
317
  #define NR_EVENTS 256
@@ -320,11 +327,12 @@ struct nr_vent {
320
327
 
321
328
  /* One slot's connection. `gen` counts the slot's closes; `lingering` is a
322
329
  close the program made while bytes were still queued, which ends when they
323
- are sent, the peer goes or a send fails. */
330
+ are sent, the peer goes or a send fails. `wshut` is a shutdown of the
331
+ write side, after which a write answers -32 as send's EPIPE would. */
324
332
  struct nr_conn {
325
333
  struct nr_vent v;
326
334
  uint32_t gen;
327
- uint8_t open, lingering, hup, wblocked, rheld;
335
+ uint8_t open, lingering, hup, wblocked, rheld, wshut;
328
336
  int32_t hup_res, werr;
329
337
  uint8_t *rbuf;
330
338
  uint32_t rlen, roff, rcap;
@@ -471,13 +479,10 @@ static void nr_dispatch(struct nr_set *S, const struct nish_uring_event *e) {
471
479
  }
472
480
  struct nr_conn *c = &S->conns[e->ring][e->slot];
473
481
  if (e->kind == 1) {
482
+ /* nr_close freed the queue of a slot reused here (NURING-38). */
474
483
  uint32_t gen = c->gen;
475
- uint8_t *buf = c->rbuf;
476
- uint32_t cap = c->rcap;
477
484
  memset(c, 0, sizeof *c);
478
485
  c->gen = gen;
479
- c->rbuf = buf;
480
- c->rcap = cap;
481
486
  c->open = 1;
482
487
  c->v.loop = -1;
483
488
  uint32_t i = (S->fhead + S->flen) % (S->fcap ? S->fcap : 1);
@@ -507,7 +512,6 @@ static void nr_dispatch(struct nr_set *S, const struct nish_uring_event *e) {
507
512
  so a read never runs past the failure it reports. */
508
513
  if (c->lingering || c->hup || e->res <= 0) return;
509
514
  uint32_t n = (uint32_t)e->res;
510
- if (c->roff == c->rlen) c->roff = c->rlen = 0;
511
515
  if ((uint64_t)(c->rlen - c->roff) + n > NR_RECV_MAX) {
512
516
  c->hup = 1;
513
517
  c->hup_res = -105;
@@ -521,7 +525,7 @@ static void nr_dispatch(struct nr_set *S, const struct nish_uring_event *e) {
521
525
  c->roff = 0;
522
526
  }
523
527
  if (c->rlen + n > c->rcap) {
524
- uint32_t nc = c->rcap ? c->rcap : 4096;
528
+ uint32_t nc = c->rcap ? c->rcap : NR_RECV_FIRST;
525
529
  while (nc < c->rlen + n) nc *= 2;
526
530
  uint8_t *p = realloc(c->rbuf, nc);
527
531
  if (!p) {
@@ -712,6 +716,13 @@ static int32_t nr_read(struct nr_set *S, struct nr_conn *c, int32_t ring, int32_
712
716
  c->roff += n;
713
717
  /* A held receive goes again once the program has read half the queue. */
714
718
  if (c->rheld && c->rlen - c->roff < NR_AREA / 2 && nish_uring_hold(S->u, ring, slot, 0) == 0) c->rheld = 0;
719
+ /* A drained queue is freed and emptied, so an idle slot holds none
720
+ (NURING-38) and a receive never finds roff at rlen past 0. */
721
+ if (c->roff == c->rlen) {
722
+ free(c->rbuf);
723
+ c->rbuf = NULL;
724
+ c->rcap = c->roff = c->rlen = 0;
725
+ }
715
726
  return (int32_t)n;
716
727
  }
717
728
  if (c->hup) return c->hup_res < 0 ? c->hup_res : 0;
@@ -730,6 +741,7 @@ static int32_t nr_fail_code(const struct nr_conn *c) {
730
741
  queue. -11 when it has none, or the connection's errno once it has failed. */
731
742
  static int32_t nr_send(struct nr_set *S, struct nr_conn *c, int32_t ring, int32_t slot, const char *p, int64_t len) {
732
743
  if (nr_failed(c)) return nr_fail_code(c);
744
+ if (c->wshut) return -32;
733
745
  int64_t q = nish_uring_queued(S->u, ring, slot);
734
746
  if (q < 0) return -9;
735
747
  if (q >= NR_AREA) {
@@ -748,6 +760,22 @@ static int32_t nr_send(struct nr_set *S, struct nr_conn *c, int32_t ring, int32_
748
760
  return len > 0 && n == 0 ? -11 : n;
749
761
  }
750
762
 
763
+ /* Half-closes a slot (NURING-39). The SHUTDOWN goes behind the queued sends,
764
+ so the peer reads them and then the FIN. A shut read side reads as the
765
+ peer's end: what is queued, then 0, with later bytes dropped. */
766
+ static int32_t nr_shutdown(struct nr_set *S, struct nr_conn *c, int32_t ring, int32_t slot, int32_t how) {
767
+ if (nr_failed(c)) return -107;
768
+ int32_t r = nish_uring_shutdown(S->u, ring, slot, how);
769
+ if (r < 0) return r;
770
+ if (how != 0) c->wshut = 1;
771
+ if (how != 1 && !c->hup) {
772
+ c->hup = 1;
773
+ c->hup_res = 0;
774
+ nr_touch(&c->v);
775
+ }
776
+ return 0;
777
+ }
778
+
751
779
  static int32_t nr_close(struct nr_set *S, struct nr_conn *c, int32_t ring, int32_t slot) {
752
780
  nr_unregister(&c->v);
753
781
  c->gen++;
@@ -1290,6 +1318,11 @@ int32_t nish_net_write2(int32_t fd, const nish_array *a, int64_t aoff, int64_t a
1290
1318
  are `SHUT_RD`, `SHUT_WR` and `SHUT_RDWR` on both platforms. */
1291
1319
  int32_t nish_net_shutdown(int32_t fd, int32_t how) {
1292
1320
  if ((uint32_t)how > 2) return -22;
1321
+ #if defined(__linux__)
1322
+ if (fd >= NR_BASE) { struct nr_set *S; int32_t ring, slot;
1323
+ struct nr_conn *c = nr_decode(fd, &S, &ring, &slot);
1324
+ return c ? nr_shutdown(S, c, ring, slot, how) : -9; }
1325
+ #endif
1293
1326
  return shutdown(fd, how) != 0 ? nish_net_fail() : 0;
1294
1327
  }
1295
1328
 
@@ -1363,8 +1396,13 @@ int32_t nish_net_set_option(int32_t fd, int32_t option, int32_t value) {
1363
1396
  options below. Linux's `TCP_INFO` answers the current `tcpi_snd_mss`; the
1364
1397
  kernel copies at most the struct it has and says how much, so an answer
1365
1398
  too short to reach the field is not read. `TCP_MAXSEG` is the fallback,
1366
- and Darwin's only reading. */
1399
+ and Darwin's only reading. A ring slot is -1 too (NURING-39): its socket
1400
+ is a fixed file with no descriptor to ask, and io_uring's getsockopt
1401
+ command reads only SOL_SOCKET options, not TCP's. */
1367
1402
  int32_t nish_net_mss(int32_t fd) {
1403
+ #if defined(__linux__)
1404
+ if (fd >= NR_BASE) return -1;
1405
+ #endif
1368
1406
  struct sockaddr_storage peer;
1369
1407
  socklen_t len = sizeof peer;
1370
1408
  if (getpeername(fd, (struct sockaddr *)&peer, &len) != 0) return -1;
@@ -92,6 +92,10 @@ extern NISH_TLS uint64_t nish_region_chunk;
92
92
  #ifdef NISH_ARENA_DEBUG
93
93
  /* runtime.c's: retire a debug arena chunk, or free one C code malloc'd. */
94
94
  void nish_debug_drop(void *c);
95
+ #elif defined NISH_ARENA_RANGE && !defined NISH_ASAN
96
+ /* runtime.c's: put a compact-range chunk on its class's free list, or free
97
+ one malloc made. */
98
+ void nish_range_drop(void *c);
95
99
  #endif
96
100
 
97
101
  #define NISH_REGION_SLOT_BITS 24
@@ -386,6 +390,8 @@ void nish_region_adopt(uint64_t r) {
386
390
  nish_chunk_link *next = head->next;
387
391
  #ifdef NISH_ARENA_DEBUG
388
392
  if (!(head->cap & NISH_CHUNK_CARVED)) nish_debug_drop(head);
393
+ #elif defined NISH_ARENA_RANGE && !defined NISH_ASAN
394
+ if (!(head->cap & NISH_CHUNK_CARVED)) nish_range_drop(head);
389
395
  #else
390
396
  if (!(head->cap & NISH_CHUNK_CARVED)) free(head);
391
397
  #endif
@@ -397,6 +403,13 @@ void nish_region_adopt(uint64_t r) {
397
403
  nish_chunk_link *last = head;
398
404
  while (last->next) last = last->next;
399
405
  struct nish_arena caller = nish_arena;
406
+ #ifdef NISH_ARENA_RANGE
407
+ /* A compact class's 4-byte bump may leave `off` 4 past an 8-byte
408
+ boundary (runtime.c's `nish_alloc_struct`): the carved chunk's header,
409
+ and every block bumped from it, start at the next one. */
410
+ caller.off = (caller.off + 7) & ~(uint64_t)7;
411
+ if (caller.off > caller.cap) caller.off = caller.cap;
412
+ #endif
400
413
  uint64_t left = caller.buf ? caller.cap - caller.off : 0;
401
414
  last->next = caller.chunks;
402
415
  nish_arena = s->arena;
@@ -70,11 +70,22 @@ extern NISH_TEXT_TLS struct nish_arena nish_arena;
70
70
  void *nish_arena_grow(uint64_t size);
71
71
  static inline void *nish_text_alloc(uint64_t size) {
72
72
  size = (size + 7) & ~(uint64_t)7;
73
+ #ifdef NISH_ARENA_RANGE
74
+ /* runtime.c's `nish_alloc_struct` gives the reason: `off` may be 4 bytes
75
+ past an 8-byte boundary once a compact class has been bumped. */
76
+ uint64_t at = (nish_arena.off + 7) & ~(uint64_t)7;
77
+ if (at + size <= nish_arena.cap) {
78
+ void *p = nish_arena.buf + at;
79
+ nish_arena.off = at + size;
80
+ return p;
81
+ }
82
+ #else
73
83
  if (nish_arena.off + size <= nish_arena.cap) {
74
84
  void *p = nish_arena.buf + nish_arena.off;
75
85
  nish_arena.off += size;
76
86
  return p;
77
87
  }
88
+ #endif
78
89
  return nish_arena_grow(size);
79
90
  }
80
91
  #else