From 960621d3b8e3b733101d180032b7bdf1e57dc711 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Fri, 20 Nov 2026 16:08:12 +0000 Subject: [PATCH] Reflect: what can't be done throws "not available" rather than giving a made-up answer deleteProperty throws a TypeError (an object's keys are fixed when it is made), and so do defineProperty, getOwnPropertyDescriptor, getPrototypeOf and setPrototypeOf (no property descriptors, no prototypes), which were left out before: a program now compiles and learns at run time that the method is not available. Tested by reflect_not_available.ts under every memory model. Co-Authored-By: Claude Opus 5.5 --- README.md | 11 +++++---- src/generics/lib.generics.ts | 33 ++++++++++++++++++++----- tests/reflect_keys.ts | 6 +---- tests/reflect_not_available.ts | 44 ++++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 16 deletions(-) create mode 100644 tests/reflect_not_available.ts diff --git a/README.md b/README.md index 793491f..78ab2e3 100644 --- a/README.md +++ b/README.md @@ -243,15 +243,16 @@ Generics (`src/generics/lib.generics.ts`): - `Atomics`: `add`, `sub`, `and`, `or`, `xor`, `exchange`, `compareExchange`, `load`, `store`, `isLockFree` over the integer typed arrays, and `wait`/`notify` over an `Int32Array` or a `BigInt64Array` (no `waitAsync`) -- `Reflect`: `get`, `set`, `has`, `ownKeys`, `deleteProperty`, `isExtensible`, `preventExtensions`, - `apply`, `construct`. A target's keys are looked up at run time, as those of a value of type `any`: +- `Reflect`: `get`, `set`, `has`, `ownKeys`, `isExtensible`, `preventExtensions`, `apply`, + `construct`. A target's keys are looked up at run time, as those of a value of type `any`: a class's public fields, accessors and methods (of its actual class), an object literal's or a tuple's fields, an array's indices; `get` gives an `any`. `ownKeys` gives what `Object.keys` does (no accessors, methods or `"length"`). No object can gain or lose a key, so `set` of a key it does not have, or of a value its type does not take, is `false`, as is `set` of an object literal (boxed - as `any`, it is a copy); `isExtensible` is `false`, and `deleteProperty` is `true` only of a key that - is not there. `apply` does not use `thisArgument`. There are no prototypes or property descriptors: - no `getPrototypeOf`, `setPrototypeOf`, `defineProperty`, `getOwnPropertyDescriptor`. The key + as `any`, it is a copy); `isExtensible` is `false`. `apply` does not use `thisArgument`. What can't + be done throws a `TypeError` saying the method is not available: `deleteProperty` (keys are fixed), + and, as there are no prototypes or property descriptors, `getPrototypeOf`, `setPrototypeOf`, + `defineProperty`, `getOwnPropertyDescriptor`. The key operations need the compiler's run-time keys of `any`, so they are not under `-mm=own`; there `construct` is lent its arguments array, so a constructor can't keep a string or an object from it. - `Thread`: `Thread.spawn(fn)` runs `fn` on a new native thread and returns a `ThreadHandle` diff --git a/src/generics/lib.generics.ts b/src/generics/lib.generics.ts index 4beba84..2ec170f 100644 --- a/src/generics/lib.generics.ts +++ b/src/generics/lib.generics.ts @@ -1763,11 +1763,12 @@ namespace Atomics { // at run time by the type it had when it was boxed, as `x[k]`, `k in x` and `Object.keys(x)` of `any` // look them up - a class's public fields and accessors (its actual class's, a base's too) and its // methods, an object literal's or a tuple's fields, an array's or a string's indices. A key is a string -// or a number; there are no symbol keys. Under -mm=own, where `any` has no keys, get, set, has, -// ownKeys and deleteProperty are compile errors. +// or a number; there are no symbol keys. Under -mm=own, where `any` has no keys, get, set, has and +// ownKeys are compile errors. // An object's keys are fixed when it is made, so none can be added or removed: isExtensible is false, -// and set of a key the object does not have is false. There are no prototypes or property descriptors: -// getPrototypeOf, setPrototypeOf, defineProperty and getOwnPropertyDescriptor are not here. +// and set of a key the object does not have is false. What can't be done throws a TypeError saying the +// method is not available: deleteProperty (a key can't be removed), and, as there are no prototypes or +// property descriptors, getPrototypeOf, setPrototypeOf, defineProperty and getOwnPropertyDescriptor. namespace Reflect { // null and undefined are not objects, a TypeError as in JavaScript. The target is not boxed: under // -mm=own it stays the caller's. @@ -1812,9 +1813,29 @@ namespace Reflect { return Object.keys(__object(target, "ownKeys")); } - // a key can't be removed: true only when there is none to remove + // what has no counterpart here: a TypeError, so a program does not go on with a made-up answer + function __notAvailable(method: string, reason: string): T { + throw new TypeError("Reflect." + method + " is not available: " + reason); + } + function deleteProperty(target: T, propertyKey: K): boolean { - return !(propertyKey in __object(target, "deleteProperty")); + return __notAvailable("deleteProperty", "an object's keys are fixed when it is made"); + } + + function defineProperty(target: T, propertyKey: K, attributes: A): boolean { + return __notAvailable("defineProperty", "there are no property descriptors"); + } + + function getOwnPropertyDescriptor(target: T, propertyKey: K): any { + return __notAvailable("getOwnPropertyDescriptor", "there are no property descriptors"); + } + + function getPrototypeOf(target: T): any { + return __notAvailable("getPrototypeOf", "there are no prototypes"); + } + + function setPrototypeOf(target: T, proto: P): boolean { + return __notAvailable("setPrototypeOf", "there are no prototypes"); } function isExtensible(target: T): boolean { diff --git a/tests/reflect_keys.ts b/tests/reflect_keys.ts index a7514bc..a94cca6 100644 --- a/tests/reflect_keys.ts +++ b/tests/reflect_keys.ts @@ -1,5 +1,5 @@ // not own: Reflect reads keys through `any`, which has none under -mm=own -// Reflect.get, set, has, ownKeys and deleteProperty: the keys are looked up at run time, by the type the +// Reflect.get, set, has and ownKeys: the keys are looked up at run time, by the type the // target has when it is boxed as `any` - of a class, its actual class's. class Point { constructor(public x: number, protected y: number) {} @@ -75,10 +75,6 @@ function main() { assert(Reflect.has(a, 2) && !Reflect.has(a, 3), "has of an index"); assert(Reflect.ownKeys(a).join(",") == "0,1,2", "ownKeys of an array"); - // a key can't be removed: deleteProperty is true only of a key that is not there - assert(!Reflect.deleteProperty(p, "x") && p.x == 30, "deleteProperty of a field"); - assert(Reflect.deleteProperty(p, "w"), "deleteProperty of a missing key"); - // null is not an object let threw = false; try { diff --git a/tests/reflect_not_available.ts b/tests/reflect_not_available.ts new file mode 100644 index 0000000..4a479cb --- /dev/null +++ b/tests/reflect_not_available.ts @@ -0,0 +1,44 @@ +// What Reflect can't do here throws a TypeError naming the method, rather than giving a made-up answer: +// deleteProperty (an object's keys are fixed), defineProperty and getOwnPropertyDescriptor (no property +// descriptors), getPrototypeOf and setPrototypeOf (no prototypes). +class Point { + constructor(public x: number) {} +} + +function main() { + const p = new Point(1); + + let message = ""; + try { + Reflect.deleteProperty(p, "x"); + } catch (e) { + message = (e).message; + } + assert(message == "Reflect.deleteProperty is not available: an object's keys are fixed when it is made", "deleteProperty"); + assert(p.x == 1, "the field is still there"); + + let threw = 0; + try { + Reflect.defineProperty(p, "y", { value: 2 }); + } catch (e) { + threw++; + } + try { + Reflect.getOwnPropertyDescriptor(p, "x"); + } catch (e) { + threw++; + } + try { + Reflect.getPrototypeOf(p); + } catch (e) { + threw++; + } + try { + Reflect.setPrototypeOf(p, null); + } catch (e) { + threw++; + } + assert(threw == 4, "defineProperty, getOwnPropertyDescriptor, getPrototypeOf and setPrototypeOf throw"); + + print("ALL DONE"); +}