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) if (!isConstructing)
return true; 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) if (!obj)
return false; return false;
args.rval().setObject(*obj); args.rval().setObject(*obj);

View File

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

View File

@ -14,14 +14,13 @@
namespace js { namespace js {
inline NumberObject* 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) if (!obj)
return nullptr; return nullptr;
NumberObject& numobj = obj->as<NumberObject>(); obj->setPrimitiveValue(d);
numobj.setPrimitiveValue(d); return obj;
return &numobj;
} }
} // namespace js } // namespace js

View File

@ -22,10 +22,11 @@ class NumberObject : public NativeObject
static const Class class_; static const Class class_;
/* /*
* Creates a new Number object boxing the given number. The object's * Creates a new Number object boxing the given number.
* [[Prototype]] is determined from context. * 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 { double unbox() const {
return getFixedSlot(PRIMITIVE_VALUE_SLOT).toNumber(); return getFixedSlot(PRIMITIVE_VALUE_SLOT).toNumber();