Changed method signature of EvaluateScript to return nsresult. Added nsJSThreadContextStack pushing and popping around script evaluation.

This commit is contained in:
vidur%netscape.com 1999-08-19 20:36:31 +00:00
parent dfdf78f01d
commit 43c7cb9748
10 changed files with 87 additions and 44 deletions

View File

@ -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);

View File

@ -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.

View File

@ -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

View File

@ -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*)

View File

@ -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();

View File

@ -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;
}

View File

@ -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) {

View File

@ -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) {

View File

@ -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);

View File

@ -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;