[gjs/wip/jasper/imports-cleanup: 1/2] Add a simple API to import a GFile as a JS module



commit 0138af8be162712bb71029acdd112837bd6badf1
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Fri Sep 27 17:49:15 2013 -0400

    Add a simple API to import a GFile as a JS module
    
    Without going through the crazy importer lookup system.
    
    (Philip Chimento: rebased, updated coding style)

 gjs/module.cpp     | 27 +++++++++++++++++++++++----
 gjs/module.h       |  4 ++++
 modules/system.cpp | 31 +++++++++++++++++++++++++++++++
 3 files changed, 58 insertions(+), 4 deletions(-)
---
diff --git a/gjs/module.cpp b/gjs/module.cpp
index cc6657a7..8861e424 100644
--- a/gjs/module.cpp
+++ b/gjs/module.cpp
@@ -114,9 +114,9 @@ class GjsModule {
 
     /* Loads JS code from a file and imports it */
     bool
-    import_file(JSContext       *cx,
-                JS::HandleObject module,
-                GFile           *file)
+    import_file_internal(JSContext       *cx,
+                         JS::HandleObject module,
+                         GFile           *file)
     {
         GError *error = nullptr;
         char *unowned_script;
@@ -226,11 +226,22 @@ public:
         JS::RootedObject module(cx, GjsModule::create(cx, name));
         if (!module ||
             !priv(module)->define_import(cx, module, importer, id) ||
-            !priv(module)->import_file(cx, module, file))
+            !priv(module)->import_file_internal(cx, module, file))
             return nullptr;
 
         return module;
     }
+
+    static JSObject *
+    import_file(JSContext  *cx,
+                const char *name,
+                GFile      *file)
+    {
+        JS::RootedObject module(cx, GjsModule::create(cx, name));
+        if (!module || !priv(module)->import_file_internal(cx, module, file))
+            return nullptr;
+        return module;
+    }
 };
 
 /**
@@ -259,5 +270,13 @@ gjs_module_import(JSContext       *cx,
     return GjsModule::import(cx, importer, id, name, file);
 }
 
+JSObject *
+gjs_module_import_file(JSContext  *cx,
+                       const char *name,
+                       GFile      *file)
+{
+    return GjsModule::import_file(cx, name, file);
+}
+
 decltype(GjsModule::klass) constexpr GjsModule::klass;
 decltype(GjsModule::class_ops) constexpr GjsModule::class_ops;
diff --git a/gjs/module.h b/gjs/module.h
index 90413917..4833d24a 100644
--- a/gjs/module.h
+++ b/gjs/module.h
@@ -37,6 +37,10 @@ gjs_module_import(JSContext       *cx,
                   const char      *name,
                   GFile           *file);
 
+JSObject *gjs_module_import_file(JSContext  *cx,
+                                 const char *name,
+                                 GFile      *file);
+
 G_END_DECLS
 
 #endif  /* GJS_MODULE_H */
diff --git a/modules/system.cpp b/modules/system.cpp
index 001e94ea..5fbacfa0 100644
--- a/modules/system.cpp
+++ b/modules/system.cpp
@@ -27,6 +27,8 @@
 #include <sys/types.h>
 #include <time.h>
 
+#include <gio/gio.h>
+
 #include "gjs/jsapi-wrapper.h"
 #include <js/Date.h>
 
@@ -35,6 +37,7 @@
 #include "gi/object.h"
 #include "gjs/context-private.h"
 #include "gjs/jsapi-util-args.h"
+#include "gjs/module.h"
 #include "system.h"
 
 /* Note that this cannot be relied on to test whether two objects are the same!
@@ -164,6 +167,33 @@ gjs_clear_date_caches(JSContext *context,
     return true;
 }
 
+static bool
+import_file(JSContext *cx,
+            unsigned   argc,
+            JS::Value *vp)
+{
+    JS::CallArgs argv = JS::CallArgsFromVp(argc, vp);
+
+    GjsAutoJSChar name;
+    JS::RootedObject file_obj(cx);
+    if (!gjs_parse_call_args(cx, "importFile", argv, "so",
+                             "name", &name,
+                             "object", &file_obj))
+        return false;
+
+    GjsAutoUnref<GFile> file = G_FILE(gjs_g_object_from_object(cx, file_obj));
+    if (!file)
+        return false;
+
+    JS::RootedObject module_obj(cx, gjs_module_import_file(cx, name, file));
+    if (!module_obj)
+        return false;
+
+    argv.rval().setObject(*module_obj);
+    return true;
+}
+
+
 static JSFunctionSpec module_funcs[] = {
     JS_FS("addressOf", gjs_address_of, 1, GJS_MODULE_PROP_FLAGS),
     JS_FS("refcount", gjs_refcount, 1, GJS_MODULE_PROP_FLAGS),
@@ -172,6 +202,7 @@ static JSFunctionSpec module_funcs[] = {
     JS_FS("gc", gjs_gc, 0, GJS_MODULE_PROP_FLAGS),
     JS_FS("exit", gjs_exit, 0, GJS_MODULE_PROP_FLAGS),
     JS_FS("clearDateCaches", gjs_clear_date_caches, 0, GJS_MODULE_PROP_FLAGS),
+    JS_FS("importFile", import_file, 1, GJS_MODULE_PROP_FLAGS),
     JS_FS_END
 };
 


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