简体   繁体   中英

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:

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];

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

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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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