summaryrefslogtreecommitdiffstats
path: root/dom
diff options
context:
space:
mode:
authorMoonchild <moonchild@palemoon.org>2020-08-06 18:31:36 +0000
committerMoonchild <moonchild@palemoon.org>2020-08-30 09:37:12 +0000
commit10a10fd3757374123eb5e3aab1e4720f86575f47 (patch)
tree2e7d794acdd6105eba65013cff421fe7dad94299 /dom
parent1012dbe9e5b2d00f967b0523f94ac8cc7ed3118d (diff)
downloadUXP-10a10fd3757374123eb5e3aab1e4720f86575f47.tar
UXP-10a10fd3757374123eb5e3aab1e4720f86575f47.tar.gz
UXP-10a10fd3757374123eb5e3aab1e4720f86575f47.tar.lz
UXP-10a10fd3757374123eb5e3aab1e4720f86575f47.tar.xz
UXP-10a10fd3757374123eb5e3aab1e4720f86575f47.zip
Issue #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')
-rw-r--r--dom/script/ScriptLoader.cpp45
-rw-r--r--dom/script/ScriptLoader.h5
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);