diff --git a/dom/base/nsJSUtils.cpp b/dom/base/nsJSUtils.cpp index 1e8c723c45ac..399bae250043 100644 --- a/dom/base/nsJSUtils.cpp +++ b/dom/base/nsJSUtils.cpp @@ -142,41 +142,6 @@ bool nsJSUtils::IsScriptable(JS::Handle aEvaluationGlobal) { return xpc::Scriptability::Get(aEvaluationGlobal).Allowed(); } -nsresult nsJSUtils::ModuleInstantiate(JSContext* aCx, - JS::Handle aModule) { - AUTO_PROFILER_LABEL("nsJSUtils::ModuleInstantiate", JS); - - MOZ_ASSERT(aCx == nsContentUtils::GetCurrentJSContext()); - MOZ_ASSERT(NS_IsMainThread()); - - NS_ENSURE_TRUE(xpc::Scriptability::Get(aModule).Allowed(), NS_OK); - - if (!JS::ModuleInstantiate(aCx, aModule)) { - return NS_ERROR_FAILURE; - } - - return NS_OK; -} - -nsresult nsJSUtils::ModuleEvaluate(JSContext* aCx, - JS::Handle aModule, - JS::MutableHandle aResult) { - AUTO_PROFILER_LABEL("nsJSUtils::ModuleEvaluate", JS); - - MOZ_ASSERT(aCx == nsContentUtils::GetCurrentJSContext()); - MOZ_ASSERT(NS_IsMainThread()); - MOZ_ASSERT(CycleCollectedJSContext::Get() && - CycleCollectedJSContext::Get()->MicroTaskLevel()); - - NS_ENSURE_TRUE(xpc::Scriptability::Get(aModule).Allowed(), NS_OK); - - if (!JS::ModuleEvaluate(aCx, aModule, aResult)) { - return NS_ERROR_FAILURE; - } - - return NS_OK; -} - static bool AddScopeChainItem(JSContext* aCx, nsINode* aNode, JS::MutableHandleVector aScopeChain) { JS::RootedValue val(aCx); diff --git a/dom/base/nsJSUtils.h b/dom/base/nsJSUtils.h index 1909967e5c08..85a21e459305 100644 --- a/dom/base/nsJSUtils.h +++ b/dom/base/nsJSUtils.h @@ -72,25 +72,6 @@ class nsJSUtils { static bool IsScriptable(JS::Handle aEvaluationGlobal); - static nsresult ModuleInstantiate(JSContext* aCx, - JS::Handle aModule); - - /* - * Wrapper for JSAPI ModuleEvaluate function. - * - * @param JSContext aCx - * The JSContext where this is executed. - * @param JS::Handle aModule - * The module to be evaluated. - * @param JS::Handle aResult - * If Top level await is enabled: - * The evaluation promise returned from evaluating the module. - * Otherwise: - * Undefined - */ - static nsresult ModuleEvaluate(JSContext* aCx, JS::Handle aModule, - JS::MutableHandle aResult); - // Returns false if an exception got thrown on aCx. Passing a null // aElement is allowed; that wil produce an empty aScopeChain. static bool GetScopeChainForElement( diff --git a/js/loader/ModuleLoaderBase.cpp b/js/loader/ModuleLoaderBase.cpp index 05ba9dbfa580..ce9dc6e0bbe3 100644 --- a/js/loader/ModuleLoaderBase.cpp +++ b/js/loader/ModuleLoaderBase.cpp @@ -674,6 +674,8 @@ bool ModuleLoaderBase::InstantiateModuleTree(ModuleLoadRequest* aRequest) { LOG(("ScriptLoadRequest (%p): Instantiate module tree", aRequest)); + AUTO_PROFILER_LABEL("ModuleLoaderBase::InstantiateModuleTree", JS); + ModuleScript* moduleScript = aRequest->mModuleScript; MOZ_ASSERT(moduleScript); @@ -692,9 +694,11 @@ bool ModuleLoaderBase::InstantiateModuleTree(ModuleLoadRequest* aRequest) { } JS::Rooted module(jsapi.cx(), moduleScript->ModuleRecord()); - bool ok = NS_SUCCEEDED(nsJSUtils::ModuleInstantiate(jsapi.cx(), module)); + if (!xpc::Scriptability::Get(module).Allowed()) { + return true; + } - if (!ok) { + if (!JS::ModuleInstantiate(jsapi.cx(), module)) { LOG(("ScriptLoadRequest (%p): Instantiate failed", aRequest)); MOZ_ASSERT(jsapi.HasException()); JS::RootedValue exception(jsapi.cx()); @@ -757,6 +761,8 @@ void ModuleLoaderBase::ProcessDynamicImport(ModuleLoadRequest* aRequest) { nsresult ModuleLoaderBase::EvaluateModule(nsIGlobalObject* aGlobalObject, ScriptLoadRequest* aRequest) { + AUTO_PROFILER_LABEL("ModuleLoaderBase::EvaluateModule", JS); + mozilla::nsAutoMicroTask mt; mozilla::dom::AutoEntryScript aes(aGlobalObject, "EvaluateModule", true); JSContext* cx = aes.cx(); @@ -792,6 +798,10 @@ nsresult ModuleLoaderBase::EvaluateModule(nsIGlobalObject* aGlobalObject, JS::Rooted module(cx, moduleScript->ModuleRecord()); MOZ_ASSERT(module); + if (!xpc::Scriptability::Get(module).Allowed()) { + return NS_OK; + } + nsresult rv = InitDebuggerDataForModuleTree(cx, request); NS_ENSURE_SUCCESS(rv, rv); @@ -804,20 +814,15 @@ nsresult ModuleLoaderBase::EvaluateModule(nsIGlobalObject* aGlobalObject, mLoader->MaybePrepareModuleForBytecodeEncodingBeforeExecute(cx, request); - rv = nsJSUtils::ModuleEvaluate(cx, module, &rval); - - if (NS_SUCCEEDED(rv)) { + if (JS::ModuleEvaluate(cx, module, &rval)) { // If we have an infinite loop in a module, which is stopped by the // user, the module evaluation will fail, but we will not have an // AutoEntryScript exception. MOZ_ASSERT(!aes.HasException()); - } - - if (NS_FAILED(rv)) { + } else { LOG(("ScriptLoadRequest (%p): evaluation failed", aRequest)); - // For a dynamic import, the promise is rejected. Otherwise an error is - // either reported by AutoEntryScript. - rv = NS_OK; + // For a dynamic import, the promise is rejected. Otherwise an error is + // reported by AutoEntryScript. } JS::Rooted aEvaluationPromise(cx); @@ -829,19 +834,19 @@ nsresult ModuleLoaderBase::EvaluateModule(nsIGlobalObject* aGlobalObject, aEvaluationPromise.set(&rval.toObject()); } if (request->IsDynamicImport()) { - FinishDynamicImport(cx, request, rv, aEvaluationPromise); + FinishDynamicImport(cx, request, NS_OK, aEvaluationPromise); } else { // If this is not a dynamic import, and if the promise is rejected, // the value is unwrapped from the promise value. if (!JS::ThrowOnModuleEvaluationFailure(cx, aEvaluationPromise)) { LOG(("ScriptLoadRequest (%p): evaluation failed on throw", aRequest)); - // For a dynamic import, the promise is rejected. Otherwise an - // error is either reported by AutoEntryScript. - rv = NS_OK; + // For a dynamic import, the promise is rejected. Otherwise an error is + // reported by AutoEntryScript. } } - rv = mLoader->MaybePrepareModuleForBytecodeEncodingAfterExecute(request, rv); + rv = mLoader->MaybePrepareModuleForBytecodeEncodingAfterExecute(request, + NS_OK); mLoader->MaybeTriggerBytecodeEncoding();