Bug 1554362 - Implement nsJSUtils::CompileModule for UTF-8 as well as UTF-16. r=bzbarsky

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

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Jeff Walden 2019-06-15 20:47:51 +00:00
parent dda2a19293
commit 563e73698d
4 changed files with 61 additions and 11 deletions

View File

@ -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"
@ -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;
@ -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());
@ -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;
}
@ -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) {

View File

@ -16,6 +16,7 @@
#include "mozilla/Assertions.h"
#include "mozilla/StaticPrefs.h"
#include "mozilla/Utf8.h" // mozilla::Utf8Unit
#include "GeckoProfiler.h"
#include "jsapi.h"
@ -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);

View File

@ -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);

View File

@ -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);
}