| 115 | } |
| 116 | |
| 117 | GJS_JSAPI_RETURN_CONVENTION |
| 118 | static bool load_native_module(JSContext* m_cx, unsigned argc, |
| 119 | JS::Value* vp) { |
| 120 | JS::CallArgs args = JS::CallArgsFromVp(argc, vp); |
| 121 | |
| 122 | // This function should never be directly exposed to user code, so we |
| 123 | // can be strict. |
| 124 | g_assert(argc == 1); |
| 125 | g_assert(args[0].isString()); |
| 126 | |
| 127 | JS::RootedString str{m_cx, args[0].toString()}; |
| 128 | JS::UniqueChars id(JS_EncodeStringToUTF8(m_cx, str)); |
| 129 | |
| 130 | if (!id) |
| 131 | return false; |
| 132 | |
| 133 | JS::RootedObject native_obj(m_cx); |
| 134 | |
| 135 | if (!Gjs::NativeModuleDefineFuncs::get().define(m_cx, id.get(), |
| 136 | &native_obj)) { |
| 137 | gjs_throw(m_cx, "Failed to load native module: %s", id.get()); |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | args.rval().setObject(*native_obj); |
| 142 | return true; |
| 143 | } |
| 144 | }; |
| 145 | |
| 146 | const JSClassOps defaultclassops = JS::DefaultGlobalClassOps; |