Bug 1711872 - Implement Object.hasOwn proposal; r=mgaudet draft
authorYulia Startsev <ystartsev@mozilla.com>
Wed, 19 May 2021 13:29:19 +0200
changeset 3764715 94515f78324e83d4fd84f4b0ab764b34aabe6d80
parent 3764500 de62b7dc09b8bfba7cdb04deda52e0b70b7d3f99
child 3764716 d778bc288f75af377412f80d45137010aac4af1d
push id693596
push userystartsev@mozilla.com
push dateWed, 19 May 2021 12:19:48 +0000
treeherdertry@d778bc288f75 [default view] [failures only]
reviewersmgaudet
bugs1711872
milestone90.0a1
Bug 1711872 - Implement Object.hasOwn proposal; r=mgaudet Differential Revision: https://phabricator.services.mozilla.com/D115483
js/src/builtin/Object.cpp
js/src/builtin/Object.js
js/src/tests/non262/object/hasOwn.js
--- a/js/src/builtin/Object.cpp
+++ b/js/src/builtin/Object.cpp
@@ -2108,16 +2108,17 @@ static const JSFunctionSpec object_stati
     JS_FN("getOwnPropertySymbols", obj_getOwnPropertySymbols, 1, 0),
     JS_SELF_HOSTED_FN("isExtensible", "ObjectIsExtensible", 1, 0),
     JS_FN("preventExtensions", obj_preventExtensions, 1, 0),
     JS_FN("freeze", obj_freeze, 1, 0),
     JS_FN("isFrozen", obj_isFrozen, 1, 0),
     JS_FN("seal", obj_seal, 1, 0),
     JS_FN("isSealed", obj_isSealed, 1, 0),
     JS_SELF_HOSTED_FN("fromEntries", "ObjectFromEntries", 1, 0),
+    JS_SELF_HOSTED_FN("hasOwn", "ObjectHasOwn", 2, 0),
     JS_FS_END};
 
 static JSObject* CreateObjectConstructor(JSContext* cx, JSProtoKey key) {
   Rooted<GlobalObject*> self(cx, cx->global());
   if (!GlobalObject::ensureConstructor(cx, self, JSProto_Function)) {
     return nullptr;
   }
 
--- a/js/src/builtin/Object.js
+++ b/js/src/builtin/Object.js
@@ -318,8 +318,17 @@ function ObjectFromEntries(iter) {
         if (!IsObject(pair))
             ThrowTypeError(JSMSG_INVALID_MAP_ITERABLE, "Object.fromEntries");
         _DefineDataProperty(obj, pair[0], pair[1]);
     }
 
     return obj;
 }
 
+// Proposal https://github.com/tc39/proposal-accessible-object-hasownproperty
+// 1. Object.hasOwn ( O, P )
+function ObjectHasOwn(O, P) {
+    var obj = ToObject(O);
+    var prop = TO_PROPERTY_KEY(P);
+
+    return hasOwn(prop, obj);
+}
+
new file mode 100644
--- /dev/null
+++ b/js/src/tests/non262/object/hasOwn.js
@@ -0,0 +1,18 @@
+/*
+ * Any copyright is dedicated to the Public Domain.
+ * https://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+assertEq(Object.hasOwn({}, "any"), false);
+assertThrowsInstanceOf(() => Object.hasOwn(null, "any"), TypeError);
+
+var x = { test: 'test value'}
+var y = {}
+var z = Object.create(x);
+
+assertEq(Object.hasOwn(x, "test"), true);
+assertEq(Object.hasOwn(y, "test"), false);
+assertEq(Object.hasOwn(z, "test"), false);
+
+if (typeof reportCompare === "function")
+    reportCompare(true, true);