[gjs] Support using GResource as a base search path for JS files



commit b8ae01f5ca273b6e04cb314d20739528316375ef
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Wed Oct 2 13:42:40 2013 -0400

    Support using GResource as a base search path for JS files
    
    This commit changes the way files are imported inside GJS to use GIO,
    transparently adding support for using a resource:/// search path to
    lookup JS files in loaded GResources.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=703440

 gjs/importer.c |   80 ++++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 52 insertions(+), 28 deletions(-)
---
diff --git a/gjs/importer.c b/gjs/importer.c
index a8e8b6e..a79fe1e 100644
--- a/gjs/importer.c
+++ b/gjs/importer.c
@@ -31,6 +31,8 @@
 #include <gjs/compat.h>
 #include <gjs/runtime.h>
 
+#include <gio/gio.h>
+
 #include <string.h>
 
 #define MODULE_INIT_FILENAME "__init__.js"
@@ -271,19 +273,20 @@ create_module_object(JSContext *context)
 static JSBool
 import_file(JSContext  *context,
             const char *name,
-            const char *full_path,
+            GFile      *file,
             JSObject   *module_obj)
 {
     JSBool ret = JS_FALSE;
     char *script = NULL;
+    char *full_path = NULL;
     gsize script_len = 0;
     jsval script_retval;
     GError *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))
+    if (!(g_file_load_contents(file, NULL, &script, &script_len, NULL, &error))) {
+        if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY) &&
+            !g_error_matches(error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY) &&
+            !g_error_matches(error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
             gjs_throw_g_error(context, error);
         else
             g_error_free(error);
@@ -291,6 +294,10 @@ import_file(JSContext  *context,
         goto out;
     }
 
+    g_assert(script != NULL);
+
+    full_path = g_file_get_parse_name (file);
+
     if (!JS_EvaluateScript(context,
                            module_obj,
                            script,
@@ -320,6 +327,7 @@ import_file(JSContext  *context,
 
  out:
     g_free(script);
+    g_free(full_path);
     return ret;
 }
 
@@ -331,6 +339,7 @@ load_module_init(JSContext  *context,
     JSObject *module_obj;
     JSBool found;
     jsid module_init_name;
+    GFile *file;
 
     /* First we check if js module has already been loaded  */
     module_init_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
@@ -347,15 +356,18 @@ load_module_init(JSContext  *context,
     }
 
     module_obj = create_module_object (context);
-    if (!import_file (context, "__init__", full_path, module_obj))
-        return NULL;
+    file = g_file_new_for_commandline_arg(full_path);
+    if (!import_file (context, "__init__", file, module_obj))
+        goto out;
 
     if (!JS_DefinePropertyById(context, in_object,
                                module_init_name, OBJECT_TO_JSVAL(module_obj),
                                NULL, NULL,
                                GJS_MODULE_PROP_FLAGS & ~JSPROP_PERMANENT))
-        return NULL;
+        goto out;
 
+ out:
+    g_object_unref (file);
     return module_obj;
 }
 
@@ -403,22 +415,21 @@ static JSBool
 import_file_on_module(JSContext  *context,
                       JSObject   *obj,
                       const char *name,
-                      const char *full_path)
+                      GFile      *file)
 {
     JSObject *module_obj;
     JSBool retval = JS_FALSE;
-
-    gjs_debug(GJS_DEBUG_IMPORTER,
-              "Importing '%s'", full_path);
+    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, full_path, module_obj))
+    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;
 
@@ -431,6 +442,7 @@ import_file_on_module(JSContext  *context,
     if (!retval)
         cancel_import(context, obj, name);
 
+    g_free (full_path);
     return retval;
 }
 
@@ -451,6 +463,8 @@ do_import(JSContext  *context,
     JSBool result;
     GPtrArray *directories;
     jsid search_path_name;
+    GFile *gfile;
+    gboolean exists;
 
     search_path_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
                                                     GJS_STRING_SEARCH_PATH);
@@ -550,8 +564,9 @@ do_import(JSContext  *context,
             g_free(full_path);
         full_path = g_build_filename(dirname, name,
                                      NULL);
+        gfile = g_file_new_for_commandline_arg(full_path);
 
-        if (g_file_test(full_path, G_FILE_TEST_IS_DIR)) {
+        if (g_file_query_file_type(gfile, 0, NULL) == G_FILE_TYPE_DIRECTORY) {
             gjs_debug(GJS_DEBUG_IMPORTER,
                       "Adding directory '%s' to child importer '%s'",
                       full_path, name);
@@ -563,6 +578,8 @@ do_import(JSContext  *context,
             full_path = NULL;
         }
 
+        g_object_unref(gfile);
+
         /* If we just added to directories, we know we don't need to
          * check for a file.  If we added to directories on an earlier
          * iteration, we want to ignore any files later in the
@@ -577,24 +594,31 @@ do_import(JSContext  *context,
         g_free(full_path);
         full_path = g_build_filename(dirname, filename,
                                      NULL);
+        gfile = g_file_new_for_commandline_arg(full_path);
+        exists = g_file_query_exists(gfile, NULL);
 
-        if (g_file_test(full_path, G_FILE_TEST_EXISTS)) {
-            if (import_file_on_module (context, obj, name, full_path)) {
-                gjs_debug(GJS_DEBUG_IMPORTER,
-                          "successfully imported module '%s'", name);
-                result = JS_TRUE;
-            }
+        if (!exists) {
+            gjs_debug(GJS_DEBUG_IMPORTER,
+                      "JS import '%s' not found in %s",
+                      name, dirname);
 
-            /* Don't keep searching path if we fail to load the file for
-             * reasons other than it doesn't exist... i.e. broken files
-             * block searching for nonbroken ones
-             */
-            goto out;
+            g_object_unref(gfile);
+            continue;
         }
 
-        gjs_debug(GJS_DEBUG_IMPORTER,
-                  "JS import '%s' not found in %s",
-                  name, dirname);
+        if (import_file_on_module (context, obj, name, gfile)) {
+            gjs_debug(GJS_DEBUG_IMPORTER,
+                      "successfully imported module '%s'", name);
+            result = JS_TRUE;
+        }
+
+        g_object_unref(gfile);
+
+        /* Don't keep searching path if we fail to load the file for
+         * reasons other than it doesn't exist... i.e. broken files
+         * block searching for nonbroken ones
+         */
+        goto out;
     }
 
     if (directories != NULL) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]