Operating on Lua values with C++ functions
Instead of extracting every Lua value seperately and pushing the result of your C++ function back onto the stack again, you can use one of the following functions to make this process easier for you.
Invoke a Callable with Lua values
The function direct
lets you specify a stack signature in order to extract the values and invoke a Callable with
them.
Consider the following:
string result = foo(luwra::read<string>(lua, n), luwra::read<int>(lua, n + 1));
It could be rewritting like this:
string result = luwra::direct<string(string, int)>(lua, n, foo);
Note: The result of foo is not pushed onto the stack. Besides the extraction of Lua values
everything happens on the C++ side.
Invoke a function with Lua values
apply is similiar to
direct. The function apply
provides specific overloads for function pointers and function objects. Although direct works
with function pointers and function objects, it is often more convenient to use apply since it
allows the compiler to infer the stack signature without providing a template parameter.
Provided a function foo which has been declared as used in the example above:
string foo(string bar, int baz);
// ... or with a function object
std::function<string(string, int)> foo = /* magic */;
One would use foo like this:
string result = luwra::apply(lua, n, foo);