Bug 976148 - Add a mechanism to identify a standard constructor. r=luke

This commit is contained in:
Bobby Holley 2014-06-19 09:57:05 -07:00
parent b160b2e1cd
commit aa8f8da6b1
2 changed files with 24 additions and 1 deletions

View File

@ -1764,7 +1764,7 @@ JS_GetClassPrototype(JSContext *cx, JSProtoKey key, JS::MutableHandle<JSObject*>
namespace JS {
/*
* Determine if the given object is an instance or prototype for a standard
* Determine if the given object is an instance/prototype/constructor for a standard
* class. If so, return the associated JSProtoKey. If not, return JSProto_Null.
*/
@ -1777,6 +1777,9 @@ IdentifyStandardPrototype(JSObject *obj);
extern JS_PUBLIC_API(JSProtoKey)
IdentifyStandardInstanceOrPrototype(JSObject *obj);
extern JS_PUBLIC_API(JSProtoKey)
IdentifyStandardConstructor(JSObject *obj);
} /* namespace JS */
extern JS_PUBLIC_API(JSProtoKey)

View File

@ -3439,6 +3439,26 @@ JS::IdentifyStandardInstanceOrPrototype(JSObject *obj)
return JSCLASS_CACHED_PROTO_KEY(obj->getClass());
}
JSProtoKey
JS::IdentifyStandardConstructor(JSObject *obj)
{
// Note that NATIVE_CTOR does not imply that we are a standard constructor,
// but the converse is true (at least until we start having self-hosted
// constructors for standard classes). This lets us avoid a costly loop for
// many functions (which, depending on the call site, may be the common case).
if (!obj->is<JSFunction>() || !(obj->as<JSFunction>().flags() & JSFunction::NATIVE_CTOR))
return JSProto_Null;
GlobalObject &global = obj->global();
for (size_t k = 0; k < JSProto_LIMIT; ++k) {
JSProtoKey key = static_cast<JSProtoKey>(k);
if (global.getConstructor(key) == ObjectValue(*obj))
return key;
}
return JSProto_Null;
}
bool
js::FindClassObject(ExclusiveContext *cx, MutableHandleObject protop, const Class *clasp)
{