[gjs/ewlsh/refactor-argv: 51/51] Refactor ARGV handling and add system.programArgs.




commit d0b197eb7c18a813596de2e248f06b187c0909b7
Author: Evan Welsh <contact evanwelsh com>
Date:   Sat Jan 30 14:02:40 2021 -0800

    Refactor ARGV handling and add system.programArgs.

 gjs/console.cpp                      | 11 ++---------
 gjs/context-private.h                |  6 ++++++
 gjs/context.cpp                      | 23 +++++++++++++++++++++++
 gjs/context.h                        |  5 +++++
 installed-tests/js/testSystem.js     | 10 ++++++++++
 js.gresource.xml                     |  2 ++
 modules/core/_system.js              | 16 ++++++++++++++++
 modules/esm/system.js                | 22 +++++++++++++---------
 modules/modules.cpp                  |  2 +-
 modules/script/_bootstrap/default.js |  8 ++++++++
 modules/script/system.js             |  6 ++++++
 modules/system.cpp                   | 14 ++++++++++++++
 12 files changed, 106 insertions(+), 19 deletions(-)
---
diff --git a/gjs/console.cpp b/gjs/console.cpp
index 7c39f39e..49c82299 100644
--- a/gjs/console.cpp
+++ b/gjs/console.cpp
@@ -165,16 +165,9 @@ check_script_args_for_stray_gjs_args(int           argc,
 int define_argv_and_eval_script(GjsContext* js_context, int argc,
                                 char* const* argv, const char* script,
                                 size_t len, const char* filename) {
-    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));
 
+    GError* error = nullptr;
     /* evaluate the script */
     int code = 0;
     if (exec_as_module) {
diff --git a/gjs/context-private.h b/gjs/context-private.h
index 6f5cba47..b0746192 100644
--- a/gjs/context-private.h
+++ b/gjs/context-private.h
@@ -10,8 +10,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>
@@ -83,6 +85,8 @@ class GjsContextPrivate : public JS::JobQueue {
 
     GjsAtoms* m_atoms;
 
+    std::vector<std::string> m_args;
+
     JobQueueStorage m_job_queue;
     unsigned m_idle_drain_handler;
 
@@ -189,6 +193,8 @@ 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);
+    GJS_JSAPI_RETURN_CONVENTION JSObject* build_args_array();
     [[nodiscard]] bool is_owner_thread() const {
         return m_owner_thread == g_thread_self();
     }
diff --git a/gjs/context.cpp b/gjs/context.cpp
index 59310a69..d54ab6ef 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -599,6 +599,14 @@ GjsContextPrivate::GjsContextPrivate(JSContext* cx, GjsContext* public_context)
     }
 }
 
+void GjsContextPrivate::set_args(std::vector<std::string>&& args) {
+    m_args = args;
+}
+
+JSObject* GjsContextPrivate::build_args_array() {
+    return gjs_build_string_array(m_cx, m_args);
+}
+
 static void
 gjs_context_get_property (GObject     *object,
                           guint        prop_id,
@@ -1337,6 +1345,13 @@ gjs_context_define_string_array(GjsContext  *js_context,
         strings = {array_values, array_values + array_length};
     }
 
+    // ARGV is a special case to preserve backwards compatibility.
+    if (strcmp(array_name, "ARGV") == 0) {
+        gjs->set_args(std::move(strings));
+
+        return true;
+    }
+
     JS::RootedObject global_root(gjs->context(), gjs->global());
     if (!gjs_define_string_array(gjs->context(), global_root, array_name,
                                  strings, JSPROP_READONLY | JSPROP_PERMANENT)) {
@@ -1351,6 +1366,14 @@ gjs_context_define_string_array(GjsContext  *js_context,
     return true;
 }
 
+void gjs_context_set_argv(GjsContext* js_context, ssize_t 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(std::move(args));
+}
+
 static GjsContext *current_context;
 
 GjsContext *
diff --git a/gjs/context.h b/gjs/context.h
index 380b98b2..97aea1a0 100644
--- a/gjs/context.h
+++ b/gjs/context.h
@@ -13,6 +13,7 @@
 
 #include <stdbool.h>    /* IWYU pragma: keep */
 #include <stdint.h>
+#include <sys/types.h>  /* for ssize_t */
 
 #ifndef _WIN32
 #    include <signal.h> /* for siginfo_t */
@@ -65,6 +66,10 @@ 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,
+                                     ssize_t array_length,
+                                     const char** array_values);
+
 GJS_EXPORT GJS_USE GList* gjs_context_get_all(void);
 
 GJS_EXPORT GJS_USE GjsContext* gjs_context_get_current(void);
diff --git a/installed-tests/js/testSystem.js b/installed-tests/js/testSystem.js
index e805f7bb..4c076737 100644
--- a/installed-tests/js/testSystem.js
+++ b/installed-tests/js/testSystem.js
@@ -61,3 +61,13 @@ describe('System.programPath', function () {
         expect(System.programPath).toBe(null);
     });
 });
+
+describe('System.programArgs', function () {
+    it('System.programArgs is equal to ARGV', function () {
+        expect(System.programArgs).toEqual(ARGV);
+    });
+
+    it('System.programArgs is an array', function () {
+        expect(Array.isArray(System.programArgs)).toBeTruthy();
+    });
+});
diff --git a/js.gresource.xml b/js.gresource.xml
index fc55e597..72e27c5b 100644
--- a/js.gresource.xml
+++ b/js.gresource.xml
@@ -30,6 +30,7 @@
     <file>modules/script/mainloop.js</file>
     <file>modules/script/jsUnit.js</file>
     <file>modules/script/signals.js</file>
