1. Introduction
This section describes some of the LUA APIs for stack operations, data type judgments, which you can use to get variable values in your scripts.
2. Steps
Write Test01.lua script, create a console C + + program in VS2003 and configure it correctly, perform view results, modify Test02.lua script and view execution results
3. Test scripts
Here is the Lua script for testing
Copy Code code as follows:
function Plustwo (x)
Local A = 2;
return x+a;
End
rows = 6;
cols = plustwo (rows);
The above script defines a function, two global variables (the LUA script variable is global by default). After the C + + program, we will get the two variables through the stack operation rows, cols.
4. Console Program
Copy Code code as follows:
#include <iostream>
extern "C"
{
#include "Lua.h"
#include "Lauxlib.h"
#include "Lualib.h"
}
using namespace Std;
int main (int argc, char* argv[])
{
cout << "01_read_stack" << Endl;
/**//* Create a LUA vmachine * *
Lua_state *l = Lua_open ();
Luaopen_base (L);
Luaopen_table (L);
Lual_openlibs (L);
Luaopen_string (L);
Luaopen_math (L);
int ierror;
ierror = Lual_loadfile (L, "...). /test01.lua ");
if (ierror)
{
cout < < "Load script failed!" << lua_tostring (L,-1) << Endl;
Lua_close (L);
return 1;
}
ierror = Lua_pcall (L, 0, 0, 0);
if (ierror)
{
cout < < "Pcall FAILED" << lua_tostring (L,-1) << ierror<< Endl;
Lua_close (L);
return 1;
}
lua_getglobal (L, "rows");
Lua_getglobal (L, " Cols ");
if (!lua_isnumber (L,-2))
{
cout << "[rows] are not a number" << Endl;
Lua_close (L);
return 1;
}
if (!lua_isnumber (L,-1))
{
cout << "[cols] is not a number" << Endl;
Lua_close (L);
return 1;
}
cout << "[Rows]"
<< static_cast<int> (Lua_tonumber (L,-2))
<< "[cols]"
<< static_cast<int> (Lua_tonumber (L,-1))
<< Endl;
Lua_pop (l,2);
Lua_close (L);
return 0;
}