michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "jsapi-tests/tests.h" michael@0: michael@0: static bool michael@0: InterruptCallback(JSContext *cx) michael@0: { michael@0: return false; michael@0: } michael@0: michael@0: static unsigned sRemain; michael@0: michael@0: static bool michael@0: RequestInterruptCallback(JSContext *cx, unsigned argc, jsval *vp) michael@0: { michael@0: JS::CallArgs args = JS::CallArgsFromVp(argc, vp); michael@0: if (!sRemain--) michael@0: JS_RequestInterruptCallback(JS_GetRuntime(cx)); michael@0: args.rval().setUndefined(); michael@0: return true; michael@0: } michael@0: michael@0: BEGIN_TEST(testSlowScript) michael@0: { michael@0: JS_SetInterruptCallback(cx, InterruptCallback); michael@0: JS_DefineFunction(cx, global, "requestInterruptCallback", RequestInterruptCallback, 0, 0); michael@0: michael@0: test("while (true)" michael@0: " for (i in [0,0,0,0])" michael@0: " requestInterruptCallback();"); michael@0: michael@0: test("while (true)" michael@0: " for (i in [0,0,0,0])" michael@0: " for (j in [0,0,0,0])" michael@0: " requestInterruptCallback();"); michael@0: michael@0: test("while (true)" michael@0: " for (i in [0,0,0,0])" michael@0: " for (j in [0,0,0,0])" michael@0: " for (k in [0,0,0,0])" michael@0: " requestInterruptCallback();"); michael@0: michael@0: test("function f() { while (true) yield requestInterruptCallback() }" michael@0: "for (i in f()) ;"); michael@0: michael@0: test("function f() { while (true) yield 1 }" michael@0: "for (i in f())" michael@0: " requestInterruptCallback();"); michael@0: michael@0: test("(function() {" michael@0: " while (true)" michael@0: " let (x = 1) { eval('requestInterruptCallback()'); }" michael@0: "})()"); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: test(const char *bytes) michael@0: { michael@0: jsval v; michael@0: michael@0: JS_SetOptions(cx, JS_GetOptions(cx) & ~(JSOPTION_METHODJIT | JSOPTION_METHODJIT_ALWAYS)); michael@0: sRemain = 0; michael@0: CHECK(!evaluate(bytes, __FILE__, __LINE__, &v)); michael@0: CHECK(!JS_IsExceptionPending(cx)); michael@0: michael@0: sRemain = 1000; michael@0: CHECK(!evaluate(bytes, __FILE__, __LINE__, &v)); michael@0: CHECK(!JS_IsExceptionPending(cx)); michael@0: michael@0: JS_SetOptions(cx, JS_GetOptions(cx) | JSOPTION_METHODJIT | JSOPTION_METHODJIT_ALWAYS); michael@0: michael@0: sRemain = 0; michael@0: CHECK(!evaluate(bytes, __FILE__, __LINE__, &v)); michael@0: CHECK(!JS_IsExceptionPending(cx)); michael@0: michael@0: sRemain = 1000; michael@0: CHECK(!evaluate(bytes, __FILE__, __LINE__, &v)); michael@0: CHECK(!JS_IsExceptionPending(cx)); michael@0: michael@0: return true; michael@0: } michael@0: END_TEST(testSlowScript)