[gjs] Don't use constructors to register static modules



commit 72afb8824f55ea722d18db803b3caaed4d97b4fa
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Wed Feb 27 21:32:23 2013 -0500

    Don't use constructors to register static modules
    
    https://bugzilla.gnome.org/show_bug.cgi?id=694873

 Makefile.am             |    2 ++
 configure.ac            |    3 +++
 gjs/context.c           |    4 ++++
 gjs/native.h            |   34 +---------------------------------
 modules/cairo-module.h  |   29 +++++++++++++++++++++++++++++
 modules/cairo-private.h |    4 +---
 modules/cairo.c         |    3 ---
 modules/console.c       |    2 --
 modules/modules.c       |   44 ++++++++++++++++++++++++++++++++++++++++++++
 modules/modules.h       |   37 +++++++++++++++++++++++++++++++++++++
 modules/system.c        |    2 --
 11 files changed, 121 insertions(+), 43 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 02971ac..ac6162e 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -119,6 +119,8 @@ libgjs_la_SOURCES =         \
        gjs/profiler.c          \
        gjs/stack.c             \
        gjs/type-module.c       \
+       modules/modules.c       \
+       modules/modules.h       \
        util/error.c            \
        util/glib.c             \
        util/crash.c            \
diff --git a/configure.ac b/configure.ac
index 771abd5..6eb8b05 100644
--- a/configure.ac
+++ b/configure.ac
@@ -110,6 +110,9 @@ AS_IF([test x$with_cairo = xyes], [
   PKG_CHECK_MODULES([GJS_CAIRO], [$gjs_cairo_packages], have_cairo=yes, have_cairo=no)
   ])
 AM_CONDITIONAL(ENABLE_CAIRO, test x$have_cairo = xyes)
+AS_IF([test x$have_cairo = xyes], [
+  AC_DEFINE([ENABLE_CAIRO],[1],[Define if you want to build with cairo support])
+])
 PKG_CHECK_MODULES([GJS_GDBUS], [$gjs_gdbus_packages])
 
 PKG_CHECK_MODULES([GJSTESTS], [$gjstests_packages])
diff --git a/gjs/context.c b/gjs/context.c
index 890510b..cedce2c 100644
--- a/gjs/context.c
+++ b/gjs/context.c
@@ -33,6 +33,8 @@
 
 #include "gi.h"
 
+#include <modules/modules.h>
+
 #include <util/log.h>
 #include <util/glib.h>
 #include <util/error.h>
@@ -355,6 +357,8 @@ gjs_context_class_init(GjsContextClass *klass)
     gjs_register_native_module("byteArray", gjs_define_byte_array_stuff, 0);
     gjs_register_native_module("_gi", gjs_define_private_gi_stuff, 0);
     gjs_register_native_module("gi", gjs_define_gi_stuff, GJS_NATIVE_SUPPLIES_MODULE_OBJ);
+
+    gjs_register_static_modules();
 }
 
 static void
diff --git a/gjs/native.h b/gjs/native.h
index 98bb4c0..902f7bc 100644
--- a/gjs/native.h
+++ b/gjs/native.h
@@ -49,43 +49,11 @@ typedef enum {
 
 } GjsNativeFlags;
 
-/*
- * In a native module, you define a GjsDefineModuleFunc that
- * adds your stuff to module_obj.
- *
- * You then declare GJS_REGISTER_NATIVE_MODULE("my.module.path", my_module_func)
- *
- * This declaration will call gjs_register_native_module() when your
- * module is dlopen'd. We can't just use a well-known symbol name
- * in your module, because we need to dlopen modules with
- * global symbols.
- */
 
 typedef JSBool (* GjsDefineModuleFunc) (JSContext *context,
                                         JSObject  *module_obj);
 
-/* FIXME - Reuse glib/glib/gconstructor.h */
-#ifdef __GNUC__
-#define _GJS_CONSTRUCTOR __attribute__((constructor))
-#elif __SUNPRO_C
-#define Pragma(x) _Pragma(#x)
-#define _GJS_CONSTRUCTOR Pragma(init(register_native_module))
-#else
-Initialization routine in a dynamic object not defined for current compiler
-#endif
-
-#define GJS_REGISTER_NATIVE_MODULE_WITH_FLAGS(module_id_string, module_func, flags) \
-    _GJS_CONSTRUCTOR static void                                 \
-    register_native_module (void)                                            \
-    {                                                                        \
-        gjs_register_native_module(module_id_string, module_func, flags); \
-    }
-
-
-#define GJS_REGISTER_NATIVE_MODULE(module_id_string, module_func)           \
-    GJS_REGISTER_NATIVE_MODULE_WITH_FLAGS(module_id_string, module_func, 0)
-
-/* called in constructor function on dlopen() load */
+/* called on context init */
 void   gjs_register_native_module (const char            *module_id,
                                    GjsDefineModuleFunc  func,
                                    GjsNativeFlags       flags);
