C/C++调用lua

1.调用普通变量

//xx.lua
teacher = "guilinwang"
org = "nzsoft"
object = "lua"
age = 18
//main.cpp
#include 
#include "lua.hpp"

extern "C"{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
};

int main()
{
	lua_State *L = luaL_newstate();
	luaL_openlibs(L);
	luaL_dofile(L,"xx.lua");//加载编译运行xx.lua
	
	lua_getgloba(L,"teacher");//lua入栈
	lua_getgloba(L,"org");
	lua_getgloba(L,"object");
	lua_getgloba(L,"age");
	
	char num= lua_tonumber(L,-1);
	printf("str = %d",num);
	lua_pop(L,1);
	
	char *str = lua_tostring(L,-1);
	printf("str = %d",str);
	
	lua_close(L);
	
	return 0;
}

测试结果:
C/C++调用lua_第1张图片
2.访问lua全局表字段

//xx.lua
tab = {
      teacher = "guilinwang"
      org = "nzsoft"
      object = "lua"
      age = 18
}

void lua_gettable (lua_State *L, int index);
把 t[k] 值压入堆栈,这里的 t 是指有效索引 index 指向的值,而 k 则是栈顶放的值。这
个函数会弹出堆栈上的 key,把结果放在栈上原来 key 的相同位置。

//main.cpp
#include 
#include "lua.hpp"

extern "C"{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
};

int main()
{
	lua_State *L = luaL_newstate();
	luaL_openlibs(L);
	luaL_dofile(L,"xx.lua");//加载编译运行xx.lua
	
	lua_getglobal(L,"tab");
	//lua_pushstring(L,"teacher");
	//lua_gettable(L,-2);//获取表,-2的位置是tab,-1位置的key被换成了value
	lua_getfiela(L,-1,"teacher");//更简单的方法,替换上面两句,直接去表中取字段
	
	char *str = lua_tostring(L,-1)//现在-1的位置是value值,-2是tab
	printf("%s\n",str);
	lua_pop(L,-1);//把value弹出栈
	
	lua_pushstring(L,"age");
	lua_gettable(L,-2);
	int number = lua_tonumber(L,-1);
	printf("%d\n",number );
	
	lua_close(L);
	
	return 0;
}

测试结果:
C/C++调用lua_第2张图片
3.访问lua全局函数(无参无返回)

//xx.lua
function func()
       print("abc")
end
//main.cpp
//加载常用头文件
int main()
{
    lua_State *L = luaL_newstate();
	luaL_openlibs(L);
	luaL_dofile(L,"xx.lua");//加载编译运行xx.lua
	
	lua_getglobal(L,"func");
	lua_pcall(L,0,0,0);//执行完后函数会出栈
	
	lua_close(L);
    return 0;
}

执行结果:
会打印出abc

4.访问lua全局函数(有参无返回)

//xx.lua
function func(num,string)
       print("number = ",num)
       print("string = ",string)
end
//main.cpp
int main()
{
    lua_State *L = luaL_newstate();
	luaL_openlibs(L);
	luaL_dofile(L,"xx.lua");//加载编译运行xx.lua
	
	lua_getglobal(L,"func");
	lua_pushnumber(L,101);
	lua_pushstring(L,"nzsoft");
	lua_pcall(L,2,0,0);//第二个参数代表有多少个参数,第三个参数代表有多少个返回值
	
	lua_close(L);
    return 0;
}

测试结果:
在这里插入图片描述
5.访问lua全局函数(有参有返回)

//xx.lua
function func(num,string)
       print("number = ",num)
       print("string = ",string)
       return 1,2,3,4,5
end
//main.cpp
//加载常用头文件
int main()
{
    lua_State *L = luaL_newstate();
	luaL_openlibs(L);
	luaL_dofile(L,"xx.lua");//加载编译运行xx.lua
	
	lua_getglobal(L,"func");
	lua_pushnumber(L,101);
	lua_pushstring(L,"nzsoft");
	lua_pcall(L,2,5,0);//第二个参数代表有多少个参数,第三个参数代表有多少个返回值
	printf("1 -> %d\n",lua_tointeger(L,-1));//注意栈的顺序
	printf("2 -> %d\n",lua_tonumber(L,-2));
	printf("3 -> %d\n",lua_tonumber(L,-3));
	
	lua_close(L);
    return 0;
}

测试结果:
C/C++调用lua_第3张图片
6.访问lua表中的函数

//xx.lua
tab = {
      teacher = "guilinwang"
      org = "nzsoft"
      object = "lua"
      age = 18
      func = function(num,string)
            print("number = ",num)
            print("string = ",string)
            return 1,2,3,4,5
      end
}
//main.cpp
//加载常用头文件
int main()
{
    lua_State *L = luaL_newstate();
	luaL_openlibs(L);
	luaL_dofile(L,"xx.lua");//加载编译运行xx.lua
	
	lua_getglobal(L,"tab");//现在栈中有一个元素,是个表
	lua_getfield(L,-1,"func");//现在栈中有两个元素
	lua_remove(L,-2);//把表移除,只剩下func函数
	
	lua_pushnumber(L,101);
	lua_pushstring(L,"nzsoft");
	lua_pcall(L,2,5,0);//第二个参数代表有多少个参数,第三个参数代表有多少个返回值
	printf("1 -> %d\n",lua_tointeger(L,-1));//注意栈的顺序
	printf("2 -> %d\n",lua_tonumber(L,-2));
	printf("3 -> %d\n",lua_tonumber(L,-3));
	
	lua_close(L);
    return 0;
}

测试结果:
C/C++调用lua_第4张图片
7.自制 lua 编译器

//main.cpp
//加载常用头文件
int main()
{
    lua_State *L = luaL_newstate();
	luaL_openlibs(L);
	//luaL_dofile(L,"xx.lua");//加载编译运行xx.lua
	
	char buf[2014];
	while(fgets(buf,2014,stdin))
	{
	     luaL_dostring(L,buf);
	}
	
	lua_close(L);
    return 0;
}

测试结果:
命令行模式下输入print(“china”)
C/C++调用lua_第5张图片
命令行模式下输入i = 100; print(i)
在这里插入图片描述

你可能感兴趣的:(C/C++与Lua的交互)