Bug 41855. Hold on to the nsIScriptContext instead of the raw JSContext. This ensures that the JSContext won't be yanked out from beneath us. r=shaver

This commit is contained in:
waterson%netscape.com 2000-06-16 21:43:04 +00:00
parent 1229310eb4
commit b176726371
2 changed files with 25 additions and 24 deletions

View File

@ -25,6 +25,7 @@
#include "nsIScriptSecurityManager.h"
#include "nsIScriptGlobalObject.h"
#include "nsJSUtils.h"
#include "nsIServiceManager.h"
#include "jsapi.h"
static NS_DEFINE_IID(kIScriptEventListenerIID, NS_ISCRIPTEVENTLISTENER_IID);
@ -34,12 +35,14 @@ static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
/*
* nsJSDOMEventListener implementation
*/
nsJSDOMEventListener::nsJSDOMEventListener(JSContext *aContext, JSObject *aTarget, JSObject *aHandler)
nsJSDOMEventListener::nsJSDOMEventListener(nsIScriptContext* aContext,
JSObject *aTarget,
JSObject *aHandler)
: mContext(aContext),
mTarget(aTarget),
mHandler(aHandler)
{
NS_INIT_REFCNT();
mContext = aContext;
mTarget = aTarget;
mHandler = aHandler;
}
nsJSDOMEventListener::~nsJSDOMEventListener()
@ -48,7 +51,7 @@ nsJSDOMEventListener::~nsJSDOMEventListener()
nsresult nsJSDOMEventListener::QueryInterface(REFNSIID aIID, void** aInstancePtr)
{
if (NULL == aInstancePtr) {
if (! aInstancePtr) {
return NS_ERROR_NULL_POINTER;
}
if (aIID.Equals(kIDOMEventListenerIID)) {
@ -75,23 +78,14 @@ NS_IMPL_RELEASE(nsJSDOMEventListener)
nsresult nsJSDOMEventListener::HandleEvent(nsIDOMEvent* aEvent)
{
jsval argv[1];
JSObject *eventObj;
nsCOMPtr<nsIScriptContext> scriptCX;
nsJSUtils::nsGetStaticScriptContext(mContext, mTarget,
getter_AddRefs(scriptCX));
if (!scriptCX) {
if (NS_OK != NS_NewScriptKeyEvent(mContext, aEvent, nsnull, (void**)&eventObj))
return NS_ERROR_FAILURE;
}
if (NS_OK != NS_NewScriptKeyEvent(scriptCX, aEvent, nsnull, (void**)&eventObj)) {
return NS_ERROR_FAILURE;
}
jsval argv[1];
argv[0] = OBJECT_TO_JSVAL(eventObj);
PRBool jsBoolResult;
if (NS_FAILED(scriptCX->CallEventHandler(mTarget, mHandler, 1, argv, &jsBoolResult, PR_FALSE))) {
if (NS_FAILED(mContext->CallEventHandler(mTarget, mHandler, 1, argv, &jsBoolResult, PR_FALSE))) {
return NS_ERROR_FAILURE;
}
return jsBoolResult ? NS_OK : NS_ERROR_FAILURE;
@ -116,10 +110,16 @@ NS_NewScriptEventListener(nsIDOMEventListener ** aInstancePtrResult,
void* aTarget,
void *aHandler)
{
JSContext *mCX = (JSContext*)aContext->GetNativeContext();
nsJSDOMEventListener* it = new nsJSDOMEventListener(mCX, (JSObject*)aTarget, (JSObject*)aHandler);
if (NULL == it) {
NS_PRECONDITION(aContext != nsnull, "null ptr");
if (! aContext)
return NS_ERROR_NULL_POINTER;
nsJSDOMEventListener* it =
new nsJSDOMEventListener(aContext,
NS_STATIC_CAST(JSObject*, aTarget),
NS_STATIC_CAST(JSObject*, aHandler));
if (! it) {
return NS_ERROR_OUT_OF_MEMORY;
}

View File

@ -23,7 +23,9 @@
#ifndef nsJSEventListener_h__
#define nsJSEventListener_h__
#include "nsCOMPtr.h"
#include "nsIDOMKeyEvent.h"
#include "nsIScriptContext.h"
#include "nsIScriptEventListener.h"
#include "nsIDOMMouseListener.h"
#include "jsapi.h"
@ -31,7 +33,7 @@
//nsIDOMMouseListener interface
class nsJSDOMEventListener : public nsIDOMEventListener, public nsIScriptEventListener {
public:
nsJSDOMEventListener(JSContext *aContext, JSObject *aTarget, JSObject *aHandler);
nsJSDOMEventListener(nsIScriptContext* aContext, JSObject *aTarget, JSObject *aHandler);
virtual ~nsJSDOMEventListener();
NS_DECL_ISUPPORTS
@ -43,10 +45,9 @@ public:
virtual nsresult CheckIfEqual(nsIScriptEventListener *aListener);
protected:
JSContext *mContext;
nsCOMPtr<nsIScriptContext> mContext;
JSObject *mTarget;
JSObject *mHandler;
};
#endif //nsJSEventListener_h__