From 79e6c4915d267e149e1f3b134901bf355d439c15 Mon Sep 17 00:00:00 2001 From: Diego Nehab Date: Wed, 29 May 2013 16:56:56 +0800 Subject: [PATCH] Export global only if LUA_COMPAT_MODULE defined. --- src/except.c | 4 ++++ src/inet.c | 4 ++++ src/luasocket.c | 25 +++++-------------------- src/makefile | 12 ++++++++---- src/mime.c | 8 ++------ src/select.c | 4 ++++ src/serial.c | 12 ++++++++---- src/tcp.c | 4 ++++ src/timeout.c | 4 ++++ src/udp.c | 4 ++++ src/unix.c | 11 ++++++++--- 11 files changed, 55 insertions(+), 37 deletions(-) diff --git a/src/except.c b/src/except.c index 1d1ade0..002e701 100644 --- a/src/except.c +++ b/src/except.c @@ -92,6 +92,10 @@ static int global_protect(lua_State *L) { * Init module \*-------------------------------------------------------------------------*/ int except_open(lua_State *L) { +#if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE) + luaL_setfuncs(L, func, 0); +#else luaL_openlib(L, NULL, func, 0); +#endif return 0; } diff --git a/src/inet.c b/src/inet.c index 1c44464..35bc438 100644 --- a/src/inet.c +++ b/src/inet.c @@ -40,7 +40,11 @@ int inet_open(lua_State *L) { lua_pushstring(L, "dns"); lua_newtable(L); +#if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE) + luaL_setfuncs(L, func, 0); +#else luaL_openlib(L, NULL, func, 0); +#endif lua_settable(L, -3); return 0; } diff --git a/src/luasocket.c b/src/luasocket.c index bea23d7..e6ee747 100644 --- a/src/luasocket.c +++ b/src/luasocket.c @@ -79,31 +79,11 @@ static int global_unload(lua_State *L) { } #if LUA_VERSION_NUM > 501 - int luaL_typerror (lua_State *L, int narg, const char *tname) { const char *msg = lua_pushfstring(L, "%s expected, got %s", tname, luaL_typename(L, narg)); return luaL_argerror(L, narg, msg); } - -#if ! defined(LUA_COMPAT_MODULE) -void luaL_openlib(lua_State *L, const char *name, const luaL_Reg *funcs, int idx) { - if (name != NULL) { -#ifdef LUASOCKET_USE_GLOBAL - lua_getglobal(L,name); - if (lua_isnil(L,-1)) { - lua_newtable(L); - lua_setglobal(L,name); - lua_getglobal(L,name); - } -#else - lua_newtable(L); -#endif - } - luaL_setfuncs(L,funcs,0); -} -#endif - #endif /*-------------------------------------------------------------------------*\ @@ -112,7 +92,12 @@ void luaL_openlib(lua_State *L, const char *name, const luaL_Reg *funcs, int idx static int base_open(lua_State *L) { if (socket_open()) { /* export functions (and leave namespace table on top of stack) */ +#if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE) + lua_newtable(L); + luaL_setfuncs(L, func, 0); +#else luaL_openlib(L, "socket", func, 0); +#endif #ifdef LUASOCKET_DEBUG lua_pushstring(L, "_DEBUG"); lua_pushboolean(L, 1); diff --git a/src/makefile b/src/makefile index 94a2f9f..42589f6 100644 --- a/src/makefile +++ b/src/makefile @@ -25,6 +25,10 @@ LUAV?=5.1 # for testing and debugging luasocket itself DEBUG?=NODEBUG +# COMPAT: COMPAT NOCOMPAT +# when compiling for 5.2, use LUA_COMPAT_MODULE +COMPAT?=NOCOMPAT + # where lua headers are found for macosx builds # LUAINC_macosx: # /opt/local/include @@ -125,7 +129,7 @@ PLATS= macosx linux win32 mingw SO_macosx=so O_macosx=o CC_macosx=gcc -DEF_macosx= -DLUASOCKET_$(DEBUG) -DUNIX_HAS_SUN_LEN -DLUA_COMPAT_MODULE \ +DEF_macosx= -DLUASOCKET_$(DEBUG) -DUNIX_HAS_SUN_LEN -DLUA_$(COMPAT)_MODULE \ -DLUASOCKET_API='__attribute__((visibility("default")))' \ -DMIME_API='__attribute__((visibility("default")))' CFLAGS_macosx= -I$(LUAINC) $(DEF) -pedantic -Wall -O2 -fno-common \ @@ -140,7 +144,7 @@ SOCKET_macosx=usocket.o SO_linux=so O_linux=o CC_linux=gcc -DEF_linux=-DLUASOCKET_$(DEBUG) -DLUA_COMPAT_MODULE \ +DEF_linux=-DLUASOCKET_$(DEBUG) -DLUA_$(COMPAT)_MODULE \ -DLUASOCKET_API='__attribute__((visibility("default")))' \ -DMIME_API='__attribute__((visibility("default")))' CFLAGS_linux= -I$(LUAINC) $(DEF) -pedantic -Wall -Wshadow -Wextra \ @@ -155,7 +159,7 @@ SOCKET_linux=usocket.o SO_mingw=dll O_mingw=o CC_mingw=gcc -DEF_mingw= -DLUASOCKET_INET_PTON -DLUASOCKET_$(DEBUG) -DLUA_COMPAT_MODULE \ +DEF_mingw= -DLUASOCKET_INET_PTON -DLUASOCKET_$(DEBUG) -DLUA_$(COMPAT)_MODULE \ -DWINVER=0x0501 -DLUASOCKET_API='__declspec(dllexport)' \ -DMIME_API='__declspec(dllexport)' CFLAGS_mingw= -I$(LUAINC) $(DEF) -pedantic -Wall -O2 -fno-common \ @@ -173,7 +177,7 @@ O_win32=obj CC_win32=cl DEF_win32= //D "WIN32" //D "NDEBUG" //D "_WINDOWS" //D "_USRDLL" \ //D "LUASOCKET_API=__declspec(dllexport)" //D "_CRT_SECURE_NO_WARNINGS" \ - //D "_WINDLL" //D "LUA_COMPAT_MODULE" \ + //D "_WINDLL" //D "LUA_$(COMPAT)_MODULE" \ //D "MIME_API=__declspec(dllexport)" \ //D "LUASOCKET_$(DEBUG)" CFLAGS_win32=//I "$(LUAINC)" $(DEF) //O2 //Ot //MD //W3 //nologo diff --git a/src/mime.c b/src/mime.c index ea8876e..dd37dcf 100644 --- a/src/mime.c +++ b/src/mime.c @@ -81,13 +81,9 @@ static UC b64unbase[256]; \*-------------------------------------------------------------------------*/ MIME_API int luaopen_mime_core(lua_State *L) { -#if LUA_VERSION_NUM > 501 +#if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE) lua_newtable(L); -#ifdef LUASOCKET_USE_GLOBAL - lua_setglobal(L,"mime"); - lua_getglobal(L,"mime"); -#endif - luaL_setfuncs(L,func,0); + luaL_setfuncs(L, func, 0); #else luaL_openlib(L, "mime", func, 0); #endif diff --git a/src/select.c b/src/select.c index 51fb198..fafaa62 100644 --- a/src/select.c +++ b/src/select.c @@ -40,7 +40,11 @@ int select_open(lua_State *L) { lua_pushstring(L, "_SETSIZE"); lua_pushnumber(L, FD_SETSIZE); lua_rawset(L, -3); +#if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE) + luaL_setfuncs(L, func, 0); +#else luaL_openlib(L, NULL, func, 0); +#endif return 0; } diff --git a/src/serial.c b/src/serial.c index 5b09e76..66e8da6 100644 --- a/src/serial.c +++ b/src/serial.c @@ -55,6 +55,8 @@ static luaL_Reg serial_methods[] = { }; /* 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[] = { {"serial", global_create}, {NULL, NULL} @@ -69,11 +71,13 @@ LUASOCKET_API int luaopen_socket_serial(lua_State *L) { auxiliar_newclass(L, "serial{client}", serial_methods); /* create class groups */ auxiliar_add2group(L, "serial{client}", "serial{any}"); - /* make sure the function ends up in the package table */ +#if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE) + lua_pushcfunction(L, global_create); +#else + /* set function into socket namespace */ luaL_openlib(L, "socket", func, 0); - /* return the function instead of the 'socket' table */ - lua_pushstring(L, "serial"); - lua_gettable(L, -2); + lua_pushcfunction(L, global_create); +#endif return 1; } diff --git a/src/tcp.c b/src/tcp.c index 60c1e8a..efb92c9 100644 --- a/src/tcp.c +++ b/src/tcp.c @@ -107,7 +107,11 @@ int tcp_open(lua_State *L) auxiliar_add2group(L, "tcp{client}", "tcp{any}"); auxiliar_add2group(L, "tcp{server}", "tcp{any}"); /* define library functions */ +#if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE) + luaL_setfuncs(L, func, 0); +#else luaL_openlib(L, NULL, func, 0); +#endif return 0; } diff --git a/src/timeout.c b/src/timeout.c index c7354b5..bdd5e1c 100644 --- a/src/timeout.c +++ b/src/timeout.c @@ -144,7 +144,11 @@ double timeout_gettime(void) { * Initializes module \*-------------------------------------------------------------------------*/ int timeout_open(lua_State *L) { +#if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE) + luaL_setfuncs(L, func, 0); +#else luaL_openlib(L, NULL, func, 0); +#endif return 0; } diff --git a/src/udp.c b/src/udp.c index d48f6fe..ec805b6 100644 --- a/src/udp.c +++ b/src/udp.c @@ -109,7 +109,11 @@ int udp_open(lua_State *L) auxiliar_add2group(L, "udp{connected}", "select{able}"); auxiliar_add2group(L, "udp{unconnected}", "select{able}"); /* define library functions */ +#if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE) + luaL_setfuncs(L, func, 0); +#else luaL_openlib(L, NULL, func, 0); +#endif return 0; } diff --git a/src/unix.c b/src/unix.c index 73e7b69..5710c27 100644 --- a/src/unix.c +++ b/src/unix.c @@ -69,6 +69,8 @@ static t_opt optset[] = { }; /* 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} @@ -87,11 +89,14 @@ int luaopen_socket_unix(lua_State *L) { auxiliar_add2group(L, "unix{master}", "unix{any}"); auxiliar_add2group(L, "unix{client}", "unix{any}"); auxiliar_add2group(L, "unix{server}", "unix{any}"); - /* make sure the function ends up in the package table */ +#if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE) + lua_pushcfunction(L, global_create); +#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 */ - lua_pushstring(L, "unix"); - lua_gettable(L, -2); return 1; }