[gjs/wip/ptomato/jasper-imports: 1/6] ns: Add gjs_import_gi_module



commit 5ec6395c5565235908413628749d510b3b62ad1d
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Thu Jan 2 15:30:15 2014 -0500

    ns: Add gjs_import_gi_module
    
    This returns the bare module, and doesn't run overrides or anything like
    that yet... that will have to wait until after we get our new importer
    scheme working...

 gi/ns.cpp   |   33 ++++++++++++++++++++++-----------
 gi/ns.h     |    6 ++++--
 gi/repo.cpp |   24 ++++++------------------
 3 files changed, 32 insertions(+), 31 deletions(-)
---
diff --git a/gi/ns.cpp b/gi/ns.cpp
index 933e818..57d13c8 100644
--- a/gi/ns.cpp
+++ b/gi/ns.cpp
@@ -179,9 +179,10 @@ JSFunctionSpec gjs_ns_proto_funcs[] = {
     JS_FS_END
 };
 
-static JSObject*
-ns_new(JSContext    *context,
-       const char   *ns_name)
+static bool
+ns_new(JSContext              *context,
+       const char             *ns_name,
+       JS::MutableHandleObject ns)
 {
     Ns *priv;
     bool found;
@@ -190,7 +191,7 @@ ns_new(JSContext    *context,
     JS::RootedObject global(context, gjs_get_import_global(context));
 
     if (!JS_HasProperty(context, global, gjs_ns_class.name, &found))
-        return NULL;
+        return false;
     if (!found) {
         JSObject *prototype;
         prototype = JS_InitClass(context, global,
@@ -222,8 +223,7 @@ ns_new(JSContext    *context,
                   gjs_ns_class.name, prototype);
     }
 
-    JS::RootedObject ns(context, JS_NewObject(context, &gjs_ns_class,
-                                              JS::NullPtr(), global));
+    ns.set(JS_NewObject(context, &gjs_ns_class, JS::NullPtr(), global));
     if (ns == NULL)
         g_error("No memory to create ns object");
 
@@ -239,12 +239,23 @@ ns_new(JSContext    *context,
 
     priv = priv_from_js(context, ns);
     priv->gi_namespace = g_strdup(ns_name);
-    return ns;
+    return true;
 }
 
-JSObject*
-gjs_create_ns(JSContext    *context,
-              const char   *ns_name)
+bool
+gjs_import_gi_module(JSContext              *cx,
+                     const char             *module_name,
+                     const char             *module_version,
+                     JS::MutableHandleObject module)
 {
-    return ns_new(context, ns_name);
+    GIRepository *repo = g_irepository_get_default();
+    GError *error = NULL;
+
+    if (!g_irepository_require(repo, module_name, module_version, (GIRepositoryLoadFlags) 0, &error)) {
+        gjs_throw_g_error(cx, error);
+        g_clear_error(&error);
+        return false;
+    }
+
+    return ns_new(cx, module_name, module);
 }
diff --git a/gi/ns.h b/gi/ns.h
index c6a1b89..4d443c7 100644
--- a/gi/ns.h
+++ b/gi/ns.h
@@ -30,8 +30,10 @@
 
 G_BEGIN_DECLS
 
-JSObject* gjs_create_ns(JSContext    *context,
-                        const char   *ns_name);
+bool gjs_import_gi_module(JSContext              *cx,
+                          const char             *module_name,
+                          const char             *module_version,
+                          JS::MutableHandleObject module);
 
 G_END_DECLS
 
diff --git a/gi/repo.cpp b/gi/repo.cpp
index 7b8a1ed..65726ef 100644
--- a/gi/repo.cpp
+++ b/gi/repo.cpp
@@ -88,8 +88,6 @@ resolve_namespace_object(JSContext       *context,
                          JS::HandleId     ns_id,
                          const char      *ns_name)
 {
-    GIRepository *repo;
-    GError *error;
     char *version;
 
     JSAutoRequest ar(context);
@@ -97,28 +95,18 @@ resolve_namespace_object(JSContext       *context,
     if (!get_version_for_ns(context, repo_obj, ns_id, &version))
         return false;
 
-    repo = g_irepository_get_default();
-
-    error = NULL;
-    g_irepository_require(repo, ns_name, version, (GIRepositoryLoadFlags) 0, &error);
-    if (error != NULL) {
-        gjs_throw(context,
-                  "Requiring %s, version %s: %s",
-                  ns_name, version?version:"none", error->message);
-
-        g_error_free(error);
+    /* Defines a property on "obj" (the javascript repo object)
+     * with the given namespace name, pointing to that namespace
+     * in the repo.
+     */
+    JS::RootedObject gi_namespace(context);
+    if (!gjs_import_gi_module(context, ns_name, version, &gi_namespace)) {
         g_free(version);
         return false;
     }
 
     g_free(version);
 
-    /* Defines a property on "obj" (the javascript repo object)
-     * with the given namespace name, pointing to that namespace
-     * in the repo.
-     */
-    JS::RootedObject gi_namespace(context, gjs_create_ns(context, ns_name));
-
     /* Define the property early, to avoid reentrancy issues if
        the override module looks for namespaces that import this */
     if (!JS_DefineProperty(context, repo_obj, ns_name, gi_namespace,


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