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 | 9b6252893876995ae4c1f278fc8d1cbdfb72e94d (patch) | |
tree | cf42ccabb78f3abb13e8df6a0512e3a43258f7c2 /js/src/shell | |
parent | c349f04f9501c5035667f8064782d06e298cb52a (diff) | |
download | uxp-9b6252893876995ae4c1f278fc8d1cbdfb72e94d.tar.gz |
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 'js/src/shell')
-rw-r--r-- | js/src/shell/js.cpp | 33 |
1 files changed, 29 insertions, 4 deletions
diff --git a/js/src/shell/js.cpp b/js/src/shell/js.cpp index 8cd821b311..bbf35459ff 100644 --- a/js/src/shell/js.cpp +++ b/js/src/shell/js.cpp @@ -274,6 +274,7 @@ struct ShellContext JS::PersistentRooted<JobQueue> jobQueue; ExclusiveData<ShellAsyncTasks> asyncTasks; bool drainingJobQueue; + JS::PersistentRootedFunction moduleResolveHook; /* * Watchdog thread state. @@ -439,7 +440,8 @@ ShellContext::ShellContext(JSContext* cx) exitCode(0), quitting(false), readLineBufPos(0), - spsProfilingStackSize(0) + spsProfilingStackSize(0), + moduleResolveHook(cx) {} static ShellContext* @@ -4030,13 +4032,34 @@ SetModuleResolveHook(JSContext* cx, unsigned argc, Value* vp) return false; } - RootedFunction hook(cx, &args[0].toObject().as<JSFunction>()); - Rooted<GlobalObject*> global(cx, cx->global()); - global->setModuleResolveHook(hook); + ShellContext* sc = GetShellContext(cx); + sc->moduleResolveHook = &args[0].toObject().as<JSFunction>(); + args.rval().setUndefined(); return true; } +static JSObject* +CallModuleResolveHook(JSContext* cx, HandleObject module, HandleString specifier) +{ + ShellContext* sc = GetShellContext(cx); + + JS::AutoValueArray<2> args(cx); + args[0].setObject(*module); + args[1].setString(specifier); + + RootedValue result(cx); + if (!JS_CallFunction(cx, nullptr, sc->moduleResolveHook, args, &result)) + return nullptr; + + if (!result.isObject() || !result.toObject().is<ModuleObject>()) { + JS_ReportErrorASCII(cx, "Module resolve hook did not return Module object"); + return nullptr; + } + + return &result.toObject(); +} + static bool GetModuleLoadPath(JSContext* cx, unsigned argc, Value* vp) { @@ -7962,6 +7985,8 @@ main(int argc, char** argv, char** envp) js::SetPreserveWrapperCallback(cx, DummyPreserveWrapperCallback); + JS::SetModuleResolveHook(cx->runtime(), CallModuleResolveHook); + result = Shell(cx, &op, envp); #ifdef DEBUG |