简体   繁体   English

如何应用lua定义

[英]How to apply lua a definition

I have a definition as follows: 我的定义如下:

#define namef (s, r, x) (p_name ((s), (r), (x)))

My file lua is follows: 我的文件lua如下:

tbl= {
    name_func = module;
};

my code is as follows: 我的代码如下:

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 is an array char r_name [11]; r_name是一个数组char r_name [11];

but it is giving the following error: 但它给出了以下错误:

PANIC: unprotected error in call to Lua API (attempt to index a nil value)

I don't know why this is occurring, in C works normally, more to change to lua error occurs 我不知道为什么会发生这种情况,在C中正常工作,发生更多更改为lua错误的情况

First, you're posting a lot of stuff that's totally unrelated to the problem. 首先,您要发布很多与问题完全无关的内容。 We don't need to see p_name or your macro or anything that's not related to the Lua error. 我们不需要看到p_name或您的宏,也不需要看到与Lua错误无关的任何内容。 See: volume is not precision . 请参阅: 体积不精确 As part of trouble shooting this problem yourself, you should have removed extraneous things until you had the smallest snippet that reproduced the problem. 作为自己解决此问题的一部分,您应该删除无关的东西,直到您拥有可重现该问题的最小代码段为止。 You would have ended up with something like this: 您最终将得到如下结果:

  lua_State *L = luaL_newstate();
  if (luaL_dofile(L, lua_filename)) {
     return;
  }
  lua_getglobal(L, "tbl");
  lua_getfield(L, -1, "name_func"); 

The problem is that your file is not being found. 问题是找不到您的文件。 Per the manual , luaL_dofile returns true if there are errors and false if there are no errors. 根据手册 ,luaL_dofile如果有错误,则返回true如果没有错误,则返回false

Your program aborts if the file is found. 如果找到该文件,程序将中止。 Only if the file isn't found does it try to index tbl , which it can't, because that global variable doesn't exist. 只有当没有找到该文件它尝试为tbl ,它不能,因为全局变量不存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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