зеркало из https://github.com/mozilla/gecko-dev.git
XULDOMJS_19991106_BRANCH landing (15146, r=vidur@netscape.com)
This commit is contained in:
Родитель
367ea33053
Коммит
90c5c9c53e
|
@ -51,7 +51,7 @@ class nsIScriptContext : public nsISupports {
|
|||
public:
|
||||
NS_DEFINE_STATIC_IID_ACCESSOR(NS_ISCRIPTCONTEXT_IID)
|
||||
|
||||
// deprecated: remove later
|
||||
// deprecated: remove later (XXXbe when is later?)
|
||||
NS_IMETHOD EvaluateString(const nsString& aScript,
|
||||
const char *aURL,
|
||||
PRUint32 aLineNo,
|
||||
|
@ -60,14 +60,15 @@ public:
|
|||
PRBool* aIsUndefined) = 0;
|
||||
|
||||
/**
|
||||
* Execute a script.
|
||||
* Compile and execute a script.
|
||||
*
|
||||
* @param aScript a string representing the script to be executed
|
||||
* @param aObj a JavaScript JSObject for the scope to execute in, or nsnull
|
||||
* to use a default scope
|
||||
* @param principal the principal that produced the script
|
||||
* @param aPrincipal the principal that produced the script
|
||||
* @param aURL the URL or filename for error messages
|
||||
* @param aLineNo the starting line number for the script for error messages
|
||||
* @param aVersion the script language version to use when executing
|
||||
* @param aRetValue the result of executing the script
|
||||
* @param aIsUndefined true if the result of executing the script is the
|
||||
* undefined value
|
||||
|
@ -77,13 +78,59 @@ public:
|
|||
**/
|
||||
NS_IMETHOD EvaluateString(const nsString& aScript,
|
||||
void *aObj,
|
||||
nsIPrincipal *principal,
|
||||
nsIPrincipal *aPrincipal,
|
||||
const char *aURL,
|
||||
PRUint32 aLineNo,
|
||||
const char* aVersion,
|
||||
nsString& aRetValue,
|
||||
PRBool* aIsUndefined) = 0;
|
||||
|
||||
/**
|
||||
* Compile a script.
|
||||
*
|
||||
* @param aText a PRUnichar buffer containing script source
|
||||
* @param aTextLength number of characters in aText
|
||||
* @param aScopeObject an object telling the scope in which to execute,
|
||||
* or nsnull to use a default scope
|
||||
* @param aPrincipal the principal that produced the script
|
||||
* @param aURL the URL or filename for error messages
|
||||
* @param aLineNo the starting line number for the script for error messages
|
||||
* @param aVersion the script language version to use when executing
|
||||
* @param aScriptObject an executable object that's the result of compiling
|
||||
* the script. The caller is responsible for GC rooting
|
||||
* this object.
|
||||
*
|
||||
* @return NS_OK if the script source was valid and got compiled
|
||||
*
|
||||
**/
|
||||
NS_IMETHOD CompileScript(const PRUnichar* aText,
|
||||
PRInt32 aTextLength,
|
||||
void *aScopeObject,
|
||||
nsIPrincipal *aPrincipal,
|
||||
const char *aURL,
|
||||
PRUint32 aLineNo,
|
||||
const char* aVersion,
|
||||
void** aScriptObject) = 0;
|
||||
|
||||
/**
|
||||
* Execute a precompiled script object.
|
||||
*
|
||||
* @param aScriptObject an object representing the script to be executed
|
||||
* @param aScopeObject an object telling the scope in which to execute,
|
||||
* or nsnull to use a default scope
|
||||
* @param aRetValue the result of executing the script, may be null in
|
||||
* which case no result string is computed
|
||||
* @param aIsUndefined true if the result of executing the script is the
|
||||
* undefined value, may be null for "don't care"
|
||||
*
|
||||
* @return NS_OK if the script was valid and got executed
|
||||
*
|
||||
*/
|
||||
NS_IMETHOD ExecuteScript(void* aScriptObject,
|
||||
void *aScopeObject,
|
||||
nsString* aRetValue,
|
||||
PRBool* aIsUndefined) = 0;
|
||||
|
||||
NS_IMETHOD CompileFunction(void *aObj, nsIAtom *aName,
|
||||
const nsString& aBody) = 0;
|
||||
|
||||
|
|
|
@ -142,7 +142,7 @@ nsJSContext::EvaluateString(const nsString& aScript,
|
|||
NS_IMETHODIMP
|
||||
nsJSContext::EvaluateString(const nsString& aScript,
|
||||
void *jsObj,
|
||||
nsIPrincipal *principal,
|
||||
nsIPrincipal *aPrincipal,
|
||||
const char *aURL,
|
||||
PRUint32 aLineNo,
|
||||
const char* aVersion,
|
||||
|
@ -157,9 +157,9 @@ nsJSContext::EvaluateString(const nsString& aScript,
|
|||
// the entities who signed this script, or the fully-qualified-domain-name
|
||||
// or "codebase" from which it was loaded.
|
||||
JSPrincipals *jsprin;
|
||||
nsCOMPtr<nsIPrincipal> principalCOMPtr = principal;
|
||||
if (principal) {
|
||||
principal->GetJSPrincipals(&jsprin);
|
||||
nsCOMPtr<nsIPrincipal> principal = aPrincipal;
|
||||
if (aPrincipal) {
|
||||
aPrincipal->GetJSPrincipals(&jsprin);
|
||||
}
|
||||
else {
|
||||
// norris TODO: Using GetGlobalObject to get principals is broken?
|
||||
|
@ -169,10 +169,10 @@ nsJSContext::EvaluateString(const nsString& aScript,
|
|||
nsCOMPtr<nsIScriptGlobalObjectData> globalData = do_QueryInterface(global, &rv);
|
||||
if (NS_FAILED(rv))
|
||||
return NS_ERROR_FAILURE;
|
||||
rv = globalData->GetPrincipal(getter_AddRefs(principalCOMPtr));
|
||||
rv = globalData->GetPrincipal(getter_AddRefs(principal));
|
||||
if (NS_FAILED(rv))
|
||||
return NS_ERROR_FAILURE;
|
||||
principalCOMPtr->GetJSPrincipals(&jsprin);
|
||||
principal->GetJSPrincipals(&jsprin);
|
||||
}
|
||||
// From here on, we must JSPRINCIPALS_DROP(jsprin) before returning...
|
||||
|
||||
|
@ -180,7 +180,7 @@ nsJSContext::EvaluateString(const nsString& aScript,
|
|||
nsCOMPtr<nsIScriptSecurityManager> securityManager;
|
||||
rv = GetSecurityManager(getter_AddRefs(securityManager));
|
||||
if (NS_SUCCEEDED(rv))
|
||||
rv = securityManager->CanExecuteScripts(principalCOMPtr, &ok);
|
||||
rv = securityManager->CanExecuteScripts(principal, &ok);
|
||||
if (NS_FAILED(rv)) {
|
||||
JSPRINCIPALS_DROP(mContext, jsprin);
|
||||
return NS_ERROR_FAILURE;
|
||||
|
@ -236,7 +236,10 @@ nsJSContext::EvaluateString(const nsString& aScript,
|
|||
if (ok) {
|
||||
*aIsUndefined = JSVAL_IS_VOID(val);
|
||||
JSString* jsstring = JS_ValueToString(mContext, val);
|
||||
aRetValue.SetString(JS_GetStringChars(jsstring));
|
||||
if (jsstring)
|
||||
aRetValue.SetString(JS_GetStringChars(jsstring));
|
||||
else
|
||||
rv = NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
else {
|
||||
*aIsUndefined = PR_TRUE;
|
||||
|
@ -247,9 +250,140 @@ nsJSContext::EvaluateString(const nsString& aScript,
|
|||
|
||||
// Pop here, after JS_ValueToString and any other possible evaluation.
|
||||
if (NS_FAILED(stack->Pop(nsnull)))
|
||||
return NS_ERROR_FAILURE;
|
||||
rv = NS_ERROR_FAILURE;
|
||||
|
||||
return NS_OK;
|
||||
return rv;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsJSContext::CompileScript(const PRUnichar* aText,
|
||||
PRInt32 aTextLength,
|
||||
void *aScopeObject,
|
||||
nsIPrincipal *aPrincipal,
|
||||
const char *aURL,
|
||||
PRUint32 aLineNo,
|
||||
const char* aVersion,
|
||||
void** aScriptObject)
|
||||
{
|
||||
nsresult rv;
|
||||
NS_ENSURE_ARG_POINTER(aPrincipal);
|
||||
|
||||
if (!aScopeObject)
|
||||
aScopeObject = JS_GetGlobalObject(mContext);
|
||||
|
||||
JSPrincipals *jsprin;
|
||||
aPrincipal->GetJSPrincipals(&jsprin);
|
||||
// From here on, we must JSPRINCIPALS_DROP(jsprin) before returning...
|
||||
|
||||
PRBool ok = PR_FALSE;
|
||||
nsCOMPtr<nsIScriptSecurityManager> securityManager;
|
||||
rv = GetSecurityManager(getter_AddRefs(securityManager));
|
||||
if (NS_SUCCEEDED(rv))
|
||||
rv = securityManager->CanExecuteScripts(aPrincipal, &ok);
|
||||
if (NS_FAILED(rv)) {
|
||||
JSPRINCIPALS_DROP(mContext, jsprin);
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
*aScriptObject = nsnull;
|
||||
if (ok) {
|
||||
JSVersion newVersion;
|
||||
|
||||
// SecurityManager said "ok", but don't compile if aVersion is specified
|
||||
// and unknown. Do compile with the default version (and avoid thrashing
|
||||
// the context's version) if aVersion is not specified.
|
||||
if (!aVersion ||
|
||||
(newVersion = JS_StringToVersion(aVersion)) != JSVERSION_UNKNOWN) {
|
||||
JSVersion oldVersion;
|
||||
if (aVersion)
|
||||
oldVersion = JS_SetVersion(mContext, newVersion);
|
||||
|
||||
JSScript* script =
|
||||
::JS_CompileUCScriptForPrincipals(mContext,
|
||||
(JSObject*) aScopeObject,
|
||||
jsprin,
|
||||
(jschar*) aText,
|
||||
aTextLength,
|
||||
aURL,
|
||||
aLineNo);
|
||||
if (script) {
|
||||
*aScriptObject = (void*) ::JS_NewScriptObject(mContext, script);
|
||||
if (! *aScriptObject) {
|
||||
::JS_DestroyScript(mContext, script);
|
||||
script = nsnull;
|
||||
}
|
||||
}
|
||||
if (!script)
|
||||
rv = NS_ERROR_OUT_OF_MEMORY;
|
||||
|
||||
if (aVersion)
|
||||
JS_SetVersion(mContext, oldVersion);
|
||||
}
|
||||
}
|
||||
|
||||
// Whew! Finally done with these manually ref-counted things.
|
||||
JSPRINCIPALS_DROP(mContext, jsprin);
|
||||
return rv;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsJSContext::ExecuteScript(void* aScriptObject,
|
||||
void *aScopeObject,
|
||||
nsString* aRetValue,
|
||||
PRBool* aIsUndefined)
|
||||
{
|
||||
nsresult rv;
|
||||
|
||||
if (!aScopeObject)
|
||||
aScopeObject = JS_GetGlobalObject(mContext);
|
||||
|
||||
// Push our JSContext on our thread's context stack, in case native code
|
||||
// called from JS calls back into JS via XPConnect.
|
||||
NS_WITH_SERVICE(nsIJSContextStack, stack, "nsThreadJSContextStack", &rv);
|
||||
if (NS_FAILED(rv) || NS_FAILED(stack->Push(mContext))) {
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
// The result of evaluation, used only if there were no errors. This need
|
||||
// not be a GC root currently, provided we run the GC only from the branch
|
||||
// callback or from ScriptEvaluated. TODO: use JS_Begin/EndRequest to keep
|
||||
// the GC from racing with JS execution on any thread.
|
||||
jsval val;
|
||||
JSBool ok;
|
||||
|
||||
mRef = nsnull;
|
||||
mTerminationFunc = nsnull;
|
||||
ok = ::JS_ExecuteScript(mContext,
|
||||
(JSObject*) aScopeObject,
|
||||
(JSScript*) JS_GetPrivate(mContext,
|
||||
(JSObject*)aScriptObject),
|
||||
&val);
|
||||
|
||||
if (ok) {
|
||||
// If all went well, convert val to a string (XXXbe unless undefined?).
|
||||
if (aIsUndefined)
|
||||
*aIsUndefined = JSVAL_IS_VOID(val);
|
||||
if (aRetValue) {
|
||||
JSString* jsstring = JS_ValueToString(mContext, val);
|
||||
if (jsstring)
|
||||
aRetValue->SetString(JS_GetStringChars(jsstring));
|
||||
else
|
||||
rv = NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
} else {
|
||||
if (aIsUndefined)
|
||||
*aIsUndefined = PR_TRUE;
|
||||
if (aRetValue)
|
||||
aRetValue->Truncate();
|
||||
}
|
||||
|
||||
ScriptEvaluated();
|
||||
|
||||
// Pop here, after JS_ValueToString and any other possible evaluation.
|
||||
if (NS_FAILED(stack->Pop(nsnull)))
|
||||
rv = NS_ERROR_FAILURE;
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
const char *gEventArgv[] = {"event"};
|
||||
|
|
|
@ -67,6 +67,18 @@ public:
|
|||
const char* aVersion,
|
||||
nsString& aRetValue,
|
||||
PRBool* aIsUndefined);
|
||||
NS_IMETHOD CompileScript(const PRUnichar* aText,
|
||||
PRInt32 aTextLength,
|
||||
void *aScopeObj,
|
||||
nsIPrincipal *principal,
|
||||
const char *aURL,
|
||||
PRUint32 aLineNo,
|
||||
const char* aVersion,
|
||||
void** aScriptObject);
|
||||
NS_IMETHOD ExecuteScript(void* aScriptObject,
|
||||
void *aScopeObject,
|
||||
nsString* aRetValue,
|
||||
PRBool* aIsUndefined);
|
||||
NS_IMETHOD CompileFunction(void *aObj, nsIAtom *aName,
|
||||
const nsString& aBody);
|
||||
NS_IMETHOD CallFunction(void *aObj, void *aFunction,
|
||||
|
|
Загрузка…
Ссылка в новой задаче