Lua C Return Value

Lua c return value index

Welcome to the third part of these Lua 5.2 personal notes. If you have not yet, you may want to read my previous notes on running Lua 5.2 scripts from C++ or passing variables from Lua 5.2 to C++.

Converts the Lua value at the given acceptable index to a C string. If len is not NULL, it also sets.len with the string length. The Lua value must be a string or a number; otherwise, the function returns NULL. If the value is a number, then luatolstring also changes the actual value in the stack to a string. Function f (x, y) return (x^2. math.sin (y))/ (1 - x) end and you want to evaluate, in C, z = f (x, y) for given x and y. Assuming that you have already opened the Lua library and run the configuration file, you can encapsulate this call in the following C function.

This new tutorial explains how to call C++ functions from Lua 5.2, including passing and receiving arguments from them. These notes are based on the Lua-User Wiki Sample Code and the Lua 5.2 Reference Manual, as usual, and on the tutorial by Christian Stigen Larsen.

First, I will show you some code (in Lua and C++) and the execution output. Afterwards comes the details of the most difficult parts.

Lua script

The Lua script will just call a C++ function with some input arguments, and then will receive and print two output arguments. The code looks like this:

C++ program

The C++ code is almost exactly the same as in the previous tutorial. The only differences are the declaration and pushing of the function that Lua will call. The whole code is:

The output is:

The details

Lua functions are first-class values. This means that they can be treated as conventional values, in particular, they can be stored in variables. Thus, one can pass a function to Lua by simply pushing the value to the Lua stack and making its name global:

Lua will be able to access displayLuaFunction() from its global name “displayLuaFunction”. Also, there is a macro, lua_register(), that allows writing these two steps in one sentence. Instead of the previous code, one might have coded it as:

Lua Function Return Multiple Values

(my thanks to kpityu for pointing this out in the comments).

In order C++ functions to communicate properly with Lua, they must follow some rules. The first one is its signature, which has to be consistent with the type defined by lua_CFcuntion:

Lua Return Statement

i. e., the function must return an integer and only accept one input argument of type pointer-to lua_state.

The second rule is just a protocolto follow for input/output function argument interchange between Lua and C++. As you may have guessed, all this passing of variables is done via the Lua stack. When a C++ function is called from Lua, for example

Lua

a new Lua stack, independent of the rest of stacks, is generated for the function. This stack contains the input arguments of the function. Thus, from inside of the C++ function one knows the amount of input arguments by getting the position of the top of this stack:

and the input arguments can be accessed directly from the stack:

Notice that the order in the stack is the same as in the function call, hence, the first element popped is actually the last input argument. Since initially the only elements of the stack are the input arguments, you may access a given index by directly pointing to it. For instance, for the second input argument can be converted to a string — having checked the stack has at least two elements — by this statement:

Lua C Return Value Chart

Once the function has finished, the return values must be placed in the stack so Lua can reach them:

Lua Return Table

The final part is to return the number of variables pushed to the stack that we want Lua to see as the C++ function return value: Bmw scan tool for mac.

And that is it!

Lua C Return Value Index

With this we have covered the basic protocol for creating new C++ functions that are Lua compatible. In case you want more examples you can check the Lua libraries, which are made of C++ functions following these rules.

Stay tuned for the next episode of these personal notes. I am not sure if I will be talking about linking C++ dynamic libraries to Lua or about encapsulating and passing objects from and to Lua.

Read the next tutorial!

simGetObjectPosition / sim.getObjectPosition

Description Retrieves the position of an object. See also the other matrix/transformation functions.
C synopsis simInt simGetObjectPosition(simInt objectHandle,simInt relativeToObjectHandle,simFloat* position)
C parameters
objectHandle: handle of the object. Can be combined with sim.handleflag_reljointbaseframe (see next argument)
relativeToObjectHandle: indicates relative to which reference frame we want the position. Specify -1 to retrieve the absolute position, sim.handle_parent to retrieve the position relative to the object's parent, or an object handle relative to whose reference frame we want the position. If this handle is the handle of a joint, then the position relative to the joint's moving frame will be returned (unless objectHandle is combined with sim.handleflag_reljointbaseframe, in which case the position relative to the joint's base frame will be returned).
C return value
Lua synopsis table[3] position=sim.getObjectPosition(int objectHandle,int relativeToObjectHandle)
Lua parameters
Similar to the C-function counterpart
Lua return values
position: table of 3 values (x, y and z)
Remote API equiv.
B0-based remote API: simxGetObjectPosition, simxGetObjectPose