diff --git a/js/public/experimental/JSStencil.h b/js/public/experimental/JSStencil.h index b6b262dd416b..5ce0149b88dd 100644 --- a/js/public/experimental/JSStencil.h +++ b/js/public/experimental/JSStencil.h @@ -52,6 +52,9 @@ JS_PUBLIC_API void StencilRelease(Stencil* stencil); extern JS_PUBLIC_API already_AddRefed CompileGlobalScriptToStencil( JSContext* cx, const ReadOnlyCompileOptions& options, SourceText& srcBuf); +extern JS_PUBLIC_API already_AddRefed CompileGlobalScriptToStencil( + JSContext* cx, const ReadOnlyCompileOptions& options, + SourceText& srcBuf); // Instantiate the Stencil into current Realm and return the JSScript. extern JS_PUBLIC_API JSScript* InstantiateGlobalStencil( diff --git a/js/src/frontend/Stencil.cpp b/js/src/frontend/Stencil.cpp index 770fd3101378..ba8970f151fd 100644 --- a/js/src/frontend/Stencil.cpp +++ b/js/src/frontend/Stencil.cpp @@ -3449,9 +3449,10 @@ void JS::StencilRelease(JS::Stencil* stencil) { } } -already_AddRefed JS::CompileGlobalScriptToStencil( - JSContext* cx, const ReadOnlyCompileOptions& options, - SourceText& srcBuf) { +template +static already_AddRefed CompileGlobalScriptToStencilImpl( + JSContext* cx, const JS::ReadOnlyCompileOptions& options, + JS::SourceText& srcBuf) { ScopeKind scopeKind = options.nonSyntacticScope ? ScopeKind::NonSyntactic : ScopeKind::Global; @@ -3466,6 +3467,18 @@ already_AddRefed JS::CompileGlobalScriptToStencil( return do_AddRef(stencil.release()); } +already_AddRefed JS::CompileGlobalScriptToStencil( + JSContext* cx, const JS::ReadOnlyCompileOptions& options, + JS::SourceText& srcBuf) { + return CompileGlobalScriptToStencilImpl(cx, options, srcBuf); +} + +already_AddRefed JS::CompileGlobalScriptToStencil( + JSContext* cx, const JS::ReadOnlyCompileOptions& options, + JS::SourceText& srcBuf) { + return CompileGlobalScriptToStencilImpl(cx, options, srcBuf); +} + JSScript* JS::InstantiateGlobalStencil( JSContext* cx, const JS::ReadOnlyCompileOptions& options, RefPtr stencil) { diff --git a/js/src/jsapi-tests/testStencil.cpp b/js/src/jsapi-tests/testStencil.cpp index 6ec58e7e3e81..e2c095c2d6cb 100644 --- a/js/src/jsapi-tests/testStencil.cpp +++ b/js/src/jsapi-tests/testStencil.cpp @@ -18,9 +18,24 @@ BEGIN_TEST(testStencil_Basic) { const char* chars = "function f() { return 42; }" "f();"; + auto result = basic_test(chars); + CHECK(result); - JS::SourceText srcBuf; - CHECK(srcBuf.init(cx, chars, strlen(chars), JS::SourceOwnership::Borrowed)); + const char16_t* chars16 = + u"function f() { return 42; }" + u"f();"; + auto result16 = basic_test(chars16); + CHECK(result16); + + return true; +} + +template +bool basic_test(const CharT* chars) { + size_t length = std::char_traits::length(chars); + + JS::SourceText srcBuf; + CHECK(srcBuf.init(cx, chars, length, JS::SourceOwnership::Borrowed)); JS::CompileOptions options(cx); RefPtr stencil =