lua-users home
lua-l archive

Re: Lua C API can't be used from other languages without C wrappers

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


>>>>> "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.

AltStyle によって変換されたページ (->オリジナル) /