lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Johannes Hager wrote:
Can someone answere me this question? And why worked lua_dofile in my programm and luaL_dofile brings: error: attempt to call a nil value

Try checking the return code from luaL_loadfile.

The return values of lua_load are:

    * 0 --- no errors;
    * LUA_ERRSYNTAX --- syntax error during pre-compilation.
    * LUA_ERRMEM --- memory allocation error.

Here is a handy function for when things go wrong:

static int report (lua_State *L, int status) {
  const char *msg;
  if (status) {
    msg = lua_tostring(L, -1);
    if (msg == NULL) msg = "(error with no message)";
    fprintf(stderr, "status=%d, %s\n", status, msg);
    lua_pop(L, 1);
  return status;

use it like so:

if (report(L, luaL_loadfile(L, filename) || lua_pcall(L, 0,0,0))) exit(1);
else printf("okay\n");

- Peter