lua-users home
lua-l archive

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


Title: Calling C++ class methods from Lua (newbie)

Hi. I'm using the 5.1-alpha package. I am changing C++ programs to use configuration files written in Lua. It is a joy to see a future where I am not maintaining many different custom parsers :)

This may not be the best approach, but I'm trying to make the lua script call directly into the C++ class that maintains the application environment. The parser was written in C++, so it carried around a context pointer and would call the methods directly (context->SetHostName(someString)). I've created a C/Lua function (in this example int fLua_SetHostName(lua_State *L);) that knows how to call the C++ class to set the host name.

I'm able to retrieve the string, but I'm having difficulty understanding how to deal with the class pointer. I'm wondering how to pass in the context. I can create a global external to the function (ApplicationEnvironment *context;) but some of our applications are threaded and need multiple contexts.

I would like to push a pointer to the context on to the Lua stack, but I'm not at all sure how to push it on and pull it off. Do I create a lua_State for each thread? Would I (should I) push the context onto the stack, or would I be better off putting it in a table? I think that tables would be more flexible. Either way, how can I handle C++ pointers on the stack or in tables?

Thanks
Mike