Re: How to pass value from C++ to lua function by reference?
1 1 <[email protected]> Thu, 7 Aug 2014 16:29:10 +0300
| Newsgroups | gmane.comp.lang.lua.bind.user |
|---|---|
| Message-ID | <CAMbZc6wkv5-_AC9Yz_Y=_VRTEbxT8Jvavhxa_0VF8FcE_vrzHg@mail.gmail.com> |
--===============2259690279066580348== Content-Type: multipart/alternative; boundary=20cf303a32c9e4101e05000a121e --20cf303a32c9e4101e05000a121e Content-Type: text/plain; charset=UTF-8 Thank you for your answer, Michael! On 7 Aug 2014 16:21, "Michael Steinberg" <[email protected]> wrote: > Hello, > > It is impossible to pass object types that are modelled in lua with > built-in types by reference. In Lua, there exists no attribute to types > like reference or value type. A type is either a reference type (tables), > or a value type (built-in types like number). > What you can do however is return the new value in the lua-side. A policy > could be established, that transforms reference integral types to a list of > parsed return types, so it is transparent on the c++ side. That would place > hard constraints on the lua side though. > > Kind regards, > Michael > > Am 03.08.2014 21:09, schrieb 1 1: > > When programming on C++, you can do the following: > > void byReference(int &y){ > y = 5;} > int main(){ > int x = 2; // x = 2 > byReference(x); // x = 5} > > How to do the same using luabind? > > Luabind docs says > <http://www.rasterbar.com/products/luabind/docs.html#calling-lua-functions> > : > > If you want to pass a parameter as a reference, you have to wrap it with > the Boost.Ref. > > Like this: > > int ret = call_function(L, "fun", boost::ref(val)); > > But when I'm trying to do this: > > #include <iostream>#include <conio.h> > extern "C" { > #include "lua.h" > #include "lualib.h" > #include "lauxlib.h"} > #include <luabind\luabind.hpp> > using namespace std;using namespace luabind; > int main() { > lua_State *myLuaState = luaL_newstate(); > open(myLuaState); > int x = 2; > > > do > { > luaL_dofile(myLuaState, "script.lua"); > cout<<"x before = "<< x <<endl; > > try > { > call_function<void>(myLuaState, "test", boost::ref(x)); > } > catch(const std::exception &TheError) > { > cerr << TheError.what() << endl; > > } > cout<<"x after = "<< x <<endl; > > } while(_getch() != 27); > > lua_close(myLuaState);} > > script.lua: > > function test(x) > x = 7end > > It gives me a "Trying to use unregistered class" error. > > So, how to pass value from C++ to lua function by reference, so I can > change it inside the script and it will be changed in c++ program too? I'm > using boost 1.55.0, lua 5.1, luabind 0.9.1 > > Thanks. > P.S: > I posted the same question on StackOverflow > <http://stackoverflow.com/questions/25099437/luabind-how-to-pass-value-from-c-to-lua-function-by-reference>, > but seems like nobody knows what to do. > > > ------------------------------------------------------------------------------ > Want fast and easy access to all the code in your enterprise? Index and > search up to 200,000 lines of code with a free copy of Black Duck > Code Sight - the same software that powers the world's largest code > search on Ohloh, the Black Duck Open Hub! Try it now.http://p.sf.net/sfu/bds > > > > _______________________________________________ > luabind-user mailing [email protected]://lists.sourceforge.net/lists/listinfo/luabind-user > > > > > ------------------------------------------------------------------------------ > Infragistics Professional > Build stunning WinForms apps today! > Reboot your WinForms applications with our WinForms controls. > Build a bridge from your legacy apps to the future. > > http://pubads.g.doubleclick.net/gampad/clk?id=153845071&iu=/4140/ostg.clktrk > _______________________________________________ > luabind-user mailing list > [email protected] > https://lists.sourceforge.net/lists/listinfo/luabind-user > > --20cf303a32c9e4101e05000a121e Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <p dir=3D"ltr">Thank you for your answer, Michael!</p> <div class=3D"gmail_quote">On 7 Aug 2014 16:21, "Michael Steinberg&quo= t; <<a href=3D"mailto:[email protected]">michael.steinbe= [email protected]</a>> wrote:<br type=3D"attribution"><blockquote class= =3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padd= ing-left:1ex"> =20 =20 =20 <div text=3D"#000000" bgcolor=3D"#FFFFFF"> <div>Hello,<br> <br> It is impossible to pass object types that are modelled in lua with built-in types by reference. In Lua, there exists no attribute to types like reference or value type. A type is either a reference type (tables), or a value type (built-in types like number).<br> What you can do however is return the new value in the lua-side. A policy could be established, that transforms reference integral types to a list of parsed return types, so it is transparent on the c++ side. That would place hard constraints on the lua side though.<br> <br> Kind regards,<br> Michael<br> <br> Am 03.08.2014 21:09, schrieb 1 1:<br> </div> <blockquote type=3D"cite"> <div dir=3D"ltr"> <div> <p>When programming on C++, you can do the following:</p> <pre><code><span>void</span><span> byReference</span><span>(</spa= n><span>int</span><span> </span><span>&</span><span>y</span><span>)</sp= an><span> </span><span>{</span><span> y </span><span>=3D</span><span> </span><span>5</span><span>;</span><spa= n> </span><span>}</span><span> </span><span>int</span><span> main</span><span>()</span><span> </span><span>{</span><span> </span><span>int</span><span> x </span><span>=3D</span><span> </span><sp= an>2</span><span>;</span><span> </span><span>// x =3D 2</span><span> byReference</span><span>(</span><span>x</span><span>);</span><span> </sp= an><span>// x =3D 5</span><span> </span><span>}</span></code></pre> <p>How to do the same using luabind?</p> <p>Luabind docs <a href=3D"http://www.rasterbar.com/products/luab= ind/docs.html#calling-lua-functions" rel=3D"nofollow" target=3D"_blank">say= s</a>:</p> <blockquote> <p>If you want to pass a parameter as a reference, you have to wrap it with the <code>Boost.Ref</code>.</p> <p>Like this:</p> <pre><code><span>int</span><span> ret </span><span>=3D</span><s= pan> call_function</span><span>(</span><span>L</span><span>,</span><span> <= /span><span>"fun"</span><span>,</span><span> boost</span><span>::= </span><span>ref</span><span>(</span><span>val</span><span>));</span></code= ></pre> </blockquote> <p>But when I'm trying to do this:</p> <pre><code><span>#include</span><span> </span><span><iostream&= gt;</span><span> </span><span>#include</span><span> </span><span><conio.h></span><span= > </span><span>extern</span><span> </span><span>"C"</span><span>=20 </span><span>{</span><span> </span><span>#include</span><span> </span><span>"lua.h"</span= ><span> </span><span>#include</span><span> </span><span>"lualib.h"</s= pan><span> </span><span>#include</span><span> </span><span>"lauxlib.h"</= span><span> </span><span>}</span><span> </span><span>#include</span><span> </span><span><</span><span>luabind\lu= abind</span><span>.</span><span>hpp</span><span>></span><span> </span><span>using</span><span> </span><span>namespace</span><span> std</sp= an><span>;</span><span> </span><span>using</span><span> </span><span>namespace</span><span> luabind= </span><span>;</span><span> </span><span>int</span><span> main</span><span>()</span><span>=20 </span><span>{</span><span> lua_State </span><span>*</span><span>myLuaState </span><span>=3D</span><s= pan> luaL_newstate</span><span>();</span><span> open</span><span>(</span><span>myLuaState</span><span>); </span><span> =C2=A0</span><span>int</span><span> x </span><span>=3D</span>= <span> </span><span>2</span><span>;</span><span> =C2=A0</span><span>do</span><span> </span><span>{</span><span> luaL_dofile</span><span>(</span><span>myLuaState</span><span>,</span><s= pan> </span><span>"script.lua"</span><span>); </span><span> =C2=A0cout</span><span><<</span><span>"x before = =3D "</span><span><<</span><span> x </span><span><<</span>= <span>endl</span><span>; </span><span>=C2=A0 try=20 { call_function<void>(myLuaState, "test", boost::ref(= x)); } catch(const std::exception &TheError)=20 { cerr << TheError.what() << endl; } </span><span></span><span> cout</span><span><<</span><span>"x after =3D "</span><s= pan><<</span><span> x </span><span><<</span><span>endl</span><s= pan>; </span><span> =C2=A0 </span><span>}</span><span> </span><span>while</span><= span>(</span><span>_getch</span><span>()</span><span> </span><span>!=3D</sp= an><span> </span><span>27</span><span>); </span><span> =C2=A0lua_close</span><span>(</span><span>myLuaState</span><s= pan>);</span><span> </span><span>}</span></code><code><span></span></code></pre> <p>script.lua:</p> <pre><code><span>function</span><span> test</span><span>(</span><= span>x</span><span>)</span><span> x </span><span>=3D</span><span> </span><span>7</span><span> </span><span>end</span></code></pre> <p>It gives me a <code>"Trying to use unregistered class&quo= t;</code> error.</p> <p>So, how to pass value from C++ to lua function by reference, so I can change it inside the script and it will be changed in c++ program too? I'm using boost 1.55.0, lua 5.1, luabind 0.9.1</p> <p>Thanks.<br> </p> P.S:<br> </div> I posted the same question on <a href=3D"http://stackoverflow.com/q= uestions/25099437/luabind-how-to-pass-value-from-c-to-lua-function-by-refer= ence" target=3D"_blank">StackOverflow</a>, but seems like nobody knows what to do.</div> <br> <fieldset></fieldset> <br> <pre>----------------------------------------------------------------= -------------- Want fast and easy access to all the code in your enterprise? Index and search up to 200,000 lines of code with a free copy of Black Duck Code Sight - the same software that powers the world's largest code search on Ohloh, the Black Duck Open Hub! Try it now. <a href=3D"http://p.sf.net/sfu/bds" target=3D"_blank">http://p.sf.net/sfu/b= ds</a></pre> <br> <fieldset></fieldset> <br> <pre>_______________________________________________ luabind-user mailing list <a href=3D"mailto:[email protected]" target=3D"_blank">lua= [email protected]</a> <a href=3D"https://lists.sourceforge.net/lists/listinfo/luabind-user" targe= t=3D"_blank">https://lists.sourceforge.net/lists/listinfo/luabind-user</a> </pre> </blockquote> <br> </div> <br>-----------------------------------------------------------------------= -------<br> Infragistics Professional<br> Build stunning WinForms apps today!<br> Reboot your WinForms applications with our WinForms controls.<br> Build a bridge from your legacy apps to the future.<br> <a href=3D"http://pubads.g.doubleclick.net/gampad/clk?id=3D153845071&iu= =3D/4140/ostg.clktrk" target=3D"_blank">http://pubads.g.doubleclick.net/gam= pad/clk?id=3D153845071&iu=3D/4140/ostg.clktrk</a><br>__________________= _____________________________<br> luabind-user mailing list<br> <a href=3D"mailto:[email protected]">[email protected]= urceforge.net</a><br> <a href=3D"https://lists.sourceforge.net/lists/listinfo/luabind-user" targe= t=3D"_blank">https://lists.sourceforge.net/lists/listinfo/luabind-user</a><= br> <br></blockquote></div> --20cf303a32c9e4101e05000a121e-- --===============2259690279066580348== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline ------------------------------------------------------------------------------ Infragistics Professional Build stunning WinForms apps today! Reboot your WinForms applications with our WinForms controls. Build a bridge from your legacy apps to the future. http://pubads.g.doubleclick.net/gampad/clk?id=153845071&iu=/4140/ostg.clktrk --===============2259690279066580348== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ luabind-user mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/luabind-user --===============2259690279066580348==--