Bug 1079188 - Coerce the argument passed to Object.getOwnPropertyDescriptor using ToObject. r=till
authorziyunfei <446240525@qq.com>
Tue, 07 Oct 2014 09:21:00 +0200
changeset 209281 d25bc2a722d9b54829a57b4a0cdbb27e9ac83115
parent 209280 1d9f0405f64d133bfd196112905cc51f62ffe177
child 209282 fe0ccde065c68fda5f2a68c69775f635993793bd
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewerstill
bugs1079188
milestone35.0a1
Bug 1079188 - Coerce the argument passed to Object.getOwnPropertyDescriptor using ToObject. r=till
js/src/builtin/Object.cpp
js/src/tests/ecma_6/Object/getOwnPropertyDescriptor.js
--- a/js/src/builtin/Object.cpp
+++ b/js/src/builtin/Object.cpp
@@ -808,26 +808,33 @@ js::obj_create(JSContext *cx, unsigned a
             return false;
     }
 
     /* 5. Return obj. */
     args.rval().setObject(*obj);
     return true;
 }
 
+// ES6 draft rev27 (2014/08/24) 19.1.2.6 Object.getOwnPropertyDescriptor(O, P)
 bool
 js::obj_getOwnPropertyDescriptor(JSContext *cx, unsigned argc, Value *vp)
 {
     CallArgs args = CallArgsFromVp(argc, vp);
-    RootedObject obj(cx);
-    if (!GetFirstArgumentAsObject(cx, args, "Object.getOwnPropertyDescriptor", &obj))
+
+    // Steps 1-2.
+    RootedObject obj(cx, ToObject(cx, args.get(0)));
+    if (!obj)
         return false;
+
+    // Steps 3-4.
     RootedId id(cx);
     if (!ValueToId<CanGC>(cx, args.get(1), &id))
         return false;
+
+    // Steps 5-7.
     return GetOwnPropertyDescriptor(cx, obj, id, args.rval());
 }
 
 // ES6 draft rev27 (2014/08/24) 19.1.2.14 Object.keys(O)
 static bool
 obj_keys(JSContext *cx, unsigned argc, Value *vp)
 {
     CallArgs args = CallArgsFromVp(argc, vp);
new file mode 100644
--- /dev/null
+++ b/js/src/tests/ecma_6/Object/getOwnPropertyDescriptor.js
@@ -0,0 +1,35 @@
+/*
+ * Any copyright is dedicated to the Public Domain.
+ * https://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+var BUGNUMBER = 1079188;
+var summary = "Coerce the argument passed to Object.getOwnPropertyDescriptor using ToObject";
+print(BUGNUMBER + ": " + summary);
+
+assertThrowsInstanceOf(() => Object.getOwnPropertyDescriptor(), TypeError);
+assertThrowsInstanceOf(() => Object.getOwnPropertyDescriptor(undefined), TypeError);
+assertThrowsInstanceOf(() => Object.getOwnPropertyDescriptor(null), TypeError);
+
+Object.getOwnPropertyDescriptor(1);
+Object.getOwnPropertyDescriptor(true);
+if (typeof Symbol === "function") {
+    Object.getOwnPropertyDescriptor(Symbol("foo"));
+}
+
+assertDeepEq(Object.getOwnPropertyDescriptor("foo", "length"), {
+    configurable: false,
+    enumerable: false,
+    value: 3,
+    writable: false
+});
+
+assertDeepEq(Object.getOwnPropertyDescriptor("foo", 0), {
+    configurable: false,
+    enumerable: true,
+    value: "f",
+    writable: false
+});
+
+if (typeof reportCompare === "function")
+    reportCompare(true, true);