Last week, Lua's Package and Oo were seen, and it feels good, Lua is not as good as Python programmer slammed.
By the way, the C function calls Lua and Lua calls the contents of the C function. 1. C-call Lua functions The general process is: 1. LUA file, form a closure and execute it 2. In the stack The name of the press function 3. Introducing the actual parameters of the function in the stack 4. Use Lua_PCALL to call the Lua function. The form is: Lua_PCall (L, parameter number, result, index of error handling function in the stack) This function is cleared. After executing Lua, if successful, the results will be inserted into the stack, if it is not successful, the error message will be pressed into the stack 5. Read the return result or error message 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
Extern "c" {# incrude "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 ("Please enter a number:"); scanf ("% d", & x); Printf ("% d / n", x);
Printf ("Enter a number:"); scanf ("% d", & y); printf ("% d / n", y);
Lua_GETGLOBAL (LUA_PUSHNUMBER (LUA_PUSHNUMBER (LUA_PUSHNUMBER (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_Inumber (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 C function (write form) LuA call library is a DLL file under Windows, in UNIX / Linux, the process of SO file VS is as follows: 1. Newly built a DLL project 2. Define a DEF file, define the export of the DLL, and my mylib.def is defined as follows: library mylib.dllDescription "AOL's First DLL for Lua" ExportsluaOpen_Mylib3. Writing Library, new CPP file .4. Define the library function, here PIL LSIN The function, the SIN () value of the incoming parameter 5. Defines the LUAL_REG array. This is a function array that registers a series of open to Lua calls. The last element of the array must be {null, null} LUAL_REG structure for end identity. 6. Declaration with LUAL_OPENLIB, as follows: / * * main.cpp * / # include
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} / * snultinel * /};
INT Luaopen_mylib (Lua_State * L) {lual_openlib (L, "MYLIB", MYLIB, 0); RETURN 1;} After generating myLib.dll, copy the file to Lua installation root directory. Run Lua, 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>