Let's look at a simple example:
Lua_state * l = NULL;
// Internally call the Lua function Double F (Double X, Double Y) {Double Z; lua_getglobal (L, "F"); // obtain the Lua function f lua_pushnumber (L, x); // Press Parameters X and Y lua_pushnumber (L, Y); If (lua_pcall (l, 2, 1, 0 )! = 0) error (L, "error running function 'F': % s", lua_tostring (L,-1); If (! Lua_isnumber (L,-1) error (L, "function 'F' must return a number"); Z = lua_tonumber (L,-1); lua_pop (L, 1); Return Z;} int main (void) {L = lua_open (); lual_openlibs (l); If (lual_loadfile (L, "C: \ luatest \ functest. lua ") | lua_pcall (L, 0, 0, 0) error (L," cannot run configuration file: % s ", lua_tostring (L,-1 )); printf ("% F \ n", F (1.0, 2.0); Return 0 ;}
Functest. Lua:
f = function(a, b) return a + bend
The most important of this is the use of calling functions. In C, the APIs that call the Lua function are mainly composed of the following:
(1) void lua_call (lua_state * l, int nargs, int nresults );
Function call. nargs indicates the number of parameters and nresults indicates the number of returned values.
First, press the Lua function on the stack, then press the parameters on the stack in sequence, and then call the function.
When a function is called, both parameters and functions pop out of the stack. After the call is returned, the result is pushed to the stack.
Nresults = lua_multret. All returned values are pushed to the stack.
Nresults! = Lua_multret. The number of returned values is adjusted according to nresults.
Lua statement:
A = f ("how", T. X, 14)
Implementation in C:
Lua_getglobal (L, "F"); // function entry Stack
Lua_pushstring (L, "how"); // parameter 1
Lua_getglobal (L, "T"); // table t
Lua_getfield (L,-1, "x"); // parameter 2
Lua_remove (L,-2); // jump t out of stack
Lua_pushinteger (L, 14); // parameter 3
Lua_call (L, 3, 1); // call a function. Both parameters and functions are output from the stack.
Lua_setglobal (L, "A"); // assign a value to a, and the stack is pushed out of the stack.
After the preceding code is executed, the stack status returns to its original state.
(2) int lua_pcall (lua_state * l, int nargs, int nresults, int msgh );
Function call. In safe mode, you can add error handling functions.
If an error occurs during the call, lua_pcall will capture the error and push the stack an error message (the function and parameter will pop out of the stack first ), and an error code (a value other than 0) is returned ).
When an error occurs, if an error processing function is specified, the error processing function is called before the error message is loaded into the stack. The specific parameter is determined by the msgh parameter:
(1) msgh = 0. No error processing function is specified, and the inbound stack information remains unchanged;
(2) msgh! = 0, msgh indicates the stack index of the error processing function. The error processing function uses the error message as the parameter and adds the returned error message to the stack. It is mainly used to add more debug information to the error message, such as stack tracing, because the information is not collected after the pcall call.
Function return code:
Lua_ OK (0): Call successful
Lua_errrun: runtime error
Lua_errmem: memory allocation error. In this case, the error processing function is not called.
Lua_errerr: errors occurred when calling the error processing function. Of course, the error processing function will not be called further.
Lua_errgcmm: an error is reported when metamethod. _ GC is called. It is caused by GC and has nothing to do with the function itself.
(3) int lua_pcallk (lua_state * l,
Int nargs,
Int nresults,
Int errfunc,
Int CTX,
Lua_cfunction K );
Function call. In safe mode, the yield function is allowed.