Skip to content

Commit

Permalink
Bug 1079090 - Coerce the argument passed to Object.getPrototypeOf usi…
Browse files Browse the repository at this point in the history
…ng ToObject. r=till
  • Loading branch information
ziyunfei committed Oct 6, 2014
1 parent 97f1639 commit 6c7ddd6
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 48 deletions.
25 changes: 6 additions & 19 deletions js/src/builtin/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -531,33 +531,20 @@ obj_lookupSetter(JSContext *cx, unsigned argc, Value *vp)
}
#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);
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);
/* Steps 1-2. */
RootedObject obj(cx, ToObject(cx, args.get(0)));
if (!obj)
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;
Expand Down
3 changes: 0 additions & 3 deletions js/src/jit-test/tests/basic/setPrototypeOf.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ function TestSetPrototypeOf(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
Expand Down

This file was deleted.

17 changes: 0 additions & 17 deletions js/src/tests/ecma_3_1/Object/regress-444787.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,5 @@ function test()
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');
}
22 changes: 22 additions & 0 deletions js/src/tests/ecma_6/Object/getPrototypeOf.js
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 6c7ddd6

Please sign in to comment.