mirror of
https://gitcode.com/JianFeeeee/LuaCangjia_api.git
synced 2026-09-19 16:42:48 +00:00
- 新增 loadFunction/callFunction/unloadFunction 预加载函数 API - 预加载语义:加载文件→顶层执行一次→返回函数存入Lua Registry→可多次调用 - 与管道模式(load/runScript)存储完全解耦:funcs[]+Registry vs pkgs[]+虚拟栈 - 新增错误码 5023/5024/5025 - 新增单元测试:仓颉侧 8 个 + C++ GTest 侧 11 个 - 本机以 Cangjie 1.1.0 实编译并通过功能运行验证 - README/doc 增加 AI 辅助编程标识及预加载函数模式说明
53 lines
1.5 KiB
C++
53 lines
1.5 KiB
C++
#ifndef LUA_RUNNER
|
||
#define LUA_RUNNER
|
||
#include "lua/lua.hpp"
|
||
#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;
|
||
|
||
#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 run(const char *path = NULL,const char *arg = NULL);
|
||
int clean();//清除状态机缓存
|
||
int dostring(const char *target);
|
||
std::string reslt;
|
||
lua_State *get_lua_State();
|
||
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 |