+    <file>modules/script/system.js</file>
     <file>modules/script/format.js</file>
     <file>modules/script/package.js</file>
 
@@ -45,5 +46,6 @@
     <file>modules/core/_format.js</file>
     <file>modules/core/_gettext.js</file>
     <file>modules/core/_signals.js</file>
+    <file>modules/core/_system.js</file>
   </gresource>
 </gresources>
diff --git a/modules/core/_system.js b/modules/core/_system.js
new file mode 100644
index 00000000..acef8765
--- /dev/null
+++ b/modules/core/_system.js
@@ -0,0 +1,16 @@
+// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
+
+// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
+// SPDX-FileCopyrightText: 2021 Evan Welsh <contact evanwelsh com>
+
+/* exported programArgs */
+
+var programArgs;
+
+(() => {
+    const {buildProgramArgs, ..._systemNative} = imports._systemNative;
+
+    Object.assign(this, _systemNative);
+
+    programArgs = buildProgramArgs();
+})();
diff --git a/modules/esm/system.js b/modules/esm/system.js
index 07cf6c2c..2b9ae3be 100644
--- a/modules/esm/system.js
+++ b/modules/esm/system.js
@@ -1,26 +1,30 @@
 // SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
 // SPDX-FileCopyrightText: 2020 Evan Welsh <contact evanwelsh com>
 
-const system = import.meta.importSync('system');
+const system = import.meta.importSync('_systemNative');
 
 export let {
     addressOf,
-    refcount,
     breakpoint,
-    gc,
+    clearDateCaches,
     exit,
-    version,
+    gc,
+    programArgs,
     programInvocationName,
-    clearDateCaches,
+    programPath,
+    refcount,
+    version,
 } = system;
 
 export default {
     addressOf,
-    refcount,
     breakpoint,
-    gc,
+    clearDateCaches,
     exit,
-    version,
+    gc,
+    programArgs,
     programInvocationName,
-    clearDateCaches,
+    programPath,
+    refcount,
+    version,
 };
diff --git a/modules/modules.cpp b/modules/modules.cpp
index c9b7061d..b95f193b 100644
--- a/modules/modules.cpp
+++ b/modules/modules.cpp
@@ -20,7 +20,7 @@ gjs_register_static_modules (void)
 #ifdef ENABLE_CAIRO
     gjs_register_native_module("cairoNative", gjs_js_define_cairo_stuff);
 #endif
-    gjs_register_native_module("system", gjs_js_define_system_stuff);
+    gjs_register_native_module("_systemNative", gjs_js_define_system_stuff);
     gjs_register_native_module("console", gjs_define_console_stuff);
     gjs_register_native_module("_print", gjs_define_print_stuff);
 }
diff --git a/modules/script/_bootstrap/default.js b/modules/script/_bootstrap/default.js
index e31d80cb..a15c2acd 100644
--- a/modules/script/_bootstrap/default.js
+++ b/modules/script/_bootstrap/default.js
@@ -8,6 +8,14 @@
     const {print, printerr, log, logError} = imports._print;
 
     Object.defineProperties(exports, {
+        ARGV: {
+            configurable: false,
+            enumerable: true,
+            get() {
+                // Wait until after bootstrap or programArgs won't be set.
+                return imports._system.programArgs;
+            },
+        },
         print: {
             configurable: false,
             enumerable: true,
diff --git a/modules/script/system.js b/modules/script/system.js
new file mode 100644
index 00000000..d2f7f716
--- /dev/null
+++ b/modules/script/system.js
@@ -0,0 +1,6 @@
+// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
+
+// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
+// SPDX-FileCopyrightText: 2021 Evan Welsh <contact evanwelsh com>
+
+Object.assign(this, imports._system);
diff --git a/modules/system.cpp b/modules/system.cpp
index 936584c2..6d228f98 100644
--- a/modules/system.cpp
+++ b/modules/system.cpp
@@ -182,6 +182,19 @@ static bool gjs_clear_date_caches(JSContext*, unsigned argc, JS::Value* vp) {
     return true;
 }
 
+static bool gjs_build_program_args(JSContext* cx, unsigned argc,
+                                   JS::Value* vp) {
+    JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
+    GjsContextPrivate* priv = GjsContextPrivate::from_cx(cx);
+
+    JS::RootedObject array(cx, priv->build_args_array());
+    if (!array)
+        return false;
+
+    args.rval().setObject(*array);
+    return true;
+}
+
 static JSFunctionSpec module_funcs[] = {
     JS_FN("addressOf", gjs_address_of, 1, GJS_MODULE_PROP_FLAGS),
     JS_FN("addressOfGObject", gjs_address_of_gobject, 1, GJS_MODULE_PROP_FLAGS),
@@ -191,6 +204,7 @@ static JSFunctionSpec module_funcs[] = {
     JS_FN("gc", gjs_gc, 0, GJS_MODULE_PROP_FLAGS),
     JS_FN("exit", gjs_exit, 0, GJS_MODULE_PROP_FLAGS),
     JS_FN("clearDateCaches", gjs_clear_date_caches, 0, GJS_MODULE_PROP_FLAGS),
+    JS_FN("buildProgramArgs", gjs_build_program_args, 0, GJS_MODULE_PROP_FLAGS),
     JS_FS_END};
 
 bool


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