summaryrefslogtreecommitdiffstats
path: root/js/src/shell/js.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'js/src/shell/js.cpp')
-rw-r--r--js/src/shell/js.cpp33
1 files changed, 29 insertions, 4 deletions
diff --git a/js/src/shell/js.cpp b/js/src/shell/js.cpp
index 8cd821b31..bbf35459f 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