下面这个简单的例子,从文本文件中读取一行脚本,并且将c中的变量值传进去,用脚本进行运算,将结果返回给c程序。
/**
* @file prototype.cpp
* @author Samuel.D
* @date 2009/11/11 14:49:39
* @brief
*
**/
extern "C"
{
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}
#include <iostream>
void stackdump_g(lua_State* l)
{
int i;
int top = lua_gettop(l);
printf(":::::total in stack %d\n",top);
for (i = 1; i <= top; i++)
{ /* repeat for each level */
int t = lua_type(l, i);
switch (t) {
case LUA_TSTRING: /* strings */
printf(":::::string: '%s'\n", lua_tostring(l, i));
break;
case LUA_TBOOLEAN: /* booleans */
printf(":::::boolean %s\n",lua_toboolean(l, i) ? "true" : "false");
break;
case LUA_TNUMBER: /* numbers */
printf(":::::number: %g\n", lua_tonumber(l, i));
break;
default: /* other values */
printf("%s\n", lua_typename(l, t));
break;
}
printf(" "); /* put a separator */
}
printf("\n"); /* end the listing */
}
int main(int argc, char * argv[])
{
int status;
// lua_open: 创建一个新的lua环境
// 这里创建一个全局的环境
lua_State* global_state = lua_open();
// 在state环境上打开标准库,
// 标准库包括:
// luaopen_base
// luaopen_package
// luaopen_table
// luaopen_io
// luaopen_os
// luaopen_string
// luaopen_math
// luaopen_debug
luaL_openlibs(global_state); /* open libraries */
/* 这个用来从文件中逐行加载他妈的脚本,每行作为一个单独的脚本,以idx为下标放到数组中 */
lua_newtable(global_state);
int idx = 0;
FILE * fp = fopen("rule.pro", "r");
assert(fp);
char rule_buf[4096];
char *fail_rule = "pack.Url = \"RULE_ERROR\"";
while(fgets(rule_buf, 4096, fp) != NULL) {
/* 规则索引号 ,到时候你就知道是干嘛使的了*/
lua_pushnumber(global_state, idx++);
status = luaL_loadbuffer(global_state, rule_buf, strlen(rule_buf), "null");
if(status != 0) {
puts(lua_tostring(global_state, -1));
lua_pop(global_state, 1);
assert(luaL_loadbuffer(global_state, fail_rule, strlen(fail_rule), "null") == 0);
}
/* 作为一个数组丢进去 */
lua_rawset(global_state, -3);
}
fclose(fp);
lua_setglobal(global_state, "rules");
/* 脚本加载完毕 */
/* 新建一个处理线程 */
lua_State* state = lua_newthread(global_state);
assert(state);
/* 应用规则 */
lua_newtable(state);
///// pack.a = 1
lua_pushstring(state, "a");
lua_pushnumber(state, 1);
lua_settable(state, -3);
///// pack.b = 2
lua_pushstring(state, "b");
lua_pushnumber(state, 2);
lua_settable(state, -3);
// 将lua_newtable新建的table置为全局对象
lua_setglobal(state, "pack");
/* 将对应的规则放到栈顶 */
lua_getglobal(state, "rules");
int ruleId = 0; // 执行第0个规则
lua_pushnumber(state, ruleId);
lua_rawget(state, -2);
if(lua_pcall(state, 0, 1, 0) != 0) {
fprintf(stderr, "LUA_ERROR:%s\n", lua_tostring(state, -1));
lua_pop(state, 1);
}
// 规则应用以后的返回值
double rule_ret = (double)lua_tonumber(state, -1);
fprintf (stdout, "Lua execute returns: %lf\n", rule_ret);
lua_pop(state, 2);
lua_close(global_state);
return 0;
}
/// rule.pro文件内容
return pack.a / pack.b;