* import_native_module_sync: * @identifier: the specifier for the module to import * * JS function exposed as `import.meta.importSync` in internal modules only. * * Synchronously imports native "modules" from the import global's * native registry. This function does not do blocking I/O so it is * safe to call it synchronously for accessing native "modules" within * modules. This function i
| 407 | * Returns: the imported JS module object. |
| 408 | */ |
| 409 | static bool import_native_module_sync(JSContext* cx, unsigned argc, |
| 410 | JS::Value* vp) { |
| 411 | JS::CallArgs args = JS::CallArgsFromVp(argc, vp); |
| 412 | JS::UniqueChars id; |
| 413 | if (!gjs_parse_call_args(cx, "importSync", args, "s", "identifier", &id)) |
| 414 | return false; |
| 415 | |
| 416 | Gjs::AutoMainRealm ar{cx}; |
| 417 | JS::RootedObject global{cx, JS::CurrentGlobalOrNull(cx)}; |
| 418 | |
| 419 | JS::AutoSaveExceptionState exc_state(cx); |
| 420 | |
| 421 | JS::RootedObject native_registry(cx, gjs_get_native_registry(global)); |
| 422 | JS::RootedObject v_module(cx); |
| 423 | |
| 424 | JS::RootedId key(cx, gjs_intern_string_to_id(cx, id.get())); |
| 425 | if (!gjs_global_registry_get(cx, native_registry, key, &v_module)) |
| 426 | return false; |
| 427 | |
| 428 | if (v_module) { |
| 429 | args.rval().setObject(*v_module); |
| 430 | return true; |
| 431 | } |
| 432 | |
| 433 | JS::RootedObject native_obj(cx); |
| 434 | if (!Gjs::NativeModuleDefineFuncs::get().define(cx, id.get(), |
| 435 | &native_obj)) { |
| 436 | gjs_throw(cx, "Failed to load native module: %s", id.get()); |
| 437 | return false; |
| 438 | } |
| 439 | |
| 440 | if (!gjs_global_registry_set(cx, native_registry, key, native_obj)) |
| 441 | return false; |
| 442 | |
| 443 | args.rval().setObject(*native_obj); |
| 444 | return true; |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * gjs_populate_module_meta: |
nothing calls this directly
no test coverage detected