Re SWIG Calling Script Language Functions (Lua)

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Re SWIG Calling Script Language Functions (Lua)

mark gossage
Hello Eddie,

Lua binding is my area. Glad you find it easy to use.
Unfortunately, SWIG only focuses on scripting calling to C/C++, not the other way around. I believe that luabind can work both ways.

However it is quite simple to call Lua directly.
For example by using the luaL_loadbuffer() function you can execute a char* command (which to me is the easiest way). See http://www.lua.org/pil/24.1.html for a starting place.

If you want to pass a C++ object as a parameter to a Lua function or similar, there are two ways to do it.
The proper way is to use the function:
void SWIG_NewPointerObj(lua_State* L,void* ptr,swig_type_info *type, int own);
This will push a C++ pointer onto the stack as Lua userdata.
L,ptr are obvious.
swig_type_info *type is a type defined in the wrapper file which usually of the form SWIGTYPE_p_<some type>
You will have to look it up in the wrapper file.
The own attribute defines up whether this pointer is dynamic memeory or not. If the value is 0 then is just a pointer, if its 1 then when you have finished with the object lua will try to delete it.

The simpler but less neat way goes like this:
addume you have a lua function (myFn) that you want to call with a pointer to a MyClass object.
If you define up a function in the .i file
MyClass* int2MyClass(int p){return (MyClass*)p;}

Then within your C++ code you can do:
MyClass* ptr=...;
char buff[80];
snprint(buff,80,"myFn(int2MyClass(%d))",(int)ptr);
call_lua(buff); // assuming a function to exec the string

This will use int2MyClass() to convert the int into a SWIG wrappered pointer.
Then pass it to the function.

I will look into updating the docs to do this properly.

Hope this helps,
Mark

> Hello!
>
> I hadn't considered this, but I saw a post somewhere that SWIG is a good
> alternative to binding C++ to Lua, versus luabind and tolua.  I've given
> this a try, and it does indeed seem easy to have Lua code access my C++
> objects.
>
> My question however, is whether it's possible to call Lua functions from
> C++.  Specifically to call them and pass them C++ objects, and have SWIG
> set the appropriate metatable while they transition.  This is important,
> especially if it can handle not only copy-by-value, but also by reference.
>
> Anyhow, in reading the documentation, I couldn't find anything of the
> sort mentioned, and I'm curious if I'm missing something, or if anyone
> could give me a brief synopsis as to how I could achieve this using
> SWIG.  If I *have* missed some piece of documentation, feel free to
> scold me, but I'd still appreciate a pointer as to where to look!
>
> Thanks in advance!
>
> -e-
>





-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

Re: Re SWIG Calling Script Language Functions (Lua)

Marcelo Matus
Hello mark:

well, since it is easy to call lua from C++, maybe you can try to play with
directors in the same way. You can look at the ruby/python implementation
as examples.

Marcelo


mark gossage wrote:

>Hello Eddie,
>
>Lua binding is my area. Glad you find it easy to use.
>Unfortunately, SWIG only focuses on scripting calling to C/C++, not the other way around. I believe that luabind can work both ways.
>
>However it is quite simple to call Lua directly.
>For example by using the luaL_loadbuffer() function you can execute a char* command (which to me is the easiest way). See http://www.lua.org/pil/24.1.html for a starting place.
>
>If you want to pass a C++ object as a parameter to a Lua function or similar, there are two ways to do it.
>The proper way is to use the function:
>void SWIG_NewPointerObj(lua_State* L,void* ptr,swig_type_info *type, int own);
>This will push a C++ pointer onto the stack as Lua userdata.
>L,ptr are obvious.
>swig_type_info *type is a type defined in the wrapper file which usually of the form SWIGTYPE_p_<some type>
>You will have to look it up in the wrapper file.
>The own attribute defines up whether this pointer is dynamic memeory or not. If the value is 0 then is just a pointer, if its 1 then when you have finished with the object lua will try to delete it.
>
>The simpler but less neat way goes like this:
>addume you have a lua function (myFn) that you want to call with a pointer to a MyClass object.
>If you define up a function in the .i file
>MyClass* int2MyClass(int p){return (MyClass*)p;}
>
>Then within your C++ code you can do:
>MyClass* ptr=...;
>char buff[80];
>snprint(buff,80,"myFn(int2MyClass(%d))",(int)ptr);
>call_lua(buff); // assuming a function to exec the string
>
>This will use int2MyClass() to convert the int into a SWIG wrappered pointer.
>Then pass it to the function.
>
>I will look into updating the docs to do this properly.
>
>Hope this helps,
>Mark
>
>  
>
>>Hello!
>>
>>I hadn't considered this, but I saw a post somewhere that SWIG is a good
>>alternative to binding C++ to Lua, versus luabind and tolua.  I've given
>>this a try, and it does indeed seem easy to have Lua code access my C++
>>objects.
>>
>>My question however, is whether it's possible to call Lua functions from
>>C++.  Specifically to call them and pass them C++ objects, and have SWIG
>>set the appropriate metatable while they transition.  This is important,
>>especially if it can handle not only copy-by-value, but also by reference.
>>
>>Anyhow, in reading the documentation, I couldn't find anything of the
>>sort mentioned, and I'm curious if I'm missing something, or if anyone
>>could give me a brief synopsis as to how I could achieve this using
>>SWIG.  If I *have* missed some piece of documentation, feel free to
>>scold me, but I'd still appreciate a pointer as to where to look!
>>
>>Thanks in advance!
>>
>>-e-
>>
>>    
>>
>
>
>
>
>
>-------------------------------------------------------
>This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
>for problems?  Stop!  Download the new AJAX search engine that makes
>searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
>http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
>_______________________________________________
>Swig-user mailing list
>[hidden email]
>https://lists.sourceforge.net/lists/listinfo/swig-user
>  
>



-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user