Bug 1501157 - Allow checking whether dynamic module import is supported from helper threads r=jandem
authorJon Coppeard <jcoppeard@mozilla.com>
Wed, 24 Oct 2018 11:03:58 +0100
changeset 491061 8468729762b5111214f3d75f7b10f841227ad842
parent 491060 3f33fb9163fcfe2fc6890808730d4a13a1769671
child 491062 3377d76041ed9940b811024745441d7b8846b29a
push id247
push userfmarier@mozilla.com
push dateSat, 27 Oct 2018 01:06:44 +0000
reviewersjandem
bugs1501157
milestone65.0a1
Bug 1501157 - Allow checking whether dynamic module import is supported from helper threads r=jandem
js/src/builtin/ModuleObject.cpp
js/src/jit-test/tests/modules/bug-1501157.js
js/src/vm/Runtime.h
--- a/js/src/builtin/ModuleObject.cpp
+++ b/js/src/builtin/ModuleObject.cpp
@@ -1839,28 +1839,37 @@ js::StartDynamicModuleImport(JSContext* 
 
     RootedObject promiseObject(cx, JS::NewPromiseObject(cx, nullptr));
     if (!promiseObject) {
         return nullptr;
     }
 
     Handle<PromiseObject*> promise = promiseObject.as<PromiseObject>();
 
-    RootedString specifier(cx, ToString(cx, specifierArg));
-    if (!specifier) {
-        if (!RejectPromiseWithPendingError(cx, promise))
+    JS::ModuleDynamicImportHook importHook = cx->runtime()->moduleDynamicImportHook;
+    if (!importHook) {
+        JS_ReportErrorASCII(cx, "Dynamic module import is disabled");
+        if (!RejectPromiseWithPendingError(cx, promise)) {
             return nullptr;
+        }
         return promise;
     }
 
-    JS::ModuleDynamicImportHook importHook = cx->runtime()->moduleDynamicImportHook;
-    MOZ_ASSERT(importHook);
+    RootedString specifier(cx, ToString(cx, specifierArg));
+    if (!specifier) {
+        if (!RejectPromiseWithPendingError(cx, promise)) {
+            return nullptr;
+        }
+        return promise;
+    }
+
     if (!importHook(cx, referencingPrivate, specifier, promise)) {
-        if (!RejectPromiseWithPendingError(cx, promise))
+        if (!RejectPromiseWithPendingError(cx, promise)) {
             return nullptr;
+        }
         return promise;
     }
 
     return promise;
 }
 
 bool
 js::FinishDynamicModuleImport(JSContext* cx, HandleValue referencingPrivate, HandleString specifier,
new file mode 100644
--- /dev/null
+++ b/js/src/jit-test/tests/modules/bug-1501157.js
@@ -0,0 +1,2 @@
+// |jit-test| skip-if: helperThreadCount() === 0
+offThreadCompileScript('import("")', {});
--- a/js/src/vm/Runtime.h
+++ b/js/src/vm/Runtime.h
@@ -977,18 +977,19 @@ struct JSRuntime : public js::MallocProv
     // The implementation-defined abstract operation HostResolveImportedModule.
     js::MainThreadData<JS::ModuleResolveHook> moduleResolveHook;
 
     // A hook that implements the abstract operations
     // HostGetImportMetaProperties and HostFinalizeImportMeta.
     js::MainThreadData<JS::ModuleMetadataHook> moduleMetadataHook;
 
     // A hook that implements the abstract operation
-    // HostImportModuleDynamically.
-    js::MainThreadData<JS::ModuleDynamicImportHook> moduleDynamicImportHook;
+    // HostImportModuleDynamically. This is also used to enable/disable dynamic
+    // module import and can accessed by off-thread parsing.
+    mozilla::Atomic<JS::ModuleDynamicImportHook> moduleDynamicImportHook;
 
   public:
 #if defined(JS_BUILD_BINAST)
     js::BinaryASTSupport& binast() {
         return binast_;
     }
   private:
     js::BinaryASTSupport binast_;