Bug 1759881 - Part 6: Remove use of nsJSUtils layer for module instantiation and evaluation r=smaug

This layer doesn't add much above calling into the JS engine and it's DOM
specific so let's remove it.

Differential Revision: https://phabricator.services.mozilla.com/D141253
This commit is contained in:
Jon Coppeard
2022-03-17 15:22:18 +00:00
parent 1b4dec7136
commit 0e14bcc89e
3 changed files with 21 additions and 70 deletions

View File

@@ -142,41 +142,6 @@ bool nsJSUtils::IsScriptable(JS::Handle<JSObject*> aEvaluationGlobal) {
return xpc::Scriptability::Get(aEvaluationGlobal).Allowed();
}
nsresult nsJSUtils::ModuleInstantiate(JSContext* aCx,
JS::Handle<JSObject*> 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<JSObject*> aModule,
JS::MutableHandle<JS::Value> 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<JSObject*> aScopeChain) {
JS::RootedValue val(aCx);

View File

@@ -72,25 +72,6 @@ class nsJSUtils {
static bool IsScriptable(JS::Handle<JSObject*> aEvaluationGlobal);
static nsresult ModuleInstantiate(JSContext* aCx,
JS::Handle<JSObject*> aModule);
/*
* Wrapper for JSAPI ModuleEvaluate function.
*
* @param JSContext aCx
* The JSContext where this is executed.
* @param JS::Handle<JSObject*> aModule
* The module to be evaluated.
* @param JS::Handle<Value*> aResult
* If Top level await is enabled:
* The evaluation promise returned from evaluating the module.
* Otherwise:
* Undefined
*/
static nsresult ModuleEvaluate(JSContext* aCx, JS::Handle<JSObject*> aModule,
JS::MutableHandle<JS::Value> 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(

View File

@@ -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<JSObject*> 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<JSObject*> 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<JSObject*> 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();