* gjs_module_load: * @cx: the current JSContext * @identifier: specifier of the module to load * @file_uri: URI to load the module from * * Loads and registers a module given a specifier and URI. * * Returns: whether an error occurred while resolving the specifier. */
| 353 | * Returns: whether an error occurred while resolving the specifier. |
| 354 | */ |
| 355 | JSObject* gjs_module_load(JSContext* cx, const char* identifier, |
| 356 | const char* file_uri) { |
| 357 | g_assert((gjs_global_is_type(cx, GjsGlobalType::DEFAULT) || |
| 358 | gjs_global_is_type(cx, GjsGlobalType::INTERNAL)) && |
| 359 | "gjs_module_load can only be called from module-enabled " |
| 360 | "globals."); |
| 361 | |
| 362 | JS::RootedObject global(cx, JS::CurrentGlobalOrNull(cx)); |
| 363 | JS::RootedValue v_loader( |
| 364 | cx, gjs_get_global_slot(global, GjsGlobalSlot::MODULE_LOADER)); |
| 365 | g_assert(v_loader.isObject()); |
| 366 | JS::RootedObject loader(cx, &v_loader.toObject()); |
| 367 | |
| 368 | JS::ConstUTF8CharsZ id_chars(identifier, strlen(identifier)); |
| 369 | JS::ConstUTF8CharsZ uri_chars(file_uri, strlen(file_uri)); |
| 370 | JS::RootedString id(cx, JS_NewStringCopyUTF8Z(cx, id_chars)); |
| 371 | if (!id) |
| 372 | return nullptr; |
| 373 | JS::RootedString uri(cx, JS_NewStringCopyUTF8Z(cx, uri_chars)); |
| 374 | if (!uri) |
| 375 | return nullptr; |
| 376 | |
| 377 | JS::RootedValueArray<2> args(cx); |
| 378 | args[0].setString(id); |
| 379 | args[1].setString(uri); |
| 380 | |
| 381 | gjs_debug(GJS_DEBUG_IMPORTER, |
| 382 | "Module load hook for module '%s' (%s), global %p", identifier, |
| 383 | file_uri, global.get()); |
| 384 | |
| 385 | JS::RootedValue result(cx); |
| 386 | if (!JS::Call(cx, loader, "moduleLoadHook", args, &result)) |
| 387 | return nullptr; |
| 388 | |
| 389 | g_assert(result.isObject() && "Module hook failed to return an object!"); |
| 390 | return &result.toObject(); |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * import_native_module_sync: |
no test coverage detected