New compat.h module implements luaL_setfuncs.

Makes initialization code simpler everywhere.
This commit is contained in:
Diego Nehab 2015-08-21 15:39:34 -03:00
parent 321c0c9b1f
commit e75444ccd1
20 changed files with 164 additions and 173 deletions

View File

@ -31,6 +31,7 @@
#include "lua.h" #include "lua.h"
#include "lauxlib.h" #include "lauxlib.h"
#include "compat.h"
int auxiliar_open(lua_State *L); int auxiliar_open(lua_State *L);
void auxiliar_newclass(lua_State *L, const char *classname, luaL_Reg *func); void auxiliar_newclass(lua_State *L, const char *classname, luaL_Reg *func);

View File

@ -4,6 +4,7 @@
\*=========================================================================*/ \*=========================================================================*/
#include "lua.h" #include "lua.h"
#include "lauxlib.h" #include "lauxlib.h"
#include "compat.h"
#include "buffer.h" #include "buffer.h"

19
src/compat.c Normal file
View File

@ -0,0 +1,19 @@
#include "compat.h"
#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM==501
/*
** Adapted from Lua 5.2
*/
void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
luaL_checkstack(L, nup+1, "too many upvalues");
for (; l->name != NULL; l++) { /* fill the table with given functions */
int i;
lua_pushstring(L, l->name);
for (i = 0; i < nup; i++) /* copy upvalues to the top */
lua_pushvalue(L, -(nup+1));
lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
lua_settable(L, -(nup + 3));
}
lua_pop(L, nup); /* remove upvalues */
}
#endif

11
src/compat.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef COMPAT_H
#define COMPAT_H
#include "lua.h"
#include "lauxlib.h"
#if LUA_VERSION_NUM==501
void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup);
#endif
#endif

View File

@ -6,6 +6,7 @@
#include "lua.h" #include "lua.h"
#include "lauxlib.h" #include "lauxlib.h"
#include "compat.h"
#include "except.h" #include "except.h"
@ -117,10 +118,6 @@ static int global_protect(lua_State *L) {
* Init module * Init module
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
int except_open(lua_State *L) { int except_open(lua_State *L) {
#if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE)
luaL_setfuncs(L, func, 0); luaL_setfuncs(L, func, 0);
#else
luaL_openlib(L, NULL, func, 0);
#endif
return 0; return 0;
} }

View File

@ -8,6 +8,7 @@
#include "lua.h" #include "lua.h"
#include "lauxlib.h" #include "lauxlib.h"
#include "compat.h"
#include "inet.h" #include "inet.h"
@ -41,11 +42,7 @@ int inet_open(lua_State *L)
{ {
lua_pushstring(L, "dns"); lua_pushstring(L, "dns");
lua_newtable(L); lua_newtable(L);
#if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE)
luaL_setfuncs(L, func, 0); luaL_setfuncs(L, func, 0);
#else
luaL_openlib(L, NULL, func, 0);
#endif
lua_settable(L, -3); lua_settable(L, -3);
return 0; return 0;
} }

View File

@ -17,6 +17,7 @@
\*=========================================================================*/ \*=========================================================================*/
#include "lua.h" #include "lua.h"
#include "lauxlib.h" #include "lauxlib.h"
#include "compat.h"
/*=========================================================================*\ /*=========================================================================*\
* LuaSocket includes * LuaSocket includes
@ -83,12 +84,8 @@ static int global_unload(lua_State *L) {
static int base_open(lua_State *L) { static int base_open(lua_State *L) {
if (socket_open()) { if (socket_open()) {
/* export functions (and leave namespace table on top of stack) */ /* export functions (and leave namespace table on top of stack) */
#if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE)
lua_newtable(L); lua_newtable(L);
luaL_setfuncs(L, func, 0); luaL_setfuncs(L, func, 0);
#else
luaL_openlib(L, "socket", func, 0);
#endif
#ifdef LUASOCKET_DEBUG #ifdef LUASOCKET_DEBUG
lua_pushstring(L, "_DEBUG"); lua_pushstring(L, "_DEBUG");
lua_pushboolean(L, 1); lua_pushboolean(L, 1);

View File

@ -260,6 +260,7 @@ SOCKET_OBJS= \
buffer.$(O) \ buffer.$(O) \
io.$(O) \ io.$(O) \
auxiliar.$(O) \ auxiliar.$(O) \
compat.$(O) \
options.$(O) \ options.$(O) \
inet.$(O) \ inet.$(O) \
$(SOCKET) \ $(SOCKET) \
@ -272,7 +273,8 @@ SOCKET_OBJS= \
# Modules belonging mime-core # Modules belonging mime-core
# #
MIME_OBJS= \ MIME_OBJS= \
mime.$(O) mime.$(O) \
compat.$(O)
#------ #------
# Modules belonging unix (local domain sockets) # Modules belonging unix (local domain sockets)
@ -383,6 +385,7 @@ clean:
#------ #------
# List of dependencies # List of dependencies
# #
compat.$(O): compat.c compat.h
auxiliar.$(O): auxiliar.c auxiliar.h auxiliar.$(O): auxiliar.c auxiliar.h
buffer.$(O): buffer.c buffer.h io.h timeout.h buffer.$(O): buffer.c buffer.h io.h timeout.h
except.$(O): except.c except.h except.$(O): except.c except.h

View File

@ -6,10 +6,7 @@
#include "lua.h" #include "lua.h"
#include "lauxlib.h" #include "lauxlib.h"
#include "compat.h"
#if !defined(LUA_VERSION_NUM) || (LUA_VERSION_NUM < 501)
#include "compat-5.1.h"
#endif
#include "mime.h" #include "mime.h"
@ -81,12 +78,8 @@ static UC b64unbase[256];
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
MIME_API int luaopen_mime_core(lua_State *L) MIME_API int luaopen_mime_core(lua_State *L)
{ {
#if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE)
lua_newtable(L); lua_newtable(L);
luaL_setfuncs(L, func, 0); luaL_setfuncs(L, func, 0);
#else
luaL_openlib(L, "mime", func, 0);
#endif
/* make version string available to scripts */ /* make version string available to scripts */
lua_pushstring(L, "_VERSION"); lua_pushstring(L, "_VERSION");
lua_pushstring(L, MIME_VERSION); lua_pushstring(L, MIME_VERSION);

View File

@ -6,6 +6,7 @@
#include "lua.h" #include "lua.h"
#include "lauxlib.h" #include "lauxlib.h"
#include "compat.h"
#include "socket.h" #include "socket.h"
#include "timeout.h" #include "timeout.h"
@ -40,11 +41,7 @@ int select_open(lua_State *L) {
lua_pushstring(L, "_SETSIZE"); lua_pushstring(L, "_SETSIZE");
lua_pushnumber(L, FD_SETSIZE); lua_pushnumber(L, FD_SETSIZE);
lua_rawset(L, -3); lua_rawset(L, -3);
#if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE)
luaL_setfuncs(L, func, 0); luaL_setfuncs(L, func, 0);
#else
luaL_openlib(L, NULL, func, 0);
#endif
return 0; return 0;
} }

View File

@ -6,6 +6,7 @@
#include "lua.h" #include "lua.h"
#include "lauxlib.h" #include "lauxlib.h"
#include "compat.h"
#include "auxiliar.h" #include "auxiliar.h"
#include "socket.h" #include "socket.h"
@ -108,11 +109,7 @@ int tcp_open(lua_State *L)
auxiliar_add2group(L, "tcp{client}", "tcp{any}"); auxiliar_add2group(L, "tcp{client}", "tcp{any}");
auxiliar_add2group(L, "tcp{server}", "tcp{any}"); auxiliar_add2group(L, "tcp{server}", "tcp{any}");
/* define library functions */ /* define library functions */
#if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE)
luaL_setfuncs(L, func, 0); luaL_setfuncs(L, func, 0);
#else
luaL_openlib(L, NULL, func, 0);
#endif
return 0; return 0;
} }

