tor-browser

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

test-unwind.cpp (2411B)


      1 #include "gdb-tests.h"
      2 #include "jsfriendapi.h"  // JSFunctionSpecWithHelp
      3 
      4 #include "jit/JitOptions.h"               // js::jit::JitOptions
      5 #include "js/CallArgs.h"                  // JS::CallArgs, JS::CallArgsFromVp
      6 #include "js/CompilationAndEvaluation.h"  // JS::Evaluate
      7 #include "js/CompileOptions.h"            // JS::CompileOptions
      8 #include "js/GlobalObject.h"              // JS::CurrentGlobalOrNull
      9 #include "js/PropertyAndElement.h"        // JS_DefineProperty
     10 #include "js/PropertyDescriptor.h"        // JSPROP_ENUMERATE
     11 #include "js/RootingAPI.h"                // JS::Rooted
     12 #include "js/SourceText.h"                // JS::Source{Ownership,Text}
     13 #include "js/Value.h"                     // JS::Value
     14 
     15 #include "mozilla/Utf8.h"  // mozilla::Utf8Unit
     16 
     17 #include <stdint.h>  // uint32_t
     18 #include <string.h>  // strlen
     19 
     20 static bool Something(JSContext* cx, unsigned argc, JS::Value* vp) {
     21  JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
     22  args.rval().setInt32(23);
     23  breakpoint();
     24  return true;
     25 }
     26 
     27 // clang-format off
     28 static const JSFunctionSpecWithHelp unwind_functions[] = {
     29    JS_FN_HELP("something", Something, 0, 0,
     30 "something()",
     31 "  Test function for test-unwind."),
     32    JS_FS_HELP_END
     33 };
     34 // clang-format on
     35 
     36 FRAGMENT(unwind, simple) {
     37  using namespace JS;
     38 
     39  JS::Rooted<JSObject*> global(cx, JS::CurrentGlobalOrNull(cx));
     40  if (!JS_DefineFunctionsWithHelp(cx, global, unwind_functions)) {
     41    return;
     42  }
     43 
     44  // Define an itercount property and use it to ensure Baseline compilation.
     45  uint32_t threshold = js::jit::JitOptions.baselineJitWarmUpThreshold;
     46  RootedValue val(cx, Int32Value(threshold + 10));
     47  if (!JS_DefineProperty(cx, global, "itercount", val, 0)) {
     48    return;
     49  }
     50 
     51  int line0 = __LINE__;
     52  const char* bytes =
     53      "\n"
     54      "function unwindFunctionInner() {\n"
     55      "    for (var i = 0; i < itercount; i++) {}\n"
     56      "    return something();\n"
     57      "}\n"
     58      "\n"
     59      "function unwindFunctionOuter() {\n"
     60      "    for (var i = 0; i < itercount; i++) {}\n"
     61      "    return unwindFunctionInner();\n"
     62      "}\n"
     63      "\n"
     64      "unwindFunctionOuter();\n";
     65 
     66  JS::CompileOptions opts(cx);
     67  opts.setFileAndLine(__FILE__, line0 + 1);
     68 
     69  JS::SourceText<mozilla::Utf8Unit> srcBuf;
     70  if (!srcBuf.init(cx, bytes, strlen(bytes), JS::SourceOwnership::Borrowed)) {
     71    return;
     72  }
     73 
     74  JS::Rooted<JS::Value> rval(cx);
     75  JS::Evaluate(cx, opts, srcBuf, &rval);
     76 }