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
Родитель 44bffcd4af
Коммит 8f66c46332
2 изменённых файлов: 24 добавлений и 1 удалений

Просмотреть файл

@ -1764,7 +1764,7 @@ JS_GetClassPrototype(JSContext *cx, JSProtoKey key, JS::MutableHandle<JSObject*>
namespace JS { 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. * class. If so, return the associated JSProtoKey. If not, return JSProto_Null.
*/ */
@ -1777,6 +1777,9 @@ IdentifyStandardPrototype(JSObject *obj);
extern JS_PUBLIC_API(JSProtoKey) extern JS_PUBLIC_API(JSProtoKey)
IdentifyStandardInstanceOrPrototype(JSObject *obj); IdentifyStandardInstanceOrPrototype(JSObject *obj);
extern JS_PUBLIC_API(JSProtoKey)
IdentifyStandardConstructor(JSObject *obj);
} /* namespace JS */ } /* namespace JS */
extern JS_PUBLIC_API(JSProtoKey) extern JS_PUBLIC_API(JSProtoKey)

Просмотреть файл

@ -3439,6 +3439,26 @@ JS::IdentifyStandardInstanceOrPrototype(JSObject *obj)
return JSCLASS_CACHED_PROTO_KEY(obj->getClass()); 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 bool
js::FindClassObject(ExclusiveContext *cx, MutableHandleObject protop, const Class *clasp) js::FindClassObject(ExclusiveContext *cx, MutableHandleObject protop, const Class *clasp)
{ {