static JSBool import_file_on_module(JSContext *context, JSObject *obj, std::string &name, std::string &filePath) { JSObject *module_obj; JSBool retval = JS_FALSE; module_obj = create_module_object (context); if (!define_import(context, obj, module_obj, name)) goto out; if (!import_file(context, name, filePath, module_obj)) goto out; if (!define_meta_properties(context, module_obj, filePath.c_str(), name, obj)) goto out; if (!seal_import(context, obj, name)) goto out; retval = JS_TRUE; out: if (!retval) cancel_import(context, obj, name); return retval; }
static JSBool import_native_file(JSContext *context, JSObject *obj, std::string &name) { JSObject *module_obj; JSBool retval = JS_FALSE; gjs_debug(GJS_DEBUG_IMPORTER, "Importing '%s'", name); // if (!gjs_import_native_module(context, name, &module_obj)) // goto out; if (!define_meta_properties(context, module_obj, NULL, name, obj)) goto out; if (JS_IsExceptionPending(context)) { /* I am not sure whether this can happen, but if it does we want to trap it. */ gjs_debug(GJS_DEBUG_IMPORTER, "Module '%s' reported an exception but gjs_import_native_module() returned true", name.c_str()); goto out; } if (!JS_DefineProperty(context, obj, name.c_str(), OBJECT_TO_JSVAL(module_obj), NULL, NULL, GJS_MODULE_PROP_FLAGS)) goto out; retval = JS_TRUE; out: return retval; }
static JSBool import_file_on_module(JSContext *context, JSObject *obj, const char *name, GFile *file) { JSObject *module_obj; JSBool retval = JS_FALSE; char *full_path = NULL; module_obj = create_module_object (context); if (!define_import(context, obj, module_obj, name)) goto out; if (!import_file(context, name, file, module_obj)) goto out; full_path = g_file_get_parse_name (file); if (!define_meta_properties(context, module_obj, full_path, name, obj)) goto out; if (!seal_import(context, obj, name)) goto out; retval = JS_TRUE; out: if (!retval) cancel_import(context, obj, name); g_free (full_path); return retval; }
static JSBool import_native_file(JSContext *context, JSObject *obj, const char *name, const char *full_path) { JSObject *module_obj; GjsNativeFlags flags; JSBool retval = JS_FALSE; gjs_debug(GJS_DEBUG_IMPORTER, "Importing '%s' from '%s'", name, full_path ? full_path : "<internal>"); module_obj = JS_ConstructObject(context, NULL, NULL, NULL); if (module_obj == NULL) { return JS_FALSE; } /* We store the module object into the parent module before * initializing the module. If the module has the * GJS_NATIVE_SUPPLIES_MODULE_OBJ flag, it will just overwrite * the reference we stored when it initializes. */ if (!define_import(context, obj, module_obj, name)) return JS_FALSE; if (!define_meta_properties(context, module_obj, name, obj)) goto out; if (!gjs_import_native_module(context, module_obj, full_path, &flags)) goto out; if (!finish_import(context, name)) goto out; if (!seal_import(context, obj, name)) goto out; retval = JS_TRUE; out: if (!retval) cancel_import(context, obj, name); return retval; }
JSObject* gjs_define_importer(JSContext *context, JSObject *in_object, const char *importer_name, const char **initial_search_path, gboolean add_standard_search_path) { JSObject *importer; char **paths[2] = {0}; char **search_path; paths[0] = (char**)initial_search_path; if (add_standard_search_path) { /* Stick the "standard" shared search path after the provided one. */ paths[1] = (char**)gjs_get_search_path(); } search_path = gjs_g_strv_concat(paths, 2); importer = importer_new(context); /* API users can replace this property from JS, is the idea */ if (!gjs_define_string_array(context, importer, "searchPath", -1, (const char **)search_path, /* settable (no READONLY) but not deleteable (PERMANENT) */ JSPROP_PERMANENT | JSPROP_ENUMERATE)) gjs_fatal("no memory to define importer search path prop"); g_strfreev(search_path); if (!define_meta_properties(context, importer, importer_name, in_object)) gjs_fatal("failed to define meta properties on importer"); if (!JS_DefineProperty(context, in_object, importer_name, OBJECT_TO_JSVAL(importer), NULL, NULL, GJS_MODULE_PROP_FLAGS)) gjs_fatal("no memory to define importer property"); gjs_debug(GJS_DEBUG_IMPORTER, "Defined importer '%s' %p in %p", importer_name, importer, in_object); return importer; }
static JSObject* gjs_create_importer(JSContext *context, const std::string &importer_name, std::vector<std::string> &initial_search_path, bool add_standard_search_path, bool is_root, JSObject *in_object) { JSObject *importer; std::vector<std::string> paths[2]; std::vector<std::string> search_path; paths[0] = initial_search_path; if (add_standard_search_path) { /* Stick the "standard" shared search path after the provided one. */ paths[1] = gjs_get_search_path(); } for(auto p : paths) { search_path.insert(search_path.begin(), p.begin(), p.end()); } for(auto &s : search_path) std::cout << "search_path: " << s << "\n"; importer = importer_new(context, is_root); /* API users can replace this property from JS, is the idea */ if (!gjs_define_string_array(context, importer, "searchPath", search_path, /* settable (no READONLY) but not deleteable (PERMANENT) */ JSPROP_PERMANENT | JSPROP_ENUMERATE)) g_error("no memory to define importer search path prop"); if (!define_meta_properties(context, importer, NULL, importer_name, in_object)) g_error("failed to define meta properties on importer"); return importer; }
static JSObject* gjs_create_importer(JSContext *context, const char *importer_name, const char **initial_search_path, gboolean add_standard_search_path, gboolean is_root, JSObject *in_object) { JSObject *importer; char **paths[2] = {0}; char **search_path; paths[0] = (char**)initial_search_path; if (add_standard_search_path) { /* Stick the "standard" shared search path after the provided one. */ paths[1] = (char**)gjs_get_search_path(); } search_path = gjs_g_strv_concat(paths, 2); importer = importer_new(context, is_root); /* API users can replace this property from JS, is the idea */ if (!gjs_define_string_array(context, importer, "searchPath", -1, (const char **)search_path, /* settable (no READONLY) but not deleteable (PERMANENT) */ JSPROP_PERMANENT | JSPROP_ENUMERATE)) g_error("no memory to define importer search path prop"); g_strfreev(search_path); if (!define_meta_properties(context, importer, NULL, importer_name, in_object)) g_error("failed to define meta properties on importer"); return importer; }
static JSBool import_file(JSContext *context, JSObject *obj, const char *name, const char *full_path) { char *script; gsize script_len; JSObject *module_obj; GError *error; jsval script_retval; JSBool retval = JS_FALSE; gjs_debug(GJS_DEBUG_IMPORTER, "Importing '%s'", full_path); module_obj = JS_NewObject(context, NULL, NULL, NULL); if (module_obj == NULL) { return JS_FALSE; } if (!define_import(context, obj, module_obj, name)) return JS_FALSE; if (!define_meta_properties(context, module_obj, full_path, name, obj)) goto out; script_len = 0; error = NULL; if (!(g_file_get_contents(full_path, &script, &script_len, &error))) { if (!g_error_matches(error, G_FILE_ERROR, G_FILE_ERROR_ISDIR) && !g_error_matches(error, G_FILE_ERROR, G_FILE_ERROR_NOTDIR) && !g_error_matches(error, G_FILE_ERROR, G_FILE_ERROR_NOENT)) gjs_throw_g_error(context, error); else g_error_free(error); goto out; } g_assert(script != NULL); if (!JS_EvaluateScript(context, module_obj, script, script_len, full_path, 1, /* line number */ &script_retval)) { g_free(script); /* If JSOPTION_DONT_REPORT_UNCAUGHT is set then the exception * would be left set after the evaluate and not go to the error * reporter function. */ if (JS_IsExceptionPending(context)) { gjs_debug(GJS_DEBUG_IMPORTER, "Module '%s' left an exception set", name); gjs_log_and_keep_exception(context); } else { gjs_throw(context, "JS_EvaluateScript() returned FALSE but did not set exception"); } goto out; } g_free(script); if (!finish_import(context, name)) goto out; if (!seal_import(context, obj, name)) goto out; retval = JS_TRUE; out: if (!retval) cancel_import(context, obj, name); return retval; }