Bug 1135429 - Object.create shouldn't throw when its second argument is a primitive value. r=till

This commit is contained in:
ziyunfei 2015-02-24 08:56:00 +01:00
parent fd69faf069
commit b7fc6b9236
3 changed files with 25 additions and 13 deletions

View File

@ -659,11 +659,13 @@ js::ObjectCreateWithTemplate(JSContext *cx, HandlePlainObject templateObj)
return ObjectCreateImpl(cx, proto, GenericObject, group);
}
/* ES5 15.2.3.5: Object.create(O [, Properties]) */
// ES6 draft rev34 (2015/02/20) 19.1.2.2 Object.create(O [, Properties])
bool
js::obj_create(JSContext *cx, unsigned argc, Value *vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
// Step 1.
if (args.length() == 0) {
JS_ReportErrorNumber(cx, js_GetErrorMessage, nullptr, JSMSG_MORE_ARGS_NEEDED,
"Object.create", "0", "s");
@ -681,27 +683,21 @@ js::obj_create(JSContext *cx, unsigned argc, Value *vp)
return false;
}
// Step 2.
RootedObject proto(cx, args[0].toObjectOrNull());
RootedPlainObject obj(cx, ObjectCreateImpl(cx, proto));
if (!obj)
return false;
/* 15.2.3.5 step 4. */
// Step 3.
if (args.hasDefined(1)) {
if (args[1].isPrimitive()) {
char *bytes = DecompileValueGenerator(cx, JSDVG_SEARCH_STACK, args[1], NullPtr());
if (!bytes)
return false;
JS_ReportErrorNumber(cx, js_GetErrorMessage, nullptr, JSMSG_NOT_NONNULL_OBJECT, bytes);
return false;
}
RootedObject props(cx, &args[1].toObject());
if (!DefineProperties(cx, obj, props))
RootedValue val(cx, args[1]);
RootedObject props(cx, ToObject(cx, val));
if (!props || !DefineProperties(cx, obj, props))
return false;
}
/* 5. Return obj. */
// Step 4.
args.rval().setObject(*obj);
return true;
}

View File

@ -0,0 +1,8 @@
// |jit-test| ion-eager
load(libdir + "asserts.js");
[1, "", true, Symbol(), undefined].forEach(props => {
assertEq(Object.getPrototypeOf(Object.create(null, props)), null);
});
assertThrowsInstanceOf(() => Object.create(null, null), TypeError);

View File

@ -0,0 +1,8 @@
[1, "", true, Symbol(), undefined].forEach(props => {
assertEq(Object.getPrototypeOf(Object.create(null, props)), null);
});
assertThrowsInstanceOf(() => Object.create(null, null), TypeError);
if (typeof reportCompare === "function")
reportCompare(true, true);