Pass C ++ object to Lua function

I have a C ++ project where one class class 1 method changes very often. Therefore, I want to take this code from C ++ to Lua. Notice I'm new to Lua.

The whole task:

  • Bind some class methods to the Lua state machine;
  • Pass a reference to a class object to a function written in Lua;
  • Work with the passed C ++ object in the Lua function.

I found how to take the first step with Lunar, and can not cope with the second and third.

I can not use SWIG and boost.

+7
c ++ lua
source share
3 answers
//This has a large number of steps, but I'm gonna post them all. This is all using native Lua 5 and the lua CAPI. int CreateInstanceOfT(lua_State* L) { new (lua_newuserdata(L, sizeof(T))) T(constructor args); return 1; } int CallSomeFuncOnT(lua_State* L) { if (lua_istable(L, 1)) { // If we're passed a table, get CData lua_getfield(L, 1, "CData"); lua_replace(L, 1); } if (!lua_touserdata(L, 1)) lua_error(L); // longjmp out. T& ref = *(T*)lua_touserdata(L, 1); ref.SomeFunc(); // If you want args, I'll assume that you can pass them yourself return 0; } int main() { lua_State* L = luaL_newstate(); lua_pushcfunction(L, CreateInstanceOfT); lua_setglobal(L, "CreateInstanceOfT"); lua_pushcfunction(L, CallSomeFuncOnT); lua_setglobal(L, "CallSomeFuncOnT"); luaL_dofile(L, "something.lua"); lua_close(L); } -- Accompanying Lua code: semicolons are optional but I do out of habit. In something.lua function CreateCInstance() local Instance = { CData = CreateInstanceOfT(); SomeFunc = CallSomeFuncOnT; } return Instance; end local object = CreateCInstance(); object:SomeFunc(); // Calls somefunc. 

I could post a large number of details on how to facilitate visualization, and how to do inheritance, etc. - and this will require a change if you want to set more than one T (I think the most common solution is a simple transaction struct { std::auto_ptr<void>, int type } ). But this should be the starting point if you do not understand anything in the process.

Basically, firstly, we ask Lua to allocate some space (userdata) and then put T. into it. When CallSomeFuncOnT is called, we first ask if we have a table (many Lua classes are based on tables, because they support object orientation , meta tags, etc.) And they get userdata data, which we then convert to a pointer to our object, and then convert to a link. Remember that lua_touserdata gives you a void *, so you better be sure of the other end. Then we call somefunc and return. In Main, we simply register functions as global.

Now, in Lua, when you call CreateInstanceOfT, it effectively just calls the T constructor, transparent to the Lua user. Then we drop it into a table that is easier for Lua newbies to call SomeFunc, passing that table to it.

+7
source share

Have you looked at luabind ? This makes it easy to expose C ++ objects and functions for LUA.

+1
source share

Like John, I used luabind and recommend it. However, since using boost is not an option for you, you can find a list of libraries on this page . You can also check the DoItYourselfCppBinding page on the lua-users wiki page .

One library mentioned on the page is oolua , which has no dependencies (so to speak).

0
source share

All Articles