| 659 | } |
| 660 | |
| 661 | bool gjs_dynamic_module_resolve(JSContext* cx, |
| 662 | JS::HandleValue importing_module_priv, |
| 663 | JS::HandleObject module_request, |
| 664 | JS::HandleObject internal_promise) { |
| 665 | g_assert(gjs_global_is_type(cx, GjsGlobalType::DEFAULT) && |
| 666 | "gjs_dynamic_module_resolve can only be called from the default " |
| 667 | "global."); |
| 668 | |
| 669 | JS::RootedObject global(cx, JS::CurrentGlobalOrNull(cx)); |
| 670 | g_assert(global && "gjs_dynamic_module_resolve must be in a realm"); |
| 671 | |
| 672 | JS::RootedValue v_loader( |
| 673 | cx, gjs_get_global_slot(global, GjsGlobalSlot::MODULE_LOADER)); |
| 674 | g_assert(v_loader.isObject()); |
| 675 | JS::RootedObject loader(cx, &v_loader.toObject()); |
| 676 | JS::RootedString specifier( |
| 677 | cx, JS::GetModuleRequestSpecifier(cx, module_request)); |
| 678 | |
| 679 | if (!canonicalize_specifier(cx, &specifier)) |
| 680 | return false; |
| 681 | |
| 682 | JS::RootedObject callback_data(cx, JS_NewPlainObject(cx)); |
| 683 | if (!callback_data || |
| 684 | !JS_DefineProperty(cx, callback_data, "module_request", module_request, |
| 685 | JSPROP_PERMANENT) || |
| 686 | !JS_DefineProperty(cx, callback_data, "promise", internal_promise, |
| 687 | JSPROP_PERMANENT) || |
| 688 | !JS_DefineProperty(cx, callback_data, "priv", importing_module_priv, |
| 689 | JSPROP_PERMANENT)) |
| 690 | return false; |
| 691 | |
| 692 | if (importing_module_priv.isObject()) { |
| 693 | gjs_debug(GJS_DEBUG_IMPORTER, |
| 694 | "Async module resolve hook for module %s (relative to %p), " |
| 695 | "global %p", |
| 696 | gjs_debug_string(specifier).c_str(), |
| 697 | &importing_module_priv.toObject(), global.get()); |
| 698 | } else { |
| 699 | gjs_debug(GJS_DEBUG_IMPORTER, |
| 700 | "Async module resolve hook for module %s (unknown path), " |
| 701 | "global %p", |
| 702 | gjs_debug_string(specifier).c_str(), global.get()); |
| 703 | } |
| 704 | |
| 705 | JS::RootedValueArray<2> args(cx); |
| 706 | args[0].set(importing_module_priv); |
| 707 | args[1].setString(specifier); |
| 708 | |
| 709 | JS::RootedValue result(cx); |
| 710 | if (!JS::Call(cx, loader, "moduleResolveAsyncHook", args, &result)) |
| 711 | return JS::FinishDynamicModuleImport(cx, nullptr, importing_module_priv, |
| 712 | module_request, internal_promise); |
| 713 | |
| 714 | // Release in finish_import |
| 715 | GjsContextPrivate* priv = GjsContextPrivate::from_cx(cx); |
| 716 | priv->main_loop_hold(); |
| 717 | |
| 718 | JS::RootedObject resolved( |
nothing calls this directly
no test coverage detected