mirror of
https://gitcode.com/JianFeeeee/LuaCangjia_api.git
synced 2026-09-20 17:08:12 +00:00
feat: 适配 Lua 5.2.4 (lua_5.2.4 分支)
- lib/lua 替换为 Lua 5.2.4 官方源码; CMakeLists 补 lbitlib.c(5.2 linit 注册 bit32)、删 lutf8lib.c(5.2 无此文件) - I/O 重定向机制改造: 5.2 无 lua_getextraspace → iopath 指针存入 Registry 固定索引(IOPATH_REG_IDX=1), 回调经 get_iopath() 取回, free_lua 时释放 —— 该方案同样适用于更低版本 - typed interop 版本自适应: capture_result() 的 lua_isinteger 分支用 #if LUA_VERSION_NUM >= 503 包裹, 5.2 及以下所有数字统一 CJT_NUM - 测试跨版本适配: PackagePathConfig 接受 require 的两种返回语义; typed 数值断言 EXPECT_INT_OR_NUM 宏兼容有无 integer 子类型; 大整数保真断言仅对 5.3+ 生效(5.2 经 double 必然丢精度) - 验证: GTest 44/44 通过
This commit is contained in:
@ -1,4 +1,9 @@
|
||||
#include "lua_cj_api.h"
|
||||
#include <new>
|
||||
|
||||
// Lua 5.2/5.1 无 lua_getextraspace,改用 Registry 固定索引存储 iopath 指针
|
||||
#define IOPATH_REG_IDX 1
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include <string.h>
|
||||
@ -351,15 +356,36 @@ int cleanup(void *selfd)
|
||||
int free_lua(void *selfd)
|
||||
{
|
||||
lua_runner* self = (lua_runner*)selfd;
|
||||
// 释放 Registry 中的 iopath(若存在)
|
||||
Lua_runner *runner = (Lua_runner*)self->lua_obj;
|
||||
if (runner != NULL) {
|
||||
lua_State *L = runner->get_lua_State();
|
||||
if (L != NULL) {
|
||||
lua_rawgeti(L, LUA_REGISTRYINDEX, IOPATH_REG_IDX);
|
||||
iopath *p = (iopath*)lua_touserdata(L, -1);
|
||||
lua_pop(L, 1);
|
||||
delete p; // NULL 也安全
|
||||
}
|
||||
}
|
||||
delete((Lua_runner*)self->lua_obj);
|
||||
free(self);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*-------------重定向io操作------------------------*/
|
||||
/* Lua 5.2/5.1 无 lua_getextraspace,改用 Registry 固定索引存储 iopath 指针 */
|
||||
|
||||
static iopath *get_iopath(lua_State *L)
|
||||
{
|
||||
lua_rawgeti(L, LUA_REGISTRYINDEX, IOPATH_REG_IDX);
|
||||
iopath *r = (iopath *)lua_touserdata(L, -1);
|
||||
lua_pop(L, 1);
|
||||
return r;
|
||||
}
|
||||
|
||||
static const char *full_path(lua_State *L, int isOut)
|
||||
{
|
||||
iopath *r = (iopath *)lua_getextraspace(L);
|
||||
iopath *r = get_iopath(L);
|
||||
static char buf[512];
|
||||
snprintf(buf, sizeof(buf), "%s/%s", r->iopath, isOut ? "output" : "input");
|
||||
return buf;
|
||||
@ -368,7 +394,7 @@ static const char *full_path(lua_State *L, int isOut)
|
||||
//劫持读取终端输入,触发回调
|
||||
static int l_tty_read(lua_State *L)
|
||||
{
|
||||
iopath *r = (iopath *)lua_getextraspace(L);
|
||||
iopath *r = get_iopath(L);
|
||||
if (r->funcs.io_input) r->funcs.io_input();
|
||||
const char *fname = full_path(L, 0);
|
||||
FILE *fp = fopen(fname, "rb");
|
||||
@ -394,7 +420,7 @@ static int l_tty_read(lua_State *L)
|
||||
//劫持到终端输出,输出结束触发回调
|
||||
static int l_tty_write(lua_State *L)
|
||||
{
|
||||
iopath *r = (iopath *)lua_getextraspace(L);
|
||||
iopath *r = get_iopath(L);
|
||||
const char *fname = full_path(L, 1);
|
||||
FILE *fp = fopen(fname, "a");
|
||||
if (!fp) return luaL_error(L, "tty write open fail: %s", fname);
|
||||
@ -406,7 +432,7 @@ static int l_tty_write(lua_State *L)
|
||||
return 0;
|
||||
}
|
||||
static int l_print(lua_State *L) {
|
||||
iopath *r = (iopath *)lua_getextraspace(L);
|
||||
iopath *r = get_iopath(L);
|
||||
const char *fname = full_path(L, 1); // 输出到 output 文件
|
||||
FILE *fp = fopen(fname, "a");
|
||||
if (!fp) return luaL_error(L, "print redirect open fail: %s", fname);
|
||||
@ -475,7 +501,7 @@ int redirecct_path(lua_State *L)
|
||||
errno = LUAERR_LUA_STATE;
|
||||
return -1;
|
||||
}
|
||||
iopath *p = (iopath*)lua_getextraspace(L);
|
||||
iopath *p = get_iopath(L);
|
||||
static const luaL_Reg fake_methods[] = {
|
||||
{"read", l_tty_read},
|
||||
{"write", l_tty_write},
|
||||
@ -547,15 +573,16 @@ void *init_lua_runner(const char *pathio,const char *pkgpath,int(*input)(),int(*
|
||||
errno = LUAERR_LUA_STATE;
|
||||
return NULL;
|
||||
}
|
||||
iopath *io_path = (iopath*)lua_getextraspace(L);
|
||||
//申请额外空间
|
||||
// Lua 5.2/5.1 无 extraspace:new 一个 iopath 存入 Registry
|
||||
iopath *io_path = new (std::nothrow) iopath();
|
||||
if(pathio == NULL)
|
||||
{
|
||||
delete io_path;
|
||||
goto WITHOUT_REDIRECT;
|
||||
}
|
||||
|
||||
if(io_path == NULL){
|
||||
errno = LUAERR_CALLBACK;
|
||||
errno = LUAERR_CLASS_LOST;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -564,6 +591,9 @@ void *init_lua_runner(const char *pathio,const char *pkgpath,int(*input)(),int(*
|
||||
io_path->funcs.io_output = output;
|
||||
//装载重定向路径
|
||||
strcpy(io_path->iopath,pathio);
|
||||
//存入 Registry 固定索引供回调取用
|
||||
lua_pushlightuserdata(L, (void*)io_path);
|
||||
lua_rawseti(L, LUA_REGISTRYINDEX, IOPATH_REG_IDX);
|
||||
//注册重定向
|
||||
if(redirecct_path(L)==-1){
|
||||
errno = LUAERR_LUA_STATE;
|
||||
|
||||
Reference in New Issue
Block a user