From d8cfb248939a9b6e1cbc9bb4c05b216daf9a0e3a Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Thu, 3 Apr 2014 16:23:52 +0200 Subject: moved compatibility code in a separate file --- lanes-3.9.4-1.rockspec | 2 +- src/compat.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/compat.h | 29 +++++++++++++++++++++++++++++ src/lanes.c | 1 + src/tools.c | 43 ------------------------------------------- src/tools.h | 25 ------------------------- 6 files changed, 75 insertions(+), 69 deletions(-) create mode 100644 src/compat.c create mode 100644 src/compat.h diff --git a/lanes-3.9.4-1.rockspec b/lanes-3.9.4-1.rockspec index fcee525..f5f7e83 100644 --- a/lanes-3.9.4-1.rockspec +++ b/lanes-3.9.4-1.rockspec @@ -58,7 +58,7 @@ build = { { ["lanes.core"] = { - sources = { "src/lanes.c", "src/keeper.c", "src/tools.c", "src/threading.c"}, + sources = { "src/compat.c", "src/lanes.c", "src/keeper.c", "src/tools.c", "src/threading.c"}, incdirs = { "src"}, }, lanes = "src/lanes.lua" diff --git a/src/compat.c b/src/compat.c new file mode 100644 index 0000000..1a66f38 --- /dev/null +++ b/src/compat.c @@ -0,0 +1,44 @@ +/* + * ############################################################################################### + * ######################################### Lua 5.1/5.2 ######################################### + * ############################################################################################### + */ +#include "compat.h" + +/* +** Copied from Lua 5.2 loadlib.c +*/ +#if LUA_VERSION_NUM == 501 +static int luaL_getsubtable (lua_State *L, int idx, const char *fname) +{ + lua_getfield(L, idx, fname); + if (lua_istable(L, -1)) + return 1; /* table already there */ + else + { + lua_pop(L, 1); /* remove previous result */ + idx = lua_absindex(L, idx); + lua_newtable(L); + lua_pushvalue(L, -1); /* copy to be left at top */ + lua_setfield(L, idx, fname); /* assign new table to field */ + return 0; /* false, because did not find table there */ + } +} + +void luaL_requiref (lua_State *L, const char *modname, lua_CFunction openf, int glb) +{ + lua_pushcfunction(L, openf); + lua_pushstring(L, modname); /* argument to open function */ + lua_call(L, 1, 1); /* open module */ + luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED"); + lua_pushvalue(L, -2); /* make copy of module (call result) */ + lua_setfield(L, -2, modname); /* _LOADED[modname] = module */ + lua_pop(L, 1); /* remove _LOADED table */ + if (glb) + { + lua_pushvalue(L, -1); /* copy of 'mod' */ + lua_setglobal(L, modname); /* _G[modname] = module */ + } +} +#endif // LUA_VERSION_NUM + diff --git a/src/compat.h b/src/compat.h new file mode 100644 index 0000000..a5801c4 --- /dev/null +++ b/src/compat.h @@ -0,0 +1,29 @@ +#if !defined( __COMPAT_H__) +#define __COMPAT_H__ 1 + +// code is now using Lua 5.2 API +// add Lua 5.2 API when building for Lua 5.1 +#if LUA_VERSION_NUM == 501 +#define lua_absindex( L, idx) (((idx) >= 0 || (idx) <= LUA_REGISTRYINDEX) ? (idx) : lua_gettop(L) + (idx) +1) +#define lua_pushglobaltable(L) lua_pushvalue( L, LUA_GLOBALSINDEX) +#define lua_setuservalue lua_setfenv +#define lua_getuservalue lua_getfenv +#define lua_rawlen lua_objlen +#define luaG_registerlibfuncs( L, _funcs) luaL_register( L, NULL, _funcs) +#define LUA_OK 0 +#define LUA_ERRGCMM 666 // doesn't exist in Lua 5.1, we don't care about the actual value +void luaL_requiref (lua_State* L, const char* modname, lua_CFunction openf, int glb); // implementation copied from Lua 5.2 sources +#endif // LUA_VERSION_NUM == 501 + +// wrap Lua 5.2 calls under Lua 5.1 API when it is simpler that way +#if LUA_VERSION_NUM == 502 +#ifndef lua_equal // already defined when compatibility is active in luaconf.h +#define lua_equal( L, a, b) lua_compare( L, a, b, LUA_OPEQ) +#endif // lua_equal +#ifndef lua_lessthan // already defined when compatibility is active in luaconf.h +#define lua_lessthan( L, a, b) lua_compare( L, a, b, LUA_OPLT) +#endif // lua_lessthan +#define luaG_registerlibfuncs( L, _funcs) luaL_setfuncs( L, _funcs, 0) +#endif // LUA_VERSION_NUM == 502 + +#endif // __COMPAT_H__ diff --git a/src/lanes.c b/src/lanes.c index 9e79f64..5f2f492 100644 --- a/src/lanes.c +++ b/src/lanes.c @@ -90,6 +90,7 @@ THE SOFTWARE. #include "lauxlib.h" #include "threading.h" +#include "compat.h" #include "tools.h" #include "keeper.h" #include "lanes.h" diff --git a/src/tools.c b/src/tools.c index ea7662b..d2b316b 100644 --- a/src/tools.c +++ b/src/tools.c @@ -64,49 +64,6 @@ void ASSERT_IMPL( lua_State* L, bool_t cond_, char const* file_, int const line_ char const* const CONFIG_REGKEY = "ee932492-a654-4506-9da8-f16540bdb5d4"; char const* const LOOKUP_REGKEY = "ddea37aa-50c7-4d3f-8e0b-fb7a9d62bac5"; -/* - * ############################################################################################### - * ######################################### Lua 5.1/5.2 ######################################### - * ############################################################################################### - */ - -/* -** Copied from Lua 5.2 loadlib.c -*/ -#if LUA_VERSION_NUM == 501 -static int luaL_getsubtable (lua_State *L, int idx, const char *fname) -{ - lua_getfield(L, idx, fname); - if (lua_istable(L, -1)) - return 1; /* table already there */ - else - { - lua_pop(L, 1); /* remove previous result */ - idx = lua_absindex(L, idx); - lua_newtable(L); - lua_pushvalue(L, -1); /* copy to be left at top */ - lua_setfield(L, idx, fname); /* assign new table to field */ - return 0; /* false, because did not find table there */ - } -} - -void luaL_requiref (lua_State *L, const char *modname, lua_CFunction openf, int glb) -{ - lua_pushcfunction(L, openf); - lua_pushstring(L, modname); /* argument to open function */ - lua_call(L, 1, 1); /* open module */ - luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED"); - lua_pushvalue(L, -2); /* make copy of module (call result) */ - lua_setfield(L, -2, modname); /* _LOADED[modname] = module */ - lua_pop(L, 1); /* remove _LOADED table */ - if (glb) - { - lua_pushvalue(L, -1); /* copy of 'mod' */ - lua_setglobal(L, modname); /* _G[modname] = module */ - } -} -#endif // LUA_VERSION_NUM - DEBUGSPEW_CODE( char const* debugspew_indent = "----+----!----+----!----+----!----+----!----+----!----+----!----+----!----+"); diff --git a/src/tools.h b/src/tools.h index 708421c..8e8bb7d 100644 --- a/src/tools.h +++ b/src/tools.h @@ -16,31 +16,6 @@ #define inline __inline #endif -// code is now using Lua 5.2 API -// add Lua 5.2 API when building for Lua 5.1 -#if LUA_VERSION_NUM == 501 -#define lua_absindex( L, idx) (((idx) >= 0 || (idx) <= LUA_REGISTRYINDEX) ? (idx) : lua_gettop(L) + (idx) +1) -#define lua_pushglobaltable(L) lua_pushvalue( L, LUA_GLOBALSINDEX) -#define lua_setuservalue lua_setfenv -#define lua_getuservalue lua_getfenv -#define lua_rawlen lua_objlen -#define luaG_registerlibfuncs( L, _funcs) luaL_register( L, NULL, _funcs) -#define LUA_OK 0 -#define LUA_ERRGCMM 666 // doesn't exist in Lua 5.1, we don't care about the actual value -void luaL_requiref (lua_State* L, const char* modname, lua_CFunction openf, int glb); // implementation copied from Lua 5.2 sources -#endif // LUA_VERSION_NUM == 501 - -// wrap Lua 5.2 calls under Lua 5.1 API when it is simpler that way -#if LUA_VERSION_NUM == 502 -#ifndef lua_equal // already defined when compatibility is active in luaconf.h -#define lua_equal( L, a, b) lua_compare( L, a, b, LUA_OPEQ) -#endif // lua_equal -#ifndef lua_lessthan // already defined when compatibility is active in luaconf.h -#define lua_lessthan( L, a, b) lua_compare( L, a, b, LUA_OPLT) -#endif // lua_lessthan -#define luaG_registerlibfuncs( L, _funcs) luaL_setfuncs( L, _funcs, 0) -#endif // LUA_VERSION_NUM == 502 - // For some reason, LuaJIT 64bits doesn't support lua_newstate() // If you build specifically for this situation, change value to 0 #define PROPAGATE_ALLOCF 1 -- cgit v1.2.3-55-g6feb