Bug 1055472 - Part 6: Make the Number constructor properly subclassable. (r=Waldo)

This commit is contained in:
Eric Faust 2015-11-13 18:22:21 -08:00
parent 6e5a905729
commit e325bd50b8
4 changed files with 14 additions and 9 deletions

View File

@ -488,7 +488,11 @@ Number(JSContext* cx, unsigned argc, Value* vp)
if (!isConstructing)
return true;
JSObject* obj = NumberObject::create(cx, args.rval().toNumber());
RootedObject newTarget(cx, &args.newTarget().toObject());
RootedObject proto(cx);
if (!GetPrototypeFromConstructor(cx, newTarget, &proto))
return false;
JSObject* obj = NumberObject::create(cx, args.rval().toNumber(), proto);
if (!obj)
return false;
args.rval().setObject(*obj);

View File

@ -25,6 +25,7 @@ testBuiltin(ReferenceError);
testBuiltin(SyntaxError);
testBuiltin(TypeError);
testBuiltin(URIError);
testBuiltin(Number);
`;

View File

@ -14,14 +14,13 @@
namespace js {
inline NumberObject*
NumberObject::create(JSContext* cx, double d)
NumberObject::create(JSContext* cx, double d, HandleObject proto /* = nullptr */)
{
JSObject* obj = NewBuiltinClassInstance(cx, &class_);
NumberObject* obj = NewObjectWithClassProto<NumberObject>(cx, proto);
if (!obj)
return nullptr;
NumberObject& numobj = obj->as<NumberObject>();
numobj.setPrimitiveValue(d);
return &numobj;
obj->setPrimitiveValue(d);
return obj;
}
} // namespace js

View File

@ -22,10 +22,11 @@ class NumberObject : public NativeObject
static const Class class_;
/*
* Creates a new Number object boxing the given number. The object's
* [[Prototype]] is determined from context.
* Creates a new Number object boxing the given number.
* If proto is nullptr, then Number.prototype will be used instead.
*/
static inline NumberObject* create(JSContext* cx, double d);
static inline NumberObject* create(JSContext* cx, double d,
HandleObject proto = nullptr);
double unbox() const {
return getFixedSlot(PRIMITIVE_VALUE_SLOT).toNumber();