tor-browser

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

testFrontendCompileStencil.cpp (1941B)


      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 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "mozilla/RefPtr.h"
      8 #include "mozilla/Utf8.h"
      9 
     10 #include <string>
     11 
     12 #include "frontend/FrontendContext.h"  // js::FrontendContext
     13 #include "js/CompileOptions.h"
     14 #include "js/experimental/CompileScript.h"
     15 #include "js/SourceText.h"
     16 #include "js/Stack.h"
     17 #include "jsapi-tests/tests.h"
     18 #include "util/NativeStack.h"  // js::GetNativeStackBase
     19 
     20 using namespace JS;
     21 
     22 BEGIN_FRONTEND_TEST(testFrontendContextCompileGlobalScriptToStencil) {
     23  JS::FrontendContext* fc = JS::NewFrontendContext();
     24  CHECK(fc);
     25 
     26  static constexpr JS::NativeStackSize stackSize = 128 * sizeof(size_t) * 1024;
     27 
     28  JS::SetNativeStackQuota(fc, stackSize);
     29 
     30 #ifndef __wasi__
     31  CHECK(fc->stackLimit() ==
     32        JS::GetNativeStackLimit(js::GetNativeStackBase(), stackSize - 1));
     33 #endif
     34 
     35  JS::PrefableCompileOptions prefableOptions;
     36  JS::CompileOptions options(prefableOptions);
     37 
     38  {
     39    const char source[] = "var a = 10;";
     40 
     41    JS::SourceText<mozilla::Utf8Unit> srcBuf;
     42    CHECK(
     43        srcBuf.init(fc, source, strlen(source), JS::SourceOwnership::Borrowed));
     44    RefPtr<JS::Stencil> stencil =
     45        JS::CompileGlobalScriptToStencil(fc, options, srcBuf);
     46    CHECK(stencil);
     47  }
     48 
     49  {
     50    const char16_t source[] = u"var a = 10;";
     51 
     52    JS::SourceText<char16_t> srcBuf;
     53    CHECK(srcBuf.init(fc, source, std::char_traits<char16_t>::length(source),
     54                      JS::SourceOwnership::Borrowed));
     55    RefPtr<JS::Stencil> stencil =
     56        JS::CompileGlobalScriptToStencil(fc, options, srcBuf);
     57    CHECK(stencil);
     58  }
     59 
     60  JS::DestroyFrontendContext(fc);
     61 
     62  return true;
     63 }
     64 
     65 END_TEST(testFrontendContextCompileGlobalScriptToStencil)