mirror of
https://github.com/RGBCube/serenity
synced 2025-05-28 16:15:10 +00:00
LibJS: Add Proxy objects
Includes all traps except the following: [[Call]], [[Construct]], [[OwnPropertyKeys]]. An important implication of this commit is that any call to any virtual Object method has the potential to throw an exception. These methods were not checked in this commit -- a future commit will have to protect these various method calls throughout the codebase.
This commit is contained in:
parent
58a72e9b81
commit
39ad42defd
29 changed files with 1697 additions and 54 deletions
62
Libraries/LibJS/Tests/Proxy.handler-has.js
Normal file
62
Libraries/LibJS/Tests/Proxy.handler-has.js
Normal file
|
@ -0,0 +1,62 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert("foo" in new Proxy({}, { has: null }) === false);
|
||||
assert("foo" in new Proxy({}, { has: undefined}) === false);
|
||||
assert("foo" in new Proxy({}, {}) === false);
|
||||
|
||||
let o = {};
|
||||
let p = new Proxy(o, {
|
||||
has(target, prop) {
|
||||
assert(target === o);
|
||||
assert(prop === "foo");
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
"foo" in p;
|
||||
|
||||
p = new Proxy(o, {
|
||||
has(target, prop) {
|
||||
if (target.checkedFoo)
|
||||
return true;
|
||||
if (prop === "foo")
|
||||
target.checkedFoo = true;
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
assert("foo" in p === false);
|
||||
assert("foo" in p === true);
|
||||
|
||||
// Invariants
|
||||
|
||||
o = {};
|
||||
Object.defineProperty(o, "foo", { configurable: false });
|
||||
Object.defineProperty(o, "bar", { value: 10, configurable: true });
|
||||
p = new Proxy(o, {
|
||||
has() {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
assertThrowsError(() => {
|
||||
"foo" in p;
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Proxy handler's has trap violates invariant: a property cannot be reported as non-existent if it exists on the target as a non-configurable property",
|
||||
});
|
||||
|
||||
Object.preventExtensions(o);
|
||||
|
||||
assertThrowsError(() => {
|
||||
"bar" in p;
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Proxy handler's has trap violates invariant: a property cannot be reported as non-existent if it exist on the target and the target is non-extensible",
|
||||
});
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue