lua和c++的交互通过栈解释

栈的操作:https://www.cnblogs.com/lsgxeva/p/11158228.html
在这里插入图片描述

print(package.path)
package.path = package.path .. ";E:\\OGL5\\c++calllua\\luascript\\?.lua;"
print("hello")
print(package.path)

require 'person'

c = add(1,4)
print(c)
myName = "beauty girl"
helloTable = {
    
    name="xiaoming", age=12}

C++访问lua的table

	int main (int argc, char **argv) {
    
    
	/* 初始化Lua */

	lua_State* L = luaL_newstate();
	/* 载入Lua基本库 */
	luaL_openlibs(L);
	/* 运行脚本 */
	luaL_dofile(L, "hello.lua");				//加载脚本
    lua_getglobal(L, "helloTable"); 			//获取全局变量的background的值,并将其放入栈顶
    lua_pushstring(L, "name");					//name变量入栈,成栈顶
    lua_gettable(L, -2); 						//取出helloTable中name的值,替换掉栈顶name
    const char* sName = lua_tostring(L, -1); 	//取出栈顶的值
    lua_pop(L, 1);								//pop掉栈顶的值
    lua_pushstring(L, "age");					//age变量入栈,成栈顶
    lua_gettable(L, -2);						//取出age的值,替换栈顶的age变量
    int age = lua_tointeger(L, -1);				//取出栈顶的值


	/* 清除Lua */
	lua_close(L);
	system("pause");
}

lua_getfield

void lua_getfield (lua_State *L, int index, const char *k);
取t[k]的值放到栈顶,index指出栈中t表的位置, k为t的key值
L是state
index——是表t的在栈中的位置
k为key值

举例:

	lua_State* L = luaL_newstate();
	/* 载入Lua基本库 */
	luaL_openlibs(L);
	/* 运行脚本 */
	luaL_dofile(L, "hello.lua");
    lua_getglobal(L, "helloTable");
    lua_getfield(L, -1, "age");             //helloTable此时在栈顶,所以-1,取出age的值,放入栈顶
    int nParam_a = lua_tointeger(L, -1);    //取出栈顶元素值
    lua_getfield(L, -2, "name");            //helloTable此时在-2位置,因为age的值压入栈了,然后name被取出,入栈,此时栈里有3个元素
    char* nParam_b = lua_tostring(L, -1);   //取出栈顶元素值

    int count= lua_gettop(L);               //栈里有3个元素

栈里元素的个数:
int count= lua_gettop(L); //栈里有3个元素

lua_setfield函数:
(1)作用

在lua脚本中表达的操作是:t[k] = v. index指明了t在栈中的位置(说明:栈中首先要有表t),k代表了t的下标,v代表了赋予的值(即:放置在栈顶的元素)

(注:调用这个函数之前,栈中应该有:表t 和 栈顶元素v)
(2)对栈的操作
调用完成后,会将栈顶元素v弹出。

lua_State* L = luaL_newstate();
	/* 载入Lua基本库 */
	luaL_openlibs(L);
	/* 运行脚本 */
	luaL_dofile(L, "hello.lua");        //加载lua文件
    lua_getglobal(L, "helloTable");     //表入栈,在栈顶
    lua_pushstring(L, "nihao");         //nihao值入栈,为栈顶
    lua_setfield(L, -2, "name");        //-2是表在栈中的位置,lua_setfield将栈顶nihao的值赋给helloTable的name属性,并且将nihao栈顶pop掉,此时栈顶为表helloTable
    lua_getfield(L, -1, "name");        //-1位helloTable所在的位置,name为属性
    char* name = lua_tostring(L, -1);   //取出栈顶的元素

    int count= lua_gettop(L);              

C++调用lua的函数:

lua_State* L = luaL_newstate();
	/* 载入Lua基本库 */
	luaL_openlibs(L);
	/* 运行脚本 */
	luaL_dofile(L, "hello.lua");        //加载lua文件
    
    lua_getglobal(L, "add");
    
    if (!lua_isfunction(L, -1))
    {
    
    
        int a = 1;
    }

    lua_pushinteger(L, 1); //参数1
    lua_pushinteger(L, 2); //参数2
    lua_pcall(L, 2, 1, 0); // 2位参数的个数,1位返回值的个数,0位错误的函数回调,暂未使用,调用之后,返回值在栈顶
    int result = lua_tointeger(L, -1); 

在这里插入图片描述

lua_rawlen
[-0, +0, –]

size_t lua_rawlen (lua_State *L, int index);
返回给定索引处值的固有“长度”: 对于字符串,它指字符串的长度; 对于表;它指不触发元方法的情况下取长度操作(’#’)应得到的值; 对于用户数据,它指为该用户数据分配的内存块的大小; 对于其它值,它为 0 。
在这里插入图片描述

	lua_State* L = luaL_newstate();
	/* 载入Lua基本库 */
	luaL_openlibs(L);
	/* 运行脚本 */
	luaL_dofile(L, "hello.lua");        //加载lua文件

    lua_getglobal(L, "helloTable2");
    int count = lua_rawlen(L, -1); //这里返回的helloTable2的长度为3,而如果是helloTable,则返回的是长度为0

lua_rawgeti

/*

  • 从table中得到相应下标的元素
    lua_rawgeti(L, index, key)
    index表示table在栈中的位置
    key表示元素在table中的位置
  • /
    /
  • 把值放到table相应下标位置
    lua_rawseti(L, index, key)
    index表示table在栈中的位置
    key表示元素在table中的位置
  • */
	size_t count = lua_rawlen(L, -1);
	std::string* str = new std::string[count];
	loop(count)
	{
    
    
		lua_rawgeti(L, -1, i + 1);
		*(str + i) = lua_tostring(L, -1);
		lua_pop(L, 1);
	}

helloTable2 = {“xiaoming”, “xxx”, “3333”}

Guess you like

Origin blog.csdn.net/wodownload2/article/details/120371978