You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by jp...@apache.org on 2012/06/12 17:51:37 UTC

[32/50] git commit: Load lua files and call init()

Load lua files and call init()


Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo
Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/32aa8150
Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/32aa8150
Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/32aa8150

Branch: refs/heads/jpeach/lua
Commit: 32aa81508f0397e57c942acd9d0a2b898218bd0a
Parents: d580abc
Author: James Peach <jp...@apache.org>
Authored: Thu Apr 5 22:25:08 2012 -0700
Committer: James Peach <jp...@apache.org>
Committed: Tue Jun 12 08:48:31 2012 -0700

----------------------------------------------------------------------
 plugins/lua/lua.cc |   29 ++++++++++++++++++++++-------
 1 files changed, 22 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafficserver/blob/32aa8150/plugins/lua/lua.cc
----------------------------------------------------------------------
diff --git a/plugins/lua/lua.cc b/plugins/lua/lua.cc
index 1f37a62..20f0c95 100644
--- a/plugins/lua/lua.cc
+++ b/plugins/lua/lua.cc
@@ -25,6 +25,8 @@ extern "C" {
 #include <lauxlib.h>
 }
 
+#include <unistd.h>
+
 static void *
 LuaAllocate(void * ud, void * ptr, size_t osize, size_t nsize)
 {
@@ -37,9 +39,9 @@ LuaAllocate(void * ud, void * ptr, size_t osize, size_t nsize)
 
   return TSrealloc(ptr, nsize);
 }
-    
+
 static int
-LuaDebug(lua_State * lua)
+TSLuaDebug(lua_State * lua)
 {
   const char * tag = luaL_checkstring(lua, 1);
   const char * message = luaL_checkstring(lua, 2);
@@ -54,9 +56,9 @@ struct LuaExport
   const char *  name;
 };
 
-static LuaExport LUAEXPORTS[] = 
+static const luaL_Reg LUAEXPORTS[] =
 {
-  { LuaDebug, "ts.debug"},
+  { "debug", TSLuaDebug },
   { NULL, NULL}
 };
 
@@ -78,10 +80,23 @@ TSRemapNewInstance(int argc, char * argv[], void ** ih, char * errbuf, int errbu
   }
 
   luaL_openlibs(lua);
+  luaL_register(lua, "ts", LUAEXPORTS);
+
+  for (int i = 0; i < argc; ++i) {
+    if (access(argv[i], R_OK) == 0) {
+      TSDebug("lua", "%s loading lua file %s", __func__, argv[i]);
+      if (luaL_dofile(lua, argv[i]) != 0) {
+        // If the load failed, it should have pushed an error message.
+        TSError("lua load error: %s", lua_tostring(lua, -1));
+        lua_pop(lua, 1);
+      }
+    }
+  }
 
-  for (const LuaExport * e = LUAEXPORTS; e->function != NULL; ++e) {
-    lua_pushcfunction(lua, e->function);
-    lua_setglobal(lua, e->name);
+  lua_getglobal(lua, "init");
+  if (lua_pcall(lua, 0, 1, 0) != 0) {
+    TSDebug("lua", "init failed: %s", lua_tostring(lua, -1));
+    lua_pop(lua, 1);
   }
 
   *ih = lua;