Skip to content

Commit

Permalink
Bug 1554362 - Implement nsJSUtils::CompileModule for UTF-8 as well as…
Browse files Browse the repository at this point in the history
… UTF-16. r=bzbarsky

Differential Revision: https://phabricator.services.mozilla.com/D34821

--HG--
extra : moz-landing-system : lando
  • Loading branch information
jswalden committed Jun 15, 2019
1 parent dda2a19 commit 563e736
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 11 deletions.
48 changes: 41 additions & 7 deletions dom/base/nsJSUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "jsapi.h"
#include "jsfriendapi.h"
#include "js/CompilationAndEvaluation.h"
#include "js/Modules.h" // JS::CompileModule, JS::GetModuleScript, JS::Module{Instantiate,Evaluate}
#include "js/Modules.h" // JS::CompileModule{,DontInflate}, JS::GetModuleScript, JS::Module{Instantiate,Evaluate}
#include "js/OffThreadScriptCompilation.h"
#include "js/SourceText.h"
#include "nsIScriptContext.h"
Expand All @@ -37,6 +37,7 @@
#include "mozilla/dom/Date.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/ScriptSettings.h"
#include "mozilla/Utf8.h" // mozilla::Utf8Unit

using namespace mozilla;
using namespace mozilla::dom;
Expand Down Expand Up @@ -450,11 +451,26 @@ nsresult nsJSUtils::ExecutionContext::ExecScript(
return NS_OK;
}

nsresult nsJSUtils::CompileModule(JSContext* aCx,
JS::SourceText<char16_t>& aSrcBuf,
JS::Handle<JSObject*> aEvaluationGlobal,
JS::CompileOptions& aCompileOptions,
JS::MutableHandle<JSObject*> aModule) {
static JSObject* CompileModule(JSContext* aCx,
JS::CompileOptions& aCompileOptions,
JS::SourceText<char16_t>& aSrcBuf) {
return JS::CompileModule(aCx, aCompileOptions, aSrcBuf);
}

static JSObject* CompileModule(JSContext* aCx,
JS::CompileOptions& aCompileOptions,
JS::SourceText<Utf8Unit>& aSrcBuf) {
// Once compile-UTF-8-without-inflating is stable, it'll be renamed to remove
// the "DontInflate" suffix, these two overloads can be removed, and
// |JS::CompileModule| can be used in the sole caller below.
return JS::CompileModuleDontInflate(aCx, aCompileOptions, aSrcBuf);
}

template <typename Unit>
static nsresult CompileJSModule(JSContext* aCx, JS::SourceText<Unit>& aSrcBuf,
JS::Handle<JSObject*> aEvaluationGlobal,
JS::CompileOptions& aCompileOptions,
JS::MutableHandle<JSObject*> aModule) {
AUTO_PROFILER_LABEL("nsJSUtils::CompileModule", JS);
MOZ_ASSERT(aCx == nsContentUtils::GetCurrentJSContext());
MOZ_ASSERT(aSrcBuf.get());
Expand All @@ -466,7 +482,7 @@ nsresult nsJSUtils::CompileModule(JSContext* aCx,

NS_ENSURE_TRUE(xpc::Scriptability::Get(aEvaluationGlobal).Allowed(), NS_OK);

JSObject* module = JS::CompileModule(aCx, aCompileOptions, aSrcBuf);
JSObject* module = CompileModule(aCx, aCompileOptions, aSrcBuf);
if (!module) {
return NS_ERROR_FAILURE;
}
Expand All @@ -475,6 +491,24 @@ nsresult nsJSUtils::CompileModule(JSContext* aCx,
return NS_OK;
}

nsresult nsJSUtils::CompileModule(JSContext* aCx,
JS::SourceText<char16_t>& aSrcBuf,
JS::Handle<JSObject*> aEvaluationGlobal,
JS::CompileOptions& aCompileOptions,
JS::MutableHandle<JSObject*> aModule) {
return CompileJSModule(aCx, aSrcBuf, aEvaluationGlobal, aCompileOptions,
aModule);
}

nsresult nsJSUtils::CompileModule(JSContext* aCx,
JS::SourceText<Utf8Unit>& aSrcBuf,
JS::Handle<JSObject*> aEvaluationGlobal,
JS::CompileOptions& aCompileOptions,
JS::MutableHandle<JSObject*> aModule) {
return CompileJSModule(aCx, aSrcBuf, aEvaluationGlobal, aCompileOptions,
aModule);
}

nsresult nsJSUtils::InitModuleSourceElement(JSContext* aCx,
JS::Handle<JSObject*> aModule,
nsIScriptElement* aElement) {
Expand Down
7 changes: 7 additions & 0 deletions dom/base/nsJSUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "mozilla/Assertions.h"
#include "mozilla/StaticPrefs.h"
#include "mozilla/Utf8.h" // mozilla::Utf8Unit

#include "GeckoProfiler.h"
#include "jsapi.h"
Expand Down Expand Up @@ -210,6 +211,12 @@ class nsJSUtils {
JS::CompileOptions& aCompileOptions,
JS::MutableHandle<JSObject*> aModule);

static nsresult CompileModule(JSContext* aCx,
JS::SourceText<mozilla::Utf8Unit>& aSrcBuf,
JS::Handle<JSObject*> aEvaluationGlobal,
JS::CompileOptions& aCompileOptions,
JS::MutableHandle<JSObject*> aModule);

static nsresult InitModuleSourceElement(JSContext* aCx,
JS::Handle<JSObject*> aModule,
nsIScriptElement* aElement);
Expand Down
11 changes: 10 additions & 1 deletion js/public/Modules.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,17 @@ extern JS_PUBLIC_API JSObject* CompileModule(
/**
* Parse the given source buffer as a module in the scope of the current global
* of cx and return a source text module record.
*
* The "DontInflate" suffix and (semantically unobservable) don't-inflate
* characteristic are temporary while bugs in UTF-8 compilation are ironed out.
* In the long term this function will be renamed |JS::CompileModule| and will
* just never inflate.
*
* NOTE: UTF-8 compilation is currently experimental, and it's possible it has
* as-yet-undiscovered bugs that the UTF-16 compilation functions do not
* have. Use only if you're willing to take a risk!
*/
extern JS_PUBLIC_API JSObject* CompileModule(
extern JS_PUBLIC_API JSObject* CompileModuleDontInflate(
JSContext* cx, const ReadOnlyCompileOptions& options,
SourceText<mozilla::Utf8Unit>& srcBuf);

Expand Down
6 changes: 3 additions & 3 deletions js/src/vm/Modules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ JS_PUBLIC_API JSObject* JS::CompileModule(JSContext* cx,
return CompileModuleHelper(cx, options, srcBuf);
}

JS_PUBLIC_API JSObject* JS::CompileModule(JSContext* cx,
const ReadOnlyCompileOptions& options,
SourceText<Utf8Unit>& srcBuf) {
JS_PUBLIC_API JSObject* JS::CompileModuleDontInflate(
JSContext* cx, const ReadOnlyCompileOptions& options,
SourceText<Utf8Unit>& srcBuf) {
return CompileModuleHelper(cx, options, srcBuf);
}

Expand Down

0 comments on commit 563e736

Please sign in to comment.