[gjs/wip/ptomato/jasper-imports: 4/6] context: Move gjs_get_search_path to here



commit 24ef7fb4c59e6f4de095eb3b08d8219c31722061
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Wed Jun 17 16:38:06 2015 -0700

    context: Move gjs_get_search_path to here
    
    ... and make it public. This will be used by the JS imports system,
    since we'll be removing the old C++ one.

 gjs/context.cpp  |   54 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 gjs/context.h    |    2 +
 gjs/importer.cpp |   56 +-----------------------------------------------------
 3 files changed, 57 insertions(+), 55 deletions(-)
---
diff --git a/gjs/context.cpp b/gjs/context.cpp
index 9208bc0..6838d69 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -887,3 +887,57 @@ gjs_get_import_global(JSContext *context)
     GjsContext *gjs_context = (GjsContext *) JS_GetContextPrivate(context);
     return gjs_context->global;
 }
+
+const char * const *
+gjs_get_search_path(void)
+{
+    static char **gjs_search_path;
+    char **search_path;
+
+    /* not thread safe */
+
+    if (!gjs_search_path) {
+        G_CONST_RETURN gchar* G_CONST_RETURN * system_data_dirs;
+        const char *envstr;
+        GPtrArray *path;
+        gsize i;
+
+        path = g_ptr_array_new();
+
+        /* in order of priority */
+
+        /* $GJS_PATH */
+        envstr = g_getenv("GJS_PATH");
+        if (envstr) {
+            char **dirs, **d;
+            dirs = g_strsplit(envstr, G_SEARCHPATH_SEPARATOR_S, 0);
+            for (d = dirs; *d != NULL; d++)
+                g_ptr_array_add(path, *d);
+            /* we assume the array and strings are allocated separately */
+            g_free(dirs);
+        }
+
+        g_ptr_array_add(path, g_strdup("resource:///org/gnome/gjs/modules/"));
+
+        /* $XDG_DATA_DIRS /gjs-1.0 */
+        system_data_dirs = g_get_system_data_dirs();
+        for (i = 0; system_data_dirs[i] != NULL; ++i) {
+            char *s;
+            s = g_build_filename(system_data_dirs[i], "gjs-1.0", NULL);
+            g_ptr_array_add(path, s);
+        }
+
+        /* ${datadir}/share/gjs-1.0 */
+        g_ptr_array_add(path, g_strdup(GJS_JS_DIR));
+
+        g_ptr_array_add(path, NULL);
+
+        search_path = (char**)g_ptr_array_free(path, FALSE);
+
+        gjs_search_path = search_path;
+    } else {
+        search_path = gjs_search_path;
+    }
+
+    return (const char * const *)search_path;
+}
diff --git a/gjs/context.h b/gjs/context.h
index a459e5b..c1abff0 100644
--- a/gjs/context.h
+++ b/gjs/context.h
@@ -78,6 +78,8 @@ void            gjs_context_gc                    (GjsContext  *context);
 
 void            gjs_dumpstack                     (void);
 
+const char * const *gjs_get_search_path(void);
+
 G_END_DECLS
 
 #endif  /* __GJS_CONTEXT_H__ */
diff --git a/gjs/importer.cpp b/gjs/importer.cpp
index f997267..4acfb8c 100644
--- a/gjs/importer.cpp
+++ b/gjs/importer.cpp
@@ -26,6 +26,7 @@
 #include <util/log.h>
 #include <util/glib.h>
 
+#include "context.h"
 #include "importer.h"
 #include "jsapi-wrapper.h"
 #include "mem.h"
@@ -37,8 +38,6 @@
 
 #define MODULE_INIT_FILENAME "__init__.js"
 
-static char **gjs_search_path = NULL;
-
 typedef struct {
     bool is_root;
 } Importer;
@@ -1022,59 +1021,6 @@ importer_new(JSContext *context,
     return importer;
 }
 
-static G_CONST_RETURN char * G_CONST_RETURN *
-gjs_get_search_path(void)
-{
-    char **search_path;
-
-    /* not thread safe */
-
-    if (!gjs_search_path) {
-        G_CONST_RETURN gchar* G_CONST_RETURN * system_data_dirs;
-        const char *envstr;
-        GPtrArray *path;
-        gsize i;
-
-        path = g_ptr_array_new();
-
-        /* in order of priority */
-
-        /* $GJS_PATH */
-        envstr = g_getenv("GJS_PATH");
-        if (envstr) {
-            char **dirs, **d;
-            dirs = g_strsplit(envstr, G_SEARCHPATH_SEPARATOR_S, 0);
-            for (d = dirs; *d != NULL; d++)
-                g_ptr_array_add(path, *d);
-            /* we assume the array and strings are allocated separately */
-            g_free(dirs);
-        }
-
-        g_ptr_array_add(path, g_strdup("resource:///org/gnome/gjs/modules/"));
-
-        /* $XDG_DATA_DIRS /gjs-1.0 */
-        system_data_dirs = g_get_system_data_dirs();
-        for (i = 0; system_data_dirs[i] != NULL; ++i) {
-            char *s;
-            s = g_build_filename(system_data_dirs[i], "gjs-1.0", NULL);
-            g_ptr_array_add(path, s);
-        }
-
-        /* ${datadir}/share/gjs-1.0 */
-        g_ptr_array_add(path, g_strdup(GJS_JS_DIR));
-
-        g_ptr_array_add(path, NULL);
-
-        search_path = (char**)g_ptr_array_free(path, false);
-
-        gjs_search_path = search_path;
-    } else {
-        search_path = gjs_search_path;
-    }
-
-    return (G_CONST_RETURN char * G_CONST_RETURN *)search_path;
-}
-
 static JSObject*
 gjs_create_importer(JSContext       *context,
                     const char      *importer_name,


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