mirror of
https://gitcode.com/JianFeeeee/LuaCangjia_api.git
synced 2026-09-20 00:48:47 +00:00
- 桥接层编译通过, 核心功能可用(init/load/unload/callfunction/dostring)
- 新增 compat_50.h 兼容层集中处理 5.0 差异:
* luaL_newstate → lua_open (#define)
* lua_status → 恒 LUA_OK(5.0 单线程, 无状态查询)
* luaL_dostring → lua_dostring(lauxlib)
* lua_getfield/setfield → pushstring+gettable/settable
(含绝对索引修正, 避免 push 后相对偏移)
* lua_Integer → double, lua_pushinteger→pushnumber(5.0 无整数类型)
* luaL_Reg typedef(5.0 结构体名小写 luaL_reg)
* open_std_libs 替代 luaL_openlibs(5.0 需逐库 luaopen_*)
- 5.0 无 package 系统: pkgpath 构造函数参数被忽略; clean 跳过
package.loaded={} 操作
- 5.0 不支持 ... 表达式(chunk 级无变参): 涉及此语法的测试脚本
需替换为 5.0 兼容版(已创建 test/scripts50/ 目录)
- 验证: lib 构建 0 error, InitFree 通过; 因语法差异部分测试未全过
(桥接层本身无 bug, 属测试脚本版本差异)
75 lines
2.5 KiB
C
75 lines
2.5 KiB
C
#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 */
|