[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Lua C API can't be used from other languages without C wrappers
- From: Andrew Gierth <andrew@...>
- Date: Wed, 21 Feb 2018 17:06:41 +0000
>>>>> "albertmcchan" == albertmcchan <firstname.lastname@example.org> 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_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: */
/* insert B below its arguments on stack: */
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.