[gjs] Windows: Define a DllMain()



commit 5ef8799a1775b96fa92a8815447bceccee72e452
Author: Chun-wei Fan <fanchunwei src gnome org>
Date:   Wed Jan 18 15:27:21 2017 +0800

    Windows: Define a DllMain()
    
    Use the DllMain() to call JS_Init()/JS_ShutDown() so that we can reduce
    the use of constructors on Windows, and ensure that JS_Init() is called
    when libgjs is being loaded.  As we automatically get a HINSTANCE/HMODULE
    with the DllMain(), make use of it so that we can construct paths to load
    introspection items and scripts at runtime so that we do not need to
    hardcode paths at build time.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=775868

 gjs/context.cpp  |   12 ++++++++++++
 gjs/importer.cpp |   14 ++++++++++++++
 gjs/runtime.cpp  |   40 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 66 insertions(+), 0 deletions(-)
---
diff --git a/gjs/context.cpp b/gjs/context.cpp
index e814841..927d967 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -44,6 +44,11 @@
 #include <util/glib.h>
 #include <util/error.h>
 
+#ifdef G_OS_WIN32
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+#endif
+
 #include <string.h>
 
 static void     gjs_context_dispose           (GObject               *object);
@@ -352,7 +357,14 @@ gjs_context_class_init(GjsContextClass *klass)
 
     /* For GjsPrivate */
     {
+#ifdef G_OS_WIN32
+        extern HMODULE gjs_dll;
+        char *basedir = g_win32_get_package_installation_directory_of_module (gjs_dll);
+        char *priv_typelib_dir = g_build_filename (basedir, "lib", "girepository-1.0", NULL);
+        g_free (basedir);
+#else
         char *priv_typelib_dir = g_build_filename (PKGLIBDIR, "girepository-1.0", NULL);
+#endif
         g_irepository_prepend_search_path(priv_typelib_dir);
     g_free (priv_typelib_dir);
     }
diff --git a/gjs/importer.cpp b/gjs/importer.cpp
index f997267..0c7b9ee 100644
--- a/gjs/importer.cpp
+++ b/gjs/importer.cpp
@@ -33,6 +33,11 @@
 
 #include <gio/gio.h>
 
+#ifdef G_OS_WIN32
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+#endif
+
 #include <string.h>
 
 #define MODULE_INIT_FILENAME "__init__.js"
@@ -1061,7 +1066,16 @@ gjs_get_search_path(void)
         }
 
         /* ${datadir}/share/gjs-1.0 */
+#ifdef G_OS_WIN32
+        extern HMODULE gjs_dll;
+        char *basedir = g_win32_get_package_installation_directory_of_module (gjs_dll);
+        char *gjs_data_dir = g_build_filename (basedir, "share", "gjs-1.0", NULL);
+        g_ptr_array_add(path, g_strdup(gjs_data_dir));
+        g_free (gjs_data_dir);
+        g_free (basedir);
+#else
         g_ptr_array_add(path, g_strdup(GJS_JS_DIR));
+#endif
 
         g_ptr_array_add(path, NULL);
 
diff --git a/gjs/runtime.cpp b/gjs/runtime.cpp
index 816dbce..fbeb6b6 100644
--- a/gjs/runtime.cpp
+++ b/gjs/runtime.cpp
@@ -27,6 +27,11 @@
 #include "jsapi-wrapper.h"
 #include "runtime.h"
 
+#ifdef G_OS_WIN32
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+#endif
+
 struct RuntimeData {
   unsigned refcount;
   bool in_gc_sweep;
@@ -231,6 +236,40 @@ gjs_destroy_runtime_for_current_thread(void)
     g_private_replace(&thread_runtime, NULL);
 }
 
+#ifdef G_OS_WIN32
+HMODULE gjs_dll;
+static bool gjs_is_inited = false;
+
+BOOL WINAPI
+DllMain (HINSTANCE hinstDLL,
+DWORD     fdwReason,
+LPVOID    lpvReserved)
+{
+  switch (fdwReason)
+  {
+  case DLL_PROCESS_ATTACH:
+    gjs_dll = hinstDLL;
+    g_assert(JS_Init());
+    gjs_is_inited = true;
+    break;
+
+  case DLL_THREAD_DETACH:
+    gjs_destroy_runtime_for_current_thread();
+    break;
+
+  case DLL_PROCESS_DETACH:
+    JS_ShutDown ();
+    break;
+
+  default:
+    /* do nothing */
+    ;
+    }
+
+  return TRUE;
+}
+
+#else
 class GjsInit {
 public:
     GjsInit() {
@@ -250,6 +289,7 @@ public:
 };
 
 static GjsInit gjs_is_inited;
+#endif
 
 static JSRuntime *
 gjs_runtime_for_current_thread(void)


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