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:
@@ -142,41 +142,6 @@ bool nsJSUtils::IsScriptable(JS::Handle<JSObject*> aEvaluationGlobal) {
|
|||||||
return xpc::Scriptability::Get(aEvaluationGlobal).Allowed();
|
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,
|
static bool AddScopeChainItem(JSContext* aCx, nsINode* aNode,
|
||||||
JS::MutableHandleVector<JSObject*> aScopeChain) {
|
JS::MutableHandleVector<JSObject*> aScopeChain) {
|
||||||
JS::RootedValue val(aCx);
|
JS::RootedValue val(aCx);
|
||||||
|
|||||||
@@ -72,25 +72,6 @@ class nsJSUtils {
|
|||||||
|
|
||||||
static bool IsScriptable(JS::Handle<JSObject*> aEvaluationGlobal);
|
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
|
// Returns false if an exception got thrown on aCx. Passing a null
|
||||||
// aElement is allowed; that wil produce an empty aScopeChain.
|
// aElement is allowed; that wil produce an empty aScopeChain.
|
||||||
static bool GetScopeChainForElement(
|
static bool GetScopeChainForElement(
|
||||||
|
|||||||
@@ -674,6 +674,8 @@ bool ModuleLoaderBase::InstantiateModuleTree(ModuleLoadRequest* aRequest) {
|
|||||||
|
|
||||||
LOG(("ScriptLoadRequest (%p): Instantiate module tree", aRequest));
|
LOG(("ScriptLoadRequest (%p): Instantiate module tree", aRequest));
|
||||||
|
|
||||||
|
AUTO_PROFILER_LABEL("ModuleLoaderBase::InstantiateModuleTree", JS);
|
||||||
|
|
||||||
ModuleScript* moduleScript = aRequest->mModuleScript;
|
ModuleScript* moduleScript = aRequest->mModuleScript;
|
||||||
MOZ_ASSERT(moduleScript);
|
MOZ_ASSERT(moduleScript);
|
||||||
|
|
||||||
@@ -692,9 +694,11 @@ bool ModuleLoaderBase::InstantiateModuleTree(ModuleLoadRequest* aRequest) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
JS::Rooted<JSObject*> module(jsapi.cx(), moduleScript->ModuleRecord());
|
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));
|
LOG(("ScriptLoadRequest (%p): Instantiate failed", aRequest));
|
||||||
MOZ_ASSERT(jsapi.HasException());
|
MOZ_ASSERT(jsapi.HasException());
|
||||||
JS::RootedValue exception(jsapi.cx());
|
JS::RootedValue exception(jsapi.cx());
|
||||||
@@ -757,6 +761,8 @@ void ModuleLoaderBase::ProcessDynamicImport(ModuleLoadRequest* aRequest) {
|
|||||||
|
|
||||||
nsresult ModuleLoaderBase::EvaluateModule(nsIGlobalObject* aGlobalObject,
|
nsresult ModuleLoaderBase::EvaluateModule(nsIGlobalObject* aGlobalObject,
|
||||||
ScriptLoadRequest* aRequest) {
|
ScriptLoadRequest* aRequest) {
|
||||||
|
AUTO_PROFILER_LABEL("ModuleLoaderBase::EvaluateModule", JS);
|
||||||
|
|
||||||
mozilla::nsAutoMicroTask mt;
|
mozilla::nsAutoMicroTask mt;
|
||||||
mozilla::dom::AutoEntryScript aes(aGlobalObject, "EvaluateModule", true);
|
mozilla::dom::AutoEntryScript aes(aGlobalObject, "EvaluateModule", true);
|
||||||
JSContext* cx = aes.cx();
|
JSContext* cx = aes.cx();
|
||||||
@@ -792,6 +798,10 @@ nsresult ModuleLoaderBase::EvaluateModule(nsIGlobalObject* aGlobalObject,
|
|||||||
JS::Rooted<JSObject*> module(cx, moduleScript->ModuleRecord());
|
JS::Rooted<JSObject*> module(cx, moduleScript->ModuleRecord());
|
||||||
MOZ_ASSERT(module);
|
MOZ_ASSERT(module);
|
||||||
|
|
||||||
|
if (!xpc::Scriptability::Get(module).Allowed()) {
|
||||||
|
return NS_OK;
|
||||||
|
}
|
||||||
|
|
||||||
nsresult rv = InitDebuggerDataForModuleTree(cx, request);
|
nsresult rv = InitDebuggerDataForModuleTree(cx, request);
|
||||||
NS_ENSURE_SUCCESS(rv, rv);
|
NS_ENSURE_SUCCESS(rv, rv);
|
||||||
|
|
||||||
@@ -804,20 +814,15 @@ nsresult ModuleLoaderBase::EvaluateModule(nsIGlobalObject* aGlobalObject,
|
|||||||
|
|
||||||
mLoader->MaybePrepareModuleForBytecodeEncodingBeforeExecute(cx, request);
|
mLoader->MaybePrepareModuleForBytecodeEncodingBeforeExecute(cx, request);
|
||||||
|
|
||||||
rv = nsJSUtils::ModuleEvaluate(cx, module, &rval);
|
if (JS::ModuleEvaluate(cx, module, &rval)) {
|
||||||
|
|
||||||
if (NS_SUCCEEDED(rv)) {
|
|
||||||
// If we have an infinite loop in a module, which is stopped by the
|
// 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
|
// user, the module evaluation will fail, but we will not have an
|
||||||
// AutoEntryScript exception.
|
// AutoEntryScript exception.
|
||||||
MOZ_ASSERT(!aes.HasException());
|
MOZ_ASSERT(!aes.HasException());
|
||||||
}
|
} else {
|
||||||
|
|
||||||
if (NS_FAILED(rv)) {
|
|
||||||
LOG(("ScriptLoadRequest (%p): evaluation failed", aRequest));
|
LOG(("ScriptLoadRequest (%p): evaluation failed", aRequest));
|
||||||
// For a dynamic import, the promise is rejected. Otherwise an error is
|
// For a dynamic import, the promise is rejected. Otherwise an error is
|
||||||
// either reported by AutoEntryScript.
|
// reported by AutoEntryScript.
|
||||||
rv = NS_OK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
JS::Rooted<JSObject*> aEvaluationPromise(cx);
|
JS::Rooted<JSObject*> aEvaluationPromise(cx);
|
||||||
@@ -829,19 +834,19 @@ nsresult ModuleLoaderBase::EvaluateModule(nsIGlobalObject* aGlobalObject,
|
|||||||
aEvaluationPromise.set(&rval.toObject());
|
aEvaluationPromise.set(&rval.toObject());
|
||||||
}
|
}
|
||||||
if (request->IsDynamicImport()) {
|
if (request->IsDynamicImport()) {
|
||||||
FinishDynamicImport(cx, request, rv, aEvaluationPromise);
|
FinishDynamicImport(cx, request, NS_OK, aEvaluationPromise);
|
||||||
} else {
|
} else {
|
||||||
// If this is not a dynamic import, and if the promise is rejected,
|
// If this is not a dynamic import, and if the promise is rejected,
|
||||||
// the value is unwrapped from the promise value.
|
// the value is unwrapped from the promise value.
|
||||||
if (!JS::ThrowOnModuleEvaluationFailure(cx, aEvaluationPromise)) {
|
if (!JS::ThrowOnModuleEvaluationFailure(cx, aEvaluationPromise)) {
|
||||||
LOG(("ScriptLoadRequest (%p): evaluation failed on throw", aRequest));
|
LOG(("ScriptLoadRequest (%p): evaluation failed on throw", aRequest));
|
||||||
// For a dynamic import, the promise is rejected. Otherwise an
|
// For a dynamic import, the promise is rejected. Otherwise an error is
|
||||||
// error is either reported by AutoEntryScript.
|
// reported by AutoEntryScript.
|
||||||
rv = NS_OK;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rv = mLoader->MaybePrepareModuleForBytecodeEncodingAfterExecute(request, rv);
|
rv = mLoader->MaybePrepareModuleForBytecodeEncodingAfterExecute(request,
|
||||||
|
NS_OK);
|
||||||
|
|
||||||
mLoader->MaybeTriggerBytecodeEncoding();
|
mLoader->MaybeTriggerBytecodeEncoding();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user