lua-users home
lua-l archive

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


Turgut Hakkı ÖZDEMİR wrote:
Issue two : How can i convert exceptions (derived from std::exception) to
lua? Well, i can do it by altering luaconf.h (i think) but how are you doing
this?

  C++ code starts a script
  script calls a C++ function
  C++ function gets frightened and raises an exception
  How the script can handle thrown exception?

What I do is with VCn

1) Compile lua.c and ldo.c as C++
2) Compile with /GX /EHsc-
3) Patch luaconf.h and ldo.c like:

luaconf.h

#undef luai_jmpbuf
#define luai_jmpbuf	int  /* dummy variable */
#undef LUAI_THROW
#define LUAI_THROW(L,c)	throw lua_exception(errcode)

#if defined(__cplusplus) && defined(ldo_c)
class lua_exception {
 int status;
public:
 lua_exception(int status_) : status(status_) { };
 int get_status() { return status; }
};
#endif


#undef LUAI_TRY
#define LUAI_TRY(L,c,a) \
 try { a }  catch (lua_exception &e) \
   { (c)->status = e.get_status();if ((c)->status == 0) (c)->status = -1; } \
 catch (...) { throw; }

ldo.c
starts with

/*
** $Id: ldo.c,v 2.37 2005/12/22 16:19:56 roberto Exp $
** Stack and Call structure of Lua
** See Copyright Notice in lua.h
*/


#if !defined(__cplusplus)
#include <setjmp.h>
#else
#pragma warning(disable : 4514)
#endif
#include <stdlib.h>
#include <string.h>
/* include all of these so that they get included before lua.h */
#if defined(__cplusplus)
#include <stdio.h>
#include <stdarg.h>
#include <stddef.h>
#include <math.h>
#include <limits.h>
#include <stdarg.h>
#include <io.h>
#endif


#define ldo_c
#define LUA_CORE

#if defined(__cplusplus)
extern "C" {
/*
** These modifications were required against the orig ldo.c becoz
** we needed the extern "C" wrapper and we need the lua_exception class
** to catch just lua errors
*/

#endif

#include "lua.h"

[rest of ldo.c]
#if defined(__cplusplus)
}
#endif


DB