tor-browser

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

testCompileNonSyntactic.cpp (2075B)


      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 "mozilla/RefPtr.h"  // RefPtr
      6 #include "mozilla/Utf8.h"    // mozilla::Utf8Unit
      7 
      8 #include <string_view>
      9 
     10 #include "js/CompilationAndEvaluation.h"  // JS::Compile
     11 #include "js/CompileOptions.h"  // JS::CompileOptions, JS::InstantiateOptions
     12 #include "js/experimental/JSStencil.h"  // JS::Stencil, JS::InstantiateGlobalStencil
     13 
     14 #include "js/SourceText.h"  // JS::Source{Ownership,Text}
     15 #include "jsapi-tests/tests.h"
     16 #include "vm/HelperThreads.h"
     17 #include "vm/Monitor.h"
     18 #include "vm/MutexIDs.h"
     19 
     20 using namespace JS;
     21 using js::AutoLockMonitor;
     22 
     23 BEGIN_TEST(testCompileNonsyntactic) {
     24  CHECK(testCompile(true));
     25 
     26  CHECK(testCompile(false));
     27  return true;
     28 }
     29 
     30 bool testCompile(bool nonSyntactic) {
     31  static constexpr std::string_view src = "42\n";
     32  static constexpr std::u16string_view src_16 = u"42\n";
     33 
     34  static_assert(src.length() == src_16.length(),
     35                "Source buffers must be same length");
     36 
     37  JS::CompileOptions options(cx);
     38  options.setNonSyntacticScope(nonSyntactic);
     39 
     40  JS::SourceText<char16_t> buf16;
     41  CHECK(buf16.init(cx, src_16.data(), src_16.length(),
     42                   JS::SourceOwnership::Borrowed));
     43 
     44  JS::SourceText<mozilla::Utf8Unit> buf8;
     45  CHECK(buf8.init(cx, src.data(), src.length(), JS::SourceOwnership::Borrowed));
     46 
     47  JS::RootedScript script(cx);
     48 
     49  script = Compile(cx, options, buf16);
     50  CHECK(script);
     51  CHECK_EQUAL(script->hasNonSyntacticScope(), nonSyntactic);
     52 
     53  script = Compile(cx, options, buf8);
     54  CHECK(script);
     55  CHECK_EQUAL(script->hasNonSyntacticScope(), nonSyntactic);
     56 
     57  {
     58    JS::SourceText<char16_t> srcBuf;
     59    CHECK(srcBuf.init(cx, src_16.data(), src_16.length(),
     60                      JS::SourceOwnership::Borrowed));
     61 
     62    script = Compile(cx, options, srcBuf);
     63    CHECK(script);
     64    CHECK_EQUAL(script->hasNonSyntacticScope(), nonSyntactic);
     65  }
     66  return true;
     67 }
     68 END_TEST(testCompileNonsyntactic);