tor-browser

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

testDebugger.cpp (2094B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
      2 * vim: set ts=8 sts=2 et sw=2 tw=80:
      3 */
      4 /* This Source Code Form is subject to the terms of the Mozilla Public
      5 * License, v. 2.0. If a copy of the MPL was not distributed with this
      6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      7 
      8 #include "js/CallAndConstruct.h"
      9 #include "js/GlobalObject.h"        // JS_NewGlobalObject
     10 #include "js/PropertyAndElement.h"  // JS_SetProperty
     11 #include "jsapi-tests/tests.h"
     12 #include "vm/JSContext.h"
     13 
     14 using namespace js;
     15 
     16 BEGIN_TEST(testDebugger_newScriptHook) {
     17  // Test that top-level indirect eval fires the newScript hook.
     18  CHECK(JS_DefineDebuggerObject(cx, global));
     19  JS::RealmOptions options;
     20  JS::RootedObject g(cx, JS_NewGlobalObject(cx, getGlobalClass(), nullptr,
     21                                            JS::FireOnNewGlobalHook, options));
     22  CHECK(g);
     23 
     24  JS::RootedObject gWrapper(cx, g);
     25  CHECK(JS_WrapObject(cx, &gWrapper));
     26  JS::RootedValue v(cx, JS::ObjectValue(*gWrapper));
     27  CHECK(JS_SetProperty(cx, global, "g", v));
     28 
     29  EXEC(
     30      "var dbg = Debugger(g);\n"
     31      "var hits = 0;\n"
     32      "dbg.onNewScript = function (s) {\n"
     33      "    hits += Number(s instanceof Debugger.Script);\n"
     34      "};\n");
     35 
     36  // Since g is a debuggee, g.eval should trigger newScript, regardless of
     37  // what scope object we use to enter the compartment.
     38  //
     39  // Scripts are associated with the global where they're compiled, so we
     40  // deliver them only to debuggers that are watching that particular global.
     41  //
     42  return testIndirectEval(g, "Math.abs(0)");
     43 }
     44 
     45 bool testIndirectEval(JS::HandleObject global, const char* code) {
     46  EXEC("hits = 0;");
     47 
     48  {
     49    JSAutoRealm ar(cx, global);
     50    JSString* codestr = JS_NewStringCopyZ(cx, code);
     51    CHECK(codestr);
     52    JS::RootedValue arg(cx, JS::StringValue(codestr));
     53    JS::RootedValue v(cx);
     54    CHECK(JS_CallFunctionName(cx, global, "eval", HandleValueArray(arg), &v));
     55  }
     56 
     57  JS::RootedValue hitsv(cx);
     58  EVAL("hits", &hitsv);
     59  CHECK(hitsv.isInt32(1));
     60  return true;
     61 }
     62 END_TEST(testDebugger_newScriptHook)