Bug 1447442 - Part 5: Use template versions of NewBuiltinClassInstance and NewObjectWithClassProto instead of manual casting. r=jorendorff

--HG--
extra : rebase_source : 55b7c0a30d7a33a29cf2acbcbbfc248aefa67d0c
This commit is contained in:
André Bargull 2018-04-13 02:26:00 -07:00
parent 1286bc0375
commit 4adabc626b
20 changed files with 86 additions and 110 deletions

View File

@ -149,11 +149,10 @@ ImportEntryObject::create(JSContext* cx,
if (!proto)
return nullptr;
RootedObject obj(cx, NewObjectWithGivenProto(cx, &class_, proto));
if (!obj)
ImportEntryObject* self = NewObjectWithGivenProto<ImportEntryObject>(cx, proto);
if (!self)
return nullptr;
RootedImportEntryObject self(cx, &obj->as<ImportEntryObject>());
self->initReservedSlot(ModuleRequestSlot, StringValue(moduleRequest));
self->initReservedSlot(ImportNameSlot, StringValue(importName));
self->initReservedSlot(LocalNameSlot, StringValue(localName));
@ -238,11 +237,10 @@ ExportEntryObject::create(JSContext* cx,
if (!proto)
return nullptr;
RootedObject obj(cx, NewObjectWithGivenProto(cx, &class_, proto));
if (!obj)
ExportEntryObject* self = NewObjectWithGivenProto<ExportEntryObject>(cx, proto);
if (!self)
return nullptr;
RootedExportEntryObject self(cx, &obj->as<ExportEntryObject>());
self->initReservedSlot(ExportNameSlot, StringOrNullValue(maybeExportName));
self->initReservedSlot(ModuleRequestSlot, StringOrNullValue(maybeModuleRequest));
self->initReservedSlot(ImportNameSlot, StringOrNullValue(maybeImportName));
@ -307,11 +305,10 @@ RequestedModuleObject::create(JSContext* cx,
if (!proto)
return nullptr;
RootedObject obj(cx, NewObjectWithGivenProto(cx, &class_, proto));
if (!obj)
RequestedModuleObject* self = NewObjectWithGivenProto<RequestedModuleObject>(cx, proto);
if (!self)
return nullptr;
RootedRequestedModuleObject self(cx, &obj->as<RequestedModuleObject>());
self->initReservedSlot(ModuleSpecifierSlot, StringValue(moduleSpecifier));
self->initReservedSlot(LineNumberSlot, NumberValue(lineNumber));
self->initReservedSlot(ColumnNumberSlot, NumberValue(columnNumber));
@ -771,12 +768,10 @@ ModuleObject::create(JSContext* cx)
if (!proto)
return nullptr;
RootedObject obj(cx, NewObjectWithGivenProto(cx, &class_, proto));
if (!obj)
RootedModuleObject self(cx, NewObjectWithGivenProto<ModuleObject>(cx, proto));
if (!self)
return nullptr;
RootedModuleObject self(cx, &obj->as<ModuleObject>());
Zone* zone = cx->zone();
IndirectBindingMap* bindings = zone->new_<IndirectBindingMap>();
if (!bindings) {

View File

@ -3629,11 +3629,10 @@ js::InitStringClass(JSContext* cx, HandleObject obj)
Handle<GlobalObject*> global = obj.as<GlobalObject>();
Rooted<JSString*> empty(cx, cx->runtime()->emptyString);
RootedObject proto(cx, GlobalObject::createBlankPrototype(cx, global, &StringObject::class_));
Rooted<StringObject*> proto(cx, GlobalObject::createBlankPrototype<StringObject>(cx, global));
if (!proto)
return nullptr;
Handle<StringObject*> protoObj = proto.as<StringObject>();
if (!StringObject::init(cx, protoObj, empty))
if (!StringObject::init(cx, proto, empty))
return nullptr;
/* Now create the String function. */
@ -3656,8 +3655,8 @@ js::InitStringClass(JSContext* cx, HandleObject obj)
RootedValue trimFn(cx);
RootedId trimId(cx, NameToId(cx->names().trimStart));
RootedId trimAliasId(cx, NameToId(cx->names().trimLeft));
if (!NativeGetProperty(cx, protoObj, trimId, &trimFn) ||
!NativeDefineDataProperty(cx, protoObj, trimAliasId, trimFn, 0))
if (!NativeGetProperty(cx, proto, trimId, &trimFn) ||
!NativeDefineDataProperty(cx, proto, trimAliasId, trimFn, 0))
{
return nullptr;
}
@ -3665,8 +3664,8 @@ js::InitStringClass(JSContext* cx, HandleObject obj)
// Create "trimRight" as an alias for "trimEnd".
trimId = NameToId(cx->names().trimEnd);
trimAliasId = NameToId(cx->names().trimRight);
if (!NativeGetProperty(cx, protoObj, trimId, &trimFn) ||
!NativeDefineDataProperty(cx, protoObj, trimAliasId, trimFn, 0))
if (!NativeGetProperty(cx, proto, trimId, &trimFn) ||
!NativeDefineDataProperty(cx, proto, trimAliasId, trimFn, 0))
{
return nullptr;
}

View File

@ -23,12 +23,11 @@ const Class SymbolObject::class_ = {
SymbolObject*
SymbolObject::create(JSContext* cx, JS::HandleSymbol symbol)
{
JSObject* obj = NewBuiltinClassInstance(cx, &class_);
SymbolObject* obj = NewBuiltinClassInstance<SymbolObject>(cx);
if (!obj)
return nullptr;
SymbolObject& symobj = obj->as<SymbolObject>();
symobj.setPrimitiveValue(symbol);
return &symobj;
obj->setPrimitiveValue(symbol);
return obj;
}
const JSPropertySpec SymbolObject::properties[] = {

View File

@ -190,7 +190,7 @@ WeakCollection_finalize(FreeOp* fop, JSObject* obj)
JS_PUBLIC_API(JSObject*)
JS::NewWeakMapObject(JSContext* cx)
{
return NewBuiltinClassInstance(cx, &WeakMapObject::class_);
return NewBuiltinClassInstance<WeakMapObject>(cx);
}
JS_PUBLIC_API(bool)

View File

@ -3319,10 +3319,10 @@ const Class DateObject::protoClass_ = {
JSObject*
js::NewDateObjectMsec(JSContext* cx, ClippedTime t, HandleObject proto /* = nullptr */)
{
JSObject* obj = NewObjectWithClassProto(cx, &DateObject::class_, proto);
DateObject* obj = NewObjectWithClassProto<DateObject>(cx, proto);
if (!obj)
return nullptr;
obj->as<DateObject>().setUTCTime(t);
obj->setUTCTime(t);
return obj;
}

View File

@ -130,8 +130,7 @@ JS_NewObjectWithUniqueType(JSContext* cx, const JSClass* clasp, HandleObject pro
* ObjectGroup attached to our proto with information about our object, since
* we're not going to be using that ObjectGroup anyway.
*/
RootedObject obj(cx, NewObjectWithGivenProto(cx, (const js::Class*)clasp, nullptr,
SingletonObject));
RootedObject obj(cx, NewObjectWithGivenProto(cx, Valueify(clasp), nullptr, SingletonObject));
if (!obj)
return nullptr;
if (!JS_SplicePrototype(cx, obj, proto))

View File

@ -1200,11 +1200,11 @@ js::InitNumberClass(JSContext* cx, HandleObject obj)
Handle<GlobalObject*> global = obj.as<GlobalObject>();
RootedObject numberProto(cx, GlobalObject::createBlankPrototype(cx, global,
&NumberObject::class_));
Rooted<NumberObject*> numberProto(cx);
numberProto = GlobalObject::createBlankPrototype<NumberObject>(cx, global);
if (!numberProto)
return nullptr;
numberProto->as<NumberObject>().setPrimitiveValue(0);
numberProto->setPrimitiveValue(0);
RootedFunction ctor(cx);
ctor = GlobalObject::createConstructor(cx, Number, cx->names().Number, 1);

View File

@ -404,14 +404,13 @@ class FileObject : public NativeObject
static const js::Class class_;
static FileObject* create(JSContext* cx, RCFile* file) {
JSObject* obj = js::NewObjectWithClassProto(cx, &class_, nullptr);
FileObject* obj = js::NewObjectWithClassProto<FileObject>(cx, nullptr);
if (!obj)
return nullptr;
FileObject* fileObj = &obj->as<FileObject>();
fileObj->setRCFile(file);
obj->setRCFile(file);
file->acquire();
return fileObj;
return obj;
}
static void finalize(FreeOp* fop, JSObject* obj) {

View File

@ -48,8 +48,7 @@ WrappedAsyncGenerator(JSContext* cx, unsigned argc, Value* vp)
return false;
// Step 2.
Rooted<AsyncGeneratorObject*> asyncGenObj(
cx, AsyncGeneratorObject::create(cx, wrapped, generatorVal));
AsyncGeneratorObject* asyncGenObj = AsyncGeneratorObject::create(cx, wrapped, generatorVal);
if (!asyncGenObj)
return false;
@ -187,12 +186,11 @@ AsyncFromSyncIteratorObject::create(JSContext* cx, HandleObject iter, HandleValu
if (!proto)
return nullptr;
RootedObject obj(cx, NewNativeObjectWithGivenProto(cx, &class_, proto));
if (!obj)
AsyncFromSyncIteratorObject* asyncIter =
NewObjectWithGivenProto<AsyncFromSyncIteratorObject>(cx, proto);
if (!asyncIter)
return nullptr;
Handle<AsyncFromSyncIteratorObject*> asyncIter = obj.as<AsyncFromSyncIteratorObject>();
// Step 3.
asyncIter->init(iter, nextMethod);
@ -263,10 +261,9 @@ const Class AsyncGeneratorObject::class_ = {
};
// ES 2017 draft 9.1.13.
template <typename ProtoGetter>
static JSObject*
OrdinaryCreateFromConstructor(JSContext* cx, HandleFunction fun,
ProtoGetter protoGetter, const Class* clasp)
// OrdinaryCreateFromConstructor specialized for AsyncGeneratorObjects.
static AsyncGeneratorObject*
OrdinaryCreateFromConstructorAsynGen(JSContext* cx, HandleFunction fun)
{
// Step 1 (skipped).
@ -277,13 +274,13 @@ OrdinaryCreateFromConstructor(JSContext* cx, HandleFunction fun,
RootedObject proto(cx, protoVal.isObject() ? &protoVal.toObject() : nullptr);
if (!proto) {
proto = protoGetter(cx, cx->global());
proto = GlobalObject::getOrCreateAsyncGeneratorPrototype(cx, cx->global());
if (!proto)
return nullptr;
}
// Step 3.
return NewNativeObjectWithGivenProto(cx, clasp, proto);
return NewObjectWithGivenProto<AsyncGeneratorObject>(cx, proto);
}
/* static */ AsyncGeneratorObject*
@ -292,15 +289,10 @@ AsyncGeneratorObject::create(JSContext* cx, HandleFunction asyncGen, HandleValue
MOZ_ASSERT(generatorVal.isObject());
MOZ_ASSERT(generatorVal.toObject().is<GeneratorObject>());
RootedObject obj(
cx, OrdinaryCreateFromConstructor(cx, asyncGen,
GlobalObject::getOrCreateAsyncGeneratorPrototype,
&class_));
if (!obj)
AsyncGeneratorObject* asyncGenObj = OrdinaryCreateFromConstructorAsynGen(cx, asyncGen);
if (!asyncGenObj)
return nullptr;
Handle<AsyncGeneratorObject*> asyncGenObj = obj.as<AsyncGeneratorObject>();
// Async Iteration proposal 6.4.3.2 AsyncGeneratorStart.
// Step 6.
asyncGenObj->setGenerator(generatorVal);
@ -391,11 +383,10 @@ const Class AsyncGeneratorRequest::class_ = {
AsyncGeneratorRequest::create(JSContext* cx, CompletionKind completionKind,
HandleValue completionValue, HandleObject promise)
{
RootedObject obj(cx, NewNativeObjectWithGivenProto(cx, &class_, nullptr));
if (!obj)
AsyncGeneratorRequest* request = NewObjectWithGivenProto<AsyncGeneratorRequest>(cx, nullptr);
if (!request)
return nullptr;
Handle<AsyncGeneratorRequest*> request = obj.as<AsyncGeneratorRequest>();
request->init(completionKind, completionValue, promise);
return request;
}

View File

@ -62,10 +62,8 @@ class AsyncGeneratorRequest : public NativeObject
Slots,
};
void init(CompletionKind completionKind, HandleValue completionValue,
HandleObject promise) {
setFixedSlot(Slot_CompletionKind,
Int32Value(static_cast<int32_t>(completionKind)));
void init(CompletionKind completionKind, const Value& completionValue, JSObject* promise) {
setFixedSlot(Slot_CompletionKind, Int32Value(static_cast<int32_t>(completionKind)));
setFixedSlot(Slot_CompletionValue, completionValue);
setFixedSlot(Slot_Promise, ObjectValue(*promise));
}

View File

@ -7498,20 +7498,18 @@ DebuggerFrame::initClass(JSContext* cx, HandleObject dbgCtor, HandleObject obj)
DebuggerFrame::create(JSContext* cx, HandleObject proto, const FrameIter& iter,
HandleNativeObject debugger)
{
JSObject* obj = NewObjectWithGivenProto(cx, &DebuggerFrame::class_, proto);
if (!obj)
DebuggerFrame* frame = NewObjectWithGivenProto<DebuggerFrame>(cx, proto);
if (!frame)
return nullptr;
DebuggerFrame& frame = obj->as<DebuggerFrame>();
FrameIter::Data* data = iter.copyData();
if (!data)
return nullptr;
frame.setPrivate(data);
frame->setPrivate(data);
frame.setReservedSlot(JSSLOT_DEBUGFRAME_OWNER, ObjectValue(*debugger));
frame->setReservedSlot(JSSLOT_DEBUGFRAME_OWNER, ObjectValue(*debugger));
return &frame;
return frame;
}
/* static */ bool
@ -8374,7 +8372,7 @@ DebuggerArguments::create(JSContext* cx, HandleObject proto, HandleDebuggerFrame
{
AbstractFramePtr referent = DebuggerFrame::getReferent(frame);
RootedNativeObject obj(cx, NewNativeObjectWithGivenProto(cx, &DebuggerArguments::class_, proto));
Rooted<DebuggerArguments*> obj(cx, NewObjectWithGivenProto<DebuggerArguments>(cx, proto));
if (!obj)
return nullptr;
@ -8407,7 +8405,7 @@ DebuggerArguments::create(JSContext* cx, HandleObject proto, HandleDebuggerFrame
getobj->setExtendedSlot(0, Int32Value(i));
}
return &obj->as<DebuggerArguments>();
return obj;
}
/* static */ bool
@ -9806,15 +9804,14 @@ DebuggerObject::create(JSContext* cx, HandleObject proto, HandleObject referent,
HandleNativeObject debugger)
{
NewObjectKind newKind = IsInsideNursery(referent) ? GenericObject : TenuredObject;
JSObject* obj = NewObjectWithGivenProto(cx, &DebuggerObject::class_, proto, newKind);
DebuggerObject* obj = NewObjectWithGivenProto<DebuggerObject>(cx, proto, newKind);
if (!obj)
return nullptr;
DebuggerObject& object = obj->as<DebuggerObject>();
object.setPrivateGCThing(referent);
object.setReservedSlot(JSSLOT_DEBUGOBJECT_OWNER, ObjectValue(*debugger));
obj->setPrivateGCThing(referent);
obj->setReservedSlot(JSSLOT_DEBUGOBJECT_OWNER, ObjectValue(*debugger));
return &object;
return obj;
}
bool
@ -11051,15 +11048,14 @@ DebuggerEnvironment::create(JSContext* cx, HandleObject proto, HandleObject refe
HandleNativeObject debugger)
{
NewObjectKind newKind = IsInsideNursery(referent) ? GenericObject : TenuredObject;
RootedObject obj(cx, NewObjectWithGivenProto(cx, &DebuggerEnvironment::class_, proto, newKind));
DebuggerEnvironment* obj = NewObjectWithGivenProto<DebuggerEnvironment>(cx, proto, newKind);
if (!obj)
return nullptr;
DebuggerEnvironment& environment = obj->as<DebuggerEnvironment>();
environment.setPrivateGCThing(referent);
environment.setReservedSlot(OWNER_SLOT, ObjectValue(*debugger));
obj->setPrivateGCThing(referent);
obj->setReservedSlot(OWNER_SLOT, ObjectValue(*debugger));
return &environment;
return obj;
}
/* static */ DebuggerEnvironmentType

View File

@ -40,14 +40,14 @@ DebuggerMemory::create(JSContext* cx, Debugger* dbg)
{
Value memoryProtoValue = dbg->object->getReservedSlot(Debugger::JSSLOT_DEBUG_MEMORY_PROTO);
RootedObject memoryProto(cx, &memoryProtoValue.toObject());
RootedNativeObject memory(cx, NewNativeObjectWithGivenProto(cx, &class_, memoryProto));
Rooted<DebuggerMemory*> memory(cx, NewObjectWithGivenProto<DebuggerMemory>(cx, memoryProto));
if (!memory)
return nullptr;
dbg->object->setReservedSlot(Debugger::JSSLOT_DEBUG_MEMORY_INSTANCE, ObjectValue(*memory));
memory->setReservedSlot(JSSLOT_DEBUGGER, ObjectValue(*dbg->object));
return &memory->as<DebuggerMemory>();
return memory;
}
Debugger*

View File

@ -36,12 +36,10 @@ GeneratorObject::create(JSContext* cx, AbstractFramePtr frame)
if (!proto)
return nullptr;
}
RootedNativeObject obj(cx,
NewNativeObjectWithGivenProto(cx, &GeneratorObject::class_, proto));
if (!obj)
Rooted<GeneratorObject*> genObj(cx, NewObjectWithGivenProto<GeneratorObject>(cx, proto));
if (!genObj)
return nullptr;
GeneratorObject* genObj = &obj->as<GeneratorObject>();
genObj->setCallee(*frame.callee());
genObj->setNewTarget(frame.newTarget());
genObj->setEnvironmentChain(*frame.environmentChain());
@ -49,7 +47,7 @@ GeneratorObject::create(JSContext* cx, AbstractFramePtr frame)
genObj->setArgsObj(frame.argsObj());
genObj->clearExpressionStack();
return obj;
return genObj;
}
bool

View File

@ -2141,11 +2141,10 @@ NewFunctionClone(JSContext* cx, HandleFunction fun, NewObjectKind newKind,
return nullptr;
}
JSObject* cloneobj = NewObjectWithClassProto(cx, &JSFunction::class_, cloneProto,
allocKind, newKind);
if (!cloneobj)
RootedFunction clone(cx);
clone = NewObjectWithClassProto<JSFunction>(cx, cloneProto, allocKind, newKind);
if (!clone)
return nullptr;
RootedFunction clone(cx, &cloneobj->as<JSFunction>());
// JSFunction::HAS_INFERRED_NAME can be set at compile-time and at
// runtime. In the latter case we should actually clear the flag before

View File

@ -622,6 +622,17 @@ NewObjectWithGivenTaggedProto(JSContext* cx, Handle<TaggedProto> proto,
return obj ? &obj->as<T>() : nullptr;
}
template <typename T>
inline T*
NewObjectWithGivenTaggedProto(JSContext* cx, Handle<TaggedProto> proto,
gc::AllocKind allocKind, NewObjectKind newKind = GenericObject,
uint32_t initialShapeFlags = 0)
{
JSObject* obj = NewObjectWithGivenTaggedProto(cx, &T::class_, proto, allocKind, newKind,
initialShapeFlags);
return obj ? &obj->as<T>() : nullptr;
}
template <typename T>
inline T*
NewObjectWithNullTaggedProto(JSContext* cx, NewObjectKind newKind = GenericObject,
@ -633,10 +644,9 @@ NewObjectWithNullTaggedProto(JSContext* cx, NewObjectKind newKind = GenericObjec
inline JSObject*
NewObjectWithGivenProto(JSContext* cx, const Class* clasp, HandleObject proto,
gc::AllocKind allocKind, NewObjectKind newKind)
gc::AllocKind allocKind, NewObjectKind newKind = GenericObject)
{
return NewObjectWithGivenTaggedProto(cx, clasp, AsTaggedProto(proto), allocKind,
newKind);
return NewObjectWithGivenTaggedProto(cx, clasp, AsTaggedProto(proto), allocKind, newKind);
}
inline JSObject*

View File

@ -998,7 +998,7 @@ CreateThisForFunctionWithGroup(JSContext* cx, HandleObjectGroup group,
if (newKind == SingletonObject) {
Rooted<TaggedProto> protoRoot(cx, group->proto());
return NewObjectWithGivenTaggedProto(cx, &PlainObject::class_, protoRoot, allocKind, newKind);
return NewObjectWithGivenTaggedProto<PlainObject>(cx, protoRoot, allocKind, newKind);
}
return NewObjectWithGroup<PlainObject>(cx, group, allocKind, newKind);
}

View File

@ -1367,10 +1367,9 @@ const Class ScriptSourceObject::class_ = {
ScriptSourceObject*
ScriptSourceObject::create(JSContext* cx, ScriptSource* source)
{
RootedObject object(cx, NewObjectWithGivenProto(cx, &class_, nullptr));
if (!object)
RootedScriptSource sourceObject(cx, NewObjectWithGivenProto<ScriptSourceObject>(cx, nullptr));
if (!sourceObject)
return nullptr;
RootedScriptSource sourceObject(cx, &object->as<ScriptSourceObject>());
source->incref(); // The matching decref is in ScriptSourceObject::finalize.
sourceObject->initReservedSlot(SOURCE_SLOT, PrivateValue(source));

View File

@ -566,12 +566,7 @@ SavedFrame::create(JSContext* cx)
return nullptr;
assertSameCompartment(cx, proto);
RootedObject frameObj(cx, NewObjectWithGivenProto(cx, &SavedFrame::class_, proto,
TenuredObject));
if (!frameObj)
return nullptr;
return &frameObj->as<SavedFrame>();
return NewObjectWithGivenProto<SavedFrame>(cx, proto, TenuredObject);
}
bool

View File

@ -32,13 +32,12 @@ StringObject::init(JSContext* cx, Handle<StringObject*> obj, HandleString str)
/* static */ inline StringObject*
StringObject::create(JSContext* cx, HandleString str, HandleObject proto, NewObjectKind newKind)
{
JSObject* obj = NewObjectWithClassProto(cx, &class_, proto, newKind);
Rooted<StringObject*> obj(cx, NewObjectWithClassProto<StringObject>(cx, proto, newKind));
if (!obj)
return nullptr;
Rooted<StringObject*> strobj(cx, &obj->as<StringObject>());
if (!StringObject::init(cx, strobj, str))
if (!StringObject::init(cx, obj, str))
return nullptr;
return strobj;
return obj;
}
} // namespace js

View File

@ -403,7 +403,7 @@ class TypedArrayObjectTemplate : public TypedArrayObject
{
MOZ_ASSERT(proto);
JSObject* obj = NewObjectWithClassProto(cx, instanceClass(), proto, allocKind);
JSObject* obj = NewObjectWithGivenProto(cx, instanceClass(), proto, allocKind);
return obj ? &obj->as<TypedArrayObject>() : nullptr;
}