Bug 1552979 - Add a JS::CompileModule overload for direct UTF-8 compilation. r=arai
authorJeff Walden <jwalden@mit.edu>
Tue, 21 May 2019 15:39:05 -0700
changeset 475408 d31060048c955584fbf1eb34648239641547060e
parent 475407 d22bd25523e5a8a49030ae35e9a5aba797c466a3
child 475409 2463d2a793da2807f57d2e73b2d955359a5ac411
push id36061
push usercbrindusan@mozilla.com
push dateFri, 24 May 2019 21:49:59 +0000
treeherdermozilla-central@5d3e1ea77693 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersarai
bugs1552979
milestone69.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1552979 - Add a JS::CompileModule overload for direct UTF-8 compilation. r=arai
js/public/Modules.h
js/src/vm/Modules.cpp
--- a/js/public/Modules.h
+++ b/js/public/Modules.h
@@ -4,16 +4,18 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 /* JavaScript module (as in, the syntactic construct) operations. */
 
 #ifndef js_Modules_h
 #define js_Modules_h
 
+#include "mozilla/Utf8.h"  // mozilla::Utf8Unit
+
 #include <stdint.h>  // uint32_t
 
 #include "jstypes.h"  // JS_PUBLIC_API
 
 #include "js/CompileOptions.h"  // JS::ReadOnlyCompileOptions
 #include "js/RootingAPI.h"      // JS::{Mutable,}Handle
 #include "js/Value.h"           // JS::Value
 
@@ -87,16 +89,24 @@ extern JS_PUBLIC_API bool FinishDynamicM
  * Parse the given source buffer as a module in the scope of the current global
  * of cx and return a source text module record.
  */
 extern JS_PUBLIC_API JSObject* CompileModule(
     JSContext* cx, const ReadOnlyCompileOptions& options,
     SourceText<char16_t>& srcBuf);
 
 /**
+ * Parse the given source buffer as a module in the scope of the current global
+ * of cx and return a source text module record.
+ */
+extern JS_PUBLIC_API JSObject* CompileModule(
+    JSContext* cx, const ReadOnlyCompileOptions& options,
+    SourceText<mozilla::Utf8Unit>& srcBuf);
+
+/**
  * Set a private value associated with a source text module record.
  */
 extern JS_PUBLIC_API void SetModulePrivate(JSObject* module,
                                            const Value& value);
 
 /**
  * Get the private value associated with a source text module record.
  */
--- a/js/src/vm/Modules.cpp
+++ b/js/src/vm/Modules.cpp
@@ -4,32 +4,35 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 /* JavaScript modules (as in, the syntactic construct) implementation. */
 
 #include "js/Modules.h"
 
 #include "mozilla/Assertions.h"  // MOZ_ASSERT
+#include "mozilla/Utf8.h"        // mozilla::Utf8Unit
 
 #include <stdint.h>  // uint32_t
 
 #include "jsapi.h"    // js::AssertHeapIsIdle
 #include "jstypes.h"  // JS_PUBLIC_API
 
 #include "builtin/ModuleObject.h"  // js::FinishDynamicModuleImport, js::{,Requested}ModuleObject
 #include "frontend/BytecodeCompiler.h"  // js::frontend::CompileModule
 #include "js/RootingAPI.h"              // JS::MutableHandle
 #include "js/Value.h"                   // JS::Value
 #include "vm/JSContext.h"               // CHECK_THREAD, JSContext
 #include "vm/JSObject.h"                // JSObject
 #include "vm/Runtime.h"                 // JSRuntime
 
 #include "vm/JSContext-inl.h"  // JSContext::{c,releaseC}heck
 
+using mozilla::Utf8Unit;
+
 using js::AssertHeapIsIdle;
 using js::ModuleObject;
 using js::RequestedModuleObject;
 
 JS_PUBLIC_API JS::ModuleResolveHook JS::GetModuleResolveHook(JSRuntime* rt) {
   AssertHeapIsIdle();
 
   return rt->moduleResolveHook;
@@ -75,26 +78,39 @@ JS_PUBLIC_API bool JS::FinishDynamicModu
   AssertHeapIsIdle();
   CHECK_THREAD(cx);
   cx->check(referencingPrivate, promise);
 
   return js::FinishDynamicModuleImport(cx, referencingPrivate, specifier,
                                        promise);
 }
 
-JS_PUBLIC_API JSObject* JS::CompileModule(JSContext* cx,
-                                          const ReadOnlyCompileOptions& options,
-                                          SourceText<char16_t>& srcBuf) {
+template <typename Unit>
+static JSObject* CompileModuleHelper(JSContext* cx,
+                                     const JS::ReadOnlyCompileOptions& options,
+                                     JS::SourceText<Unit>& srcBuf) {
   MOZ_ASSERT(!cx->zone()->isAtomsZone());
   AssertHeapIsIdle();
   CHECK_THREAD(cx);
 
   return js::frontend::CompileModule(cx, options, srcBuf);
 }
 
+JS_PUBLIC_API JSObject* JS::CompileModule(JSContext* cx,
+                                          const ReadOnlyCompileOptions& options,
+                                          SourceText<char16_t>& srcBuf) {
+  return CompileModuleHelper(cx, options, srcBuf);
+}
+
+JS_PUBLIC_API JSObject* JS::CompileModule(JSContext* cx,
+                                          const ReadOnlyCompileOptions& options,
+                                          SourceText<Utf8Unit>& srcBuf) {
+  return CompileModuleHelper(cx, options, srcBuf);
+}
+
 JS_PUBLIC_API void JS::SetModulePrivate(JSObject* module, const Value& value) {
   JSRuntime* rt = module->zone()->runtimeFromMainThread();
   module->as<ModuleObject>().scriptSourceObject()->setPrivate(rt, value);
 }
 
 JS_PUBLIC_API JS::Value JS::GetModulePrivate(JSObject* module) {
   return module->as<ModuleObject>().scriptSourceObject()->canonicalPrivate();
 }