Bug 910823 - Factor out the code for casting from a JSClass* to an nsXBLJSClass* into a helper function. r=waldo

This commit is contained in:
Dan Gohman 2013-09-11 05:49:04 -07:00
parent ddb5923fdc
commit 5dcee75fad
3 changed files with 31 additions and 5 deletions

View File

@ -76,7 +76,7 @@ XBLFinalize(JSFreeOp *fop, JSObject *obj)
static_cast<nsXBLDocumentInfo*>(::JS_GetPrivate(obj));
nsContentUtils::DeferredFinalize(docInfo);
nsXBLJSClass* c = static_cast<nsXBLJSClass*>(::JS_GetClass(obj));
nsXBLJSClass* c = nsXBLJSClass::fromJSClass(::JS_GetClass(obj));
c->Drop();
}
@ -138,6 +138,19 @@ nsXBLJSClass::Destroy()
return 0;
}
nsXBLJSClass*
nsXBLService::getClass(const nsCString& k)
{
nsCStringKey key(k);
return getClass(&key);
}
nsXBLJSClass*
nsXBLService::getClass(nsCStringKey *k)
{
return static_cast<nsXBLJSClass*>(nsXBLService::gClassTable->Get(k));
}
// Implementation /////////////////////////////////////////////////////////////////
// Constructors/Destructors
@ -926,9 +939,8 @@ nsXBLBinding::DoInitJSClass(JSContext *cx, JS::Handle<JSObject*> global,
PR_snprintf(buf, sizeof(buf), " %llx", parent_proto_id.get());
}
xblKey.Append(buf);
nsCStringKey key(xblKey);
c = static_cast<nsXBLJSClass*>(nsXBLService::gClassTable->Get(&key));
c = nsXBLService::getClass(xblKey);
if (c) {
className.Assign(c->name);
} else {
@ -954,7 +966,7 @@ nsXBLBinding::DoInitJSClass(JSContext *cx, JS::Handle<JSObject*> global,
nsCStringKey key(xblKey);
if (!c) {
c = static_cast<nsXBLJSClass*>(nsXBLService::gClassTable->Get(&key));
c = nsXBLService::getClass(&key);
}
if (c) {
// If c is on the LRU list, remove it now!

View File

@ -61,7 +61,7 @@ nsXBLProtoImpl::InstallImplementation(nsXBLPrototypeBinding* aPrototypeBinding,
MOZ_ASSERT(targetClassObject);
// Stash a strong reference to the JSClass in the binding.
aBinding->SetJSClass(static_cast<nsXBLJSClass*>(JS_GetClass(targetClassObject)));
aBinding->SetJSClass(nsXBLJSClass::fromJSClass(JS_GetClass(targetClassObject)));
// If the prototype already existed, we don't need to install anything. return early.
if (!targetObjectIsNew)

View File

@ -15,6 +15,7 @@
#include "js/Class.h" // nsXBLJSClass derives from JSClass
#include "nsTArray.h"
class nsCStringKey;
class nsXBLBinding;
class nsXBLDocumentInfo;
class nsXBLJSClass;
@ -132,6 +133,10 @@ public:
static bool gAllowDataURIs; // Whether we should allow data
// urls in -moz-binding. Needed for
// testing.
// Look up the class by key in gClassTable.
static nsXBLJSClass *getClass(const nsCString &key);
static nsXBLJSClass *getClass(nsCStringKey *key);
};
class nsXBLJSClass : public mozilla::LinkedListElement<nsXBLJSClass>
@ -156,6 +161,15 @@ public:
nsrefcnt Drop() { return --mRefCnt ? mRefCnt : Destroy(); }
nsrefcnt AddRef() { return Hold(); }
nsrefcnt Release() { return Drop(); }
// Downcast from a pointer to JSClass to a pointer to nsXBLJSClass.
static nsXBLJSClass*
fromJSClass(JSClass* c)
{
nsXBLJSClass* x = static_cast<nsXBLJSClass*>(c);
MOZ_ASSERT(nsXBLService::getClass(x->mKey) == x);
return x;
}
};
#endif