[gjs] Convert many native modules to JS modules



commit b5e467d89aea43a8e32a1138d232c8a32e6b0785
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Sun Jun 17 20:25:02 2012 +0200

    Convert many native modules to JS modules
    
    Less C code and more JS + introspection means more maintenability
    and fewer bugs.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=692025

 Makefile-modules.am                             |   51 +---
 Makefile.am                                     |    4 +-
 modules/debugger.h => libgjs-private/gjs-util.c |   34 ++-
 modules/mainloop.h => libgjs-private/gjs-util.h |   20 +-
 modules/debugger.c                              |   52 ---
 modules/format.c                                |   67 ----
 modules/format.h                                |   38 ---
 modules/format.js                               |    4 +-
 modules/gettext-native.c                        |  290 -----------------
 modules/gettext-native.h                        |   38 ---
 modules/gettext.js                              |   46 +++-
 modules/mainloop.c                              |  379 -----------------------
 modules/mainloop.js                             |   81 +++++
 13 files changed, 161 insertions(+), 943 deletions(-)
---
diff --git a/Makefile-modules.am b/Makefile-modules.am
index 539c629..7a14264 100644
--- a/Makefile-modules.am
+++ b/Makefile-modules.am
@@ -11,12 +11,13 @@ dist_gjsoverride_DATA = 		\
 dist_gjsjs_DATA +=		\
 	modules/gettext.js	\
 	modules/lang.js		\
+	modules/mainloop.js	\
 	modules/jsUnit.js	\
 	modules/signals.js	\
 	modules/promise.js	\
 	modules/format.js
 
-gjsnative_LTLIBRARIES += console.la debugger.la langNative.la mainloop.la gettextNative.la system.la formatNative.la
+gjsnative_LTLIBRARIES += console.la langNative.la system.la
 
 if ENABLE_CAIRO
 dist_gjsjs_DATA +=		\
@@ -48,30 +49,6 @@ langNative_la_SOURCES =				\
 	modules/lang.h				\
 	modules/lang.c
 
-mainloop_la_CFLAGS = 				\
-	$(JS_NATIVE_MODULE_CFLAGS)
-mainloop_la_LIBADD = \
-	libgjs.la				\
-	$(JS_NATIVE_MODULE_LIBADD)
-mainloop_la_LDFLAGS = 				\
-	$(JS_NATIVE_MODULE_LDFLAGS)
-
-mainloop_la_SOURCES =		\
-	modules/mainloop.h	\
-	modules/mainloop.c
-
-gettextNative_la_CFLAGS = 				\
-	$(JS_NATIVE_MODULE_CFLAGS)
-gettextNative_la_LIBADD = \
-	libgjs.la				\
-	$(JS_NATIVE_MODULE_LIBADD)
-gettextNative_la_LDFLAGS = 				\
-	$(JS_NATIVE_MODULE_LDFLAGS)
-
-gettextNative_la_SOURCES =		\
-	modules/gettext-native.h	\
-	modules/gettext-native.c
-
 cairoNative_la_CFLAGS =                         \
         $(JS_NATIVE_MODULE_CFLAGS)              \
         $(GJS_CAIRO_CFLAGS)                     \
@@ -123,27 +100,3 @@ console_la_LDFLAGS = 				\
 console_la_SOURCES =		\
 	modules/console.h	\
 	modules/console.c
-
-debugger_la_CFLAGS = 				\
-	$(JS_NATIVE_MODULE_CFLAGS)
-debugger_la_LIBADD = \
-	libgjs.la				\
-	$(JS_NATIVE_MODULE_LIBADD)
-debugger_la_LDFLAGS = 				\
-	$(JS_NATIVE_MODULE_LDFLAGS)
-
-debugger_la_SOURCES =		\
-	modules/debugger.h	\
-	modules/debugger.c
-
-formatNative_la_CFLAGS = \
-	$(JS_NATIVE_MODULE_CFLAGS)
-formatNative_la_LIBADD = \
-	libgjs.la \
-	$(JS_NATIVE_MODULE_LIBADD)
-formatNative_la_LDFLAGS = \
-	$(JS_NATIVE_MODULE_LDFLAGS)
-
-formatNative_la_SOURCES = \
-	modules/format.h \
-	modules/format.c
diff --git a/Makefile.am b/Makefile.am
index d97cd19..02971ac 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -149,7 +149,9 @@ libgjs_la_SOURCES += \
 # Also, these files used to be a separate library
 libgjs_private_source_files = \
 	libgjs-private/gjs-gdbus-wrapper.c	\
