tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

testSlowScript.cpp (1990B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 #include "js/PropertyAndElement.h"  // JS_DefineFunction
      6 #include "jsapi-tests/tests.h"
      7 
      8 static bool InterruptCallback(JSContext* cx) { return false; }
      9 
     10 static unsigned sRemain;
     11 
     12 static bool RequestInterruptCallback(JSContext* cx, unsigned argc,
     13                                     JS::Value* vp) {
     14  JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
     15  if (!sRemain--) {
     16    JS_RequestInterruptCallback(cx);
     17  }
     18  args.rval().setUndefined();
     19  return true;
     20 }
     21 
     22 BEGIN_TEST(testSlowScript) {
     23  JS_AddInterruptCallback(cx, InterruptCallback);
     24  JS_DefineFunction(cx, global, "requestInterruptCallback",
     25                    RequestInterruptCallback, 0, 0);
     26 
     27  CHECK(
     28      test("while (true)"
     29           "  for (i in [0,0,0,0])"
     30           "    requestInterruptCallback();"));
     31 
     32  CHECK(
     33      test("while (true)"
     34           "  for (i in [0,0,0,0])"
     35           "    for (j in [0,0,0,0])"
     36           "      requestInterruptCallback();"));
     37 
     38  CHECK(
     39      test("while (true)"
     40           "  for (i in [0,0,0,0])"
     41           "    for (j in [0,0,0,0])"
     42           "      for (k in [0,0,0,0])"
     43           "        requestInterruptCallback();"));
     44 
     45  CHECK(
     46      test("function* f() { while (true) yield requestInterruptCallback() }"
     47           "for (i of f()) ;"));
     48 
     49  CHECK(
     50      test("function* f() { while (true) yield 1 }"
     51           "for (i of f())"
     52           "  requestInterruptCallback();"));
     53 
     54  return true;
     55 }
     56 
     57 bool test(const char* bytes) {
     58  JS::RootedValue v(cx);
     59 
     60  sRemain = 0;
     61  CHECK(!evaluate(bytes, __FILE__, __LINE__, &v));
     62  CHECK(!JS_IsExceptionPending(cx));
     63  JS_ClearPendingException(cx);
     64 
     65  sRemain = 1000;
     66  CHECK(!evaluate(bytes, __FILE__, __LINE__, &v));
     67  CHECK(!JS_IsExceptionPending(cx));
     68  JS_ClearPendingException(cx);
     69 
     70  return true;
     71 }
     72 END_TEST(testSlowScript)