[gjs/esm-local-imports: 3/6] Refactor ARGV handling.



commit d47a28063ef8fbeb38b8f0ee1994c46d3836b7cb
Author: Evan Welsh <noreply evanwelsh com>
Date:   Sat Jun 6 05:50:38 2020 -0500

    Refactor ARGV handling.

 gjs/console.cpp       |  7 +------
 gjs/context-private.h |  8 ++++++++
 gjs/context.cpp       | 14 ++++++++++++++
 gjs/context.h         |  3 +++
 gjs/global.cpp        | 26 ++++++++++++++++++++++++++
 5 files changed, 52 insertions(+), 6 deletions(-)
---
diff --git a/gjs/console.cpp b/gjs/console.cpp
index e3729ed7..428fe1c4 100644
--- a/gjs/console.cpp
+++ b/gjs/console.cpp
@@ -196,12 +196,7 @@ int define_argv_and_eval_script(GjsContext* js_context, int argc,
     GError* error = nullptr;
 
     /* prepare command line arguments */
-    if (!gjs_context_define_string_array(
-            js_context, "ARGV", argc, const_cast<const char**>(argv), &error)) {
-        g_critical("Failed to define ARGV: %s", error->message);
-        g_clear_error(&error);
-        return 1;
-    }
+    gjs_context_set_argv(js_context, argc, const_cast<const char**>(argv));
 
     /* evaluate the script */
     int code;
diff --git a/gjs/context-private.h b/gjs/context-private.h
index e109743f..537ae2de 100644
--- a/gjs/context-private.h
+++ b/gjs/context-private.h
@@ -29,8 +29,10 @@
 #include <stdint.h>
 #include <sys/types.h>  // for ssize_t
 
+#include <string>
 #include <type_traits>  // for is_same
 #include <unordered_map>
+#include <vector>
 
 #include <glib-object.h>
 #include <glib.h>
@@ -103,6 +105,8 @@ class GjsContextPrivate : public JS::JobQueue {
 
     GjsAtoms* m_atoms;
 
+    std::vector<std::string> m_args;
+
     JobQueueStorage m_job_queue;
     unsigned m_idle_drain_handler;
 
@@ -195,6 +199,10 @@ class GjsContextPrivate : public JS::JobQueue {
     void set_should_listen_sigusr2(bool value) {
         m_should_listen_sigusr2 = value;
     }
+
+    void set_args(std::vector<std::string> args);
+    std::vector<std::string> get_args();
+
     GJS_USE bool is_owner_thread(void) const {
         return m_owner_thread == g_thread_self();
     }
diff --git a/gjs/context.cpp b/gjs/context.cpp
index fd36d30d..e705a793 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -544,6 +544,12 @@ GjsContextPrivate::GjsContextPrivate(JSContext* cx, GjsContext* public_context)
     }
 }
 
+void GjsContextPrivate::set_args(std::vector<std::string> args) {
+    m_args = args;
+}
+
+std::vector<std::string> GjsContextPrivate::get_args() { return m_args; }
+
 static void
 gjs_context_get_property (GObject     *object,
                           guint        prop_id,
@@ -1221,6 +1227,14 @@ gjs_context_define_string_array(GjsContext  *js_context,
     return true;
 }
 
+void gjs_context_set_argv(GjsContext* js_context, gssize array_length,
+                          const char** array_values) {
+    g_return_if_fail(GJS_IS_CONTEXT(js_context));
+    GjsContextPrivate* gjs = GjsContextPrivate::from_object(js_context);
+    std::vector<std::string> args(array_values, array_values + array_length);
+    gjs->set_args(args);
+}
+
 static GjsContext *current_context;
 
 GjsContext *
diff --git a/gjs/context.h b/gjs/context.h
index 1b1846ab..6853c74d 100644
--- a/gjs/context.h
+++ b/gjs/context.h
@@ -80,6 +80,9 @@ GJS_EXPORT GJS_USE bool gjs_context_eval_module(GjsContext* context,
 GJS_EXPORT GJS_USE bool gjs_context_define_string_array(
     GjsContext* js_context, const char* array_name, gssize array_length,
     const char** array_values, GError** error);
+GJS_EXPORT void gjs_context_set_argv(GjsContext* js_context,
+                                     gssize array_length,
+                                     const char** array_values);
 
 GJS_EXPORT GJS_USE GList* gjs_context_get_all(void);
 
diff --git a/gjs/global.cpp b/gjs/global.cpp
index 3143acd1..e05644a4 100644
--- a/gjs/global.cpp
+++ b/gjs/global.cpp
@@ -278,6 +278,20 @@ gjs_printerr(JSContext *context,
     return true;
 }
 
+GJS_JSAPI_RETURN_CONVENTION
+static bool gjs_argv(JSContext* cx, unsigned argc, JS::Value* vp) {
+    JS::CallArgs argv = JS::CallArgsFromVp(argc, vp);
+    GjsContextPrivate* gjs = GjsContextPrivate::from_cx(cx);
+    auto array = gjs_build_string_array(cx, gjs->get_args());
+
+    if (!array) {
+        return false;
+    }
+
+    argv.rval().setObjectOrNull(array);
+    return true;
+}
+
 const JSClassOps defaultclassops = JS::DefaultGlobalClassOps;
 
 class GjsGlobal {
@@ -341,6 +355,18 @@ class GjsGlobal {
                                    GJS_MODULE_PROP_FLAGS))
             return false;
 
+        bool has_argv = false;
+
+        if (!JS_HasProperty(cx, global, "ARGV", &has_argv))
+            return false;
+
+        if (!has_argv &&
+            !JS_DefineProperty(cx, global, "ARGV", gjs_argv, nullptr,
+                               GJS_MODULE_PROP_FLAGS | JSPROP_READONLY)) {
+            gjs_log_exception(cx);
+            return false;
+        }
+
         if (bootstrap_script) {
             if (!run_bootstrap(cx, bootstrap_script, global))
                 return false;


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