lua-users home
lua-l archive

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


Hello 

>  How nicely (if at
> all) does this play with C++'s unwinding? For example:
>

Depends on your compiler. I can tell you what happens with MS VC6.
I suspect similar issues with other compilers. This has been previously
discussed on this list.

You need to compile your C++ with /EHsc-, this generates code that
tells C++ that C can throw exceptions. Without this C++ code assumes
that C will never throw an exception.

If you are going to be using Lua under a lot of C++ for a small 
performance hit you can replace the Lua longjmp code with 
try/catch and compile one Lua module as C++. This for shaky
compilers puts all of the exception handling under one exception
management mechanism.

I would be interested to know what is the current state of play with gcc.

For MS VC6 I did the following ( I can send you the complete patch
if you like)

1) copied ldo.c to ldo.cpp
2) In ldo.cpp added

class lua_exception {
  int status;
public:
  lua_exception(int status_) : status(status_) { };
  int get_status() { return status; }
};


3) Changed

void luaD_throw (lua_State *L, int errcode) {
  if (L->errorJmp) {
    throw lua_exception(errcode);
  }
  else {
    G(L)->panic(L);
    exit(EXIT_FAILURE);
  }
}

int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
  struct lua_longjmp lj;
  lj.status = 0;
  lj.previous = L->errorJmp;  /* chain new error handler */
  L->errorJmp = &lj;
  try {
    (*f)(L, ud);
  }
  catch (lua_exception &e) {  /* an error occurred: restore the state */
    lj.status = e.get_status();
  }
  catch (...) {
    throw;
  }
  L->errorJmp = lj.previous;  /* restore old error handler */
  return lj.status;
}

4) Compiled everything with /GX /EHsc-
This has been working ok for me.

Finally Lua5.1 work6 has build time options for using try/catch rather
than longjmp. The important point is that the exception handling is
localized to ldo.c in two functions so that it is rather easy to install
whatever exception handling mechanism that you prefer.

DB