最近研究如何通过C++将数组或向量数据压入Lua栈,然后通过Lua对数组或向量进行处理之后,返回处理结果给C++。使用Userdata当然是一种选择,但是需要在C++中为Lua定义操作Userdata的接口,比较麻烦。最后还是决定用Lua Table来实现。
在Lua中,table是一种很强大的存在,可以用来处理数组、向量、字典等常用数据结构,并且可以使用nil之外的任意类型的值做索引,且可根据需要动态扩容。
前面的文章已经分享了如何通过C++遍历Lua表中的数据进行处理:lua_next()函数遍历lua表
本篇是一个反向的尝试,通过表的方式,使得Lua能够处理C++中的数组或向量数据。简单起见,这篇先介绍处理一维表。
先来定义一个函数,用于将向量压入Lua栈,并调用Lua函数进行处理,并读取处理之后的结果:
/* 定义一个函数,将一个字符串向量中的内容压入Lua栈,并调用Lua函数对其进行处理 */
int put_array_to_lua(lua_State *L, std::vector<std::string> str_vec)
{
lua_getglobal(L, "test_table");
std::cout << "lua_isfunction(L, -1) = " << lua_isfunction(L, -1) << std::endl; //判断栈顶是否函数
lua_newtable(L);
std::cout << "luaL_typename(L, -1) = " << luaL_typename(L, -1) << std::endl; //现在table位于栈顶
std::cout << "luaL_typename(L, -2) = " << luaL_typename(L, -2) << std::endl; //函数位于-2位置
for(int i = 0; i < str_vec.size(); i++)
{
lua_pushinteger(L, i + 1); //压入表key值,这里是1~n的整数
lua_pushstring(L, str_vec[i].c_str()); // 压入key对应的value值
std::cout << "luaL_typename(L, -3) = " << luaL_typename(L, -3) << std::endl;
lua_settable(L, -3); //此时表位于-3
}
lua_pcall(L, 1, 1, 0); //调用名为“test_table”的lua函数
int result = lua_tointeger(L, -1); //在栈顶读取函数的返回结果
lua_pop(L, 1); //将函数出栈
return result;
}
我们来尝试调用该函数:
#include <iostream>
#include <vector>
#include <string>
#include "lua.hpp"
/* 初始化函数 */
lua_State* script_initialize(const char* script_name)
{
lua_State *pLua = luaL_newstate(); //创建一个Lua虚拟机
if(!pLua)
{
std::cout << "Failed to open Lua!" << std::endl;
return NULL;
}
luaL_openlibs(pLua); //将标准类库加载到Lua虚拟机
luaL_dofile(pLua, script_name); //加载并运行lua文件
return pLua;
}
int main()
{
std::vector<std::string> strs = {"My", "name", "is", "Lua"};
lua_State* pL = script_initialize("table_test.lua");
if(!pL)
{
std::cout << "Error initialize lua state!" << std::endl;
exit(1);
}
int num = put_array_to_lua(pL, strs);
std::cout << "After lua call, table element num is " << num << std::endl;
lua_close(pL);
return 0;
}
编译一下:g++ -std=c++11 table_test.cpp -o table_test -I/usr/local/include -L/usr/local/lib -llua -ldl
对应的Lua脚本如下:
function test_table(mytable)
local table_len = #mytable --获取表长度
print("table size: ", table_len) --打印表长度
for k, v in pairs(mytable) do --打印表中的元素
print(v)
end
return table_len --返回表长度
end
执行一下看看:
成功!