I checked Lua's package and OO last week. It feels good. Lua is not as elegant as it is attacked by Python programmers.
By the way, let's take a look at the content in PIL where the C function calls Lua and Lua calls the C function.
I. The general process for C to call the Lua function is:
1. Load a Lua file to form a closure and execute it.
2. Name of the Input Function in the stack
3. input the real parameters of the function in sequence in the stack.
4. Use lua_pcall to call the Lua function. Format: lua_pcall (L, number of parameters, number of results, index of the error processing function in the stack)
At this time, the function-related stacks have been cleared. After Lua is executed, if successful, the result will be pushed to the stack in sequence. If unsuccessful, the error message will be pushed to the stack.
5. Read the returned results or error messages from the stack.
The following is an example:
--[[
Func. Lua
--]
Function f (x, y)
Return (x ^ 2 * Math. Sin (y)/(1-x)
End
/**
* Main. cpp
*/
# Include <stdio. h>
# Include <stdlib. h>
# Include <stdarg. h>
Extern "C"
{
# Include "Lua. H"
# Include "lualib. H"
# Include "lauxlib. H"
}
Void error (lua_state * l, const char * FMT ,...)
{
Va_list argp;
Va_start (argp, FMT );
Vfprintf (stderr, FMT, argp );
Va_end (argp );
Lua_close (L );
Exit (exit_failure );
}
Int main (void)
{
Lua_state * l = lual_newstate ();
Lua_cpcall (L, luaopen_base, 0 );
Lua_cpcall (L, luaopen_io, 0 );
Lua_cpcall (L, luaopen_math, 0 );
Lua_cpcall (L, luaopen_string, 0 );
// Load func. Lua and run
If (lual_loadfile (L, "func. Lua") | lua_pcall (L, 0, 0, 0 ))
Error (L, "cannot run configuration file: % s", lua_tostring (L,-1 ));
Double X, Y;
Printf ("enter a number :");
Scanf ("% d", & X );
Printf ("% d/N", X );
Printf ("enter another number :");
Scanf ("% d", & Y );
Printf ("% d/N", y );
Lua_getglobal (L, "F ");
Lua_pushnumber (L, X );
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 ");
Double Z = lua_tonumber (L,-1 );
Lua_pop (L, 1);/* Pop returned value */
Printf ("Result:/T % D'/N", Z );
Lua_close (L );
}
Ii. Lua calls the C function (written as a database example)
The Lua call library is a DLL file in windows and a so file in Unix/Linux.
The vs process is as follows:
1. Create a DLL Project
2. Define a Def file and define DLL export. My mylib. Def definition is as follows:
Library mylib. dll
Description "AOL's first DLL for Lua"
Exports
Luaopen_mylib
3. Compile the library and create a CPP file.
4. Define the library function. Here we use the lsin function of PIL to output the sin () value of the input parameter.
5. Define the lual_reg array. This is the array of functions that are registered to be called by Lua. The last element of the array must be the lual_reg structure of {null, null} For end identification.
6. Use lual_openlib to declare the main function
Example:
/*
* Main. cpp
*/
# Include <math. h>
Extern "C"
{
# Include "Lua. H"
# Include "lualib. H"
# Include "lauxlib. H"
}
Static int l_sin (lua_state * l)
{
Double D = lual_checknumber (L, 1 );
Lua_pushnumber (L, sin (d ));
Return 1;/* Number of Results */
}
Static const struct lual_reg mylib [] = {
{"Lsin", l_sin },
{Null, null}/* Sentinel */
};
Int luaopen_mylib (lua_state * l)
{
Lual_openlib (L, "mylib", mylib, 0 );
Return 1;
}
After mylib. dll is generated, upload the file to the installation root directory of Lua.
Run Lua and enter the Lua environment. The call is as follows (LUA version 5.1 ):
> Package. loadlib ("mylib. dll", "luaopen_mylib ")()
> Print (mylib. lsin (10 ))
-0.54402111088937
>