From 8f66c463324b9837202ff146642ebff4f628a82e Mon Sep 17 00:00:00 2001 From: Bobby Holley Date: Thu, 19 Jun 2014 09:57:05 -0700 Subject: [PATCH] Bug 976148 - Add a mechanism to identify a standard constructor. r=luke --- js/src/jsapi.h | 5 ++++- js/src/jsobj.cpp | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/js/src/jsapi.h b/js/src/jsapi.h index f0708cd21ce4..f9d46b583f3b 100644 --- a/js/src/jsapi.h +++ b/js/src/jsapi.h @@ -1764,7 +1764,7 @@ JS_GetClassPrototype(JSContext *cx, JSProtoKey key, JS::MutableHandle 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) diff --git a/js/src/jsobj.cpp b/js/src/jsobj.cpp index 1e1dd7b62896..071e7ff3ca04 100644 --- a/js/src/jsobj.cpp +++ b/js/src/jsobj.cpp @@ -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() || !(obj->as().flags() & JSFunction::NATIVE_CTOR)) + return JSProto_Null; + + GlobalObject &global = obj->global(); + for (size_t k = 0; k < JSProto_LIMIT; ++k) { + JSProtoKey key = static_cast(k); + if (global.getConstructor(key) == ObjectValue(*obj)) + return key; + } + + return JSProto_Null; +} + bool js::FindClassObject(ExclusiveContext *cx, MutableHandleObject protop, const Class *clasp) {