View File

@ -8,6 +8,7 @@
#include "lua.h" #include "lua.h"
#include "lauxlib.h" #include "lauxlib.h"
#include "compat.h"
#include "auxiliar.h" #include "auxiliar.h"
#include "timeout.h" #include "timeout.h"
@ -144,11 +145,7 @@ double timeout_gettime(void) {
* Initializes module * Initializes module
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
int timeout_open(lua_State *L) { int timeout_open(lua_State *L) {
#if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE)
luaL_setfuncs(L, func, 0); luaL_setfuncs(L, func, 0);
#else
luaL_openlib(L, NULL, func, 0);
#endif
return 0; return 0;
} }

View File

@ -7,6 +7,7 @@
#include "lua.h" #include "lua.h"
#include "lauxlib.h" #include "lauxlib.h"
#include "compat.h"
#include "auxiliar.h" #include "auxiliar.h"
#include "socket.h" #include "socket.h"
@ -120,11 +121,7 @@ int udp_open(lua_State *L)
auxiliar_add2group(L, "udp{connected}", "select{able}"); auxiliar_add2group(L, "udp{connected}", "select{able}");
auxiliar_add2group(L, "udp{unconnected}", "select{able}"); auxiliar_add2group(L, "udp{unconnected}", "select{able}");
/* define library functions */ /* define library functions */
#if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE)
luaL_setfuncs(L, func, 0); luaL_setfuncs(L, func, 0);
#else
luaL_openlib(L, NULL, func, 0);
#endif
return 0; return 0;
} }

View File

@ -68,15 +68,6 @@ static t_opt optset[] = {
{NULL, NULL} {NULL, NULL}
}; };
/* our socket creation function */
/* this is an ad-hoc module that returns a single function
* as such, do not include other functions in this array. */
static luaL_Reg func[] = {
{"unix", global_create},
{NULL, NULL}
};
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Initializes module * Initializes module
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
@ -89,15 +80,8 @@ int luaopen_socket_unix(lua_State *L) {
auxiliar_add2group(L, "unix{master}", "unix{any}"); auxiliar_add2group(L, "unix{master}", "unix{any}");
auxiliar_add2group(L, "unix{client}", "unix{any}"); auxiliar_add2group(L, "unix{client}", "unix{any}");
auxiliar_add2group(L, "unix{server}", "unix{any}"); auxiliar_add2group(L, "unix{server}", "unix{any}");
#if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE)
lua_pushcfunction(L, global_create);
(void) func;
#else
/* set function into socket namespace */
luaL_openlib(L, "socket", func, 0);
lua_pushcfunction(L, global_create);
#endif
/* return the function instead of the 'socket' table */ /* return the function instead of the 'socket' table */
lua_pushcfunction(L, global_create);
return 1; return 1;
} }