Browse Source

Do not use `lua_pushglobaltable` in Lua 5.1

See comment on #194
bel 9 years ago
parent
commit
d082320ba0
4 changed files with 32 additions and 14 deletions
  1. 3 1
      format.bat
  2. 0 1
      src/civetweb_private_lua.h
  3. 15 6
      src/mod_lua.inl
  4. 14 6
      src/third_party/civetweb_lua.h

+ 3 - 1
format.bat

@@ -1,11 +1,13 @@
 clang-format -i src/civetweb.c
 clang-format -i src/main.c
 clang-format -i src/CivetServer.cpp
-clang-format -i src/lua_civet.h
+clang-format -i src/civetweb_private_lua.h
 clang-format -i src/md5.inl
 clang-format -i src/mod_lua.inl
 clang-format -i src/timer.inl
 
+clang-format -i src/third_party/civetweb_lua.h
+
 clang-format -i include/civetweb.h
 clang-format -i include/CivetServer.h
 

+ 0 - 1
src/civetweb_private_lua.h

@@ -8,4 +8,3 @@
 void civetweb_open_lua_libs(lua_State *L);
 
 #endif
-

+ 15 - 6
src/mod_lua.inl

@@ -343,8 +343,8 @@ static int lsp(struct mg_connection *conn,
 						data.begin = p + (i + 3);
 						data.len = j - (i + 3);
 						data.state = 0;
-						lua_ok =
-						    mg_lua_load(L, lsp_var_reader, &data, chunkname, NULL);
+						lua_ok = mg_lua_load(
+						    L, lsp_var_reader, &data, chunkname, NULL);
 					} else {
 						lua_ok = luaL_loadbuffer(
 						    L, p + (i + 2), j - (i + 2), chunkname);
@@ -1146,10 +1146,10 @@ static void prepare_lua_environment(struct mg_context *ctx,
 	civetweb_open_lua_libs(L);
 
 #if LUA_VERSION_NUM == 502
-        /* Keep the "connect" method for compatibility, 
-         * but do not backport it to Lua 5.1.
-         * TODO: Redesign the interface.
-         */
+	/* Keep the "connect" method for compatibility,
+	 * but do not backport it to Lua 5.1.
+	 * TODO: Redesign the interface.
+	 */
 	luaL_newmetatable(L, LUASOCKET);
 	lua_pushliteral(L, "__index");
 	luaL_newlib(L, luasocket_methods);
@@ -1314,6 +1314,7 @@ static void mg_exec_lua_script(struct mg_connection *conn,
 		lua_pushcclosure(L, &lua_error_handler, 0);
 
 		if (exports != NULL) {
+#if LUA_VERSION_NUM > 501
 			lua_pushglobaltable(L);
 			for (i = 0; exports[i] != NULL && exports[i + 1] != NULL; i += 2) {
 				lua_CFunction func;
@@ -1322,6 +1323,14 @@ static void mg_exec_lua_script(struct mg_connection *conn,
 				lua_pushcclosure(L, func, 0);
 				lua_rawset(L, -3);
 			}
+#else
+			for (i = 0; exports[i] != NULL && exports[i + 1] != NULL; i += 2) {
+				lua_CFunction func;
+				const char *name = (const char *)(exports[i]));
+				*(const void **)(&func) = exports[i + 1];
+				lua_register(L, name, func);
+			}
+#endif
 		}
 
 		if (luaL_loadfile(L, path) != 0) {

+ 14 - 6
src/third_party/civetweb_lua.h

@@ -19,9 +19,9 @@
  * THE SOFTWARE.
  */
 
- /* This header is intended to support Lua 5.1, Lua 5.2 and Lua 5.3 in the same
-  * C source code.
-  */
+/* This header is intended to support Lua 5.1, Lua 5.2 and Lua 5.3 in the same
+ * C source code.
+ */
 
 #ifndef CIVETWEB_LUA_H
 #define CIVETWEB_LUA_H
@@ -38,11 +38,19 @@
 /* Lua 5.1 detected */
 #define LUA_OK 0
 #define LUA_ERRGCMM 999 /* not supported */
-#define mg_lua_load(a,b,c,d,e) lua_load(a,b,c,d)
+#define mg_lua_load(a, b, c, d, e) lua_load(a, b, c, d)
 #define lua_rawlen lua_objlen
-#define lua_newstate(a, b) luaL_newstate() /* Must use luaL_newstate() for 64 bit target */
+#define lua_newstate(a, b)                                                     \
+	luaL_newstate() /* Must use luaL_newstate() for 64 bit target */
 #define lua_pushinteger lua_pushnumber
-#define luaL_newlib(L, t) {luaL_Reg const *r = t; while (r->name) {lua_register(L, r->name, r->func); r++;}}
+#define luaL_newlib(L, t)                                                      \
+	{                                                                          \
+		luaL_Reg const *r = t;                                                 \
+		while (r->name) {                                                      \
+			lua_register(L, r->name, r->func);                                 \
+			r++;                                                               \
+		}                                                                      \
+	}
 #define luaL_setfuncs(L, r, u) lua_register(L, r->name, r->func)