diff --git a/modules/cairo-module.h b/modules/cairo-module.h
new file mode 100644
index 0000000..beb6a56
--- /dev/null
+++ b/modules/cairo-module.h
@@ -0,0 +1,29 @@
+/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
+/* Copyright 2010 litl, LLC.
+ *
+ * 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.
+ */
+
+#ifndef __CAIRO_MODULE_H__
+#define __CAIRO_MODULE_H__
+
+JSBool           gjs_js_define_cairo_stuff              (JSContext       *context,
+                                                         JSObject        *module);
+
+#endif /* __CAIRO_MODULE_H__ */
diff --git a/modules/cairo-private.h b/modules/cairo-private.h
index 5619d08..a1ca1ec 100644
--- a/modules/cairo-private.h
+++ b/modules/cairo-private.h
@@ -23,11 +23,9 @@
 #ifndef __CAIRO_PRIVATE_H__
 #define __CAIRO_PRIVATE_H__
 
+#include "cairo-module.h"
 #include <cairo.h>
 
-JSBool           gjs_js_define_cairo_stuff              (JSContext       *context,
-                                                         JSObject        *module);
-
 JSBool           gjs_cairo_check_status                 (JSContext       *context,
                                                          cairo_status_t   status,
                                                          const char      *name);
diff --git a/modules/cairo.c b/modules/cairo.c
index e662ede..6cde95d 100644
--- a/modules/cairo.c
+++ b/modules/cairo.c
@@ -123,6 +123,3 @@ gjs_js_define_cairo_stuff(JSContext *context,
 
     return JS_TRUE;
 }
-
-GJS_REGISTER_NATIVE_MODULE("cairoNative", gjs_js_define_cairo_stuff)
-
diff --git a/modules/console.c b/modules/console.c
index 71a95b4..35add3f 100644
--- a/modules/console.c
+++ b/modules/console.c
@@ -237,5 +237,3 @@ gjs_define_console_stuff(JSContext *context,
 
     return JS_TRUE;
 }
-
-GJS_REGISTER_NATIVE_MODULE("console", gjs_define_console_stuff);
diff --git a/modules/modules.c b/modules/modules.c
new file mode 100644
index 0000000..479fb4c
--- /dev/null
+++ b/modules/modules.c
@@ -0,0 +1,44 @@
+/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
+/*
+ * Copyright 2013 Red Hat, Inc.
+ *
+ * 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.
+ */
+
+#include <config.h>
+
+#include <gjs/native.h>
+#include "modules.h"
+
+#ifdef ENABLE_CAIRO
+#include "cairo-module.h"
+#endif
+
+#include "system.h"
+#include "console.h"
+
+void
+gjs_register_static_modules (void)
+{
+#ifdef ENABLE_CAIRO
+    gjs_register_native_module("cairoNative", gjs_js_define_cairo_stuff, 0);
+#endif
+    gjs_register_native_module("system", gjs_js_define_system_stuff, 0);
+    gjs_register_native_module("console", gjs_define_console_stuff, 0);
+}
diff --git a/modules/modules.h b/modules/modules.h
new file mode 100644
index 0000000..efbd22b
--- /dev/null
+++ b/modules/modules.h
@@ -0,0 +1,37 @@
+/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
+/*
+ * Copyright 2013 Red Hat, Inc.
+ *
+ * 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.
+ */
+
+#ifndef __GJS_MODULES_H__
+#define __GJS_MODULES_H__
+
+#include <config.h>
+#include <glib.h>
+#include "gjs/jsapi-util.h"
+
+G_BEGIN_DECLS
+
+void gjs_register_static_modules (void);
+
+G_END_DECLS
+
+#endif  /* __GJS_CONSOLE_H__ */
diff --git a/modules/system.c b/modules/system.c
index 2d23285..c650a8a 100644
--- a/modules/system.c
+++ b/modules/system.c
@@ -153,5 +153,3 @@ gjs_js_define_system_stuff(JSContext *context,
 
     return JS_TRUE;
 }
-
-GJS_REGISTER_NATIVE_MODULE("system", gjs_js_define_system_stuff)


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