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
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"
21 using js::AutoLockMonitor
;
23 BEGIN_TEST(testCompileNonsyntactic
) {
24 CHECK(testCompile(true));
26 CHECK(testCompile(false));
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
);
51 CHECK_EQUAL(script
->hasNonSyntacticScope(), nonSyntactic
);
53 script
= Compile(cx
, options
, buf8
);
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
);
64 CHECK_EQUAL(script
->hasNonSyntacticScope(), nonSyntactic
);
68 END_TEST(testCompileNonsyntactic
);