diff options
author | Moonchild <moonchild@palemoon.org> | 2020-08-06 18:31:36 +0000 |
---|---|---|
committer | Moonchild <moonchild@palemoon.org> | 2020-08-06 18:31:36 +0000 |
commit | 66654d32a7b38e0ba79ccb04281355c6211ce8f9 (patch) | |
tree | cf42ccabb78f3abb13e8df6a0512e3a43258f7c2 /dom/script | |
parent | df7bdc99dc4d3d6e23f4951448b2f59be9f6ba25 (diff) | |
download | aura-central-66654d32a7b38e0ba79ccb04281355c6211ce8f9.tar.gz |
Issue mcp-graveyard/UXP%618 - Simplify module resolve hook to be a function pointer
This is an ahead-of time port to try and address %1624.
This is based on BZ 1461751 and Jon Coppeard's work in it.
Diffstat (limited to 'dom/script')
-rw-r--r-- | dom/script/ScriptLoader.cpp | 45 | ||||
-rw-r--r-- | dom/script/ScriptLoader.h | 5 |
2 files changed, 19 insertions, 31 deletions
diff --git a/dom/script/ScriptLoader.cpp b/dom/script/ScriptLoader.cpp index 7a3cd8fe3..9de9a1d03 100644 --- a/dom/script/ScriptLoader.cpp +++ b/dom/script/ScriptLoader.cpp @@ -787,24 +787,20 @@ ScriptLoader::StartFetchingModuleAndDependencies(ModuleLoadRequest* aParent, } // 8.1.3.8.1 HostResolveImportedModule(referencingModule, specifier) -bool -HostResolveImportedModule(JSContext* aCx, unsigned argc, JS::Value* vp) +JSObject* +HostResolveImportedModule(JSContext* aCx, JS::Handle<JSObject*> aModule, + JS::Handle<JSString*> aSpecifier) { - MOZ_ASSERT(argc == 2); - JS::CallArgs args = JS::CallArgsFromVp(argc, vp); - JS::Rooted<JSObject*> module(aCx, &args[0].toObject()); - JS::Rooted<JSString*> specifier(aCx, args[1].toString()); - // Let referencing module script be referencingModule.[[HostDefined]]. - JS::Value value = JS::GetModuleHostDefinedField(module); + JS::Value value = JS::GetModuleHostDefinedField(aModule); auto script = static_cast<ModuleScript*>(value.toPrivate()); - MOZ_ASSERT(script->ModuleRecord() == module); + MOZ_ASSERT(script->ModuleRecord() == aModule); // Let url be the result of resolving a module specifier given referencing // module script and specifier. nsAutoJSString string; - if (!string.init(aCx, specifier)) { - return false; + if (!string.init(aCx, aSpecifier)) { + return nullptr; } nsCOMPtr<nsIURI> uri = ResolveModuleSpecifier(script, string); @@ -820,27 +816,20 @@ HostResolveImportedModule(JSContext* aCx, unsigned argc, JS::Value* vp) MOZ_ASSERT(ms, "Resolved module not found in module map"); MOZ_ASSERT(!ms->HasParseError()); + MOZ_ASSERT(ms->ModuleRecord()); - *vp = JS::ObjectValue(*ms->ModuleRecord()); - return true; + return ms->ModuleRecord(); } -static nsresult +static void EnsureModuleResolveHook(JSContext* aCx) { - if (JS::GetModuleResolveHook(aCx)) { - return NS_OK; - } - - JS::Rooted<JSFunction*> func(aCx); - func = JS_NewFunction(aCx, HostResolveImportedModule, 2, 0, - "HostResolveImportedModule"); - if (!func) { - return NS_ERROR_FAILURE; + JSRuntime* rt = JS_GetRuntime(aCx); + if (JS::GetModuleResolveHook(rt)) { + return; } - JS::SetModuleResolveHook(aCx, func); - return NS_OK; + JS::SetModuleResolveHook(rt, HostResolveImportedModule); } void @@ -928,8 +917,7 @@ ScriptLoader::InstantiateModuleTree(ModuleLoadRequest* aRequest) return false; } - nsresult rv = EnsureModuleResolveHook(jsapi.cx()); - NS_ENSURE_SUCCESS(rv, false); + EnsureModuleResolveHook(jsapi.cx()); JS::Rooted<JSObject*> module(jsapi.cx(), moduleScript->ModuleRecord()); bool ok = NS_SUCCEEDED(nsJSUtils::ModuleInstantiate(jsapi.cx(), module)); @@ -1963,8 +1951,7 @@ ScriptLoader::EvaluateScript(ScriptLoadRequest* aRequest) } if (aRequest->IsModuleRequest()) { - rv = EnsureModuleResolveHook(cx); - NS_ENSURE_SUCCESS(rv, rv); + EnsureModuleResolveHook(cx); ModuleLoadRequest* request = aRequest->AsModuleRequest(); MOZ_ASSERT(request->mModuleScript); diff --git a/dom/script/ScriptLoader.h b/dom/script/ScriptLoader.h index 3cbecbf03..4155f08f8 100644 --- a/dom/script/ScriptLoader.h +++ b/dom/script/ScriptLoader.h @@ -579,8 +579,9 @@ private: RefPtr<mozilla::GenericPromise> WaitForModuleFetch(nsIURI* aURL); ModuleScript* GetFetchedModule(nsIURI* aURL) const; - friend bool - HostResolveImportedModule(JSContext* aCx, unsigned argc, JS::Value* vp); + friend JSObject* + HostResolveImportedModule(JSContext* aCx, JS::Handle<JSObject*> aModule, + JS::Handle<JSString*> aSpecifier); nsresult CreateModuleScript(ModuleLoadRequest* aRequest); nsresult ProcessFetchedModuleSource(ModuleLoadRequest* aRequest); |