Bug 1079090 - Coerce the argument passed to Object.getPrototypeOf using ToObject. r=till
authorziyunfei <446240525@qq.com>
Mon, 06 Oct 2014 23:39:00 +0200
changeset 233814 55b7368ee7f86ad27df12138c14b8a8468accc4e
parent 233813 0ea4be6186b5778ef64180763abc1eb243798950
child 233815 1d9f0405f64d133bfd196112905cc51f62ffe177
push id611
push userraliiev@mozilla.com
push dateMon, 05 Jan 2015 23:23:16 +0000
treeherdermozilla-release@345cd3b9c445 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerstill
bugs1079090
milestone35.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1079090 - Coerce the argument passed to Object.getPrototypeOf using ToObject. r=till
js/src/builtin/Object.cpp
js/src/jit-test/tests/basic/setPrototypeOf.js
js/src/jit-test/tests/basic/testErrorReportIn_getPrototypeOf.js
js/src/tests/ecma_3_1/Object/regress-444787.js
js/src/tests/ecma_6/Object/getPrototypeOf.js
--- a/js/src/builtin/Object.cpp
+++ b/js/src/builtin/Object.cpp
@@ -526,43 +526,30 @@ obj_lookupSetter(JSContext *cx, unsigned
             if (shape->hasSetterValue())
                 args.rval().set(shape->setterValue());
         }
     }
     return true;
 }
 #endif /* JS_OLD_GETTER_SETTER_METHODS */
 
-/* ES5 15.2.3.2. */
+// ES6 draft rev27 (2014/08/24) 19.1.2.9 Object.getPrototypeOf(O)
 bool
 js::obj_getPrototypeOf(JSContext *cx, unsigned argc, Value *vp)
 {
     CallArgs args = CallArgsFromVp(argc, vp);
 
-    /* Step 1. */
-    if (args.length() == 0) {
-        js_ReportMissingArg(cx, args.calleev(), 0);
+    /* Steps 1-2. */
+    RootedObject obj(cx, ToObject(cx, args.get(0)));
+    if (!obj)
         return false;
-    }
 
-    if (args[0].isPrimitive()) {
-        RootedValue val(cx, args[0]);
-        char *bytes = DecompileValueGenerator(cx, JSDVG_SEARCH_STACK, val, NullPtr());
-        if (!bytes)
-            return false;
-        JS_ReportErrorNumber(cx, js_GetErrorMessage, nullptr,
-                             JSMSG_UNEXPECTED_TYPE, bytes, "not an object");
-        js_free(bytes);
-        return false;
-    }
-
-    /* Step 2. */
-    RootedObject thisObj(cx, &args[0].toObject());
+    /* Step 3. */
     RootedObject proto(cx);
-    if (!JSObject::getProto(cx, thisObj, &proto))
+    if (!JSObject::getProto(cx, obj, &proto))
         return false;
     args.rval().setObjectOrNull(proto);
     return true;
 }
 
 static bool
 obj_setPrototypeOf(JSContext *cx, unsigned argc, Value *vp)
 {
--- a/js/src/jit-test/tests/basic/setPrototypeOf.js
+++ b/js/src/jit-test/tests/basic/setPrototypeOf.js
@@ -26,19 +26,16 @@ var valuesWithoutNull = coercibleValues.
 function TestSetPrototypeOf(object, proto) {
     assertEq(Object.setPrototypeOf(object, proto), object);
     assertEq(Object.getPrototypeOf(object), proto);
 }
 
 // check if Object.setPrototypeOf works with coercible values
 for(var value of coercibleValues) {
     assertEq(Object.setPrototypeOf(value, {}), value);
-
-    assertThrowsInstanceOf(() => Object.getPrototypeOf(value),
-        TypeError, "Coercible values should not have a prototype");
 }
 
 // check if Object.setPrototypeOf fails on non-coercible values
 for (var value of nonCoercibleValues) {
     assertThrowsInstanceOf(() => Object.setPrototypeOf(value, {}),
         TypeError, "Object.setPrototypeOf shouldn't work on non-coercible values");
 }
 
deleted file mode 100644
--- a/js/src/jit-test/tests/basic/testErrorReportIn_getPrototypeOf.js
+++ /dev/null
@@ -1,9 +0,0 @@
-actual = "";
-expect = "TypeError: \"kittens\" is not an object";
-try {
-    Object.getPrototypeOf.apply(null, ["kittens",4,3])
-} catch (e) {
-    actual = "" + e;
-}
-
-assertEq(actual, expect);
--- a/js/src/tests/ecma_3_1/Object/regress-444787.js
+++ b/js/src/tests/ecma_3_1/Object/regress-444787.js
@@ -96,27 +96,10 @@ function test()
   {
     instance = objects[i].instance;
     type     = objects[i].type;
     expect   = type.prototype;
     actual   = Object.getPrototypeOf(instance);
     reportCompare(expect, actual, summary + ' instance: ' + instance + ', type: ' + type.name);
   }
 
-  var non_objects = [ true, false, 1.0, Infinity, NaN, Math.PI, "bar" ];
-
-  for (i = 0; i < non_objects.length; i++)
-  {
-    instance = non_objects[i];
-    expect = 'TypeError: instance is not an object';
-    try
-    {
-      actual = Object.getPrototypeOf(instance);
-    }
-    catch(ex)
-    {
-      actual = ex + '';
-    }
-    reportCompare(expect, actual, summary + ' non-object: ' + actual);
-  }
-
   exitFunc ('test');
 }
new file mode 100644
--- /dev/null
+++ b/js/src/tests/ecma_6/Object/getPrototypeOf.js
@@ -0,0 +1,22 @@
+/*
+ * Any copyright is dedicated to the Public Domain.
+ * https://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+var BUGNUMBER = 1079090;
+var summary = "Coerce the argument passed to Object.getPrototypeOf using ToObject";
+print(BUGNUMBER + ": " + summary);
+
+assertThrowsInstanceOf(() => Object.getPrototypeOf(), TypeError);
+assertThrowsInstanceOf(() => Object.getPrototypeOf(undefined), TypeError);
+assertThrowsInstanceOf(() => Object.getPrototypeOf(null), TypeError);
+
+assertEq(Object.getPrototypeOf(1), Number.prototype);
+assertEq(Object.getPrototypeOf(true), Boolean.prototype);
+assertEq(Object.getPrototypeOf("foo"), String.prototype);
+if (typeof Symbol === "function") {
+    assertEq(Object.getPrototypeOf(Symbol("foo")), Symbol.prototype);
+}
+
+if (typeof reportCompare === "function")
+    reportCompare(true, true);