Export global only if LUA_COMPAT_MODULE defined.

This commit is contained in:
Diego Nehab 2013-05-29 16:56:56 +08:00
parent 5167ddaf49
commit 79e6c4915d
11 changed files with 55 additions and 37 deletions

View File

@ -92,6 +92,10 @@ 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);
#else
luaL_openlib(L, NULL, func, 0); luaL_openlib(L, NULL, func, 0);
#endif
return 0; return 0;
} }

View File

@ -40,7 +40,11 @@ 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);
#else
luaL_openlib(L, NULL, func, 0); luaL_openlib(L, NULL, func, 0);
#endif
lua_settable(L, -3); lua_settable(L, -3);
return 0; return 0;
} }

View File

@ -79,31 +79,11 @@ static int global_unload(lua_State *L) {
} }
#if LUA_VERSION_NUM > 501 #if LUA_VERSION_NUM > 501
int luaL_typerror (lua_State *L, int narg, const char *tname) { int luaL_typerror (lua_State *L, int narg, const char *tname) {
const char *msg = lua_pushfstring(L, "%s expected, got %s", const char *msg = lua_pushfstring(L, "%s expected, got %s",
tname, luaL_typename(L, narg)); tname, luaL_typename(L, narg));
return luaL_argerror(L, narg, msg); 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 #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) { 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);
luaL_setfuncs(L, func, 0);
#else
luaL_openlib(L, "socket", func, 0); 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

@ -25,6 +25,10 @@ LUAV?=5.1
# for testing and debugging luasocket itself # for testing and debugging luasocket itself
DEBUG?=NODEBUG DEBUG?=NODEBUG
# COMPAT: COMPAT NOCOMPAT
# when compiling for 5.2, use LUA_COMPAT_MODULE
COMPAT?=NOCOMPAT
# where lua headers are found for macosx builds # where lua headers are found for macosx builds
# LUAINC_macosx: # LUAINC_macosx:
# /opt/local/include # /opt/local/include
@ -125,7 +129,7 @@ PLATS= macosx linux win32 mingw
SO_macosx=so SO_macosx=so
O_macosx=o O_macosx=o
CC_macosx=gcc 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")))' \ -DLUASOCKET_API='__attribute__((visibility("default")))' \
-DMIME_API='__attribute__((visibility("default")))' -DMIME_API='__attribute__((visibility("default")))'
CFLAGS_macosx= -I$(LUAINC) $(DEF) -pedantic -Wall -O2 -fno-common \ CFLAGS_macosx= -I$(LUAINC) $(DEF) -pedantic -Wall -O2 -fno-common \
@ -140,7 +144,7 @@ SOCKET_macosx=usocket.o
SO_linux=so SO_linux=so
O_linux=o O_linux=o
CC_linux=gcc CC_linux=gcc
DEF_linux=-DLUASOCKET_$(DEBUG) -DLUA_COMPAT_MODULE \ DEF_linux=-DLUASOCKET_$(DEBUG) -DLUA_$(COMPAT)_MODULE \
-DLUASOCKET_API='__attribute__((visibility("default")))' \ -DLUASOCKET_API='__attribute__((visibility("default")))' \
-DMIME_API='__attribute__((visibility("default")))' -DMIME_API='__attribute__((visibility("default")))'
CFLAGS_linux= -I$(LUAINC) $(DEF) -pedantic -Wall -Wshadow -Wextra \ CFLAGS_linux= -I$(LUAINC) $(DEF) -pedantic -Wall -Wshadow -Wextra \
@ -155,7 +159,7 @@ SOCKET_linux=usocket.o
SO_mingw=dll SO_mingw=dll
O_mingw=o O_mingw=o
CC_mingw=gcc 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)' \ -DWINVER=0x0501 -DLUASOCKET_API='__declspec(dllexport)' \
-DMIME_API='__declspec(dllexport)' -DMIME_API='__declspec(dllexport)'
CFLAGS_mingw= -I$(LUAINC) $(DEF) -pedantic -Wall -O2 -fno-common \ CFLAGS_mingw= -I$(LUAINC) $(DEF) -pedantic -Wall -O2 -fno-common \
@ -173,7 +177,7 @@ O_win32=obj
CC_win32=cl CC_win32=cl
DEF_win32= //D "WIN32" //D "NDEBUG" //D "_WINDOWS" //D "_USRDLL" \ DEF_win32= //D "WIN32" //D "NDEBUG" //D "_WINDOWS" //D "_USRDLL" \
//D "LUASOCKET_API=__declspec(dllexport)" //D "_CRT_SECURE_NO_WARNINGS" \ //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 "MIME_API=__declspec(dllexport)" \
//D "LUASOCKET_$(DEBUG)" //D "LUASOCKET_$(DEBUG)"
CFLAGS_win32=//I "$(LUAINC)" $(DEF) //O2 //Ot //MD //W3 //nologo CFLAGS_win32=//I "$(LUAINC)" $(DEF) //O2 //Ot //MD //W3 //nologo

View File

@ -81,12 +81,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 #if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE)
lua_newtable(L); 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 #else
luaL_openlib(L, "mime", func, 0); luaL_openlib(L, "mime", func, 0);

View File

@ -40,7 +40,11 @@ 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);
#else
luaL_openlib(L, NULL, func, 0); luaL_openlib(L, NULL, func, 0);
#endif
return 0; return 0;
} }

View File

@ -55,6 +55,8 @@ static luaL_Reg serial_methods[] = {
}; };
/* our socket creation function */ /* 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[] = { static luaL_Reg func[] = {
{"serial", global_create}, {"serial", global_create},
{NULL, NULL} {NULL, NULL}
@ -69,11 +71,13 @@ LUASOCKET_API int luaopen_socket_serial(lua_State *L) {
auxiliar_newclass(L, "serial{client}", serial_methods); auxiliar_newclass(L, "serial{client}", serial_methods);
/* create class groups */ /* create class groups */
auxiliar_add2group(L, "serial{client}", "serial{any}"); 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); luaL_openlib(L, "socket", func, 0);
/* return the function instead of the 'socket' table */ lua_pushcfunction(L, global_create);
lua_pushstring(L, "serial"); #endif
lua_gettable(L, -2);
return 1; return 1;
} }

View File

@ -107,7 +107,11 @@ 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);
#else
luaL_openlib(L, NULL, func, 0); luaL_openlib(L, NULL, func, 0);
#endif
return 0; return 0;
} }

View File

@ -144,7 +144,11 @@ 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);
#else
luaL_openlib(L, NULL, func, 0); luaL_openlib(L, NULL, func, 0);
#endif
return 0; return 0;
} }

View File

@ -109,7 +109,11 @@ 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);
#else
luaL_openlib(L, NULL, func, 0); luaL_openlib(L, NULL, func, 0);
#endif
return 0; return 0;
} }

View File

@ -69,6 +69,8 @@ static t_opt optset[] = {
}; };
/* our socket creation function */ /* 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[] = { static luaL_Reg func[] = {
{"unix", global_create}, {"unix", global_create},
{NULL, NULL} {NULL, NULL}
@ -87,11 +89,14 @@ 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}");
/* 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); 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_pushstring(L, "unix");
lua_gettable(L, -2);
return 1; return 1;
} }