Bug 1279086 - Allow multiple interrupt callbacks (r=dvander)

This commit is contained in:
Bill McCloskey 2016-06-23 17:03:03 -07:00
Родитель 7b67a45c6a
Коммит 1613c2cbc9
10 изменённых файлов: 40 добавлений и 24 удалений

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

@ -11121,14 +11121,14 @@ nsGlobalWindow::ShowSlowScriptDialog()
buttonFlags += nsIPrompt::BUTTON_TITLE_IS_STRING * nsIPrompt::BUTTON_POS_2;
// Null out the operation callback while we're re-entering JS here.
JSInterruptCallback old = JS_SetInterruptCallback(cx, nullptr);
bool old = JS_DisableInterruptCallback(cx);
// Open the dialog.
rv = prompt->ConfirmEx(title, msg, buttonFlags, waitButton, stopButton,
debugButton, neverShowDlg, &neverShowDlgChk,
&buttonPressed);
JS_SetInterruptCallback(cx, old);
JS_ResetInterruptCallback(cx, old);
if (NS_SUCCEEDED(rv) && (buttonPressed == 0)) {
return neverShowDlgChk ? AlwaysContinueSlowScript : ContinueSlowScript;

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

@ -906,7 +906,7 @@ InitJSContextForWorker(WorkerPrivate* aWorkerPrivate, JSContext* aWorkerCx)
return false;
}
JS_SetInterruptCallback(aWorkerCx, InterruptCallback);
JS_AddInterruptCallback(aWorkerCx, InterruptCallback);
js::SetCTypesActivityCallback(aWorkerCx, CTypesActivityCallback);

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

@ -24,7 +24,7 @@ RequestInterruptCallback(JSContext* cx, unsigned argc, jsval* vp)
BEGIN_TEST(testSlowScript)
{
JS_SetInterruptCallback(cx, InterruptCallback);
JS_AddInterruptCallback(cx, InterruptCallback);
JS_DefineFunction(cx, global, "requestInterruptCallback", RequestInterruptCallback, 0, 0);
test("while (true)"

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

@ -4641,18 +4641,24 @@ JS_CheckForInterrupt(JSContext* cx)
return js::CheckForInterrupt(cx);
}
JS_PUBLIC_API(JSInterruptCallback)
JS_SetInterruptCallback(JSContext* cx, JSInterruptCallback callback)
JS_PUBLIC_API(bool)
JS_AddInterruptCallback(JSContext* cx, JSInterruptCallback callback)
{
JSInterruptCallback old = cx->interruptCallback;
cx->interruptCallback = callback;
return old;
return cx->interruptCallbacks.append(callback);
}
JS_PUBLIC_API(JSInterruptCallback)
JS_GetInterruptCallback(JSContext* cx)
JS_PUBLIC_API(bool)
JS_DisableInterruptCallback(JSContext* cx)
{
return cx->interruptCallback;
bool result = cx->interruptCallbackDisabled;
cx->interruptCallbackDisabled = true;
return result;
}
JS_PUBLIC_API(void)
JS_ResetInterruptCallback(JSContext* cx, bool enable)
{
cx->interruptCallbackDisabled = enable;
}
/************************************************************************/

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

@ -4320,11 +4320,14 @@ JS_CheckForInterrupt(JSContext* cx);
* if it re-enters the JS engine. The embedding must ensure that the callback
* is disconnected before attempting such re-entry.
*/
extern JS_PUBLIC_API(JSInterruptCallback)
JS_SetInterruptCallback(JSContext* cx, JSInterruptCallback callback);
extern JS_PUBLIC_API(bool)
JS_AddInterruptCallback(JSContext* cx, JSInterruptCallback callback);
extern JS_PUBLIC_API(JSInterruptCallback)
JS_GetInterruptCallback(JSContext* cx);
extern JS_PUBLIC_API(bool)
JS_DisableInterruptCallback(JSContext* cx);
extern JS_PUBLIC_API(void)
JS_ResetInterruptCallback(JSContext* cx, bool enable);
extern JS_PUBLIC_API(void)
JS_RequestInterruptCallback(JSContext* cx);

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

@ -7563,7 +7563,7 @@ main(int argc, char** argv, char** envp)
JS_SetSecurityCallbacks(cx, &ShellPrincipals::securityCallbacks);
JS_InitDestroyPrincipalsCallback(cx, ShellPrincipals::destroy);
JS_SetInterruptCallback(cx, ShellInterruptCallback);
JS_AddInterruptCallback(cx, ShellInterruptCallback);
JS::SetBuildIdOp(cx, ShellBuildId);
JS::SetAsmJSCacheOps(cx, &asmJSCacheOps);

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

@ -150,7 +150,7 @@ JSRuntime::JSRuntime(JSRuntime* parentRuntime)
telemetryCallback(nullptr),
handlingSegFault(false),
handlingJitInterrupt_(false),
interruptCallback(nullptr),
interruptCallbackDisabled(false),
getIncumbentGlobalCallback(nullptr),
enqueuePromiseJobCallback(nullptr),
enqueuePromiseJobCallbackData(nullptr),
@ -526,11 +526,16 @@ InvokeInterruptCallback(JSContext* cx)
// Important: Additional callbacks can occur inside the callback handler
// if it re-enters the JS engine. The embedding must ensure that the
// callback is disconnected before attempting such re-entry.
JSInterruptCallback cb = cx->runtime()->interruptCallback;
if (!cb)
if (cx->runtime()->interruptCallbackDisabled)
return true;
if (cb(cx)) {
bool stop = false;
for (JSInterruptCallback cb : cx->runtime()->interruptCallbacks) {
if (!cb(cx))
stop = true;
}
if (!stop) {
// Debugger treats invoking the interrupt callback as a "step", so
// invoke the onStep handler.
if (cx->compartment()->isDebuggee()) {

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

@ -633,7 +633,9 @@ struct JSRuntime : public JS::shadow::Runtime,
return handlingJitInterrupt_;
}
JSInterruptCallback interruptCallback;
using InterruptCallbackVector = js::Vector<JSInterruptCallback, 2, js::SystemAllocPolicy>;
InterruptCallbackVector interruptCallbacks;
bool interruptCallbackDisabled;
JSGetIncumbentGlobalCallback getIncumbentGlobalCallback;
JSEnqueuePromiseJobCallback enqueuePromiseJobCallback;

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

@ -3529,7 +3529,7 @@ XPCJSRuntime::Initialize()
#endif
JS_SetAccumulateTelemetryCallback(cx, AccumulateTelemetryCallback);
js::SetActivityCallback(cx, ActivityCallback, this);
JS_SetInterruptCallback(cx, InterruptCallback);
JS_AddInterruptCallback(cx, InterruptCallback);
js::SetWindowProxyClass(cx, &OuterWindowProxyClass);
#ifdef MOZ_CRASHREPORTER
js::AutoEnterOOMUnsafeRegion::setAnnotateOOMAllocationSizeCallback(

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

@ -1421,7 +1421,7 @@ XRE_XPCShellMain(int argc, char** argv, char** envp,
sScriptedInterruptCallback = new PersistentRootedValue;
sScriptedInterruptCallback->init(cx, UndefinedValue());
JS_SetInterruptCallback(cx, XPCShellInterruptCallback);
JS_AddInterruptCallback(cx, XPCShellInterruptCallback);
argc--;
argv++;