From 1ab6fac9197b194ea94166dff7ad81579f6113ed Mon Sep 17 00:00:00 2001 From: Bruno Silvestre Date: Thu, 12 Feb 2015 16:32:54 -0200 Subject: [PATCH] Don't set globals from C. --- src/config.h | 6 +++++- src/context.c | 21 +-------------------- src/ssl.c | 34 +--------------------------------- src/x509.c | 23 +---------------------- 4 files changed, 8 insertions(+), 76 deletions(-) diff --git a/src/config.h b/src/config.h index 43fe5e2..5b6b47d 100644 --- a/src/config.h +++ b/src/config.h @@ -14,7 +14,11 @@ #endif #if (LUA_VERSION_NUM == 501) -#define lua_rawlen(L, i) lua_objlen(L, i) +#define setfuncs(L, R) luaL_register(L, NULL, R) +#define lua_rawlen(L, i) lua_objlen(L, i) +#define luaL_newlib(L, R) do { lua_newtable(L); luaL_register(L, NULL, R); } while(0) +#else +#define setfuncs(L, R) luaL_setfuncs(L, R, 0) #endif #endif diff --git a/src/context.c b/src/context.c index 5925818..6a295e1 100644 --- a/src/context.c +++ b/src/context.c @@ -723,30 +723,12 @@ int lsec_getmode(lua_State *L, int idx) /** * Registre the module. */ -#if (LUA_VERSION_NUM == 501) LSEC_API int luaopen_ssl_context(lua_State *L) { luaL_newmetatable(L, "SSL:DH:Registry"); /* Keep all DH callbacks */ luaL_newmetatable(L, "SSL:Verify:Registry"); /* Keep all verify flags */ luaL_newmetatable(L, "SSL:Context"); - luaL_register(L, NULL, meta); - - /* Create __index metamethods for context */ - lua_newtable(L); - luaL_register(L, NULL, meta_index); - lua_setfield(L, -2, "__index"); - - /* Register the module */ - luaL_register(L, "ssl.context", funcs); - return 1; -} -#else -LSEC_API int luaopen_ssl_context(lua_State *L) -{ - luaL_newmetatable(L, "SSL:DH:Registry"); /* Keep all DH callbacks */ - luaL_newmetatable(L, "SSL:Verify:Registry"); /* Keep all verify flags */ - luaL_newmetatable(L, "SSL:Context"); - luaL_setfuncs(L, meta, 0); + setfuncs(L, meta); /* Create __index metamethods for context */ luaL_newlib(L, meta_index); @@ -757,4 +739,3 @@ LSEC_API int luaopen_ssl_context(lua_State *L) return 1; } -#endif diff --git a/src/ssl.c b/src/ssl.c index 464e65a..1780ccc 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -816,7 +816,6 @@ static luaL_Reg funcs[] = { /** * Initialize modules. */ -#if (LUA_VERSION_NUM == 501) LSEC_API int luaopen_ssl_core(lua_State *L) { /* Initialize SSL */ @@ -836,37 +835,7 @@ LSEC_API int luaopen_ssl_core(lua_State *L) /* Register the functions and tables */ luaL_newmetatable(L, "SSL:Connection"); - luaL_register(L, NULL, meta); - - lua_newtable(L); - luaL_register(L, NULL, methods); - lua_setfield(L, -2, "__index"); - - luaL_register(L, "ssl.core", funcs); - - return 1; -} -#else -LSEC_API int luaopen_ssl_core(lua_State *L) -{ - /* Initialize SSL */ - if (!SSL_library_init()) { - lua_pushstring(L, "unable to initialize SSL library"); - lua_error(L); - } - OpenSSL_add_all_algorithms(); - SSL_load_error_strings(); - -#if defined(WITH_LUASOCKET) - /* Initialize internal library */ - socket_open(); -#endif - - luaL_newmetatable(L, "SSL:SNI:Registry"); - - /* Register the functions and tables */ - luaL_newmetatable(L, "SSL:Connection"); - luaL_setfuncs(L, meta, 0); + setfuncs(L, meta); luaL_newlib(L, methods); lua_setfield(L, -2, "__index"); @@ -875,4 +844,3 @@ LSEC_API int luaopen_ssl_core(lua_State *L) return 1; } -#endif diff --git a/src/x509.c b/src/x509.c index eb1b6f6..02e858f 100644 --- a/src/x509.c +++ b/src/x509.c @@ -566,30 +566,11 @@ static luaL_Reg funcs[] = { /*--------------------------------------------------------------------------*/ -#if (LUA_VERSION_NUM == 501) - LSEC_API int luaopen_ssl_x509(lua_State *L) { /* Register the functions and tables */ luaL_newmetatable(L, "SSL:Certificate"); - luaL_register(L, NULL, meta); - - lua_newtable(L); - luaL_register(L, NULL, methods); - lua_setfield(L, -2, "__index"); - - luaL_register(L, "ssl.x509", funcs); - - return 1; -} - -#else - -LSEC_API int luaopen_ssl_x509(lua_State *L) -{ - /* Register the functions and tables */ - luaL_newmetatable(L, "SSL:Certificate"); - luaL_setfuncs(L, meta, 0); + setfuncs(L, meta); luaL_newlib(L, methods); lua_setfield(L, -2, "__index"); @@ -598,5 +579,3 @@ LSEC_API int luaopen_ssl_x509(lua_State *L) return 1; } - -#endif