[gjs] Expose the launched binary to JS
- From: Giovanni Campagna <gcampagna src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs] Expose the launched binary to JS
- Date: Tue, 7 May 2013 16:15:47 +0000 (UTC)
commit 47e06c7efc9d7a143e83d331b8ca068937d81003
Author: Giovanni Campagna <gcampagna src gnome org>
Date: Mon May 6 23:02:35 2013 +0200
Expose the launched binary to JS
Usually in various languages the launched program is available
as ARGV[0], but in the gjs case, ARGV only contains command line
arguments, so we need a separate property to retrieve the launched
script.
This is now exposed as System.programInvocationName.
The use case is allowing package.js to determine if the app is
launched from the source without hacks.
https://bugzilla.gnome.org/show_bug.cgi?id=699784
gjs/console.c | 16 +++++++++++++---
gjs/context.c | 20 +++++++++++++++++++-
modules/system.c | 29 +++++++++++++++++++++++++++++
3 files changed, 61 insertions(+), 4 deletions(-)
---
diff --git a/gjs/console.c b/gjs/console.c
index 9e808c4..bcf2a7e 100644
--- a/gjs/console.c
+++ b/gjs/console.c
@@ -48,6 +48,7 @@ main(int argc, char **argv)
GjsContext *js_context;
char *script;
const char *filename;
+ const char *program_name;
gsize len;
int code;
const char *source_js_version;
@@ -73,11 +74,13 @@ main(int argc, char **argv)
source_js_version = gjs_context_scan_buffer_for_js_version(script, 1024);
len = strlen(script);
filename = "<command line>";
+ program_name = NULL;
} else if (argc <= 1) {
source_js_version = NULL;
script = g_strdup("const Console = imports.console; Console.interact();");
len = strlen(script);
filename = "<stdin>";
+ program_name = NULL;
} else /*if (argc >= 2)*/ {
error = NULL;
if (!g_file_get_contents(argv[1], &script, &len, &error)) {
@@ -86,6 +89,7 @@ main(int argc, char **argv)
}
source_js_version = gjs_context_scan_buffer_for_js_version(script, 1024);
filename = argv[1];
+ program_name = argv[1];
argc--;
argv++;
}
@@ -94,10 +98,16 @@ main(int argc, char **argv)
if (js_version != NULL)
source_js_version = js_version;
if (source_js_version != NULL)
- js_context = g_object_new(GJS_TYPE_CONTEXT, "search-path", include_path,
- "js-version", source_js_version, NULL);
+ js_context = g_object_new(GJS_TYPE_CONTEXT,
+ "search-path", include_path,
+ "js-version", source_js_version,
+ "program-name", program_name,
+ NULL);
else
- js_context = g_object_new(GJS_TYPE_CONTEXT, "search-path", include_path, NULL);
+ js_context = g_object_new(GJS_TYPE_CONTEXT,
+ "search-path", include_path,
+ "program-name", program_name,
+ NULL);
/* prepare command line arguments */
if (!gjs_context_define_string_array(js_context, "ARGV",
diff --git a/gjs/context.c b/gjs/context.c
index b6dd7dd..52b2dbf 100644
--- a/gjs/context.c
+++ b/gjs/context.c
@@ -71,6 +71,7 @@ struct _GjsContext {
GjsProfiler *profiler;
char *jsversion_string;
+ char *program_name;
char **search_path;
@@ -96,7 +97,8 @@ enum {
PROP_0,
PROP_JS_VERSION,
PROP_SEARCH_PATH,
- PROP_GC_NOTIFICATIONS
+ PROP_GC_NOTIFICATIONS,
+ PROP_PROGRAM_NAME,
};
@@ -328,6 +330,16 @@ gjs_context_class_init(GjsContextClass *klass)
PROP_GC_NOTIFICATIONS,
pspec);
+ pspec = g_param_spec_string("program-name",
+ "Program Name",
+ "The filename of the launched JS program",
+ "",
+ G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
+
+ g_object_class_install_property(object_class,
+ PROP_PROGRAM_NAME,
+ pspec);
+
signals[SIGNAL_GC] = g_signal_new("gc", G_TYPE_FROM_CLASS(klass),
G_SIGNAL_RUN_LAST, 0,
NULL, NULL,
@@ -690,6 +702,9 @@ gjs_context_get_property (GObject *object,
case PROP_GC_NOTIFICATIONS:
g_value_set_boolean(value, js_context->gc_notifications_enabled);
break;
+ case PROP_PROGRAM_NAME:
+ g_value_set_string(value, js_context->program_name);
+ break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@@ -720,6 +735,9 @@ gjs_context_set_property (GObject *object,
case PROP_GC_NOTIFICATIONS:
js_context->gc_notifications_enabled = g_value_get_boolean(value);
break;
+ case PROP_PROGRAM_NAME:
+ js_context->program_name = g_value_dup_string(value);
+ break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
diff --git a/modules/system.c b/modules/system.c
index a766646..36270db 100644
--- a/modules/system.c
+++ b/modules/system.c
@@ -123,6 +123,10 @@ JSBool
gjs_js_define_system_stuff(JSContext *context,
JSObject *module)
{
+ GjsContext *gjs_context;
+ char *program_name;
+ jsval value;
+
if (!JS_DefineFunction(context, module,
"addressOf",
(JSNative) gjs_address_of,
@@ -153,5 +157,30 @@ gjs_js_define_system_stuff(JSContext *context,
0, GJS_MODULE_PROP_FLAGS))
return JS_FALSE;
+ gjs_context = JS_GetContextPrivate(context);
+ g_object_get(gjs_context,
+ "program-name", &program_name,
+ NULL);
+
+ if (!gjs_string_from_utf8(context, program_name,
+ -1, &value)) {
+ g_free(program_name);
+ return JS_FALSE;
+ }
+
+ /* The name is modeled after program_invocation_name,
+ part of the glibc */
+ if (!JS_DefineProperty(context, module,
+ "programInvocationName",
+ value,
+ JS_PropertyStub,
+ JS_StrictPropertyStub,
+ GJS_MODULE_PROP_FLAGS | JSPROP_READONLY)) {
+ g_free(program_name);
+ return JS_FALSE;
+ }
+
+ g_free(program_name);
+
return JS_TRUE;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]