-	libgjs-private/gjs-gdbus-wrapper.h
+	libgjs-private/gjs-gdbus-wrapper.h	\
+	libgjs-private/gjs-util.c		\
+	libgjs-private/gjs-util.h
 libgjs_la_SOURCES += $(libgjs_private_source_files)
 
 GjsPrivate-1.0.gir: libgjs.la
diff --git a/modules/debugger.h b/libgjs-private/gjs-util.c
similarity index 69%
rename from modules/debugger.h
rename to libgjs-private/gjs-util.c
index af86a7c..0fb0f2e 100644
--- a/modules/debugger.h
+++ b/libgjs-private/gjs-util.c
@@ -1,6 +1,5 @@
 /* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
-/*
- * Copyright (c) 2008  litl, LLC
+/* Copyright 2012 Giovanni Campagna <scampa giovanni gmail com>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to
@@ -21,18 +20,31 @@
  * IN THE SOFTWARE.
  */
 
-#ifndef __GJS_DEBUGGER_H__
-#define __GJS_DEBUGGER_H__
-
 #include <config.h>
+#include <string.h>
+
 #include <glib.h>
-#include "gjs/jsapi-util.h"
+#include <glib/gi18n.h>
 
-G_BEGIN_DECLS
+#include "gjs-util.h"
 
-JSBool        gjs_define_debugger_stuff    (JSContext      *context,
-                                            JSObject       *in_object);
+char *
+gjs_format_int_alternative_output(int n)
+{
+    return g_strdup_printf("%Id", n);
+}
 
-G_END_DECLS
+void
+gjs_textdomain(const char *domain)
+{
+    textdomain(domain);
+}
 
-#endif  /* __GJS_DEBUGGER_H__ */
+void
+gjs_bindtextdomain(const char *domain,
+                   const char *location)
+{
+    bindtextdomain(domain, location);
+    /* Always use UTF-8; we assume it internally here */
+    bind_textdomain_codeset(domain, "UTF-8");
+}
diff --git a/modules/mainloop.h b/libgjs-private/gjs-util.h
similarity index 76%
rename from modules/mainloop.h
rename to libgjs-private/gjs-util.h
index da7bb51..0eb55f6 100644
--- a/modules/mainloop.h
+++ b/libgjs-private/gjs-util.h
@@ -1,6 +1,5 @@
 /* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
-/*
- * Copyright (c) 2008  litl, LLC
+/* Copyright 2012 Giovanni Campagna <scampa giovanni gmail com>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to
@@ -21,18 +20,21 @@
  * IN THE SOFTWARE.
  */
 
-#ifndef __GJS_MAINLOOP_H__
-#define __GJS_MAINLOOP_H__
+#ifndef __GJS_PRIVATE_UTIL_H__
+#define __GJS_PRIVATE_UTIL_H__
 
-#include <config.h>
 #include <glib.h>
-#include "gjs/jsapi-util.h"
 
 G_BEGIN_DECLS
 
-JSBool        gjs_define_mainloop_stuff     (JSContext      *context,
-                                             JSObject       *in_object);
+/* For imports.format */
+char * gjs_format_int_alternative_output (int n);
+
+/* For imports.gettext */
+void gjs_textdomain     (const char *domain);
+void gjs_bindtextdomain (const char *domain,
+                         const char *location);
 
 G_END_DECLS
 
-#endif  /* __GJS_MAINLOOP_H__ */
+#endif
diff --git a/modules/format.js b/modules/format.js
index 56bce7d..e6b58d5 100644
--- a/modules/format.js
+++ b/modules/format.js
@@ -1,6 +1,6 @@
 // -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
 
