lua-users home
lua-l archive

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


OK. I think I have misunderstood your problem.
Now if I am right, your concern has nothing to do with
luaL_checkstring, but with Lua errors in unprotected environments.
Your example can in fact be simplified quite a bit more. The following
first snippet has the same symptoms:

#include "lua.h"
#include "lauxlib.h"

int main()
{
   lua_State* L = luaL_newstate();
   try {
	   lua_error(L);
   }
   catch (...) {
   }
   lua_close(L);
}


The problem is that lua_error (the base function for errors) in called
in an unprotected environment.
Normally, we always use lua_pcall function like in the following code:

int main()
{
   lua_State* L = luaL_newstate();
   lua_pushcfunction(L, lua_error);
   if(lua_pcall(L, 0, 0, 0)) {
	   // error occurred here
   }
   lua_close(L);
}

However, like you probably, I would *not* have expected the current
behavior that when Lua uses C++ exceptions for errors (i.e. compiled
in C++ mode without LUA_USE_LONGJMP).
It seems natural that raising an error in an unprotected environment
shall throw an exception anyway, which could be caught in the calling
process.
This does not work, because luaD_throw checks for the L->errorJmp
member, which is not set.
If I *hack* a little bit (disclaimer: don't do the following in your
code), the snippet actually works, the error is now caught:

#include "lua.h"
#include "lauxlib.h"
#include "lstate.h" // for lua_State definition

int main()
{
   lua_State* L = luaL_newstate();
   int dummy[3];
   L->errorJmp = (lua_longjmp*)dummy; // Hack!
   try {
	   lua_error(L);
   }
   catch (...) {
   }
   lua_close(L);
}

In Lua 5.1, it was exactly the same behavior.