[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: lite push in the right direction..
- From: Choonster TheMage <choonster.2010@...>
- Date: Thu, 14 Mar 2013 21:20:36 +1100
On Thu, Mar 14, 2013 at 8:59 PM, Thierry Douez <th.douez@gmail.com> wrote:
> Hi,
>
> some context before my questions..
>
> I started learning Lua 2 years with Pil 5.2 book, and made a plugin for a
> third party (Gui toolbox).
> Did code more or less only one month at this time then have to stop :(
>
> Re-started this old project 2 days ago trying to make it alive and
> re-learned Lua.
>
> Re-discovering what I've done at this time was quite interesting and almost
> all my 3000 lines of C
> was even readable, except few lines (my bad, forgot to put some comments).
>
> So, that said, here is my question:
>
> I have a function:
>
> static int LCvar( lua_State *L )
> {
> const char *e ;
> const char *s ;
> int iRevVal,i ;
>
> // drop some code; no interest here..
> s = val.buffer ; // <-- a string: "1,2,3,4,.."
> i = 0;
> while ((e = strchr(s, (int)',')) != NULL ) {
> lua_pushlstring( L, s, e-s );
> s = e+1; // skip separator
> i++;
> }
> lua_pushstring( L, s );
> return ++i ;
> }
>
> in Lua script, I call it this way:
>
> local m,n,o,p = LCvar("test") and it works!
>
> Now, i would like to do:
>
> local m,n,o,p = LCvar[ "test" ]
>
> So, during the init phase of the plugin, I did:
>
> ....
> lua_newtable(L);
> lua_setfield(L, 1, "LCvar");
> lua_newtable(L);
> lua_pushliteral(L, "__index");
> // lua_pushcclosure(L, LCvar, 0);
> lua_pushcfunction( L, LCvar );
> lua_pushliteral(L, "__newindex");
> lua_pushcclosure(L, LCErr, 0);
> lua_settable(L,-5);
> lua_settable(L,-3);
> lua_getglobal(L, "setmetatable");
> lua_getfield(L, 1, "LCvar");
> lua_pushvalue(L, 2);
> lua_call(L,2,0);
> ....
>
> This works if LCvar returns only 1 value, otherwise the local m is nil.
>
> And now, I'm lost; don't know what to do to return a list of values with
> LCvar[ "test" ]
>
> Any pointer or a bit of teaching will be much appreciated.
>
> Regards,
>
> Thierry
>
>
>
This conversation explains a bit about why __index only returns one
value and offers a few alternatives:
http://lua-users.org/lists/lua-l/2007-07/msg00177.html