* gjs_load_internal_module: * @cx: the current JSContext * @identifier: the identifier of the internal module * * Loads a module source from an internal resource, * resource:///org/gnome/gjs/modules/internal/{#identifier}.js, registers it in * the internal global's module registry, and proceeds to compile, initialize, * and evaluate the module. * * Returns: whether an error occurred while
| 67 | * Returns: whether an error occurred while loading or evaluating the module. |
| 68 | */ |
| 69 | bool gjs_load_internal_module(JSContext* cx, const char* identifier) { |
| 70 | Gjs::AutoChar full_path(g_strdup_printf( |
| 71 | "resource:///org/gnome/gjs/modules/internal/%s.js", identifier)); |
| 72 | |
| 73 | gjs_debug(GJS_DEBUG_IMPORTER, "Loading internal module '%s' (%s)", |
| 74 | identifier, full_path.get()); |
| 75 | |
| 76 | Gjs::AutoChar script; |
| 77 | size_t script_len; |
| 78 | if (!gjs_load_internal_source(cx, full_path, script.out(), &script_len)) |
| 79 | return false; |
| 80 | |
| 81 | JS::SourceText<mozilla::Utf8Unit> buf; |
| 82 | if (!buf.init(cx, script.get(), script_len, JS::SourceOwnership::Borrowed)) |
| 83 | return false; |
| 84 | |
| 85 | JS::CompileOptions options(cx); |
| 86 | options.setIntroductionType("Internal Module Bootstrap"); |
| 87 | options.setFileAndLine(full_path, 1); |
| 88 | options.setSelfHostingMode(false); |
| 89 | |
| 90 | Gjs::AutoInternalRealm ar{cx}; |
| 91 | GjsContextPrivate* gjs = GjsContextPrivate::from_cx(cx); |
| 92 | JS::RootedObject internal_global{cx, gjs->internal_global()}; |
| 93 | JS::RootedObject module{cx, JS::CompileModule(cx, options, buf)}; |
| 94 | if (!module) |
| 95 | return false; |
| 96 | |
| 97 | JS::RootedObject registry{cx, gjs_get_module_registry(internal_global)}; |
| 98 | |
| 99 | JS::RootedId key{cx, gjs_intern_string_to_id(cx, full_path)}; |
| 100 | if (key.isVoid()) |
| 101 | return false; |
| 102 | |
| 103 | JS::RootedValue ignore{cx}; |
| 104 | return gjs_global_registry_set(cx, registry, key, module) && |
| 105 | JS::ModuleLink(cx, module) && |
| 106 | JS::ModuleEvaluate(cx, module, &ignore); |
| 107 | } |
| 108 | |
| 109 | static bool handle_wrong_args(JSContext* cx) { |
| 110 | gjs_log_exception(cx); |
no test coverage detected