Files
LuaCangjia_api/lib/lua_cj_api.h

94 lines
2.8 KiB
C
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef LUA_CJ_API
#define LUA_CJ_API
#define MAX_RESULT 4096
#include "lua_runner.hpp"
/*内部使用结构体,仓颉无需关心*/
typedef struct redirectfuncs
{
int (*io_input)();
int (*io_output)();
}redirectfuncs;
typedef struct iopath
{
char iopath[256];
redirectfuncs funcs;
}iopath;
#define LUAI_EXTRASPACE sizeof(iopath)
typedef struct lua_runner
{
char result[4096];//返回值每次调用run方法时更新脚本返回至result当前支持单个字符串或整形返回
void *lua_obj;//cpp对象指向在cpp中封装的luarunner对象
}lua_runner;
/*内部使用结构体,仓颉无需关心*/
#ifdef __cplusplus
extern "C"{
#endif
/// @brief 用于在仓颉中初始化lua解释器返回void形指针
/// @param pathio 重定向终端输入出入到执行文件
/// @param pkgpath 用户包搜索路径
/// @param input 输入回调函数
/// @param output 输出回调函数
/// @return 成功返回对应指针失败返回NULL
void *init_lua_runner(const char *pathio,const char *pkgpath,int(*input)(),int(*output)());
/// @brief 用于在lua状态机中加载lua源码
/// @param selfd init_lua_runner返回的void指针
/// @param path 源码路径
/// @param name 为加载的源码命名unload时使用
/// @return 成功返回0,失败返回-1
int load_lib(void *selfd,const char *path,const char *name);
/// @brief 用于在lua状态机中卸载加载过的lua源码
/// @param selfd init_lua_runner返回的void指针
/// @param name 加载库时候的命名
/// @return 成功返回0,失败返回-1
int unload_lib(void *selfd,const char *name);
/// @brief 运行lua脚本
/// @param selfd init_lua_runner返回的void指针
/// @param path 脚本路径
/// @param arg 脚本入参(字符串类型)
/// @return 成功返回0,失败返回-1,并将lua出错原因拷贝至result中
int run(void *selfd,const char *path,const char *arg);
/// @brief 执行单句lua语句
/// @param selfd init_lua_runner返回的void指针
/// @param target 执行语句(字符串类型)
/// @return 成功返回0,失败返回-1,并将lua出错原因拷贝至result中
int dostring(void *selfd,const char *target);
/// @brief 清理lua状态机缓存不销毁状态机
/// @param selfd init_lua_runner返回的void指针
/// @return 成功返回0,失败返回-1
int cleanup(void *selfd);
/// @brief 获取lua状态机执行过程中产生的错误码
/// @param 不需要入参
/// @return 返回errrors中定义的错误码
int get_errno(void);
/// @brief 用于获取脚本返回值
/// @param selfd init_lua_runner返回的void指针
/// @return 返回运行结果
char *getresult(void *selfd);
/// @brief 清理lua状态机并释放内存
/// @param selfd init_lua_runner返回的void指针
/// @return 成功返回0,失败返回-1
int free_lua(void *selfd);
#ifdef __cplusplus
}
#endif
#endif