1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:18:11 +00:00

LibJS: Add getter/setter support

This patch adds a GetterSetterPair object. Values can now store pointers
to objects of this type. These objects are created when using
Object.defineProperty and providing an accessor descriptor.
This commit is contained in:
Matthew Olsson 2020-05-21 11:14:23 -07:00 committed by Andreas Kling
parent a4d04cc748
commit 45dfa094e9
7 changed files with 259 additions and 12 deletions

View file

@ -40,6 +40,81 @@ try {
assert(d.writable === true);
assert(d.value === 9);
Object.defineProperty(o, "qux", {
configurable: true,
get() {
return o.secret_qux + 1;
},
set(value) {
this.secret_qux = value + 1;
},
});
o.qux = 10;
assert(o.qux === 12);
o.qux = 20;
assert(o.qux = 22);
Object.defineProperty(o, "qux", { configurable: true, value: 4 });
assert(o.qux === 4);
o.qux = 5;
assert(o.qux = 4);
Object.defineProperty(o, "qux", {
configurable: false,
get() {
return this.secret_qux + 2;
},
set(value) {
o.secret_qux = value + 2;
},
});
o.qux = 10;
assert(o.qux === 14);
o.qux = 20;
assert(o.qux = 24);
assertThrowsError(() => {
Object.defineProperty(o, "qux", {
configurable: false,
get() {
return this.secret_qux + 2;
},
});
}, {
error: TypeError,
message: "Cannot change attributes of non-configurable property 'qux'",
});
assertThrowsError(() => {
Object.defineProperty(o, "qux", { value: 2 });
}, {
error: TypeError,
message: "Cannot change attributes of non-configurable property 'qux'",
});
assertThrowsError(() => {
Object.defineProperty(o, "a", {
get() {},
value: 9,
});
}, {
error: TypeError,
message: "Accessor property descriptors cannot specify a value or writable key",
});
assertThrowsError(() => {
Object.defineProperty(o, "a", {
set() {},
writable: true,
});
}, {
error: TypeError,
message: "Accessor property descriptors cannot specify a value or writable key",
});
console.log("PASS");
} catch (e) {
console.log(e)