繁体   English   中英

如何应用lua定义

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM