[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: New error behaviour of lua_dostring in 5.0alpha
- From: Luiz Henrique de Figueiredo <lhf@...>
- Date: Thu, 5 Dec 2002 15:52:20 -0200
>In Lua4.0 if there was an error in the text passed to lua_dostring an error
>message would be output and control would be returned to the calling
>application. With Lua5.0alpha the program simply exists so the calling
>application can not do anything. Does lua_load have the same behaviour?
lua_load returns an error code and so does lua_pcall.
lua_dostring is now a convenience function and has been moved to lauxlib,
but you probably what luaL_loadbuffer (see below).
lua_load was introduced in 5.0 exactly to handle errors nicely (and also
>Is this change likely to remain, and if so is there a simple way to execute
>potentially erroneus code without the risk of the program exiting.
Try this (suggested before here in lua-l)
void my_dostring(lua_State *L, const char *cmd)
if (luaL_loadbuffer(L, cmd, strlen(cmd), cmd) || lua_pcall(L, 0, 0, 0))
fprintf(stderr, "%s\n", lua_tostring(L, -1));
The point is that if the parsing or the execution fails than you're left with
an error message on top of the stack. The code above simply outputs it to
stderr, but of course you can do whatever is adequate for your application.