* gjs_module_resolve: * @cx: the current JSContext * @importing_module_priv: the private JS Object of the #Module object * initiating the import, or a JS null value * @module_request: the module request object * * This function resolves import specifiers. It is called internally by * SpiderMonkey as a hook. * * Returns: whether an error occurred while resolving the specifier. */
| 540 | * Returns: whether an error occurred while resolving the specifier. |
| 541 | */ |
| 542 | JSObject* gjs_module_resolve(JSContext* cx, |
| 543 | JS::HandleValue importing_module_priv, |
| 544 | JS::HandleObject module_request) { |
| 545 | g_assert((gjs_global_is_type(cx, GjsGlobalType::DEFAULT) || |
| 546 | gjs_global_is_type(cx, GjsGlobalType::INTERNAL)) && |
| 547 | "gjs_module_resolve can only be called from module-enabled " |
| 548 | "globals."); |
| 549 | JS::RootedString specifier( |
| 550 | cx, JS::GetModuleRequestSpecifier(cx, module_request)); |
| 551 | |
| 552 | JS::RootedObject global(cx, JS::CurrentGlobalOrNull(cx)); |
| 553 | JS::RootedValue v_loader( |
| 554 | cx, gjs_get_global_slot(global, GjsGlobalSlot::MODULE_LOADER)); |
| 555 | g_assert(v_loader.isObject()); |
| 556 | JS::RootedObject loader(cx, &v_loader.toObject()); |
| 557 | |
| 558 | if (!canonicalize_specifier(cx, &specifier)) |
| 559 | return nullptr; |
| 560 | |
| 561 | JS::RootedValueArray<2> args(cx); |
| 562 | args[0].set(importing_module_priv); |
| 563 | args[1].setString(specifier); |
| 564 | |
| 565 | gjs_debug(GJS_DEBUG_IMPORTER, |
| 566 | "Module resolve hook for module %s (relative to %s), global %p", |
| 567 | gjs_debug_string(specifier).c_str(), |
| 568 | gjs_debug_value(importing_module_priv).c_str(), global.get()); |
| 569 | |
| 570 | JS::RootedValue result(cx); |
| 571 | if (!JS::Call(cx, loader, "moduleResolveHook", args, &result)) |
| 572 | return nullptr; |
| 573 | |
| 574 | g_assert(result.isObject() && "resolve hook failed to return an object!"); |
| 575 | return &result.toObject(); |
| 576 | } |
| 577 | |
| 578 | // Call JS::FinishDynamicModuleImport() with the values stashed in the function. |
| 579 | // Can fail in JS::FinishDynamicModuleImport(), but will assert if anything |
nothing calls this directly
no test coverage detected