Bug 1711872 - Implement Object.hasOwn proposal; r=mgaudet
Differential Revision:
https://phabricator.services.mozilla.com/D115483
--- 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);