| 312 | } |
| 313 | |
| 314 | GJS_JSAPI_RETURN_CONVENTION |
| 315 | static JSObject* load_module_init(JSContext* cx, JS::HandleObject in_object, |
| 316 | GFile* file) { |
| 317 | bool found; |
| 318 | const GjsAtoms& atoms = GjsContextPrivate::atoms(cx); |
| 319 | |
| 320 | // First we check if js module has already been loaded |
| 321 | if (!JS_HasPropertyById(cx, in_object, atoms.module_init(), &found)) |
| 322 | return nullptr; |
| 323 | if (found) { |
| 324 | JS::RootedValue v_module(cx); |
| 325 | if (!JS_GetPropertyById(cx, in_object, atoms.module_init(), |
| 326 | &v_module)) |
| 327 | return nullptr; |
| 328 | if (v_module.isObject()) |
| 329 | return &v_module.toObject(); |
| 330 | |
| 331 | Gjs::AutoChar full_path{g_file_get_parse_name(file)}; |
| 332 | gjs_throw(cx, "Unexpected non-object module __init__ imported from %s", |
| 333 | full_path.get()); |
| 334 | return nullptr; |
| 335 | } |
| 336 | |
| 337 | JS::RootedObject module_obj(cx, JS_NewPlainObject(cx)); |
| 338 | if (!module_obj) |
| 339 | return nullptr; |
| 340 | |
| 341 | if (!import_module_init(cx, file, module_obj)) |
| 342 | return nullptr; |
| 343 | |
| 344 | if (!JS_DefinePropertyById(cx, in_object, atoms.module_init(), module_obj, |
| 345 | GJS_MODULE_PROP_FLAGS & ~JSPROP_PERMANENT)) |
| 346 | return nullptr; |
| 347 | |
| 348 | return module_obj; |
| 349 | } |
| 350 | |
| 351 | GJS_JSAPI_RETURN_CONVENTION |
| 352 | static bool load_module_elements(JSContext* cx, JS::HandleObject in_object, |
no test coverage detected