From 65e600e8fe63ac5b253ae24bb71dca0944d14846 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Thu, 5 Nov 2026 17:26:44 +0000 Subject: [PATCH 1/2] Atomics.load/store use the atomic load/store builtins; Promise functions over settled values Atomics.load was an atomic `or 0` (a locked write) and Atomics.store an exchange whose result was dropped; they are now `atomicload` and `atomicstore` (TypeScriptCompiler#767). The atomics test no longer declares each array's type (`new Uint8Array([250])` now keeps u8) and checks that a value wraps to the element's width as in JavaScript. Promise: the compiler takes Promise as T, so these are functions over values that have already settled: resolve, reject (throws its reason, keeping its class), all (a new array, so the result can leave an `await` under -mm=own), allSettled, race and any. No then/catch/finally. promise_reject.ts is `// not own`: under -mm=own a thrown parameter does not build, and a string Promise.resolve returns borrows its argument and cannot leave an `await`. Co-Authored-By: Claude Opus 5.5 --- README.md | 8 +++- src/generics/lib.generics.ts | 75 ++++++++++++++++++++++++++++++++---- tests/atomics.ts | 21 +++++----- tests/promise.ts | 35 +++++++++++++++++ tests/promise_reject.ts | 31 +++++++++++++++ 5 files changed, 152 insertions(+), 18 deletions(-) create mode 100644 tests/promise.ts create mode 100644 tests/promise_reject.ts diff --git a/README.md b/README.md index acbf744..f42ad8c 100644 --- a/README.md +++ b/README.md @@ -135,11 +135,17 @@ Generics (`src/generics/lib.generics.ts`): `BigUint64Array`, `Float32Array`, `Float64Array`) - `Map` - `Set` +- `Atomics`: `add`, `sub`, `and`, `or`, `xor`, `exchange`, `compareExchange`, `load`, + `store`, `isLockFree` over the integer typed arrays (no `wait`/`notify`) +- `Promise`: `resolve`, `reject`, `all`, `allSettled`, `race`, `any`. The compiler takes + `Promise` as `T` (an async function returns its value and `await` gives it back), so + these work on values that have already settled and a rejection is a thrown exception. + There is no `then`/`catch`/`finally`. Under `-mm=own`, `reject` doesn't build yet. Errors (`src/core/core.d.ts`): - `Error` - `RangeError` -Not yet implemented: `Promise`, `JSON.parse`/`JSON.stringify`, `Symbol` (beyond the +Not yet implemented: a `Promise` object (`then`/`catch`/`finally`), `JSON.parse`/`JSON.stringify`, `Symbol` (beyond the well-known symbols used internally), `WeakMap`/`WeakSet`. diff --git a/src/generics/lib.generics.ts b/src/generics/lib.generics.ts index a8529cf..90f2cbf 100644 --- a/src/generics/lib.generics.ts +++ b/src/generics/lib.generics.ts @@ -749,10 +749,10 @@ type Float32Array = TypedArray; type Float64Array = TypedArray; // Atomic operations on an integer TypedArray's elements, each sequentially consistent, as in -// JavaScript. Each is one LLVM `atomicrmw` or `cmpxchg` on the element. The operation codes are -// written out, not passed in: the builtins take them only as constants. The value is a `number`, -// not a `T`, so the array alone decides T - a literal `1` would otherwise make it s32 and reject a -// Uint16Array. There is no SharedArrayBuffer and a TypedArray is not a view over a buffer, so an +// JavaScript. Each is one LLVM atomic `load`, `store`, `atomicrmw` or `cmpxchg` on the element. The +// operation codes are written out, not passed in: the builtins take them only as constants. The +// value is a `number`, as in JavaScript, and wraps to the element's width (300 added to a Uint8Array +// element adds 44). There is no SharedArrayBuffer and a TypedArray is not a view over a buffer, so an // operation acts on the array's own elements. `wait`, `waitAsync` and `notify` are not here: there // is no other thread to wait for yet. namespace Atomics { @@ -787,16 +787,14 @@ namespace Atomics { return cmpxchg(typedArray.__elementRef(index), expectedValue, replacementValue, 7, 7)[0]; } - // an `or` with 0 reads the element and leaves it as it was function load(typedArray: TypedArray, index: int): T { - const zero: T = 0; - return atomicrmw(5, typedArray.__elementRef(index), zero, 7); + return atomicload(typedArray.__elementRef(index), 7); } // returns the value stored, as JavaScript does, not the one it replaced function store(typedArray: TypedArray, index: int, value: number): T { const stored = value; - atomicrmw(0, typedArray.__elementRef(index), stored, 7); + atomicstore(typedArray.__elementRef(index), stored, 7); return stored; } @@ -807,6 +805,67 @@ namespace Atomics { } } +// The compiler takes `Promise` as T: an async function returns its value, and `await` gives the +// value back once the expression has run. So a promise here is the value itself, already settled, +// and these are the functions over such values. A rejected promise is a thrown exception. There is +// no `then`: a value has no methods. Each is generic, as `isLockFree` is above. +namespace Promise { + function resolve(value: T): T { + return value; + } + + // throws the reason: what awaiting a rejected promise does. The reason keeps its own type, so a + // `catch (e: Error)` catches an Error. -mm=own cannot build it yet: a thrown parameter is "kept", + // but `@keeps` on it is "keeps nothing". + function reject(reason: R): T { + throw reason; + } + + // the values, in order: every one has already settled. A new array, as in JavaScript: under + // -mm=own the result then owns its elements and can leave an `await`, which the argument cannot. + function all(values: T[]): T[] { + const results: T[] = []; + for (const value of values) { + results.push(value); + } + + return results; + } + + // the first value: it settled first + function race(values: T[]): T { + if (values.length == 0) { + throw new Error("Promise.race of no values never settles"); + } + + return values[0]; + } + + // the first value: none of them was rejected + function any(values: T[]): T { + if (values.length == 0) { + throw new Error("All promises were rejected"); + } + + return values[0]; + } + + // every value as fulfilled: a rejection would have thrown before the array was built + function allSettled(values: T[]): PromiseFulfilledResult[] { + const results: PromiseFulfilledResult[] = []; + for (const value of values) { + results.push({ status: "fulfilled", value: value }); + } + + return results; + } +} + +interface PromiseFulfilledResult { + status: string; + value: T; +} + enum InsertionBehavior { None, OverwriteExisting, diff --git a/tests/atomics.ts b/tests/atomics.ts index 9e4274a..2344c3a 100644 --- a/tests/atomics.ts +++ b/tests/atomics.ts @@ -1,10 +1,9 @@ // Atomics over the integer TypedArrays. Each operation returns the value the element had before it, // except store, which returns the value stored. The array alone decides the element type: a plain -// literal value does not make it s32. Each array is declared with its type: `new Uint8Array([250])` -// alone builds an array of s32. +// literal value does not make it s32, and a value wraps to the element's width as in JavaScript. function main() { - const ta: Int32Array = new Int32Array([5, 10, 0]); + const ta = new Int32Array([5, 10, 0]); assert(Atomics.add(ta, 0, 3) == 5 && ta[0] == 8, "add"); assert(Atomics.sub(ta, 1, 4) == 10 && ta[1] == 6, "sub"); @@ -20,19 +19,23 @@ function main() { assert(Atomics.compareExchange(ta, 0, 8, 200) == 100 && ta[0] == 100, "compareExchange miss"); // narrower and wider elements wrap at their own width - const u8: Uint8Array = new Uint8Array([250]); + const u8 = new Uint8Array([250]); assert(Atomics.add(u8, 0, 10) == 250 && u8[0] == 4, "Uint8Array wraps"); + assert(Atomics.add(u8, 0, 300) == 4 && u8[0] == 48, "a value wraps to the element width"); + assert(Atomics.sub(u8, 0, -1) == 48 && u8[0] == 49, "a negative value wraps too"); + assert(Atomics.load(u8, 0) == 49, "load of a narrow element"); + assert(Atomics.store(u8, 0, 7) == 7 && u8[0] == 7, "store to a narrow element"); - const u16: Uint16Array = new Uint16Array([65535]); + const u16 = new Uint16Array([65535]); assert(Atomics.add(u16, 0, 2) == 65535 && u16[0] == 1, "Uint16Array wraps"); - const s8: Int8Array = new Int8Array([127]); + const s8 = new Int8Array([127]); assert(Atomics.add(s8, 0, 1) == 127 && s8[0] == -128, "Int8Array wraps"); - const big: BigInt64Array = new BigInt64Array([4000000000]); + const big = new BigInt64Array([4000000000]); assert(Atomics.add(big, 0, 1) == 4000000000 && big[0] == 4000000001, "BigInt64Array is 64 bits"); - const counters: Int32Array = new Int32Array([0, 0, 0, 0]); + const counters = new Int32Array([0, 0, 0, 0]); for (let i = 0; i < 1000; i++) { Atomics.add(counters, i % 4, 1); } @@ -42,7 +45,7 @@ function main() { assert(Atomics.isLockFree(4) && Atomics.isLockFree(8) && !Atomics.isLockFree(3), "isLockFree"); // a Float64Array still builds: no atomic operation is made for its elements - const f: Float64Array = new Float64Array([1.5]); + const f = new Float64Array([1.5]); assert(f[0] == 1.5, "Float64Array unaffected"); print("ALL DONE"); diff --git a/tests/promise.ts b/tests/promise.ts new file mode 100644 index 0000000..72931e3 --- /dev/null +++ b/tests/promise.ts @@ -0,0 +1,35 @@ +// Promise is T: an async function returns its value and `await` gives it back. The Promise +// functions take and return such values, already settled. Promise.reject is in promise_reject.ts. + +async function twice(n: int): Promise { + return n * 2; +} + +function main() { + const one: Promise = Promise.resolve(1); + assert(one == 1, "resolve"); + + const both = Promise.all([twice(2), twice(5)]); + assert(both.length == 2 && both[0] == 4 && both[1] == 10, "all"); + + const awaited = await Promise.all([twice(3), twice(4)]); + assert(awaited[0] == 6 && awaited[1] == 8, "await all"); + + assert(Promise.race([twice(7), twice(8)]) == 14, "race"); + assert(Promise.any(["a", "b"]) == "a", "any"); + + const settled = Promise.allSettled([1, 2]); + assert(settled.length == 2 && settled[0].status == "fulfilled" && settled[1].value == 2, "allSettled"); + + let raceCaught = false; + try { + const none: int[] = []; + Promise.race(none); + } catch (e: Error) { + raceCaught = true; + } + + assert(raceCaught, "race of no values"); + + print("ALL DONE"); +} diff --git a/tests/promise_reject.ts b/tests/promise_reject.ts new file mode 100644 index 0000000..6031320 --- /dev/null +++ b/tests/promise_reject.ts @@ -0,0 +1,31 @@ +// not own: -mm=own cannot build Promise.reject (a thrown parameter), nor carry the string +// Promise.resolve returns, a borrow of its argument, out of an `await`. +// A rejected promise is a thrown exception: Promise.reject throws its reason, with its own class. + +async function greet(name: string): Promise { + return "hello " + name; +} + +function main() { + assert(await Promise.resolve(greet("you")) == "hello you", "await resolve"); + + let caught = false; + try { + Promise.reject(new Error("no")); + } catch (e: Error) { + caught = e.message == "no"; + } + + assert(caught, "reject throws its reason"); + + let caughtRange = false; + try { + Promise.reject(new RangeError("range")); + } catch (e: RangeError) { + caughtRange = e.message == "range"; + } + + assert(caughtRange, "reject keeps the reason's class"); + + print("ALL DONE"); +} From 2b2db86f87d1e4462d8a4fe9ba542e3cc954b3ae Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Thu, 5 Nov 2026 17:49:15 +0000 Subject: [PATCH 2/2] promise_reject runs under -mm=own TypeScriptCompiler#768 keeps a thrown parameter and copies an awaited string that borrows its argument, so Promise.reject and `await Promise.resolve(someString)` build under own. Needs #768. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- src/generics/lib.generics.ts | 3 +-- tests/promise_reject.ts | 3 +-- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index f42ad8c..87404f4 100644 --- a/README.md +++ b/README.md @@ -140,7 +140,7 @@ Generics (`src/generics/lib.generics.ts`): - `Promise`: `resolve`, `reject`, `all`, `allSettled`, `race`, `any`. The compiler takes `Promise` as `T` (an async function returns its value and `await` gives it back), so these work on values that have already settled and a rejection is a thrown exception. - There is no `then`/`catch`/`finally`. Under `-mm=own`, `reject` doesn't build yet. + There is no `then`/`catch`/`finally`. Errors (`src/core/core.d.ts`): diff --git a/src/generics/lib.generics.ts b/src/generics/lib.generics.ts index 90f2cbf..f36b2b2 100644 --- a/src/generics/lib.generics.ts +++ b/src/generics/lib.generics.ts @@ -815,8 +815,7 @@ namespace Promise { } // throws the reason: what awaiting a rejected promise does. The reason keeps its own type, so a - // `catch (e: Error)` catches an Error. -mm=own cannot build it yet: a thrown parameter is "kept", - // but `@keeps` on it is "keeps nothing". + // `catch (e: Error)` catches an Error. function reject(reason: R): T { throw reason; } diff --git a/tests/promise_reject.ts b/tests/promise_reject.ts index 6031320..e939820 100644 --- a/tests/promise_reject.ts +++ b/tests/promise_reject.ts @@ -1,6 +1,5 @@ -// not own: -mm=own cannot build Promise.reject (a thrown parameter), nor carry the string -// Promise.resolve returns, a borrow of its argument, out of an `await`. // A rejected promise is a thrown exception: Promise.reject throws its reason, with its own class. +// The string Promise.resolve returns borrows its argument; under -mm=own the await gives back a copy. async function greet(name: string): Promise { return "hello " + name;