Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<R>`
Expand Down
33 changes: 27 additions & 6 deletions src/generics/lib.generics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<T = int>(method: string, reason: string): T {
throw new TypeError("Reflect." + method + " is not available: " + reason);
}

function deleteProperty<T, K>(target: T, propertyKey: K): boolean {
return !(propertyKey in __object(target, "deleteProperty"));
return __notAvailable<boolean>("deleteProperty", "an object's keys are fixed when it is made");
}

function defineProperty<T, K, A>(target: T, propertyKey: K, attributes: A): boolean {
return __notAvailable<boolean>("defineProperty", "there are no property descriptors");
}

function getOwnPropertyDescriptor<T, K>(target: T, propertyKey: K): any {
return __notAvailable<any>("getOwnPropertyDescriptor", "there are no property descriptors");
}

function getPrototypeOf<T>(target: T): any {
return __notAvailable<any>("getPrototypeOf", "there are no prototypes");
}

function setPrototypeOf<T, P>(target: T, proto: P): boolean {
return __notAvailable<boolean>("setPrototypeOf", "there are no prototypes");
}

function isExtensible<T>(target: T): boolean {
Expand Down
6 changes: 1 addition & 5 deletions tests/reflect_keys.ts
Original file line number Diff line number Diff line change
@@ -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) {}
Expand Down Expand Up @@ -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 {
Expand Down
44 changes: 44 additions & 0 deletions tests/reflect_not_available.ts
Original file line number Diff line number Diff line change
@@ -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 = (<TypeError>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");
}
Loading