[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Calling a function defined in lua from C/C++
- From: "mark gossage " <mark@...>
- Date: Wed, 10 May 2006 18:51:46 MDT
Hello,
I will try to anwser some of these area's
> 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?
>
I maintain the Lua part of SWIG, which it as tool for wrappering C/C++ code to make it callable from within scripting languages. So this is how SWIG handles it.
Assuming I have a function
void fn() throw (std::out_of_range);
the wrappering looks a little like this
static int _wrap_fn(lua_State* L) {
try {
fn();
}
catch(std::out_of_range &_e) {
lua_pushfstring(L,"std::out_of_range:%s",e.what());
lua_error(L);
}
return 0;
}
This code will happily catch the std::out_of_range's, print the error and then calls lua_error() to report it.
> Issue four : What about C++ interoperatability? Any wrappers? (Actually i
> saw one but ended up with 404 error), How can i represent my classes/objects
> in lua in a little bit reasonable way (saw wiki). Where is the gooooooood
> sample "let's involve c++" code? :)
>
This gets a bit harder, I suggest either looking in PIL (Programming In Lua, http://www.lua.org/pil/), or you could look at one of the Lua wrappering tools (SWIG, tolua++, LuaBind, etc.)
Hope this helps,
Mark Gossage