lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> This was hammered home for me when I watched ThePhD's Lua Workshop
presentation on sol2 and how clean and beautiful it is; but it took a
DOCTOR to put that together.

(Sorry for the extra post, but I just saw that!) I really need to change my online handle at some point. I'm not a Doctor! (Yet). And, when I started my education it wasn't even in Computer Science. What I accomplished with sol2 was a lot of hard work and took a lot of different opinions and ideas to formulate. Plus, the ground has been well-trodden before: there's well over 2 dozen C API bindings with many of them trying different ideas. That's a lot of prior knowledge, which helps you design better because you can see what did/doesn't work. There were 3 versions of PiL available by the time I got serious with sol2, too. sol2 brings some novel ideas, but at the end of the day I'm still standing on the shoulders of giants.


On Wed, Feb 21, 2018 at 12:06 PM, Andrew Gierth <andrew@tao11.riddles.org.uk> wrote:
>>>>> "albertmcchan" == albertmcchan  <albertmcchan@yahoo.com> writes:

 albertmcchan> Thanks Andrew,
 albertmcchan> Can I assume call B with argument "foo!", "whatever" like
 albertmcchan> this ?

Of course you can, but you had a -1 there where you probably meant -2:

    lua_pushcfunction(L, B);          // stack = foo! B
                                      // -1 here is B as a function
                                      // value, -2 is "foo!"
    lua_pushvalue(L, -2);             // stack = foo! B foo!
    lua_pushstring(L, "whatever");
    lua_call(L, 2, 1);                // == lua call B("foo!", "whatever")
    return 2;                         // stack = foo! foo!, thus return everything

I sometimes find it more convenient to do this:

    /* set up the arguments to B: */
    lua_pushvalue(L, -1);
    lua_pushstring(L, "whatever");
    /* insert B below its arguments on stack: */
    lua_pushcfunction(L, B);
    lua_insert(L, -3);  /* -(nargs+1) */
    lua_call(L, 2, 1);

This is especially handy if you have the args on stack already and don't
need them afterwards. You can also wrap the pushcfunction/insert/call in
your own convenience function too.

--
Andrew.