LUA interacts with C by lua_state this stack.
1.....lua stack index upward growth such as: 1 2 3 4 5 62.....lua Stack index is the following index to the bottom is 3 2 1 0-1-2-3, the stack corresponds to a value of 1 2 3 x 1 2 3 3......lua functions multiple return values if the function above returns 3 return values, then return a, B,c a=3 b=2 c=1 first return value first into the stack
Stack pop problem: Lua_pop (x), X is the number of POPs, general call function after pop (1) because the general return value only one
PS: When calling a function, there is no stack balance, the return is balanced, and the value requires a stack cleanup for the returned value.
Examples of data in C + + using LUA
extern "C" {#include "src/lualib.h" #include "src/lauxlib.h" #include "src/lua.h"} #include "iostream" Using namespace std;lua_state*l;int get_sum (int x, int y) {int sum=0;lua_getglobal (l, "get_sum");/* Call Function */lua_pushnumber (l, x); Lua_pushnumber (l, y); lua_call (l, 2, 3);/* Parameter 2, return value 3 x */cout << "top is " < < lua_gettop (L) << endl;cout << lua_tonumber (L, lua_gettop (L) - 0) << endl;cout << lua_tonumber (L, lua_gettop (L) - 1) << endl;cout << lua_tonumber (L, lua_gettop (l) - 2) << endl;cout << lua_tonumber (L, lua_gettop (L) - 2) < < endl;cout << lua_tonumber (L, lua_gettop (L) - 3) << Endl;cout << lua_tonumber(L, lua_gettop (L) - 4) << endl;cout << lua_tonumber (L, lua _gettop (L) - 5) << endl;cout << lua_tonumber (L, lua_gettop (L) - 6) << endl;cout << lua_tonumber (L, lua_gettop (L) - 7) << endl;cout << lua_tonumber (L, lua_gettop (l) - 8) << endl;cout << lua_tonumber (L, lua_gettop (L) - 9) < < endl;lua_pop (l, 3);/*function returned 3 values */cout << "\ n" << endl;lua _getglobal (l, "B");/* Get variable Press in stack */cout << "b=" <<lua_tonumber (L, lua_gettop ( L)/*1*/ ) << endl;lua_getglobal (l, "a");/* Get variable Press in stack */cout << "a = " << lua_tonumber (l, lua_gettop (L)/*1*/) << endl;lua_getglobal (l, ") C ");/* Get variable press in stack */cout << "c=" << lua_tonumber (L, lua_gettop (L)/*1*/) << Endl;lua_pop (l, 3);/* Clear Stack */cout << "Top is" << lua_gettop (l) << endl;return sum;} Int main () {l = lua_open (); Lual_openlibs (L); Lual_dofile (l, "A.lua");//cout << get_sum (1, 2) << endl;get_sum (1, 2); Lua_close (l); system ("pause"); return 0 ;}
a=10;
b=11;
c=12;
function Get_sum (arg_1,arg_2)
Return arg_1+arg_2, "100", "200";
End
Deep LUA stack interaction-cpp calling LUA data