[gjs/wip/package: 109/110] system: add a wrapper around exec()



commit a32c461bdfc7dfcff5a96570157fbc292a2f5cb9
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Tue May 7 23:29:32 2013 +0200

    system: add a wrapper around exec()
    
    The main use case for this is replacing the current program with
    a debugger, so that you can run "my-js-app --debug --arg" which
    becomes the equivalent of
    /usr/bin/gdb --args /usr/bin/gjs /usr/bin/my-js-app --arg
    
    In the future we might have a JS debugger too, but for now having
    a C debugger is an improvement.

 modules/system.cpp |   43 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 43 insertions(+), 0 deletions(-)
---
diff --git a/modules/system.cpp b/modules/system.cpp
index b12f766..8bd7ddc 100644
--- a/modules/system.cpp
+++ b/modules/system.cpp
@@ -26,6 +26,7 @@
 
 #include <sys/types.h>
 #include <unistd.h>
+#include <glib-unix.h>
 
 #include <gjs/gjs-module.h>
 #include <gi/object.h>
@@ -119,12 +120,54 @@ gjs_exit(JSContext *context,
     return JS_TRUE;
 }
 
+static JSBool
+gjs_exec(JSContext *context,
+         unsigned   argc,
+         jsval     *vp)
+{
+    jsval *argv = JS_ARGV(cx, vp);
+    jsval v;
+    JSObject *arg_array;
+    char **c_argv;
+    unsigned i;
+    GError *error;
+
+    if (!gjs_parse_args(context, "exec", "o", argc, argv,
+                        "argv", &arg_array))
+        return JS_FALSE;
+
+    if (!JS_GetArrayLength(context, arg_array, &argc))
+        return JS_FALSE;
+
+    c_argv = g_new0(char *, argc + 1);
+
+    for (i = 0; i < argc; i++) {
+        if (!JS_GetElement(context, arg_array, i, &v))
+            goto out;
+
+        if (!gjs_string_to_utf8(context, v, &c_argv[i]))
+            goto out;
+    }
+
+    execvp(c_argv[0], c_argv);
+
+    error = g_error_new(G_UNIX_ERROR, errno,
+                        "Failed to execute new program: %s", strerror(errno));
+    gjs_throw_g_error(context, error);
+    g_error_free(error);
+
+ out:
+    g_strfreev(c_argv);
+    return JS_FALSE;
+}
+
 static JSFunctionSpec module_funcs[] = {
     { "addressOf", JSOP_WRAPPER (gjs_address_of), 1, GJS_MODULE_PROP_FLAGS },
     { "refcount", JSOP_WRAPPER (gjs_refcount), 1, GJS_MODULE_PROP_FLAGS },
     { "breakpoint", JSOP_WRAPPER (gjs_breakpoint), 0, GJS_MODULE_PROP_FLAGS },
     { "gc", JSOP_WRAPPER (gjs_gc), 0, GJS_MODULE_PROP_FLAGS },
     { "exit", JSOP_WRAPPER (gjs_exit), 0, GJS_MODULE_PROP_FLAGS },
+    { "exec", JSOP_WRAPPER (gjs_exec), 1, GJS_MODULE_PROP_FLAGS },
     { NULL },
 };
 


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