diff options
author | Philip Chimento <philip.chimento@gmail.com> | 2021-02-05 16:27:46 -0800 |
---|---|---|
committer | Philip Chimento <philip.chimento@gmail.com> | 2021-02-05 18:25:21 -0800 |
commit | 88405d71cf87f3e084bd7b1f3a5aa4c4b6d1d58b (patch) | |
tree | 0d6bf49ecd4139c373c7e3fb953e5ab2c471445f /gjs/internal.cpp | |
parent | a457f6e1b31129f079ad73ba3d82167f7f511768 (diff) | |
download | gjs-esm/static-imports.tar.gz |
SQUASH: store module loader instead of hook functionsesm/static-imports
this way, the entire set of module loading behaviours is associated with
one object.
Diffstat (limited to 'gjs/internal.cpp')
-rw-r--r-- | gjs/internal.cpp | 70 |
1 files changed, 16 insertions, 54 deletions
diff --git a/gjs/internal.cpp b/gjs/internal.cpp index 388f2406..779e0864 100644 --- a/gjs/internal.cpp +++ b/gjs/internal.cpp @@ -106,68 +106,30 @@ bool gjs_load_internal_module(JSContext* cx, const char* identifier) { } /** - * Asserts the correct arguments for a hook setting function. + * gjs_internal_set_global_module_loader: * - * Asserts: (arg0: object, arg1: Function) => void - */ -static void set_module_hook(JS::CallArgs args, GjsGlobalSlot slot) { - JS::Value v_global = args[0]; - JS::Value v_hook = args[1]; - - g_assert(v_global.isObject()); - g_assert(v_hook.isObject()); - - g_assert(JS::IsCallable(&v_hook.toObject())); - gjs_set_global_slot(&v_global.toObject(), slot, v_hook); - - args.rval().setUndefined(); -} - -/** - * gjs_internal_global_set_module_hook: - * - * @brief Sets the MODULE_HOOK slot of the passed global object. - * Asserts that the second argument must be callable (e.g. Function) - * The passed callable is called by gjs_module_load. - * - * @example (in JavaScript) - * setModuleLoadHook(globalThis, (id, uri) => { - * id // the module's identifier - * uri // the URI to load from - * }); + * @brief Sets the MODULE_LOADER slot of the passed global object. + * The second argument should be an instance of ModuleLoader or + * InternalModuleLoader. Its moduleResolveHook and moduleLoadHook properties + * will be called. * * @returns guaranteed to return true or assert. */ -bool gjs_internal_global_set_module_hook([[maybe_unused]] JSContext* cx, - unsigned argc, JS::Value* vp) { +bool gjs_internal_set_global_module_loader(JSContext*, unsigned argc, + JS::Value* vp) { JS::CallArgs args = JS::CallArgsFromVp(argc, vp); - g_assert(args.length() == 2 && "setModuleLoadHook takes 2 arguments"); + g_assert(args.length() == 2 && "setGlobalModuleLoader takes 2 arguments"); - set_module_hook(args, GjsGlobalSlot::MODULE_HOOK); - return true; -} + JS::Value v_global = args[0]; + JS::Value v_loader = args[1]; -/** - * gjs_internal_global_set_module_resolve_hook: - * - * @brief Sets the IMPORT_HOOK slot of the passed global object. - * Asserts that the second argument must be callable (e.g. Function) - * The passed callable is called by gjs_module_resolve. - * - * @example (in JavaScript) - * setModuleResolveHook(globalThis, (module, specifier) => { - * module // the importing module object - * specifier // the import specifier - * }); - * - * @returns guaranteed to return true or assert. - */ -bool gjs_internal_global_set_module_resolve_hook([[maybe_unused]] JSContext* cx, - unsigned argc, JS::Value* vp) { - JS::CallArgs args = JS::CallArgsFromVp(argc, vp); - g_assert(args.length() == 2 && "setModuleResolveHook takes 2 arguments"); + g_assert(v_global.isObject() && "first argument must be an object"); + g_assert(v_loader.isObject() && "second argument must be an object"); - set_module_hook(args, GjsGlobalSlot::IMPORT_HOOK); + gjs_set_global_slot(&v_global.toObject(), GjsGlobalSlot::MODULE_LOADER, + v_loader); + + args.rval().setUndefined(); return true; } |