summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Tests/Object.defineProperty.js
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-04-09 22:15:26 +0200
committerAndreas Kling <kling@serenityos.org>2020-04-10 00:36:06 +0200
commite6d920d87ddd7b02b4fbbca3d6d6e98b9c8107dd (patch)
tree22b62c37aa1ffee77a7d1a27b71808e76f02fa75 /Libraries/LibJS/Tests/Object.defineProperty.js
parent1570e678819e53723dfdd5c8b668d1b75d4ced8f (diff)
downloadserenity-e6d920d87ddd7b02b4fbbca3d6d6e98b9c8107dd.zip
LibJS: Add Object.defineProperty() and start caring about attributes
We now care (a little bit) about the "configurable" and "writable" property attributes. Property attributes are stored together with the property name in the Shape object. Forward transitions are not attribute-savvy and will cause poor Shape reuse in the case of multiple same-name properties with different attributes. Oh, and this patch also adds Object.getOwnPropertyDescriptor() :^)
Diffstat (limited to 'Libraries/LibJS/Tests/Object.defineProperty.js')
-rw-r--r--Libraries/LibJS/Tests/Object.defineProperty.js39
1 files changed, 39 insertions, 0 deletions
diff --git a/Libraries/LibJS/Tests/Object.defineProperty.js b/Libraries/LibJS/Tests/Object.defineProperty.js
new file mode 100644
index 0000000000..4f3b7049a6
--- /dev/null
+++ b/Libraries/LibJS/Tests/Object.defineProperty.js
@@ -0,0 +1,39 @@
+function assert(x) { if (!x) throw 1; }
+
+try {
+
+ var o = {};
+ Object.defineProperty(o, "foo", { value: 1, writable: false, enumerable: false });
+
+ assert(o.foo === 1);
+ o.foo = 2;
+ assert(o.foo === 1);
+
+ var d = Object.getOwnPropertyDescriptor(o, "foo");
+ assert(d.configurable === false);
+ assert(d.enumerable === false);
+ assert(d.writable === false);
+ assert(d.value === 1);
+
+ Object.defineProperty(o, "bar", { value: "hi", writable: true, enumerable: true });
+
+ assert(o.bar === "hi");
+ o.bar = "ho";
+ assert(o.bar === "ho");
+
+ d = Object.getOwnPropertyDescriptor(o, "bar");
+ assert(d.configurable === false);
+ assert(d.enumerable === true);
+ assert(d.writable === true);
+ assert(d.value === "ho");
+
+ try {
+ Object.defineProperty(o, "bar", { value: "xx", enumerable: false });
+ } catch (e) {
+ assert(e.name === "TypeError");
+ }
+
+ console.log("PASS");
+} catch (e) {
+ console.log(e)
+}