Bug 1852740: add tests for the `fetchpriority` attribute in Link headers. r=necko...
[gecko.git] / js / src / jsapi-tests / testCompileNonSyntactic.cpp
blob13985d90bba3182c713999e3f7f14d530941fdde
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/. */
5 #include "mozilla/RefPtr.h" // RefPtr
6 #include "mozilla/Utf8.h" // mozilla::Utf8Unit
8 #include <string_view>
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
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"
20 using namespace JS;
21 using js::AutoLockMonitor;
23 BEGIN_TEST(testCompileNonsyntactic) {
24 CHECK(testCompile(true));
26 CHECK(testCompile(false));
27 return true;
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";
34 static_assert(src.length() == src_16.length(),
35 "Source buffers must be same length");
37 JS::CompileOptions options(cx);
38 options.setNonSyntacticScope(nonSyntactic);
40 JS::SourceText<char16_t> buf16;
41 CHECK(buf16.init(cx, src_16.data(), src_16.length(),
42 JS::SourceOwnership::Borrowed));
44 JS::SourceText<mozilla::Utf8Unit> buf8;
45 CHECK(buf8.init(cx, src.data(), src.length(), JS::SourceOwnership::Borrowed));
47 JS::RootedScript script(cx);
49 script = Compile(cx, options, buf16);
50 CHECK(script);
51 CHECK_EQUAL(script->hasNonSyntacticScope(), nonSyntactic);
53 script = Compile(cx, options, buf8);
54 CHECK(script);
55 CHECK_EQUAL(script->hasNonSyntacticScope(), nonSyntactic);
58 JS::SourceText<char16_t> srcBuf;
59 CHECK(srcBuf.init(cx, src_16.data(), src_16.length(),
60 JS::SourceOwnership::Borrowed));
62 script = Compile(cx, options, srcBuf);
63 CHECK(script);
64 CHECK_EQUAL(script->hasNonSyntacticScope(), nonSyntactic);
66 return true;
68 END_TEST(testCompileNonsyntactic);