lua-users home
lua-l archive

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


>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
for flexibility).

>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));
   lua_pop(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.
--lhf