mirror of
https://repo.dactyloidae.xyz/Dactyloidae/UXP.git
synced 2026-09-08 16:58:38 +09:00
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.
This commit is contained in:
parent
976239c8d0
commit
34db761758
10 changed files with 76 additions and 74 deletions
|
|
@ -4667,21 +4667,16 @@ JS::Evaluate(JSContext* cx, const ReadOnlyCompileOptions& optionsArg,
|
|||
return ::Evaluate(cx, optionsArg, filename, rval);
|
||||
}
|
||||
|
||||
JS_PUBLIC_API(JSFunction*)
|
||||
JS::GetModuleResolveHook(JSContext* cx)
|
||||
JS_PUBLIC_API(JS::ModuleResolveHook)
|
||||
JS::GetModuleResolveHook(JSRuntime* rt)
|
||||
{
|
||||
AssertHeapIsIdle(cx);
|
||||
CHECK_REQUEST(cx);
|
||||
return cx->global()->moduleResolveHook();
|
||||
return rt->moduleResolveHook;
|
||||
}
|
||||
|
||||
JS_PUBLIC_API(void)
|
||||
JS::SetModuleResolveHook(JSContext* cx, HandleFunction func)
|
||||
JS::SetModuleResolveHook(JSRuntime* rt, JS::ModuleResolveHook func)
|
||||
{
|
||||
AssertHeapIsIdle(cx);
|
||||
CHECK_REQUEST(cx);
|
||||
assertSameCompartment(cx, func);
|
||||
cx->global()->setModuleResolveHook(func);
|
||||
rt->moduleResolveHook = func;
|
||||
}
|
||||
|
||||
JS_PUBLIC_API(bool)
|
||||
|
|
|
|||
|
|
@ -4325,17 +4325,19 @@ extern JS_PUBLIC_API(bool)
|
|||
Evaluate(JSContext* cx, const ReadOnlyCompileOptions& options,
|
||||
const char* filename, JS::MutableHandleValue rval);
|
||||
|
||||
/**
|
||||
* Get the HostResolveImportedModule hook for a global.
|
||||
*/
|
||||
extern JS_PUBLIC_API(JSFunction*)
|
||||
GetModuleResolveHook(JSContext* cx);
|
||||
using ModuleResolveHook = JSObject* (*)(JSContext*, HandleObject, HandleString);
|
||||
|
||||
/**
|
||||
* Set the HostResolveImportedModule hook for a global to the given function.
|
||||
* Get the HostResolveImportedModule hook for the runtime.
|
||||
*/
|
||||
extern JS_PUBLIC_API(ModuleResolveHook)
|
||||
GetModuleResolveHook(JSRuntime* rt);
|
||||
|
||||
/**
|
||||
* Set the HostResolveImportedModule hook for the runtime to the given function.
|
||||
*/
|
||||
extern JS_PUBLIC_API(void)
|
||||
SetModuleResolveHook(JSContext* cx, JS::HandleFunction func);
|
||||
SetModuleResolveHook(JSRuntime* rt, ModuleResolveHook func);
|
||||
|
||||
/**
|
||||
* Parse the given source buffer as a module in the scope of the current global
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -119,7 +119,6 @@ class GlobalObject : public NativeObject
|
|||
DEBUGGERS,
|
||||
INTRINSICS,
|
||||
FOR_OF_PIC_CHAIN,
|
||||
MODULE_RESOLVE_HOOK,
|
||||
WINDOW_PROXY,
|
||||
GLOBAL_THIS_RESOLVED,
|
||||
|
||||
|
|
@ -879,19 +878,6 @@ class GlobalObject : public NativeObject
|
|||
setReservedSlot(WINDOW_PROXY, ObjectValue(*windowProxy));
|
||||
}
|
||||
|
||||
void setModuleResolveHook(HandleFunction hook) {
|
||||
MOZ_ASSERT(hook);
|
||||
setSlot(MODULE_RESOLVE_HOOK, ObjectValue(*hook));
|
||||
}
|
||||
|
||||
JSFunction* moduleResolveHook() {
|
||||
Value value = getSlotRef(MODULE_RESOLVE_HOOK);
|
||||
if (value.isUndefined())
|
||||
return nullptr;
|
||||
|
||||
return &value.toObject().as<JSFunction>();
|
||||
}
|
||||
|
||||
// Returns either this global's star-generator function prototype, or null
|
||||
// if that object was never created. Dodgy; for use only in also-dodgy
|
||||
// GlobalHelperThreadState::mergeParseTaskCompartment().
|
||||
|
|
|
|||
|
|
@ -241,8 +241,10 @@ JSRuntime::JSRuntime(JSRuntime* parentRuntime)
|
|||
lastAnimationTime(0),
|
||||
performanceMonitoring(thisFromCtor()),
|
||||
ionLazyLinkListSize_(0),
|
||||
stackFormat_(parentRuntime ? js::StackFormat::Default
|
||||
: js::StackFormat::SpiderMonkey)
|
||||
stackFormat_(parentRuntime ?
|
||||
js::StackFormat::Default :
|
||||
js::StackFormat::SpiderMonkey),
|
||||
moduleResolveHook()
|
||||
{
|
||||
setGCStoreBufferPtr(&gc.storeBuffer);
|
||||
|
||||
|
|
|
|||
|
|
@ -1294,6 +1294,9 @@ struct JSRuntime : public JS::shadow::Runtime,
|
|||
// For inherited heap state accessors.
|
||||
friend class js::gc::AutoTraceSession;
|
||||
friend class JS::AutoEnterCycleCollection;
|
||||
|
||||
// The implementation-defined abstract operation HostResolveImportedModule.
|
||||
JS::ModuleResolveHook moduleResolveHook;
|
||||
};
|
||||
|
||||
namespace js {
|
||||
|
|
|
|||
|
|
@ -2026,25 +2026,26 @@ intrinsic_HostResolveImportedModule(JSContext* cx, unsigned argc, Value* vp)
|
|||
{
|
||||
CallArgs args = CallArgsFromVp(argc, vp);
|
||||
MOZ_ASSERT(args.length() == 2);
|
||||
MOZ_ASSERT(args[0].toObject().is<ModuleObject>());
|
||||
MOZ_ASSERT(args[1].isString());
|
||||
RootedModuleObject module(cx, &args[0].toObject().as<ModuleObject>());
|
||||
RootedString specifier(cx, args[1].toString());
|
||||
|
||||
RootedFunction moduleResolveHook(cx, cx->global()->moduleResolveHook());
|
||||
JS::ModuleResolveHook moduleResolveHook = cx->runtime()->moduleResolveHook;
|
||||
if (!moduleResolveHook) {
|
||||
JS_ReportErrorASCII(cx, "Module resolve hook not set");
|
||||
return false;
|
||||
}
|
||||
|
||||
RootedValue result(cx);
|
||||
if (!JS_CallFunction(cx, nullptr, moduleResolveHook, args, &result))
|
||||
RootedObject result(cx);
|
||||
result = moduleResolveHook(cx, module, specifier);
|
||||
if (!result)
|
||||
return false;
|
||||
|
||||
if (!result.isObject() || !result.toObject().is<ModuleObject>()) {
|
||||
if (!result->is<ModuleObject>()) {
|
||||
JS_ReportErrorASCII(cx, "Module resolve hook did not return Module object");
|
||||
return false;
|
||||
}
|
||||
|
||||
args.rval().set(result);
|
||||
args.rval().setObject(*result);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue