我想从C调用一个外部Lua_5.2函数,所以我做了一个最小的例子来试用它.
最小测试文件:
--- filename: play.lua
function hello()
print("Hello World!\n")
end
try 从C:调用此函数:
#include <stdio.h>
#include <stdlib.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
int
main(void) {
lua_State *L;
int status;
int result;
L = luaL_newstate();
luaL_openlibs(L);
status = luaL_loadfile(L, "play.lua");
if (status != LUA_OK) {
fprintf(stderr, "Could not load 'play.lua'!");
exit(1);
}
lua_getglobal(L, "hello");
if (lua_isfunction(L, -1)) {
fprintf(stderr, "ERROR: Not a function!\n");
exit(1);
}
result = lua_pcall(L, 0, 0, 0);
if (result!= LUA_OK) {
fprintf(sterr, "Error running lua: %i\n", result);
exit(1);
}
fprintf(stdout, "lua ran fine\n");
lua_pop(L, lua_gettop(L));
lua_close(L);
return 0;
}
但是,调用该可执行文件会产生lua_errun(2)
Error running lua: 2
我不太确定我在这里做错了什么,文档对我来说有点不透明--根据我正确使用的5.2 reference manual,pcall
(具有零参数和零返回值的函数),而且我显然是正确地从堆栈中获取了函数(否则他们前面的错误就会显示).
你知道我做错了什么吗?