#ifndef COMPAT_50_H #define COMPAT_50_H /* Lua 5.0.x 兼容层: 让面向 5.1+ API 的桥接层无需大改即可编译。 在 include lua 头文件之后、桥接代码之前引入。 */ /* ---- 版本宏 ---- */ #ifndef LUA_VERSION_NUM #define LUA_VERSION_NUM 500 #endif /* ---- 成功码 ---- */ #ifndef LUA_OK #define LUA_OK 0 #endif /* ---- luaL_newstate: 5.0 是 lua_open(void) ---- */ #if LUA_VERSION_NUM == 500 #define luaL_newstate() lua_open() #endif /* ---- lua_status: 5.0 无线程状态查询, 恒 OK(单主线程语义足够) ---- */ #if LUA_VERSION_NUM == 500 static inline int compat_lua_status(lua_State *L) { (void)L; return LUA_OK; } #define lua_status(L) compat_lua_status(L) #endif /* ---- luaL_dostring/dofile: 5.0 名字是 lua_dostring/lua_dofile(lauxlib), 0=成功 ---- */ #if LUA_VERSION_NUM == 500 #define luaL_dostring(L, s) lua_dostring(L, s) #define luaL_dofile(L, f) lua_dofile(L, f) #endif /* ---- lua_setfield: 5.0 无。模拟: 先转绝对索引(避免 push 后相对索引偏移), push k → insert(-2) 把 [t][v][k] 变 [t][k][v] → settable(abs) 弹 k,v 设 t[k]=v。 走元方法, 语义与 5.1+ 一致。 */ #if LUA_VERSION_NUM == 500 #define lua_setfield(L, idx, k) \ do { \ int _abs = (idx) >= 0 ? (idx) : lua_gettop(L) + 1 + (idx); \ lua_pushstring(L, k); \ lua_insert(L, -2); \ lua_settable(L, _abs); \ } while (0) #endif /* ---- lua_getfield: 5.0 无。模拟: push k → gettable(idx) 弹 k 压 t[k]。 同样先转绝对索引。 */ #if LUA_VERSION_NUM == 500 #define lua_getfield(L, idx, k) \ do { \ int _abs = (idx) >= 0 ? (idx) : lua_gettop(L) + 1 + (idx); \ lua_pushstring(L, k); \ lua_gettable(L, _abs); \ } while (0) #endif /* ---- luaL_Reg: 5.0 的结构体名是小写 luaL_reg ---- */ #if LUA_VERSION_NUM == 500 typedef struct luaL_reg luaL_Reg; #endif /* ---- integer API: 5.0 数字只有 double, 全部映射到 number。 注: 大整数(>2^53)经 double 往返会丢精度 —— 这是 5.0 的固有限制, 测试中已用 #if LUA_VERSION_NUM >= 503 区分断言。 */ #if LUA_VERSION_NUM == 500 #define lua_Integer double #define lua_pushinteger(L, n) lua_pushnumber(L, (lua_Number)(n)) #define lua_tointeger(L, idx) ((lua_Integer)lua_tonumber(L, idx)) /* lua_isinteger 恒假: capture_result() 的 #if >= 503 分支不会走到这里, 此宏仅为编译通过而设 */ #define lua_isinteger(L, idx) (0) #endif #endif /* COMPAT_50_H */