Bug 698394 - Pass JSObject handler to nsIScriptContext::BindCompiledEventHandler; r=sicking

This commit is contained in:
Ms2ger 2011-11-26 11:11:35 +01:00
parent 1404baaf43
commit b744235be1
7 changed files with 25 additions and 16 deletions

View File

@ -701,7 +701,7 @@ nsEventListenerManager::CompileEventHandlerInternal(nsListenerStruct *aListenerS
// Bind it
nsScriptObjectHolder boundHandler(context);
context->BindCompiledEventHandler(mTarget, listener->GetEventScope(),
handler, boundHandler);
handler.getObject(), boundHandler);
listener->SetHandler(
static_cast<JSObject*>(
static_cast<void*>(boundHandler)));

View File

@ -323,7 +323,7 @@ nsXBLPrototypeHandler::ExecuteHandler(nsIDOMEventTarget* aTarget,
JSObject* scope = boundGlobal->GetGlobalJSObject();
nsScriptObjectHolder boundHandler(boundContext);
rv = boundContext->BindCompiledEventHandler(scriptTarget, scope,
handler, boundHandler);
handler.getObject(), boundHandler);
NS_ENSURE_SUCCESS(rv, rv);
// Execute it.

View File

@ -86,6 +86,9 @@ public:
JSScript* getScript() const {
return static_cast<JSScript*>(mObject);
}
JSObject* getObject() const {
return static_cast<JSObject*>(mObject);
}
// Drop the script object - but *not* the nsIScriptContext.
nsresult drop() {
@ -96,6 +99,10 @@ public:
}
return rv;
}
nsresult setObject(JSObject* aObject) {
return set(aObject);
}
nsresult set(void *object) {
NS_ASSERTION(getScriptTypeID() != nsIProgrammingLanguage::UNKNOWN,
"Must know the language!");

View File

@ -9305,7 +9305,7 @@ nsGlobalWindow::RunTimeout(nsTimeout *aTimeout)
}
nsCOMPtr<nsIScriptTimeoutHandler> handler(timeout->mScriptHandler);
void *scriptObject = handler->GetScriptObject();
JSObject* scriptObject = static_cast<JSObject*>(handler->GetScriptObject());
if (!scriptObject) {
// Evaluate the timeout expression.
const PRUnichar *script = handler->GetHandlerText();

View File

@ -74,8 +74,8 @@ NS_DEFINE_STATIC_IID_ACCESSOR(nsIScriptContextPrincipal,
NS_ISCRIPTCONTEXTPRINCIPAL_IID)
#define NS_ISCRIPTCONTEXT_IID \
{ 0x1843adb8, 0xd646, 0x4103, \
{ 0x8d, 0xa3, 0xc5, 0x12, 0xb9, 0xca, 0xfb, 0xcd } }
{ 0xe0ff5703, 0xc92b, 0x46b5, \
{ 0x87, 0x82, 0xd0, 0xdc, 0x61, 0xba, 0x44, 0x03 } }
/* This MUST match JSVERSION_DEFAULT. This version stuff if we don't
know what language we have is a little silly... */
@ -247,7 +247,7 @@ public:
*/
virtual nsresult BindCompiledEventHandler(nsISupports* aTarget,
JSObject* aScope,
void* aHandler,
JSObject* aHandler,
nsScriptObjectHolder& aBoundHandler) = 0;
/**

View File

@ -1966,7 +1966,7 @@ nsJSContext::CallEventHandler(nsISupports* aTarget, JSObject* aScope,
nsresult
nsJSContext::BindCompiledEventHandler(nsISupports* aTarget, JSObject* aScope,
void *aHandler,
JSObject* aHandler,
nsScriptObjectHolder& aBoundHandler)
{
NS_ENSURE_ARG(aHandler);
@ -1980,17 +1980,15 @@ nsJSContext::BindCompiledEventHandler(nsISupports* aTarget, JSObject* aScope,
nsresult rv = JSObjectFromInterface(aTarget, aScope, &target);
NS_ENSURE_SUCCESS(rv, rv);
JSObject *funobj = (JSObject*) aHandler;
#ifdef DEBUG
{
JSAutoEnterCompartment ac;
if (!ac.enter(mContext, funobj)) {
if (!ac.enter(mContext, aHandler)) {
return NS_ERROR_FAILURE;
}
NS_ASSERTION(JS_TypeOfValue(mContext,
OBJECT_TO_JSVAL(funobj)) == JSTYPE_FUNCTION,
OBJECT_TO_JSVAL(aHandler)) == JSTYPE_FUNCTION,
"Event handler object not a function");
}
#endif
@ -2000,14 +1998,18 @@ nsJSContext::BindCompiledEventHandler(nsISupports* aTarget, JSObject* aScope,
return NS_ERROR_FAILURE;
}
JSObject* funobj;
// Make sure the handler function is parented by its event target object
if (funobj) { // && ::JS_GetParent(mContext, funobj) != target) {
funobj = ::JS_CloneFunctionObject(mContext, funobj, target);
if (!funobj)
if (aHandler) {
funobj = JS_CloneFunctionObject(mContext, aHandler, target);
if (!funobj) {
rv = NS_ERROR_OUT_OF_MEMORY;
}
} else {
funobj = NULL;
}
aBoundHandler.set(funobj);
aBoundHandler.setObject(funobj);
return rv;
}

View File

@ -113,7 +113,7 @@ public:
nsIArray *argv, nsIVariant **rv);
virtual nsresult BindCompiledEventHandler(nsISupports *aTarget,
JSObject *aScope,
void *aHandler,
JSObject* aHandler,
nsScriptObjectHolder& aBoundHandler);
virtual nsresult CompileFunction(JSObject* aTarget,
const nsACString& aName,