-const FormatNative = imports.formatNative;
+const GjsPrivate = imports.gi.GjsPrivate;
 
 function vprintf(str, args) {
     let i = 0;
@@ -33,7 +33,7 @@ function vprintf(str, args) {
         case 'd':
             let intV = parseInt(args[i++]);
             if (hasAlternativeIntFlag)
-                s = FormatNative.format_int_alternative_output(intV);
+                s = GjsPrivate.format_int_alternative_output(intV);
             else
                 s = intV.toString();
             break;
diff --git a/modules/gettext.js b/modules/gettext.js
index ab39972..8f73c13 100644
--- a/modules/gettext.js
+++ b/modules/gettext.js
@@ -19,7 +19,9 @@
 // IN THE SOFTWARE.
 
 /**
- * This module provides bindings for the "gettext" family of functions.
+ * This module provides a convenience layer for the "gettext" family of functions,
+ * relying on GLib for the actual implementation.
+ *
  * Usage:
  *
  * const Gettext = imports.gettext;
@@ -30,10 +32,40 @@
  * let translated = Gettext.gettext("Hello world!");
  */
 
-const Lang = imports.lang;
+const GLib = imports.gi.GLib;
+const GjsPrivate = imports.gi.GjsPrivate;
+
+function textdomain(domain) {
+    return GjsPrivate.textdomain(domain);
+}
+function bindtextdomain(domain, location) {
+    return GjsPrivate.bindtextdomain(domain, location);
+}
+
+function gettext(msgid) {
+    return GLib.dgettext(null, msgid);
+}
+function dgettext(domain, msgid) {
+    return GLib.dgetext(domain, msgid);
+}
+function dcgettext(domain, msgid, category) {
+    return GLib.dcgettext(domain, msgid, category);
+}
+
+function ngettext(msgid1, msgid2, n) {
+    return GLib.dngettext(null, msgid1, msgid2, n);
+}
+function dngettext(domain, msgid1, msgid2, n) {
+    return GLib.dngettext(domain, msgid1, msgid2, n);
+}
+// FIXME: missing dcngettext ?
 
-// Merge stuff defined in native code
-Lang.copyProperties(imports.gettextNative, this);
+function pgettext(context, msgid) {
+    return GLib.dpgettext2(null, context, msgid);
+}
+function dpgettext(domain, context, msgid) {
+    return GLib.dpgettext2(domain, context, msgid);
+}
 
 /**
  * Create an object with bindings for gettext, ngettext,
@@ -46,15 +78,15 @@ Lang.copyProperties(imports.gettextNative, this);
 var domain = function(domainName) {
     return {
         gettext: function(msgid) {
-            return dgettext(domainName, msgid);
+            return GLib.dgettext(domainName, msgid);
         },
 
         ngettext: function(msgid1, msgid2, n) {
-            return dngettext(domainName, msgid1, msgid2, n);
+            return GLib.dngettext(domainName, msgid1, msgid2, n);
         },
 
         pgettext: function(context, msgid) {
-            return dpgettext(domainName, context, msgid);
+            return GLib.dpgettext2(domainName, context, msgid);
         }
     }
 };
diff --git a/modules/mainloop.js b/modules/mainloop.js
new file mode 100644
index 0000000..06458a3
--- /dev/null
+++ b/modules/mainloop.js
@@ -0,0 +1,81 @@
+/* -*- mode: js; indent-tabs-mode: nil; -*- */
+// Copyright (c) 2012 Giovanni Campagna <scampa giovanni gmail com>
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to
+// deal in the Software without restriction, including without limitation the
+// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+// sell copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+// IN THE SOFTWARE.
+
+// A layer of convenience and backwards-compatibility over GLib MainLoop facilities
+
+const GLib = imports.gi.GLib;
+const GObject = imports.gi.GObject;
+
+var _mainLoops = {};
+
+function run(name) {
+    if (!_mainLoops[name])
+        _mainLoops[name] = GLib.MainLoop.new(null, false);
+
+    _mainLoops[name].run();
+}
+
+function quit(name) {
+    if (!_mainLoops[name])
+	throw new Error("No main loop with this id");
+
+    let loop = _mainLoops[name];
+    delete _mainLoops[name];
+
+    if (!loop.is_running())
+	throw new Error("Main loop was stopped already");
+
+    loop.quit();
+}
+
+function idle_source(handler) {
+    let s = GLib.idle_source_new();
+    GObject.source_set_closure(s, handler);
+    return s;
+}
+
+function idle_add(handler) {
+    return idle_source(handler).attach(null);
+}
+
+function timeout_source(timeout, handler) {
+    let s = GLib.timeout_source_new(timeout);
+    GObject.source_set_closure(s, handler);
+    return s;
+}
+
+function timeout_seconds_source(timeout, handler) {
+    let s = GLib.timeout_source_new_seconds(timeout);
+    GObject.source_set_closure(s, handler);
+    return s;
+}
+
+function timeout_add(timeout, handler) {
+    return timeout_source(timeout, handler).attach(null);
+}
+
+function timeout_add_seconds(timeout, handler) {
+    return timeout_seconds_source(timeout, handler).attach(null);
+}
+
+function source_remove(id) {
+    return GLib.source_remove(id);
+}



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