Using a home made Lua C++ binding I'm able to create C++ classes that are available from Lua scripts.
Now I would like to create class objects in a Lua script, store its reference in C++ and when required call a function of this Lua class object from the C++ code.
In a Lua script I created a class object with a global function to pass it to the C++ code.
MyClass = {}
function MyClass:hello()
debug.codeSite.sendMsg( "MyClass:hello()" )
end
function MyClass:new()
local tNewClass = {}
setmetatable( tNewClass, self )
self.__index = self
return tNewClass
end
local tClass = MyClass:new()
function getClass()
debug.codeSite.sendMsg( "Return class object" )
return tClass;
end
Now in C++ I would like to get its instance and call the function hello(). Here what I tried :
- Call getClass() so it pushes the Lua object (a table) to the stack.
- Store its address into a C++ const void* using function lua_topointer().
At this point I don't know how to push back this pointer to the Lua stack, so the Lua table is pushed back to the stack, push the function name and then call it.
The idea of all of this is to create several objects in Lua scripts and use them in C++.
In lua:
tObject1 = MyClass:new()
tObject2 = MyClass:new()
and in C++ use tObject1:hello() or tObject2:hello() when required.
Edit:
According to this description of lua_topointer(), converting Lua table to C pointer is only for debugging purpose.
Thanks.