Fix Components.utils.evalInSandbox bugs: 1) GC'ing after even eval blows performance, no need; 2) borrowing the calling cx's error reporter doesn't work, we must turn uncaught exception errors back into thrown exceptions on the calling cx (301353, r+sr+a=shaver).

This commit is contained in:
brendan%mozilla.org 2005-07-20 03:35:40 +00:00
Родитель 2a1e999a12
Коммит 7b8f6fcef2
3 изменённых файлов: 74 добавлений и 47 удалений

Просмотреть файл

@ -4474,6 +4474,13 @@ JS_ErrorFromException(JSContext *cx, jsval v)
#endif
}
JS_PUBLIC_API(JSBool)
JS_ThrowReportedError(JSContext *cx, const char *message,
JSErrorReport *reportp)
{
js_ErrorToException(cx, message, reportp);
}
#ifdef JS_THREADSAFE
JS_PUBLIC_API(jsword)
JS_GetContextThread(JSContext *cx)

Просмотреть файл

@ -1902,6 +1902,14 @@ JS_DropExceptionState(JSContext *cx, JSExceptionState *state);
extern JS_PUBLIC_API(JSErrorReport *)
JS_ErrorFromException(JSContext *cx, jsval v);
/*
* Given a reported error's message and JSErrorReport struct pointer, throw
* the corresponding exception on cx.
*/
extern JS_PUBLIC_API(JSBool)
JS_ThrowReportedError(JSContext *cx, const char *message,
JSErrorReport *reportp);
#ifdef JS_THREADSAFE
/*

Просмотреть файл

@ -2083,6 +2083,18 @@ static JSFunctionSpec SandboxFunctions[] = {
#endif /* !XPCONNECT_STANDALONE */
/*
* Throw an exception on caller_cx that is made from message and report,
* which were reported as uncaught on cx.
*/
static void
SandboxErrorReporter(JSContext *cx, const char *message, JSErrorReport *report)
{
JSContext *caller_cx = NS_STATIC_CAST(JSContext*, JS_GetContextPrivate(cx));
JS_ThrowReportedError(caller_cx, message, report);
}
/* void evalInSandbox(in AString source, in AUTF8String codebase); */
NS_IMETHODIMP
nsXPCComponents_Utils::EvalInSandbox(const nsAString &source,
@ -2188,10 +2200,10 @@ nsXPCComponents_Utils::EvalInSandbox(const nsAString &source,
}
JS_SetGlobalObject(sandcx, sandbox);
// There's no JS_GetErrorReporter, so we fake it
JSErrorReporter borrowedReporter = JS_SetErrorReporter(cx, nsnull);
JS_SetErrorReporter(sandcx, borrowedReporter);
JS_SetErrorReporter(cx, borrowedReporter);
// Capture uncaught exceptions reported as errors on sandcx and
// re-throw them on cx.
JS_SetContextPrivate(sandcx, cx);
JS_SetErrorReporter(sandcx, SandboxErrorReporter);
if (!JS_EvaluateUCScriptForPrincipals(sandcx, sandbox, jsPrincipals,
NS_REINTERPRET_CAST(const jschar *,
@ -2207,7 +2219,7 @@ nsXPCComponents_Utils::EvalInSandbox(const nsAString &source,
cc->SetReturnValueWasSet(PR_TRUE);
}
JS_DestroyContext(sandcx);
JS_DestroyContextNoGC(sandcx);
JSPRINCIPALS_DROP(cx, jsPrincipals);
return rv;
#endif /* !XPCONNECT_STANDALONE */