diff --git a/README.md b/README.md index 78ab2e3..87088da 100644 --- a/README.md +++ b/README.md @@ -243,18 +243,32 @@ 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`, `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`. `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. +- `ObjectDictionary`, the value of a string index signature (`{ [key: string]: T }`) and of an + object literal with a key known only at run time: the one object that gains and loses keys. The + compiler's `Object` functions take it: `Object.keys`, `getOwnPropertyNames`, `values` and + `entries` give its entries in the order they were added, `Object.hasOwn(d, k)` is `k in d`, + `Object.assign(d, ...sources)` copies each source's fields or entries into it, and + `Object.fromEntries(pairs)` makes one of an array of `[key, value]` pairs. + `Object.preventExtensions`, `seal` and `freeze` close it, and `Object.isExtensible`, `isSealed` + and `isFrozen` say how far; a write it no longer takes then throws a `TypeError`, as in + JavaScript's strict mode. Any other object's keys are fixed when it is made, as a sealed object's + are: `Object.isSealed` of it is `true`, `isExtensible` `false`, and `Object.freeze` of it is the + object itself (not frozen). An array grows, so it is extensible; a number or a string is frozen. +- `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, a dictionary's entries; `get` gives an + `any`. `ownKeys` gives what `Object.keys` does (no accessors, methods or `"length"`). A + dictionary gains a key by `set` and loses one by `deleteProperty`, until `preventExtensions` (or + `Object.seal`, `Object.freeze`) closes it. Any other object is sealed: `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), and `deleteProperty` of a key it has; `isExtensible` is `false`. An array + is extensible but can't be closed: `preventExtensions` of one is `false`. `apply` does not use + `thisArgument`. What can't be done throws a `TypeError` saying the method is not available: as + there are no prototypes or property descriptors, `getPrototypeOf`, `setPrototypeOf`, + `defineProperty`, `getOwnPropertyDescriptor`. The key operations (`deleteProperty` too) 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 dd23903..0f3e47b 100644 --- a/src/generics/lib.generics.ts +++ b/src/generics/lib.generics.ts @@ -1790,12 +1790,15 @@ 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 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. 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. +// 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. +// A dictionary (`{ [key: string]: T }`, ObjectDictionary) gains and loses keys: set adds one, deleteProperty +// removes one, and it is extensible until preventExtensions (or Object.seal, Object.freeze) closes it. Any +// other object's keys are fixed when it is made, as a sealed object's are: isExtensible is false, set of a +// key it does not have is false, and so is deleteProperty of a key it has. An array is extensible (it grows +// by one past its end) but can't be closed: preventExtensions of one is false. What can't be done throws a +// TypeError saying the method is not available: 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. @@ -1845,8 +1848,20 @@ namespace Reflect { throw new TypeError("Reflect." + method + " is not available: " + reason); } + // true when the target no longer has the key: a dictionary's entry is deleted, unless it is sealed or + // frozen (false); any other object is sealed, false for a key it has function deleteProperty(target: T, propertyKey: K): boolean { - return __notAvailable("deleteProperty", "an object's keys are fixed when it is made"); + const object = __object(target, "deleteProperty"); + if (!(propertyKey in object)) { + return true; + } + + if (Object.isSealed(object)) { + return false; + } + + delete object[propertyKey]; + return true; } function defineProperty(target: T, propertyKey: K, attributes: A): boolean { @@ -1865,15 +1880,18 @@ namespace Reflect { return __notAvailable("setPrototypeOf", "there are no prototypes"); } + // by the target's own type, not through `any`: a dictionary that is not closed, or an array function isExtensible(target: T): boolean { __checkObject(target, "isExtensible"); - return false; + return Object.isExtensible(target); } - // there is nothing to prevent: no object can gain a key + // true when the target can't gain a key now: a dictionary is closed, an object with fixed keys already + // is; an array can't be (false) function preventExtensions(target: T): boolean { __checkObject(target, "preventExtensions"); - return true; + Object.preventExtensions(target); + return !Object.isExtensible(target); } // `target(...argumentsList)`. A function is not called with a `this` of the caller's choosing: @@ -3856,16 +3874,36 @@ class Set { // writes them with `set`; outside this class they are always entries, never these members (@dictionary). // Under -mm=own it owns its values: a value written in moves in, one read borrows, one overwritten or // deleted is dropped. The keys are its own copies. +// It is the one object that gains and loses keys, so the one Object.preventExtensions, Object.seal and +// Object.freeze close (the compiler calls `close`; `closed` answers Object.isExtensible, isSealed and +// isFrozen): then a write it no longer takes throws a TypeError, as JavaScript's strict mode does. @dictionary class ObjectDictionary { private index: Map; private _keys: string[]; private _values: T[]; + // how far it is closed: 0 open, 1 no key added (preventExtensions), 2 nor deleted (seal), 3 nor any + // value written (freeze) + private _closed: int; constructor() { this.index = new Map(); this._keys = []; this._values = []; + this._closed = 0; + } + + // closes it as far as `level` (closed no less than it was: a frozen dictionary stays frozen) + close(level: int) { + if (level > this._closed) { + this._closed = level; + } + } + + // how far it is closed, as JavaScript reports it: one that takes no key and has none is sealed and + // frozen too + closed(): int { + return this._closed > 0 && this._keys.length == 0 ? 3 : this._closed; } get size() { @@ -3890,6 +3928,10 @@ class ObjectDictionary { const i = this.index.get(key); if (i === undefined) { + if (this._closed > 0) { + throw new TypeError("Cannot add property " + key + ", object is not extensible"); + } + // a copy of the key: the caller's stays the caller's this.index.set("" + key, this._values.length); this._keys.push("" + key); @@ -3897,6 +3939,10 @@ class ObjectDictionary { return; } + if (this._closed == 3) { + throw new TypeError("Cannot assign to read only property '" + key + "' of object"); + } + this._values[i] = value; } @@ -3912,6 +3958,10 @@ class ObjectDictionary { return false; } + if (this._closed >= 2) { + throw new TypeError("Cannot delete property '" + key + "' of object"); + } + this.index.delete(key); this._keys.splice(i, 1); this._values.splice(i, 1); @@ -3937,6 +3987,38 @@ function objectDictionaryAssign(target: ObjectDictionary, source: ObjectDi } } +// `Object.values(d)` and `Object.entries(d)` of a dictionary: its values, or its [key, value] pairs, in the +// order its entries were added (the compiler calls them). Functions of their own, as objectDictionaryAssign, +// so that under -mm=own a dictionary of objects, which can't be copied out, fails only where one is used. +function objectDictionaryValues(d: ObjectDictionary): T[] { + const r: T[] = []; + for (const k in d) { + r.push(d[k]); + } + + return r; +} + +function objectDictionaryEntries(d: ObjectDictionary): [string, T][] { + const r: [string, T][] = []; + for (const k in d) { + r.push([k, d[k]]); + } + + return r; +} + +// `Object.fromEntries(entries)`: a dictionary of the [key, value] pairs, in their order, a later value of a +// key in place of an earlier one (the compiler calls it, T the pairs' value type). A key is its text. +function objectDictionaryFromEntries(entries: E): ObjectDictionary { + const d = new ObjectDictionary(); + for (const e of entries) { + d["" + e[0]] = e[1]; + } + + return d; +} + namespace __Iterator { // example how to restrict to iterators only diff --git a/tests/object_dictionary_builtins.ts b/tests/object_dictionary_builtins.ts new file mode 100644 index 0000000..d6e9e90 --- /dev/null +++ b/tests/object_dictionary_builtins.ts @@ -0,0 +1,120 @@ +// Object's functions of a dictionary (`{ [key: string]: T }`), the one object that gains and loses keys: +// keys, values and entries in the order its entries were added, hasOwn, assign, fromEntries; closed by +// preventExtensions, seal and freeze, after which a write it no longer takes throws a TypeError. Of any +// other object, whose keys are fixed when it is made, isExtensible is false and isSealed true; an array +// grows, so it is extensible; what is no object is frozen. +function entriesAndAssign() { + const d: { [key: string]: number } = { a: 1, b: 2 }; + d["c"] = 3; + + assert(Object.keys(d).join(",") == "a,b,c", "keys"); + assert(Object.getOwnPropertyNames(d).join(",") == "a,b,c", "getOwnPropertyNames"); + assert(Object.values(d).join(",") == "1,2,3", "values"); + + const entries = Object.entries(d); + assert(entries.length == 3 && entries[1][0] == "b" && entries[1][1] == 2, "entries"); + + assert(Object.hasOwn(d, "a") && !Object.hasOwn(d, "z"), "hasOwn"); + + const more: { [key: string]: number } = { c: 30, d: 4 }; + const same = Object.assign(d, more, { e: 5 }); + assert(Object.keys(same).join(",") == "a,b,c,d,e", "assign adds the sources' keys, in order"); + assert(d["c"] == 30 && d["e"] == 5, "assign writes over a key it has"); + + const pairs: [string, number][] = [["x", 1], ["y", 2], ["x", 3]]; + const fromPairs = Object.fromEntries(pairs); + assert(Object.keys(fromPairs).join(",") == "x,y" && fromPairs["x"] == 3, "fromEntries: a later value in place"); + + const back = Object.fromEntries(Object.entries(d)); + assert(Object.values(back).join(",") == "1,2,30,4,5", "fromEntries of entries"); +} + +function closing() { + const open: { [key: string]: number } = { a: 1 }; + assert(Object.isExtensible(open) && !Object.isSealed(open) && !Object.isFrozen(open), "a dictionary is open"); + + const noMore: { [key: string]: number } = { a: 1, b: 2 }; + Object.preventExtensions(noMore); + assert(!Object.isExtensible(noMore) && !Object.isSealed(noMore), "preventExtensions"); + let caught = ""; + try { + noMore["c"] = 3; + } catch (e) { + caught = (e).message; + } + assert(caught == "Cannot add property c, object is not extensible", "no key added"); + noMore["a"] = 10; + delete noMore["b"]; + assert(noMore["a"] == 10 && !Object.hasOwn(noMore, "b"), "a key written and deleted"); + + const sealed: { [key: string]: number } = { a: 1 }; + assert(Object.seal(sealed) === sealed, "seal is its object"); + assert(Object.isSealed(sealed) && !Object.isFrozen(sealed), "sealed"); + sealed["a"] = 2; + caught = ""; + try { + delete sealed["a"]; + } catch (e) { + caught = (e).message; + } + assert(caught == "Cannot delete property 'a' of object", "no key deleted"); + assert(sealed["a"] == 2, "a key written"); + + const frozen: { [key: string]: number } = { a: 1 }; + Object.freeze(frozen); + assert(Object.isFrozen(frozen) && Object.isSealed(frozen) && !Object.isExtensible(frozen), "frozen"); + caught = ""; + try { + frozen["a"] = 2; + } catch (e) { + caught = (e).message; + } + assert(caught == "Cannot assign to read only property 'a' of object", "no value written"); + assert(frozen["a"] == 1, "the value is as it was"); + + // closed no less than it was + Object.preventExtensions(frozen); + assert(Object.isFrozen(frozen), "still frozen"); + + // one that takes no key and has none is sealed and frozen too + const empty: { [key: string]: number } = {}; + Object.preventExtensions(empty); + assert(Object.isSealed(empty) && Object.isFrozen(empty), "an empty one closed"); +} + +class Point { + constructor(public x: number) {} + + get twice() { + return this.x * 2; + } + + norm() { + return this.x; + } +} + +function others() { + const p = new Point(1); + assert(!Object.isExtensible(p) && Object.isSealed(p) && !Object.isFrozen(p), "an object's keys are fixed"); + assert(Object.freeze(p) === p && p.x == 1, "freeze of an object is the object"); + assert(Object.hasOwn(p, "x") && !Object.hasOwn(p, "norm") && !Object.hasOwn(p, "twice"), + "hasOwn: a field, not a method or an accessor"); + + const o = { a: 1 }; + assert(!Object.isExtensible(o) && Object.isSealed(o), "an object literal's keys are fixed"); + assert(Object.hasOwn(o, "a") && !Object.hasOwn(o, "b"), "hasOwn of an object literal"); + + const a = [1, 2]; + assert(Object.isExtensible(a) && !Object.isSealed(a) && !Object.isFrozen(a), "an array grows"); + assert(Object.hasOwn(a, 1) && !Object.hasOwn(a, 2), "hasOwn of an array's index"); + + assert(Object.isFrozen(5) && Object.isFrozen("s") && !Object.isExtensible(true), "what is no object is frozen"); +} + +function main() { + entriesAndAssign(); + closing(); + others(); + print("ALL DONE"); +} diff --git a/tests/reflect_apply.ts b/tests/reflect_apply.ts index 2ea6540..5aaf281 100644 --- a/tests/reflect_apply.ts +++ b/tests/reflect_apply.ts @@ -1,5 +1,5 @@ // 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. +// of one type or several; isExtensible and preventExtensions of an object, whose keys are fixed when it is made. class Counter { constructor(public n: number, public up: boolean) {} } diff --git a/tests/reflect_dictionary.ts b/tests/reflect_dictionary.ts new file mode 100644 index 0000000..3c9d2e7 --- /dev/null +++ b/tests/reflect_dictionary.ts @@ -0,0 +1,72 @@ +// not own: Reflect reads keys through `any`, which has none under -mm=own +// Reflect of a dictionary (`{ [key: string]: T }`), the one object that gains and loses keys: set adds one, +// deleteProperty removes one, isExtensible until preventExtensions closes it. Any other object's keys are +// fixed when it is made, as a sealed object's: deleteProperty of a key it has is false. Through `any`, +// `delete x[k]` and Object.isFrozen and its kin look at the value at run time. +class Point { + constructor(public x: number) {} +} + +function dictionary() { + const d: { [key: string]: number } = { a: 1 }; + + assert(Reflect.set(d, "b", 2) && d["b"] == 2, "set adds a key"); + assert(Reflect.ownKeys(d).join(",") == "a,b", "ownKeys"); + + assert(Reflect.deleteProperty(d, "a") && !Reflect.has(d, "a"), "deleteProperty removes a key"); + assert(Reflect.deleteProperty(d, "none"), "deleteProperty of a key it has not got"); + + assert(Reflect.isExtensible(d), "a dictionary is extensible"); + assert(Reflect.preventExtensions(d) && !Reflect.isExtensible(d), "until preventExtensions"); + assert(!Reflect.set(d, "c", 3) && !Reflect.has(d, "c"), "then set of a new key is false"); + assert(Reflect.set(d, "b", 20) && d["b"] == 20, "a key it has is written"); + assert(Reflect.deleteProperty(d, "b"), "and deleted"); + + const sealed: { [key: string]: number } = { a: 1 }; + Object.seal(sealed); + assert(!Reflect.deleteProperty(sealed, "a") && sealed["a"] == 1, "deleteProperty of a sealed one is false"); + + const frozen: { [key: string]: number } = { a: 1 }; + Object.freeze(frozen); + assert(!Reflect.set(frozen, "a", 2) && frozen["a"] == 1, "set of a frozen one is false"); +} + +function objects() { + const p = new Point(1); + assert(!Reflect.deleteProperty(p, "x") && p.x == 1, "an object's key is not deleted"); + assert(Reflect.deleteProperty(p, "y"), "nor is there one it has not got"); + assert(!Reflect.isExtensible(p) && Reflect.preventExtensions(p), "an object's keys are fixed"); + + const a = [1, 2]; + assert(Reflect.isExtensible(a) && !Reflect.preventExtensions(a), "an array grows and can't be closed"); +} + +function throughAny() { + const d: { [key: string]: number } = { a: 1, b: 2 }; + const x: any = d; + delete x["a"]; + assert(Object.keys(d).join(",") == "b", "delete through any deletes the entry"); + assert(Object.isExtensible(x) && !Object.isFrozen(x), "an open dictionary through any"); + Object.freeze(x); + assert(Object.isFrozen(d), "freeze through any closes the dictionary"); + + const p: any = new Point(1); + assert(Object.isSealed(p) && !Object.isExtensible(p) && !Object.isFrozen(p), "an object through any is sealed"); + let threw = false; + try { + delete p["x"]; + } catch (e) { + threw = true; + } + assert(threw, "delete of an object's key through any throws"); + + const n: any = 5; + assert(Object.isFrozen(n), "a number through any is frozen"); +} + +function main() { + dictionary(); + objects(); + throughAny(); + print("ALL DONE"); +} diff --git a/tests/reflect_not_available.ts b/tests/reflect_not_available.ts index 4a479cb..2bb2ac2 100644 --- a/tests/reflect_not_available.ts +++ b/tests/reflect_not_available.ts @@ -1,6 +1,7 @@ // 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). +// defineProperty and getOwnPropertyDescriptor (no property descriptors), getPrototypeOf and setPrototypeOf +// (no prototypes). (deleteProperty of an object, whose keys are fixed as a sealed object's, is false: +// reflect_dictionary.ts.) class Point { constructor(public x: number) {} } @@ -8,15 +9,6 @@ class Point { 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 });