diff --git a/README.md b/README.md index a7dfce0..793491f 100644 --- a/README.md +++ b/README.md @@ -243,6 +243,17 @@ 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`: + 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 + 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` whose `join()` waits for it and gives back `fn`'s result, or throws an `Error` with the message of what `fn` threw. `Thread.hardwareConcurrency()` says how many threads the machine runs at diff --git a/src/generics/lib.generics.ts b/src/generics/lib.generics.ts index 3aa164f..4beba84 100644 --- a/src/generics/lib.generics.ts +++ b/src/generics/lib.generics.ts @@ -1759,6 +1759,88 @@ namespace Atomics { } } +// The object operations of JavaScript's Reflect. A target is read through `any`: its keys are looked up +// 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. +// 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. +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. + function __checkObject(target: T, method: string): void { + if (target == null) { + throw new TypeError("Reflect." + method + " called on non-object"); + } + } + + // the target as `any`, its keys then looked up at run time + function __object(target: T, method: string): any { + __checkObject(target, method); + const value: any = target; + return value; + } + + // the value of the key, undefined when the target has no such key; a method reads bound to its object + function get(target: T, propertyKey: K): any { + return __object(target, "get")[propertyKey]; + } + + // false when the value can't be written: the target has no such key, the key's type does not take + // the value, or the target is held by value (an object literal or a tuple boxed as `any` is a copy) + function set(target: T, propertyKey: K, value: V): boolean { + const object = __object(target, "set"); + try { + object[propertyKey] = value; + return true; + } catch (e) { + return false; + } + } + + function has(target: T, propertyKey: K): boolean { + return propertyKey in __object(target, "has"); + } + + // the keys Object.keys gives: a class's public fields, base class first, not its accessors or + // methods (an optional field only once it is set); an array's indices, without JavaScript's + // "length", which is not enumerable + function ownKeys(target: T): string[] { + return Object.keys(__object(target, "ownKeys")); + } + + // a key can't be removed: true only when there is none to remove + function deleteProperty(target: T, propertyKey: K): boolean { + return !(propertyKey in __object(target, "deleteProperty")); + } + + function isExtensible(target: T): boolean { + __checkObject(target, "isExtensible"); + return false; + } + + // there is nothing to prevent: no object can gain a key + function preventExtensions(target: T): boolean { + __checkObject(target, "preventExtensions"); + return true; + } + + // `target(...argumentsList)`. A function is not called with a `this` of the caller's choosing: + // thisArgument is not used, and a function that declares a `this` parameter is a compile error. + function apply(target: F, thisArgument: This, argumentsList: A) { + return target(...argumentsList); + } + + // `new target(...argumentsList)`. Under -mm=own the array is only lent, so a constructor can't keep + // a string or an object from it. + function construct(target: C, argumentsList: A) { + return new target(...argumentsList); + } +} + // What a thread runs: the function, then its result or what it threw. The thread reaches it only // through the Opaque handed to the native side, so the ThreadHandle keeps it until join. class ThreadStart { diff --git a/tests/reflect_apply.ts b/tests/reflect_apply.ts new file mode 100644 index 0000000..2ea6540 --- /dev/null +++ b/tests/reflect_apply.ts @@ -0,0 +1,42 @@ +// Reflect.apply and Reflect.construct: a call and a `new` with their arguments given as an array, +// of one type or several; isExtensible and preventExtensions, as no object can gain a key. +class Counter { + constructor(public n: number, public up: boolean) {} +} + +class Empty { + made = true; +} + +function add(x: number, y: number) { + return x + y; +} + +function main() { + assert(Reflect.apply(add, undefined, [2, 5]) == 7, "apply of a function"); + assert(Reflect.apply((s: string) => s + "!", null, ["hi"]) == "hi!", "apply of an arrow"); + assert(Reflect.apply(() => 42, undefined, []) == 42, "apply with no arguments"); + assert(Reflect.apply((a: number, s: string) => s + a, undefined, [1, "x"]) == "x1", "apply with arguments of two types"); + + const base = 10; + assert(Reflect.apply((x: number) => x + base, undefined, [5]) == 15, "apply of a capturing arrow"); + + const counter = Reflect.construct(Counter, [3, true]); + assert(counter.n == 3 && counter.up, "construct with arguments of two types"); + assert(counter instanceof Counter, "construct makes the class"); + assert(Reflect.construct(Empty, []).made, "construct with no arguments"); + + assert(!Reflect.isExtensible(counter), "no object can gain a key"); + assert(!Reflect.isExtensible({ a: 1 }), "an object literal can't either"); + assert(Reflect.preventExtensions(counter), "preventExtensions succeeds"); + + let threw = false; + try { + Reflect.isExtensible(null); + } catch (e) { + threw = true; + } + assert(threw, "isExtensible of null throws"); + + print("ALL DONE"); +} diff --git a/tests/reflect_construct_kept.ts b/tests/reflect_construct_kept.ts new file mode 100644 index 0000000..451a2e8 --- /dev/null +++ b/tests/reflect_construct_kept.ts @@ -0,0 +1,13 @@ +// not own: there the arguments array is only lent to Reflect.construct, which can't give its string to the object +// Reflect.construct of a class that keeps what it is given: the object holds the arguments' values. +class Pair { + constructor(public name: string, public values: number[]) {} +} + +function main() { + const pair = Reflect.construct(Pair, ["a", [1, 2]]); + assert(pair.name == "a", "the string kept"); + assert(pair.values.length == 2 && pair.values[1] == 2, "the array kept"); + + print("ALL DONE"); +} diff --git a/tests/reflect_keys.ts b/tests/reflect_keys.ts new file mode 100644 index 0000000..a7514bc --- /dev/null +++ b/tests/reflect_keys.ts @@ -0,0 +1,100 @@ +// 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 +// target has when it is boxed as `any` - of a class, its actual class's. +class Point { + constructor(public x: number, protected y: number) {} + + get sum() { + return this.x + this.y; + } + + norm() { + return this.x * this.x + this.y * this.y; + } +} + +class Point3 extends Point { + label?: string; + + constructor(x: number, y: number, public z: number) { + super(x, y); + } +} + +interface Named { + name: string; +} + +function main() { + // an object literal: read, but not written, since `any` holds a copy of it + const o = { a: 1, b: "two" }; + assert(Reflect.get(o, "a") == 1, "get of a literal"); + assert(Reflect.get(o, "b") == "two", "get of a literal's string"); + assert(typeof Reflect.get(o, "c") == "undefined", "get of a missing key"); + assert(Reflect.has(o, "a") && !Reflect.has(o, "c"), "has of a literal"); + assert(Reflect.ownKeys(o).join(",") == "a,b", "ownKeys of a literal"); + assert(!Reflect.set(o, "a", 5) && o.a == 1, "set of a literal is false"); + + // a class: public fields, accessors and methods; a protected field is not seen + const p = new Point(3, 4); + let key = "x"; + assert(Reflect.get(p, key) == 3, "get by a run-time key"); + assert(Reflect.get(p, "sum") == 7, "get of an accessor"); + assert(typeof Reflect.get(p, "norm") == "function", "get of a method"); + assert(typeof Reflect.get(p, "y") == "undefined", "a protected field is not read"); + assert(Reflect.has(p, "norm") && Reflect.has(p, "sum"), "has of a method and an accessor"); + assert(!Reflect.has(p, "y") && !Reflect.has(p, "w"), "has of a protected and a missing key"); + assert(Reflect.ownKeys(p).join(",") == "x", "ownKeys: fields, not accessors or methods"); + + // set writes through to the object + assert(Reflect.set(p, "x", 30) && p.x == 30, "set of a field"); + assert(Reflect.get(p, "sum") == 34, "an accessor sees the field set"); + // a value the field's type does not take, and a key the object has not, are false + assert(!Reflect.set(p, "x", "thirty") && p.x == 30, "set of a wrong type is false"); + assert(!Reflect.set(p, "w", 1), "set of a missing key is false"); + + // held as its base class, an object shows its actual class's keys + const q: Point = new Point3(1, 2, 3); + assert(Reflect.ownKeys(q).join(",") == "x,z", "ownKeys of the actual class"); + assert(Reflect.get(q, "z") == 3, "get of the actual class's field"); + assert(Reflect.has(q, "z"), "has of the actual class's field"); + + // an optional field is a key once it is set + const r = new Point3(1, 2, 3); + assert(!Reflect.has(r, "label"), "an optional field not set"); + r.label = "r"; + assert(Reflect.has(r, "label") && Reflect.get(r, "label") == "r", "an optional field set"); + + // an object held through an interface is written through + const n: Named = { name: "n" }; + assert(Reflect.set(n, "name", "m") && n.name == "m", "set through an interface"); + + // an array: its indices + const a = [10, 20, 30]; + assert(Reflect.get(a, 1) == 20, "get of an index"); + 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 { + Reflect.ownKeys(null); + } catch (e) { + threw = true; + } + assert(threw, "ownKeys of null throws"); + + threw = false; + try { + Reflect.set(null, "x", 1); + } catch (e) { + threw = true; + } + assert(threw, "set of null throws, not false"); + + print("ALL DONE"); +}