diff options
author | Andreas Kling <kling@serenityos.org> | 2020-04-26 13:53:40 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-04-26 15:51:07 +0200 |
commit | f897c410927817050c9f98081d45a32e893e6d73 (patch) | |
tree | 1b45402c82e1785b649d29c3252f1df9bdceb677 /Libraries/LibJS/Tests/delete-basic.js | |
parent | 1617be1e6f1bb356ce1ae08d5443795a9c5f2f33 (diff) | |
download | serenity-f897c410927817050c9f98081d45a32e893e6d73.zip |
LibJS: Implement basic support for the "delete" operator
It turns out "delete" is actually a unary op :)
This patch implements deletion of object properties, it doesn't yet
work for casually deleting properties from the global object.
When deleting a property from an object, we switch that object to
having a unique shape, no longer sharing shapes with others.
Once an object has a unique shape, it no longer needs to care about
shape transitions.
Diffstat (limited to 'Libraries/LibJS/Tests/delete-basic.js')
-rw-r--r-- | Libraries/LibJS/Tests/delete-basic.js | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/Libraries/LibJS/Tests/delete-basic.js b/Libraries/LibJS/Tests/delete-basic.js new file mode 100644 index 0000000000..eda9741b6b --- /dev/null +++ b/Libraries/LibJS/Tests/delete-basic.js @@ -0,0 +1,60 @@ +load("test-common.js"); + +try { + o = {}; + o.x = 1; + o.y = 2; + o.z = 3; + assert(Object.getOwnPropertyNames(o).length === 3); + + assert(delete o.x === true); + assert(o.hasOwnProperty('x') === false); + assert(o.hasOwnProperty('y') === true); + assert(o.hasOwnProperty('z') === true); + assert(Object.getOwnPropertyNames(o).length === 2); + + assert(delete o.y === true); + assert(o.hasOwnProperty('x') === false); + assert(o.hasOwnProperty('y') === false); + assert(o.hasOwnProperty('z') === true); + assert(Object.getOwnPropertyNames(o).length === 1); + + assert(delete o.z === true); + assert(o.hasOwnProperty('x') === false); + assert(o.hasOwnProperty('y') === false); + assert(o.hasOwnProperty('z') === false); + assert(Object.getOwnPropertyNames(o).length === 0); + + a = [ 3, 5, 7 ]; + + assert(Object.getOwnPropertyNames(a).length === 4); + + assert(delete a[0] === true); + assert(a.hasOwnProperty(0) === false); + assert(a.hasOwnProperty(1) === true); + assert(a.hasOwnProperty(2) === true); + assert(Object.getOwnPropertyNames(a).length === 3); + + assert(delete a[1] === true); + assert(a.hasOwnProperty(0) === false); + assert(a.hasOwnProperty(1) === false); + assert(a.hasOwnProperty(2) === true); + assert(Object.getOwnPropertyNames(a).length === 2); + + assert(delete a[2] === true); + assert(a.hasOwnProperty(0) === false); + assert(a.hasOwnProperty(1) === false); + assert(a.hasOwnProperty(2) === false); + assert(Object.getOwnPropertyNames(a).length === 1); + + q = {}; + Object.defineProperty(q, "foo", { value: 1, writable: false, enumerable: false }); + assert(q.foo === 1); + + assert(delete q.foo === false); + assert(q.hasOwnProperty('foo') === true); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +} |