From 43c7cb974863e473e85f790d661b28aca66cefd3 Mon Sep 17 00:00:00 2001 From: "vidur%netscape.com" Date: Thu, 19 Aug 1999 20:36:31 +0000 Subject: [PATCH] Changed method signature of EvaluateScript to return nsresult. Added nsJSThreadContextStack pushing and popping around script evaluation. --- content/xml/document/src/nsXMLContentSink.cpp | 4 +- dom/public/nsIScriptContext.h | 12 ++--- dom/src/base/nsGlobalWindow.cpp | 13 +++++ dom/src/base/nsJSEnvironment.cpp | 54 ++++++++++++------- dom/src/base/nsJSEnvironment.h | 10 ++-- dom/src/events/nsJSEventListener.cpp | 22 ++++++-- dom/src/jsurl/jsurl.cpp | 4 +- dom/src/jsurl/nsJSProtocolHandler.cpp | 4 +- layout/xml/document/src/nsXMLContentSink.cpp | 4 +- webshell/tests/viewer/JSConsole.cpp | 4 +- 10 files changed, 87 insertions(+), 44 deletions(-) diff --git a/content/xml/document/src/nsXMLContentSink.cpp b/content/xml/document/src/nsXMLContentSink.cpp index 5986b8a9def5..9b8a2c4214ee 100644 --- a/content/xml/document/src/nsXMLContentSink.cpp +++ b/content/xml/document/src/nsXMLContentSink.cpp @@ -1676,8 +1676,8 @@ nsXMLContentSink::EvaluateScript(nsString& aScript, PRUint32 aLineNo) nsAutoString val; PRBool isUndefined; - PRBool result = context->EvaluateString(aScript, url, aLineNo, - val, &isUndefined); + nsresult result = context->EvaluateString(aScript, url, aLineNo, + val, &isUndefined); NS_IF_RELEASE(docURL); diff --git a/dom/public/nsIScriptContext.h b/dom/public/nsIScriptContext.h index 713a30fa805d..f0bba4fd121b 100644 --- a/dom/public/nsIScriptContext.h +++ b/dom/public/nsIScriptContext.h @@ -50,14 +50,14 @@ public: * @param aScriptSize the length of aScript * @param aRetValue return value * - * @return true if the script was valid and got executed + * @return NS_OK if the script was valid and got executed * **/ - NS_IMETHOD_(PRBool) EvaluateString(const nsString& aScript, - const char *aURL, - PRUint32 aLineNo, - nsString& aRetValue, - PRBool* aIsUndefined) = 0; + NS_IMETHOD EvaluateString(const nsString& aScript, + const char *aURL, + PRUint32 aLineNo, + nsString& aRetValue, + PRBool* aIsUndefined) = 0; /** * Return the global object. diff --git a/dom/src/base/nsGlobalWindow.cpp b/dom/src/base/nsGlobalWindow.cpp index 6e347aea1b58..efd1e4069118 100644 --- a/dom/src/base/nsGlobalWindow.cpp +++ b/dom/src/base/nsGlobalWindow.cpp @@ -66,6 +66,7 @@ #include "nsHistory.h" #include "nsBarProps.h" #include "nsIScriptSecurityManager.h" +#include "nsIJSContextStack.h" #ifndef NECKO #include "nsINetService.h" #else @@ -1623,6 +1624,7 @@ GlobalWindowImpl::RunTimeout(nsTimeoutImpl *aTimeout) PRInt64 now; jsval result; nsITimer *timer; + nsresult rv; /* Make sure that the window or the script context don't go away as a result of running timeouts */ @@ -1680,6 +1682,16 @@ GlobalWindowImpl::RunTimeout(nsTimeoutImpl *aTimeout) HoldTimeout(timeout); mRunningTimeout = timeout; + NS_WITH_SERVICE(nsIJSContextStack, stack, "nsThreadJSContextStack", &rv); + if (NS_FAILED(rv)) { + NS_RELEASE(temp); + NS_RELEASE(tempContext); + return PR_TRUE; + } + + rv = stack->Push(cx); + // XXX Should check for rv. If failed, then what? + if (timeout->expr) { /* Evaluate the timeout expression. */ #if 0 @@ -1716,6 +1728,7 @@ GlobalWindowImpl::RunTimeout(nsTimeoutImpl *aTimeout) } tempContext->ScriptEvaluated(); + rv = stack->Pop(nsnull); mRunningTimeout = nsnull; /* If the temporary reference is the only one that is keeping diff --git a/dom/src/base/nsJSEnvironment.cpp b/dom/src/base/nsJSEnvironment.cpp index 3f969970e85a..74d0f4f94337 100644 --- a/dom/src/base/nsJSEnvironment.cpp +++ b/dom/src/base/nsJSEnvironment.cpp @@ -38,6 +38,7 @@ #include "nsIServiceManager.h" #include "nsIXPConnect.h" #include "nsIXPCSecurityManager.h" +#include "nsIJSContextStack.h" #if defined(OJI) #include "nsIJVMManager.h" @@ -123,7 +124,7 @@ nsJSContext::~nsJSContext() NS_IMPL_ISUPPORTS(nsJSContext, kIScriptContextIID); -NS_IMETHODIMP_(PRBool) +NS_IMETHODIMP nsJSContext::EvaluateString(const nsString& aScript, const char *aURL, PRUint32 aLineNo, @@ -134,7 +135,8 @@ nsJSContext::EvaluateString(const nsString& aScript, nsIScriptGlobalObject *global = GetGlobalObject(); nsIScriptGlobalObjectData *globalData; JSPrincipals* principals = nsnull; - + nsresult rv = NS_OK; + if (global && NS_SUCCEEDED(global->QueryInterface(kIScriptGlobalObjectDataIID, (void**)&globalData))) { if (NS_FAILED(globalData->GetPrincipals((void**)&principals))) { NS_RELEASE(global); @@ -145,27 +147,39 @@ nsJSContext::EvaluateString(const nsString& aScript, } NS_IF_RELEASE(global); - PRBool ret = ::JS_EvaluateUCScriptForPrincipals(mContext, - JS_GetGlobalObject(mContext), - principals, - (jschar*)aScript.GetUnicode(), - aScript.Length(), - aURL, - aLineNo, - &val); + NS_WITH_SERVICE(nsIJSContextStack, stack, "nsThreadJSContextStack", &rv); + if (NS_FAILED(rv)) { + return rv; + } + + rv = stack->Push(mContext); + if (NS_FAILED(rv)) { + return rv; + } + + PRBool ret = ::JS_EvaluateUCScriptForPrincipals(mContext, + JS_GetGlobalObject(mContext), + principals, + (jschar*)aScript.GetUnicode(), + aScript.Length(), + aURL, + aLineNo, + &val); - if (ret) { - *aIsUndefined = JSVAL_IS_VOID(val); - JSString* jsstring = JS_ValueToString(mContext, val); - aRetValue.SetString(JS_GetStringChars(jsstring)); - } - else { - aRetValue.Truncate(); - } + if (ret) { + *aIsUndefined = JSVAL_IS_VOID(val); + JSString* jsstring = JS_ValueToString(mContext, val); + aRetValue.SetString(JS_GetStringChars(jsstring)); + } + else { + aRetValue.Truncate(); + } - ScriptEvaluated(); + ScriptEvaluated(); + + rv = stack->Pop(nsnull); - return ret; + return rv; } NS_IMETHODIMP_(nsIScriptGlobalObject*) diff --git a/dom/src/base/nsJSEnvironment.h b/dom/src/base/nsJSEnvironment.h index a3ca79b8da72..022a27d35e7b 100644 --- a/dom/src/base/nsJSEnvironment.h +++ b/dom/src/base/nsJSEnvironment.h @@ -38,11 +38,11 @@ public: NS_DECL_ISUPPORTS - NS_IMETHOD_(PRBool) EvaluateString(const nsString& aScript, - const char *aURL, - PRUint32 aLineNo, - nsString& aRetValue, - PRBool* aIsUndefined); + NS_IMETHOD EvaluateString(const nsString& aScript, + const char *aURL, + PRUint32 aLineNo, + nsString& aRetValue, + PRBool* aIsUndefined); NS_IMETHOD_(nsIScriptGlobalObject*) GetGlobalObject(); NS_IMETHOD_(void*) GetNativeContext(); NS_IMETHOD InitClasses(); diff --git a/dom/src/events/nsJSEventListener.cpp b/dom/src/events/nsJSEventListener.cpp index 25f639a6846e..c93fa64b4911 100644 --- a/dom/src/events/nsJSEventListener.cpp +++ b/dom/src/events/nsJSEventListener.cpp @@ -18,6 +18,8 @@ #include "nsJSEventListener.h" #include "nsString.h" #include "nsIScriptEventListener.h" +#include "nsIServiceManager.h" +#include "nsIJSContextStack.h" static NS_DEFINE_IID(kIDOMEventListenerIID, NS_IDOMEVENTLISTENER_IID); static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); @@ -67,6 +69,7 @@ nsresult nsJSEventListener::HandleEvent(nsIDOMEvent* aEvent) JSObject *mEventObj; char* mEventChars; nsString mEventString; + nsresult rv; if (NS_OK != aEvent->GetType(mEventString)) { //JS can't handle this event yet or can't handle it at all @@ -93,15 +96,28 @@ nsresult nsJSEventListener::HandleEvent(nsIDOMEvent* aEvent) return NS_ERROR_FAILURE; } + NS_WITH_SERVICE(nsIJSContextStack, stack, "nsThreadJSContextStack", &rv); + if (NS_FAILED(rv)) { + return rv; + } + + rv = stack->Push(mContext); + if (NS_FAILED(rv)) { + return rv; + } + argv[0] = OBJECT_TO_JSVAL(mEventObj); - if (PR_TRUE == JS_CallFunctionValue(mContext, mJSObj, funval, 1, argv, &result)) { - mScriptCX->ScriptEvaluated(); + PRBool ok = JS_CallFunctionValue(mContext, mJSObj, funval, 1, argv, &result); + + mScriptCX->ScriptEvaluated(); + rv = stack->Pop(nsnull); + + if (PR_TRUE == ok) { if (JSVAL_IS_BOOLEAN(result) && JSVAL_TO_BOOLEAN(result) == JS_FALSE) { aEvent->PreventDefault(); } return NS_OK; } - mScriptCX->ScriptEvaluated(); return NS_ERROR_FAILURE; } diff --git a/dom/src/jsurl/jsurl.cpp b/dom/src/jsurl/jsurl.cpp index 07ae3728452a..3515101b7f41 100644 --- a/dom/src/jsurl/jsurl.cpp +++ b/dom/src/jsurl/jsurl.cpp @@ -168,10 +168,10 @@ evaluate_script(URL_Struct* urls, const char *what, JSConData *con_data) nsAutoString ret; PRBool isUndefined; - if (script_context->EvaluateString(nsString(what), + if (NS_SUCCEEDED(script_context->EvaluateString(nsString(what), nsnull, 0, ret, - &isUndefined)) { + &isUndefined))) { JSContext *cx = (JSContext *)script_context->GetNativeContext(); // Find out if it can be converted into a string if (!isUndefined) { diff --git a/dom/src/jsurl/nsJSProtocolHandler.cpp b/dom/src/jsurl/nsJSProtocolHandler.cpp index 3799a451bc3f..6aefffcd8e12 100644 --- a/dom/src/jsurl/nsJSProtocolHandler.cpp +++ b/dom/src/jsurl/nsJSProtocolHandler.cpp @@ -105,11 +105,11 @@ public: nsAutoString ret; PRBool isUndefined; - PRBool ok; + nsresult ok; ok = scriptContext->EvaluateString(nsString(jsExpr), nsnull, 0, ret, &isUndefined); nsCRT::free(jsExpr); - if (ok) { + if (NS_SUCCEEDED(ok)) { // JSContext* cx = (JSContext*)scriptContext->GetNativeContext(); // Find out if it can be converted into a string if (!isUndefined) { diff --git a/layout/xml/document/src/nsXMLContentSink.cpp b/layout/xml/document/src/nsXMLContentSink.cpp index 5986b8a9def5..9b8a2c4214ee 100644 --- a/layout/xml/document/src/nsXMLContentSink.cpp +++ b/layout/xml/document/src/nsXMLContentSink.cpp @@ -1676,8 +1676,8 @@ nsXMLContentSink::EvaluateScript(nsString& aScript, PRUint32 aLineNo) nsAutoString val; PRBool isUndefined; - PRBool result = context->EvaluateString(aScript, url, aLineNo, - val, &isUndefined); + nsresult result = context->EvaluateString(aScript, url, aLineNo, + val, &isUndefined); NS_IF_RELEASE(docURL); diff --git a/webshell/tests/viewer/JSConsole.cpp b/webshell/tests/viewer/JSConsole.cpp index 73055f582682..8615ae6bd56a 100644 --- a/webshell/tests/viewer/JSConsole.cpp +++ b/webshell/tests/viewer/JSConsole.cpp @@ -884,11 +884,11 @@ void JSConsole::EvaluateText(UINT aStartSel, UINT aEndSel) nsAutoString returnValue; PRBool isUndefined; - if (mContext->EvaluateString(nsString(cleanBuffer), + if (NS_SUCCEEDED(mContext->EvaluateString(nsString(cleanBuffer), nsnull, 0, returnValue, - &isUndefined)) { + &isUndefined))) { // output the result on the console and on the edit area CHAR result[128]; LPSTR res = result;