refactor lua-plugin gobject interface.
authorPeng Wu <epico@dhcp-65-116.nay.redhat.com>
Sun, 11 Apr 2010 07:10:41 +0000 (15:10 +0800)
committerPeng Wu <alexepico@gmail.com>
Wed, 19 May 2010 02:09:32 +0000 (10:09 +0800)
lua/lua-plugin-init.c
lua/lua-plugin.c
lua/lua-plugin.h

index 403be44..42c6f5c 100644 (file)
@@ -29,41 +29,6 @@ void lua_plugin_openlibs (lua_State *L) {
   }
 }
 
-int lua_plugin_init(IBusEnginePluginPrivate * plugin){
-  g_assert(NULL == plugin->L);
-  /* initialize Lua */
-  plugin->L = lua_open();
-
-  /* enable libs in sandbox */
-  lua_plugin_openlibs(plugin->L);
-
-  if ( NULL == plugin->lua_commands )
-    plugin->lua_commands = g_array_new(TRUE, TRUE, sizeof(lua_command_t));
-
-  return 0;
-}
-
-int lua_plugin_fini(IBusEnginePluginPrivate * plugin){
-  size_t i;
-  lua_command_t * command;
-
-  if ( plugin->lua_commands ){
-    for ( i = 0; i < plugin->lua_commands->len; ++i){
-      command = &g_array_index(plugin->lua_commands, lua_command_t, i);
-      g_free((gpointer)command->command_name);
-      g_free((gpointer)command->lua_function_name);
-      g_free((gpointer)command->description);
-      g_free((gpointer)command->leading);
-      g_free((gpointer)command->help);
-    }
-    g_array_free(plugin->lua_commands, TRUE);
-    plugin->lua_commands = NULL;
-  }
-
-  lua_close(plugin->L);
-  return 0;
-}
-
 static int ime_get_last_commit(lua_State* L){
   /*TODO: not implemented. */
   fprintf(stderr, "TODO: ime_get_last_commit unimplemented.\n");
index db8aa43..beab34c 100644 (file)
@@ -1,9 +1,18 @@
+#include <string.h>
 #include <lua.h>
+#include <lualib.h>
+#include <lauxlib.h>
 
 #include "lua-plugin.h"
 
 #define IBUS_ENGINE_PLUGIN_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), IBUS_TYPE_ENGINE_PLUGIN, IBusEnginePluginPrivate))
 
+struct _IBusEnginePluginPrivate{
+  lua_State * L;
+  GArray * lua_commands; /* Array of lua_command_t. */
+};
+
+
 G_DEFINE_TYPE (IBusEnginePlugin, ibus_engine_plugin, G_TYPE_OBJECT);
 
 static void
@@ -37,6 +46,43 @@ ibus_engine_plugin_class_init (IBusEnginePluginClass *klass)
   g_type_class_add_private (klass, sizeof (IBusEnginePluginPrivate));
 }
 
+static int
+lua_plugin_init(IBusEnginePluginPrivate * plugin){
+  g_assert(NULL == plugin->L);
+  /* initialize Lua */
+  plugin->L = lua_open();
+
+  /* enable libs in sandbox */
+  lua_plugin_openlibs(plugin->L);
+
+  if ( NULL == plugin->lua_commands )
+    plugin->lua_commands = g_array_new(TRUE, TRUE, sizeof(lua_command_t));
+
+  return 0;
+}
+
+static int
+lua_plugin_fini(IBusEnginePluginPrivate * plugin){
+  size_t i;
+  lua_command_t * command;
+
+  if ( plugin->lua_commands ){
+    for ( i = 0; i < plugin->lua_commands->len; ++i){
+      command = &g_array_index(plugin->lua_commands, lua_command_t, i);
+      g_free((gpointer)command->command_name);
+      g_free((gpointer)command->lua_function_name);
+      g_free((gpointer)command->description);
+      g_free((gpointer)command->leading);
+      g_free((gpointer)command->help);
+    }
+    g_array_free(plugin->lua_commands, TRUE);
+    plugin->lua_commands = NULL;
+  }
+
+  lua_close(plugin->L);
+  return 0;
+}
+
 static void
 ibus_engine_plugin_init (IBusEnginePlugin *self)
 {
@@ -44,7 +90,6 @@ ibus_engine_plugin_init (IBusEnginePlugin *self)
 
   self->priv = priv = IBUS_ENGINE_PLUGIN_GET_PRIVATE (self);
 
-  priv->L = NULL;
-  priv->lua_commands = NULL;
+  memset(priv, 0, sizeof(IBusEnginePluginPrivate));
 }
 
index 1964101..677aab3 100644 (file)
@@ -6,6 +6,8 @@
 #define LUA_IMELIBNAME   "ime"
 LUALIB_API int (luaopen_ime) (lua_State * L);
 
+void lua_plugin_openlibs (lua_State *L);
+
 typedef struct{
   const char * command_name;
   const char * lua_function_name;
@@ -64,12 +66,4 @@ const char * lua_plugin_ime_get_retval(IBusEnginePlugin * plugin);
  */
 GArray * lua_plugin_ime_get_retvals(IBusEnginePlugin * plugin);
 
-/*< private >*/
-int lua_plugin_init(IBusEnginePluginPrivate * private);
-int lua_plugin_fini(IBusEnginePluginPrivate * private);
-
-struct _IBusEnginePluginPrivate{
-  lua_State * L;
-  GArray * lua_commands; /* Array of lua_command_t. */
-};
 #endif