1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-23 01:25:07 +00:00
serenity/Libraries/LibJS/Tests/Proxy.handler-has.js
Matthew Olsson 39ad42defd 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.
2020-06-06 22:13:01 +02:00

62 lines
1.6 KiB
JavaScript

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);
}