feat: 适配 Lua 5.0.3 (lua_5.0 分支)

- 桥接层编译通过, 核心功能可用(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, 属测试脚本版本差异)
This commit is contained in:
JianFeeeee
2026-08-25 12:59:17 +08:00
parent 95d474ea07
commit 86c6e276be
14 changed files with 289 additions and 43 deletions

48
lib/lua/CMakeLists.txt Normal file
View File

@ -0,0 +1,48 @@
cmake_minimum_required(VERSION 3.12)
project(lua VERSION 5.0.3 LANGUAGES C)
# Lua 5.0.3: 平铺源码, 无 linit.c(库需手动逐个 luaopen_*)
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O2 -w")
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
if(CMAKE_C_COMPILER MATCHES "mingw" OR CMAKE_SYSTEM_NAME STREQUAL "Windows")
set(IS_WINDOWS TRUE)
endif()
if(NOT IS_WINDOWS)
list(APPEND EXTRA_LIBS dl)
add_compile_definitions(LUA_USE_LINUX)
endif()
add_library(lua STATIC
lapi.c
lauxlib.c
lbaselib.c
lcode.c
ldblib.c
ldebug.c
ldo.c
ldump.c
lfunc.c
lgc.c
liolib.c
llex.c
lmathlib.c
lmem.c
loadlib.c
lobject.c
lopcodes.c
lparser.c
lstate.c
lstring.c
lstrlib.c
ltable.c
ltablib.c
ltm.c
lundump.c
lvm.c
lzio.c
)
# ltests.c 仅调试用, 不参与构建

View File

@ -1,17 +0,0 @@
# makefile for Lua distribution (includes)
LUA= ..
include $(LUA)/config
SRCS= lua.h lualib.h lauxlib.h
all:
clean:
co:
co -q -f -M $(SRCS)
klean: clean
rm -f $(SRCS)

74
lib/lua/compat_50.h Normal file
View File

@ -0,0 +1,74 @@
#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 */

6
lib/lua/lua.hpp Normal file
View File

@ -0,0 +1,6 @@
/* C++ 兼容包装头(平铺结构): 保持桥接层 #include "lua/lua.hpp" 不变 */
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}