Re: [Lua-icxx-users] lua_icxx + swig
Brought to you by:
schoenborno
|
From: Domenico M. <dm...@gm...> - 2012-02-05 22:09:00
|
After looking carefully at the source code, I found that you make use
of pushValToStack.
This can be used for a custom stack allocation to handle the swig call.
Here is what I've done :
I restored the original lua_icxx source code and added to LuaObjRef.h the
following code
struct LUA_ICXX_CPP_API
UserStackAllocation
{
virtual void pushToStack(lua_State* L) const = 0;
};
void pushValToStack(lua_State* L, const UserStackAllocation& objRef)
{
objRef.pushToStack(L);
}
In my code I derive from UserStackAllocation to handle the swig calls
class Swig : public UserStackAllocation
{
Swig(Foo* f);
void pushToStack(lua_State* L) const {
SWIG_Lua_NewPointerObj( L, .... );
}
}
Now I can simply use the standard way of calling a function
void execCallback()
{
cb( Swig( new Foo ) );
}
This solution is cleaner
Let me know if you are interested in adding it to the project
Thanks
Dom
On Sun, Feb 5, 2012 at 7:12 PM, Domenico Mangieri <dm...@gm...> wrote:
> I doesn't work as it is so I modified slightly lua_icxx code to
> accommodate this requirement.
>
> This is just a quick solution..
>
> with the changed code I can do this
>
>
> void execCallback()
> {
> LuaFuncCallParams call= cb.prepCall() // this is now public
>
> SWIG_Lua_NewPointerObj( L, new Foo() .... ) // push wrapped object
> on the stack
>
> call.doCall(1); // doCall gets extra number of params pushed on
> the stack
> }
>
>
> This works.
>
> Maybe it can be implemented differently but I'm sure this could be
> a popular feature for lua_icxx as I think the combo swig + lua_icxx is a
> winner!
>
>
> Dom
>
>
>
> On Sun, Feb 5, 2012 at 5:26 PM, Domenico Mangieri <dm...@gm...>wrote:
>
>> Hi .. more on this..
>>
>> I found that I can use SWIG_Lua_NewPointerObj provided by swig
>> generate file to create the wrapping object
>>
>>
>> void execCallback()
>> {
>> SWIG_Lua_NewPointerObj( L, new Foo() .... )
>>
>> cb();
>> }
>>
>> SWIG_Lua_NewPointerObj pushes the object into lua stack.
>>
>> Now the question is, can I call cb with no args given the pointer is
>> already in the stack ?
>>
>> I'll give it a try..
>>
>> Dom
>>
>>
>>
>>
>> On Sun, Feb 5, 2012 at 3:34 PM, Domenico Mangieri <dm...@gm...>wrote:
>>
>>> Hi,
>>>
>>> I'm playing around with lua_icxx. I find it very useful with its unique
>>> approach to lua calls within c++.
>>>
>>> I'm currently using swig to wrap a large library and I'm using lua_icxx
>>> to provide callback supports from within c++ into lua.
>>>
>>> Here is an example
>>>
>>> *c++ *
>>>
>>> // Note : Foo is wrapped by swig
>>>
>>> class Foo
>>> {
>>> public:
>>>
>>> LuaFuncRef cb;
>>>
>>> void registerCallback(const char* cb)
>>> {
>>> cb = _lua.eval(cb);
>>> }
>>> .....
>>>
>>> void execCallback()
>>> {
>>> cb();
>>> }
>>> };
>>>
>>>
>>>
>>>
>>>
>>> *lua :*
>>>
>>>
>>> function test()
>>> print("cool")
>>> end
>>>
>>> f = Foo()
>>> f:registerCallback("test");
>>>
>>>
>>>
>>>
>>> That's all good.
>>>
>>> My problem now is to make use of swig wrap to pass c++ objects to lua
>>> callback;
>>>
>>> Something like this
>>>
>>> void execCallback()
>>> {
>>> cb( new Foo );
>>> }
>>>
>>> The callback works fine with basic types but not for UDT.
>>>
>>> Any idead how that could be solved ?
>>>
>>>
>>> Thank you for sharing this lib.
>>>
>>> Dom
>>>
>>>
>>
>
|