lua plugin hello world is ok.
authorPeng Wu <epico@dhcp-65-116.nay.redhat.com>
Thu, 11 Mar 2010 02:45:43 +0000 (10:45 +0800)
committerPeng Wu <alexepico@gmail.com>
Wed, 19 May 2010 02:09:31 +0000 (10:09 +0800)
lua/lua-plugin-init.c
lua/lua-plugin.h [new file with mode: 0644]
lua/test-lua-plugin.c [new file with mode: 0644]
lua/test.lua [new file with mode: 0644]

index 4dcd5a6..0ee2677 100644 (file)
@@ -5,23 +5,55 @@
 #include "lualib.h"
 #include "lauxlib.h"
 
-/* the Lua interpreter */
-static lua_State * L = NULL;
+#include "lua-plugin.h"
 
-static int lua_plugin_init(){
-  /* initialize Lua */
-  L = lua_open();
-  
+static const luaL_Reg lualibs[] = {
+  {"", luaopen_base},
+  {LUA_TABLIBNAME, luaopen_table},
+  {LUA_IOLIBNAME, luaopen_io},
+  {LUA_OSLIBNAME, luaopen_os},
+  {LUA_STRLIBNAME, luaopen_string},
+  {LUA_MATHLIBNAME, luaopen_math},
+  {LUA_IMELIBNAME, luaopen_ime},
+  {NULL, NULL}
+};
+
+
+void lua_plugin_openlibs (lua_State *L) {
+  const luaL_Reg *lib = lualibs;
+  for (; lib->func; lib++) {
+    lua_pushcfunction(L, lib->func);
+    lua_pushstring(L, lib->name);
+    lua_call(L, 1, 0);
+  }
+}
+
+
+int lua_plugin_init(lua_State *L){
   /* enable libs in sandbox */
-  luaopen_base(L);
-  luaopen_io(L);
-  luaopen_string(L);
-  luaopen_math(L);
-  luaopen_table(L);
-  
-  
+  lua_plugin_openlibs(L);
+
+  return 0;
+}
+
+int lua_plugin_fini(lua_State *L){
+  lua_close(L);
+} 
+
+static int ime_get_last_commit(lua_State* L){
+  /*TODO: not implemented. */
+  g_assert_not_reached();
+  lua_pushstring(L, "");
+  return 1;
 }
 
+static int ime_get_version(lua_State* L){
+  /* TODO: replace this with C macros. */
+  lua_pushstring(L, "ibus-pinyin 1.2.99");
+  return 1;
+}
+
+
 static const luaL_Reg imelib[] = {
   {"get_last_commit", ime_get_last_commit},
   {"get_version", ime_get_version},
@@ -39,15 +71,8 @@ static const luaL_Reg imelib[] = {
   {NULL, NULL}
 };
 
-static int ime_get_last_commit(lua_State* L){
-  /*TODO: not implemented. */
-  g_assert_not_reached();
-  lua_pushstring(L, "");
+LUALIB_API int luaopen_ime (lua_State *L) {
+  luaL_register(L, LUA_IMELIBNAME, imelib);
   return 1;
 }
 
-static int ime_get_version(lua_State* L){
-  /* TODO: replace this with C macros. */
-  lua_pushstring(L, "ibus-pinyin 1.2.99");
-  return 1;
-}
diff --git a/lua/lua-plugin.h b/lua/lua-plugin.h
new file mode 100644 (file)
index 0000000..b2fe8e5
--- /dev/null
@@ -0,0 +1,10 @@
+#ifndef LUA_PLUGIN_H
+#define LUA_PLUGIN_H
+
+int lua_plugin_init(lua_State * L);
+int lua_plugin_fini(lua_State * L);
+
+#define LUA_IMELIBNAME   "ime"
+LUALIB_API int (luaopen_ime) (lua_State *L);
+
+#endif
diff --git a/lua/test-lua-plugin.c b/lua/test-lua-plugin.c
new file mode 100644 (file)
index 0000000..5dec834
--- /dev/null
@@ -0,0 +1,21 @@
+#include <stdio.h>
+
+#include "lua.h"
+#include "lualib.h"
+#include "lauxlib.h"
+
+#include "lua-plugin.h"
+
+static lua_State * L = NULL;
+
+int main(int argc, char * argv[]){
+  printf("starting test...\n");
+
+  /* initialize Lua */
+  L = lua_open();
+
+  lua_plugin_init(L);
+  luaL_dofile(L, "test.lua");
+  lua_plugin_fini(L);
+  return 0;
+}
diff --git a/lua/test.lua b/lua/test.lua
new file mode 100644 (file)
index 0000000..184c9f0
--- /dev/null
@@ -0,0 +1 @@
+print(ime.get_version())