mirror of
https://gitcode.com/JianFeeeee/LuaCangjia_api.git
synced 2026-09-19 16:42:48 +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, 属测试脚本版本差异)
80 lines
3.1 KiB
C++
80 lines
3.1 KiB
C++
#ifndef LUA_RUNNER
|
||
#define LUA_RUNNER
|
||
#include "lua/lua.hpp"
|
||
#include "compat_50.h" /* Lua 5.0/5.1 API 兼容层 */
|
||
#ifdef __cplusplus
|
||
#include <string>
|
||
#endif
|
||
#define MAX_LUA_LIB 20
|
||
|
||
|
||
|
||
typedef struct loaded_package
|
||
{
|
||
char name[128];
|
||
int ref;
|
||
|
||
}ld_pkg;
|
||
|
||
// 预加载函数表:使用 luaL_ref 存入 Lua Registry,ref 为绝对索引,不受栈变化影响
|
||
#define MAX_LUA_FUNC 20
|
||
typedef struct loaded_function
|
||
{
|
||
char name[128];
|
||
int ref; // luaL_ref 返回的绝对索引
|
||
|
||
}ld_func;
|
||
|
||
// 结果类型标记(typed interop,v0.2.2)
|
||
#define CJT_NIL 0
|
||
#define CJT_BOOL 1
|
||
#define CJT_INT 2
|
||
#define CJT_NUM 3
|
||
#define CJT_STR 4
|
||
|
||
#ifdef __cplusplus
|
||
class Lua_runner//lua运行器对象
|
||
{
|
||
public:
|
||
Lua_runner(const char *path = NULL);//创建lua状态机并持久化持有
|
||
~Lua_runner();//销毁状态机
|
||
int load_lib(const char *path,const char *name);//加载库(块压栈)
|
||
int unload_lib(const char *name);//解除加载
|
||
int loadfunction(const char *path,const char *name);//预加载函数(顶层执行,结果存入独立函数表)
|
||
int unloadfunction(const char *name);//卸载预加载函数
|
||
int callfunction(const char *name,const char *arg);//按名称调用已预加载的函数(字符串参数)
|
||
int callfunction_int(const char *name, long long val);//按名称调用,传 Int64 参数
|
||
int callfunction_num(const char *name, double val);//按名称调用,传 Float64 参数
|
||
int callfunction_bool(const char *name, int val);//按名称调用,传 Bool 参数
|
||
int callfunction_void(const char *name);//按名称调用,不带参数
|
||
int run(const char *path = NULL,const char *arg = NULL);//运行脚本(字符串参数)
|
||
int run_int(const char *path, long long val);//运行脚本,传 Int64 参数
|
||
int run_num(const char *path, double val);//运行脚本,传 Float64 参数
|
||
int run_bool(const char *path, int val);//运行脚本,传 Bool 参数
|
||
int run_void(const char *path);//运行脚本,不带参数
|
||
int clean();//清除状态机缓存
|
||
int dostring(const char *target);
|
||
std::string reslt;
|
||
lua_State *get_lua_State();
|
||
// typed interop:最近一次调用的结果类型与值(callfunction_*/run_* 更新)
|
||
int last_result_type; // CJT_NIL/CJT_BOOL/CJT_INT/CJT_NUM/CJT_STR
|
||
long long last_result_int;
|
||
double last_result_num;
|
||
int last_result_bool;
|
||
// 内部辅助:读取 Lua 栈顶值并记录类型到 last_result_*
|
||
void capture_result();
|
||
// 计算 lua_pcall 实际返回值数量并记录结果;pop_results=true 时弹出全部返回值
|
||
// (管道模式的 run_* 传 false 以保留栈上数据供后续节点使用)
|
||
// 返回实际返回值数量
|
||
int finish_call(int pre_top, int nargs, bool pop_results);
|
||
private:
|
||
lua_State *L;
|
||
int pkg_cont;
|
||
ld_pkg pkgs[MAX_LUA_LIB];
|
||
int func_cont;
|
||
ld_func funcs[MAX_LUA_FUNC];
|
||
bool check_luastatue();
|
||
bool check_file_exists(const char *path);
|
||
};
|
||
#endif
|
||
#endif |