static JSBool gjs_importer_add_subimporter(JSContext *context, unsigned argc, jsval *vp) { if (argc != 2) { gjs_throw(context, "Must pass two arguments to addSubImporter()"); return JS_FALSE; } JSObject *importer; jsval *argv = JS_ARGV(context, vp); char *name; char *path; char *search_path[2] = { 0, 0 }; JS_BeginRequest(context); importer = JS_THIS_OBJECT(context, vp); gjs_string_to_utf8(context, argv[0], &name); gjs_string_to_utf8(context, argv[1], &path); search_path[0] = path; gjs_define_importer(context, importer, name, (const char **)search_path, FALSE); JS_EndRequest(context); JS_SET_RVAL(context, vp, JSVAL_VOID); return JS_TRUE; }
/* If this were called twice for the same runtime with different args it * would basically be a bug, but checking for that is a lot of code so * we just ignore all calls after the first and hope the args are the same. */ JSBool gjs_create_root_importer(JSContext *context, const char **initial_search_path, gboolean add_standard_search_path) { JSObject *global; global = gjs_get_import_global(context); JS_BeginRequest(context); if (!gjs_object_has_property(context, global, "imports")) { if (gjs_define_importer(context, global, "imports", initial_search_path, add_standard_search_path) == NULL) { JS_EndRequest(context); return JS_FALSE; } } else { gjs_debug(GJS_DEBUG_IMPORTER, "Someone else already created root importer, ignoring second request"); JS_EndRequest(context); return JS_TRUE; } JS_EndRequest(context); return JS_TRUE; }
/** * shell_global_add_extension_importer: * @target_object_script: JavaScript code evaluating to a target object * @target_property: Name of property to use for importer * @directory: Source directory: * @error: A #GError * * This function sets a property named @target_property on the object * resulting from the evaluation of @target_object_script code, which * acts as a GJS importer for directory @directory. * * Returns: %TRUE on success */ gboolean shell_global_add_extension_importer (ShellGlobal *global, const char *target_object_script, const char *target_property, const char *directory, GError **error) { jsval target_object; JSObject *importer; JSContext *context = gjs_context_get_native_context (global->js_context); char *search_path[2] = { 0, 0 }; // This is a bit of a hack; ideally we'd be able to pass our target // object directly into this function, but introspection doesn't // support that at the moment. Instead evaluate a string to get it. if (!JS_EvaluateScript(context, JS_GetGlobalObject(context), target_object_script, strlen (target_object_script), "<target_object_script>", 0, &target_object)) { char *message; gjs_log_exception(context, &message); g_set_error(error, G_IO_ERROR, G_IO_ERROR_FAILED, "%s", message ? message : "(unknown)"); g_free(message); return FALSE; } if (!JSVAL_IS_OBJECT (target_object)) { g_error ("shell_global_add_extension_importer: invalid target object"); return FALSE; } search_path[0] = (char*)directory; importer = gjs_define_importer (context, JSVAL_TO_OBJECT (target_object), target_property, (const char **)search_path, FALSE); return TRUE; }
static JSBool import_directory(JSContext *context, JSObject *obj, const char *name, const char **full_paths) { JSObject *importer; gjs_debug(GJS_DEBUG_IMPORTER, "Importing directory '%s'", name); /* We define a sub-importer that has only the given directories on * its search path. gjs_define_importer() exits if it fails, so * this always succeeds. */ importer = gjs_define_importer(context, obj, name, full_paths, FALSE); if (importer == NULL) return JS_FALSE; return JS_TRUE; }