[英]How to apply lua a definition
我的定義如下:
#define namef (s, r, x) (p_name ((s), (r), (x)))
我的文件lua如下:
tbl= {
name_func = module;
};
我的代碼如下:
void getname(void) {
lua_State *L = luaL_newstate();
luaL_openlibs(L);
char *arc = "luafun.lua";
if (luaL_dofile(L, arc)) {
printf("Error in %s", arc);
return;
}
lua_getglobal(L, "tbl");
lua_getfield(L, -1, "name_func");
namef(r_name, lua_tostring(L, -1), sizeof(r_name));
lua_close(L);
printf("done");
}
r_name是一個數組char r_name [11];
但它給出了以下錯誤:
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
我不知道為什么會發生這種情況,在C中正常工作,發生更多更改為lua錯誤的情況
首先,您要發布很多與問題完全無關的內容。 我們不需要看到p_name
或您的宏,也不需要看到與Lua錯誤無關的任何內容。 請參閱: 體積不精確 。 作為自己解決此問題的一部分,您應該刪除無關的東西,直到您擁有可重現該問題的最小代碼段為止。 您最終將得到如下結果:
lua_State *L = luaL_newstate();
if (luaL_dofile(L, lua_filename)) {
return;
}
lua_getglobal(L, "tbl");
lua_getfield(L, -1, "name_func");
問題是找不到您的文件。 根據手冊 ,luaL_dofile如果有錯誤,則返回true
如果沒有錯誤,則返回false
。
如果找到該文件,程序將中止。 只有當沒有找到該文件它嘗試為tbl
,它不能,因為全局變量不存在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.