feat: 适配 Lua 5.1.5 (lua_5.1.5 分支)

- lib/lua 替换为 Lua 5.1.5 官方源码; CMakeLists 删除 5.1 不存在的
  lcorolib.c/lctype.c/lutf8lib.c/lbitlib.c
- 补充 lua.hpp C++ 包装头(5.1 官方不提供, 保持桥接 include 路径不变)
- LUA_OK 兼容宏: 5.1 无此定义(成功返回值为 0)
- luaL_tolstring 兼容: 5.1 无此 API, l_print 改用 lua_tostring +
  lua_typename 兜底(#if LUA_VERSION_NUM >= 502 条件编译, 弹栈逻辑同步分支)
- luaL_setfuncs 兼容: 5.1 无此 API, push_tty_file 改手动循环注册
- I/O 重定向沿用 Registry 方案(自 5.2.4 分支同步), typed interop
  版本自适应断言同步
- 验证: GTest 44/44 通过
This commit is contained in:
JianFeeeee
2026-08-25 11:13:41 +08:00
parent 4af121c1f3
commit c23f8c5e32
70 changed files with 9553 additions and 22456 deletions

View File

@ -282,7 +282,12 @@ TEST_F(LuaCjApiTest, PackagePathConfig) {
EXPECT_EQ(ret, 0) << "Require failed, maybe pkgpath not set correctly. Errno: " << get_errno();
char* res = getresult(runner);
EXPECT_STREQ(res, lib_path.c_str());
// 跨版本语义适配:
// - Lua 5.3 的 require 返回 1 个值模块本身run() 读栈顶得模块返回值 "LoadedViaPkgPath"
// - Lua 5.4+ 的 require 返回 2 个值(模块 + loader datarun() 读栈顶得 loader data文件路径
// 两者都表明 require 经 pkgpath 成功定位并加载了模块
EXPECT_TRUE(strcmp(res, lib_path.c_str()) == 0 || strcmp(res, "LoadedViaPkgPath") == 0)
<< "unexpected require result: " << (res ? res : "(null)");
}
// ==================== 新增测试doString 功能 ====================
@ -487,13 +492,21 @@ TEST_F(LuaCjApiTest, CallFunctionAfterCleanup) {
}
// ==================== typed interop 测试v0.2.2====================
// 版本自适应Lua 5.3+ 有 integer 子类型CJT_INT5.2 及以下所有数字均为
// numberCJT_NUM。数值断言用 INT_OR_NUM 宏兼容两种情况。
#if LUA_VERSION_NUM >= 503
#define EXPECT_INT_OR_NUM(type) EXPECT_EQ(type, CJT_INT)
#else
// 5.2 及以下:整型结果也报为 CJT_NUM且 lua_tostring(10.0) 输出 "10"
#define EXPECT_INT_OR_NUM(type) EXPECT_EQ(type, CJT_NUM)
#endif
// 整型参数直通Lua 收到 integer返回 integer+1仓颉侧读回 Int64
TEST_F(LuaCjApiTest, TypedCallInt) {
ASSERT_EQ(loadfunction(runner, get_script_path("func_typed_add.lua").c_str(), "inc"), 0);
EXPECT_EQ(callfunction_int(runner, "inc", 41), 0);
EXPECT_EQ(result_type(runner), CJT_INT);
EXPECT_INT_OR_NUM(result_type(runner));
EXPECT_EQ(result_int(runner), 42);
// 字符串形式同步可用(向后兼容)
EXPECT_STREQ(getresult(runner), "42");
@ -506,7 +519,12 @@ TEST_F(LuaCjApiTest, TypedCallNum) {
EXPECT_EQ(callfunction_num(runner, "mul", 4.0), 0);
EXPECT_EQ(result_type(runner), CJT_NUM);
EXPECT_DOUBLE_EQ(result_num(runner), 10.0);
#if LUA_VERSION_NUM >= 503
EXPECT_STREQ(getresult(runner), "10.0");
#else
// 5.2 的 %.14g 格式化把 10.0 输出为 "10"
EXPECT_STREQ(getresult(runner), "10");
#endif
}
// 布尔参数直通:返回逻辑非,读回 Bool
@ -529,7 +547,7 @@ TEST_F(LuaCjApiTest, TypedCallVoid) {
ASSERT_EQ(loadfunction(runner, get_script_path("func_typed_void.lua").c_str(), "forty_two"), 0);
EXPECT_EQ(callfunction_void(runner, "forty_two"), 0);
EXPECT_EQ(result_type(runner), CJT_INT);
EXPECT_INT_OR_NUM(result_type(runner));
EXPECT_EQ(result_int(runner), 42);
}
@ -539,13 +557,21 @@ TEST_F(LuaCjApiTest, TypedEchoRoundTrip) {
// int 往返
EXPECT_EQ(callfunction_int(runner, "echo", -123456789LL), 0);
EXPECT_EQ(result_type(runner), CJT_INT);
EXPECT_INT_OR_NUM(result_type(runner));
EXPECT_EQ(result_int(runner), -123456789LL);
// 大整数(超过 double 精确表示范围)往返
// 大整数(超过 double 精确表示范围)往返
// 仅 Lua 5.3+integer 类型可保真5.2 及以下经 double 必然丢精度
#if LUA_VERSION_NUM >= 503
EXPECT_EQ(callfunction_int(runner, "echo", 9007199254740993LL), 0);
EXPECT_EQ(result_type(runner), CJT_INT);
EXPECT_INT_OR_NUM(result_type(runner));
EXPECT_EQ(result_int(runner), 9007199254740993LL);
#else
EXPECT_EQ(callfunction_int(runner, "echo", 9007199254740993LL), 0);
EXPECT_EQ(result_type(runner), CJT_NUM);
// 经 double 往返,精度丢失为可预期值
EXPECT_EQ(result_int(runner), (long long)(double)9007199254740993LL);
#endif
// num 往返
EXPECT_EQ(callfunction_num(runner, "echo", 3.14159), 0);
@ -592,7 +618,7 @@ TEST_F(LuaCjApiTest, TypedRunVariants) {
// int
EXPECT_EQ(run_int(runner, path.c_str(), 777), 0);
EXPECT_EQ(result_type(runner), CJT_INT);
EXPECT_INT_OR_NUM(result_type(runner));
EXPECT_EQ(result_int(runner), 777);
// num
@ -638,7 +664,7 @@ TEST_F(LuaCjApiTest, TypedAndStringCoexist) {
TEST_F(LuaCjApiTest, TypedStateAfterCleanup) {
ASSERT_EQ(loadfunction(runner, get_script_path("func_typed_void.lua").c_str(), "v"), 0);
EXPECT_EQ(callfunction_void(runner, "v"), 0);
EXPECT_EQ(result_type(runner), CJT_INT);
EXPECT_INT_OR_NUM(result_type(runner));
EXPECT_EQ(cleanup(runner), 0);