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:36 UTC

[7/50] git commit: Move Lua utilities into a separate file

Move Lua utilities into a separate file


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

Branch: refs/heads/jpeach/lua
Commit: 2835d03b9d10fef76e5cf4df9d56ed81b3459165
Parents: 1dd587c
Author: James Peach <jp...@apache.org>
Authored: Tue Apr 24 22:14:57 2012 -0700
Committer: James Peach <jp...@apache.org>
Committed: Tue Jun 12 08:48:38 2012 -0700

----------------------------------------------------------------------
 plugins/lua/Makefile.am |    2 +-
 plugins/lua/lapi.cc     |   10 +-----
 plugins/lua/lua.cc      |   19 +++-------
 plugins/lua/lutil.cc    |   80 ++++++++++++++++++++++++++++++++++++++++++
 plugins/lua/lutil.h     |   25 +++++++++++++
 5 files changed, 112 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafficserver/blob/2835d03b/plugins/lua/Makefile.am
----------------------------------------------------------------------
diff --git a/plugins/lua/Makefile.am b/plugins/lua/Makefile.am
index 6cc863a..5806e16 100644
--- a/plugins/lua/Makefile.am
+++ b/plugins/lua/Makefile.am
@@ -27,7 +27,7 @@ AM_CXXFLAGS = \
 pkglib_LTLIBRARIES = lua.la
 
 lua_la_LIBADD = ${LUA_LIB}
-lua_la_SOURCES = lua.cc lapi.cc
+lua_la_SOURCES = lua.cc lapi.cc lutil.cc
 lua_la_LDFLAGS = -module -avoid-version -shared
 
 endif

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/2835d03b/plugins/lua/lapi.cc
----------------------------------------------------------------------
diff --git a/plugins/lua/lapi.cc b/plugins/lua/lapi.cc
index ad956ee..18d1bb6 100644
--- a/plugins/lua/lapi.cc
+++ b/plugins/lua/lapi.cc
@@ -20,6 +20,7 @@
 #include <ts/remap.h>
 #include <string.h>
 #include "lapi.h"
+#include "lutil.h"
 
 // Return the type name string for the given index.
 #define LTYPEOF(L, index) lua_typename(L, lua_type(L, index))
@@ -66,15 +67,6 @@ LuaRemapRequest::alloc(lua_State * lua)
   return rq;
 }
 
-static void
-LuaPushMetatable(lua_State * lua, const char * name, const luaL_Reg * exports)
-{
-  luaL_newmetatable(lua, name);
-  lua_pushvalue(lua, -1);
-  lua_setfield(lua, -2, "__index");
-  luaL_register(lua, NULL, exports);
-}
-
 // Given a URL table on the top of the stack, pop it's values into the URL buffer.
 bool
 LuaPopUrl(lua_State * lua, TSMBuffer buffer, TSMLoc url)

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/2835d03b/plugins/lua/lua.cc
----------------------------------------------------------------------
diff --git a/plugins/lua/lua.cc b/plugins/lua/lua.cc
index b13537c..fb75848 100644
--- a/plugins/lua/lua.cc
+++ b/plugins/lua/lua.cc
@@ -24,6 +24,7 @@
 #include <string>
 #include <vector>
 #include "lapi.h"
+#include "lutil.h"
 
 static pthread_key_t LuaStateKey;
 
@@ -40,19 +41,6 @@ struct LuaPluginState
   pathlist paths;
 };
 
-static void *
-LuaAllocate(void * ud, void * ptr, size_t osize, size_t nsize)
-{
-  TSReleaseAssert(ud == NULL);
-
-  if (nsize == 0) {
-    TSfree(ptr);
-    return NULL;
-  }
-
-  return TSrealloc(ptr, nsize);
-}
-
 static TSReturnCode
 LuaPluginInit(lua_State * lua)
 {
@@ -140,8 +128,10 @@ LuaPluginNewState(void)
     return NULL;
   }
 
-  luaL_openlibs(lua);
+//  LuaLoadLibraries(lua);
+//  LuaRegisterLibrary(lua, "ts", LuaApiInit);
 
+  luaL_openlibs(lua);
   // Pull up the preload table.
   lua_getglobal(lua, "package");
   lua_getfield(lua, -1, "preload");
@@ -156,6 +146,7 @@ LuaPluginNewState(void)
   // Pop the 'preload' table.
   lua_pop(lua, -1);
 
+
   return lua;
 }
 

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/2835d03b/plugins/lua/lutil.cc
----------------------------------------------------------------------
diff --git a/plugins/lua/lutil.cc b/plugins/lua/lutil.cc
new file mode 100644
index 0000000..b8241f1
--- /dev/null
+++ b/plugins/lua/lutil.cc
@@ -0,0 +1,80 @@
+/*
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you under the Apache License, Version 2.0 (the
+  "License"); you may not use this file except in compliance
+  with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+*/
+
+#include <ts/ts.h>
+
+extern "C" {
+#include <lua.h>
+#include <lualib.h>
+#include <lauxlib.h>
+}
+
+void *
+LuaAllocate(void * ud, void * ptr, size_t osize, size_t nsize)
+{
+  TSReleaseAssert(ud == NULL);
+
+  if (nsize == 0) {
+    TSfree(ptr);
+    return NULL;
+  }
+
+  return TSrealloc(ptr, nsize);
+}
+
+void
+LuaPushMetatable(lua_State * lua, const char * name, const luaL_Reg * exports)
+{
+  luaL_newmetatable(lua, name);
+  lua_pushvalue(lua, -1);
+  lua_setfield(lua, -2, "__index");
+  luaL_register(lua, NULL, exports);
+}
+
+void
+LuaRegisterLibrary(lua_State * lua, const char * name, lua_CFunction loader)
+{
+  // Pull up the preload table.
+  lua_getglobal(lua, "package");
+  lua_getfield(lua, -1, "preload");
+
+  lua_pushcfunction(lua, loader);
+  lua_setfield(lua, -2, name);
+
+  // Pop the 'package' and 'preload' tables.
+  lua_pop(lua, 2);
+}
+
+void
+LuaLoadLibraries(lua_State * lua)
+{
+#define REGISTER_LIBRARY(name) LuaRegisterLibrary(lua, #name, luaopen_ ## name)
+
+    lua_cpcall(lua, luaopen_base, NULL);
+    lua_cpcall(lua, luaopen_package, NULL);
+
+    REGISTER_LIBRARY(io);
+    REGISTER_LIBRARY(os);
+    REGISTER_LIBRARY(table);
+    REGISTER_LIBRARY(string);
+    REGISTER_LIBRARY(math);
+    REGISTER_LIBRARY(debug);
+
+#undef REGISTER_LIBRARY
+}
+

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/2835d03b/plugins/lua/lutil.h
----------------------------------------------------------------------
diff --git a/plugins/lua/lutil.h b/plugins/lua/lutil.h
new file mode 100644
index 0000000..d6e24a6
--- /dev/null
+++ b/plugins/lua/lutil.h
@@ -0,0 +1,25 @@
+/*
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you under the Apache License, Version 2.0 (the
+  "License"); you may not use this file except in compliance
+  with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+*/
+
+// Return the type name string for the given index.
+#define LTYPEOF(L, index) lua_typename(L, lua_type(L, index))
+
+void * LuaAllocate(void * ud, void * ptr, size_t osize, size_t nsize);
+void LuaPushMetatable(lua_State * lua, const char * name, const luaL_Reg * exports);
+void LuaLoadLibraries(lua_State * lua);
+void LuaRegisterLibrary(lua_State * lua, const char * name, lua_CFunction loader);