Lua and C ++ interaction --lua c api usage --02

1 is introduced in the header file lua

extern "C"
{

#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

}
2 Lua commonly used API

1 lua C ++ to interact with mainly achieved through lua virtual stack

2 when c lua wants to obtain the data, the data will need to be added lua in the stack, the stack by to get the value c

Drawing Functions

lua_pushnil void (Lua_State L);
lua_pushboolean void (Lua_State L, bool int);
lua_pushnumber void (Lua_State L double n);
lua_pushstring void (Lua_State
L, const char
s);
lua_pushuserdata void (Lua_State
L, void * p)
从栈取值

lua_toboolean BOOL (lua_State L, int IDX);
int lua_tonumber (lua_State
L, int IDX);
const char * lua_tostring (lua_State L, int IDX, size_t len);
void
lua_touserdata (lua_State
L, int IDX);
detecting the specified type

int lua_gettop (luaState * L);
void lua_settop (luaState * L, int IDX);
void lua_pushvalue (luaState * L, int IDX);
void lua_remove (luaState * L, int IDX);
void lua_insert (luaState * L, int IDX );
void lua_replace (luaState * L, int IDX);
when C and Lua calls to each other, Lua virtual stack strict accordance with LIFO rule action will only change the top of the stack section. But by the Lua API, you can query any element on the stack, or even in any position insert and delete elements.

Note that the order of the stack

3 Create a lua state machine and open the relevant library

lua_State * L = luaL_newstate (); // create lua state machine
luaopen_base (L); // open the base library
luaL_openlibs (L); // Open advanced libraries such as IO, String, the Math, the Table
lua_close (L); // close the state machine
4 is pressed into the data and the read data to the state machine

lua_pushstring(L,“i am test lua && lua”);
lua_pushnumber(L,19);
if (lua_isstring(L, 1))
{
cout << lua_tostring(L,1)<<endl;
}
if (lua_isnumber(L, 2))
{
cout << lua_tonumber(L,2)<<endl;
}

Guess you like

Origin blog.csdn.net/weixin_36285892/article/details/83716738