lua-users home
lua-l archive

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


>  luaL_loadbuffer( state, buffer, size, filename );
>  lua_pcall( state, 0, LUA_MULTRET, 0 );

>  Is this the correct way to execute my source file

Yes

> and get full error reporting?

No. To get full error reporting, you need to install an error handler. The
error handler is called when an error occurs, before the stack is popped,
and can then, if it wants to, add traceback information to the error
message.

The Lua standalone interpreter uses a traceback creator which is found in
the debug library, but normally you don't actually want to give access to
the debug library to scripts in non-interactive mode. However, you don't
need to register the debug library functions; you just need to link the
copy the function from the debug library into your application.

The traceback function is called errorfb; it is self-contained except for
the two #defines which precede it; you will find it at line 225 in
lib/ldblib.c

To use it, you have to push it onto the stack and then give it's stack
index to lua_pcall:

lua_pushcclosure(L, errorfb, 0);
luaL_loadbuffer(state, buffer, size, filename);
lua_pcall(state, 0, LUA_MULTRET, -2);

The -2 is the stack index of the error function.

In embedding applications, I usually put this at the top of the stack at
the beginning, so that it is always at a known stack index, like 1, and I
don't have to worry about popping it off the stack after every call. That
also makes it easier to replace it with the identity function when I don't
want stack tracing visible.


----

Lua gang: wouldn't it be better to put this function in lauxlib instead of
ldblib so that it could be more conveniently used by embedded applications?
Just a question...

Rici