lua创建table,子table
lua_newtable(L);
lua_pushinteger(L, 123); // value
lua_setfield(L, -2, "id"); // key
lua_pushstring(L, "Lily"); // value
lua_setfield(L, -2, "name"); // key
// 添加子table
lua_pushstring(L, "others"); // key
lua_newtable(L); // 子table
lua_pushstring(L, "linear algebra"); // value
lua_setfield(L, -2, "math"); // key
lua_pushstring(L, "badminton"); // value
lua_setfield(L, -2, "habits"); // key
lua_settable(L, -3); // others push value
lua c 修改查找的路径
// 环境路径修改,c中指定package查找的路径
// 获取全局变量package
lua_getglobal(m_state, "package");
if (lua_type(m_state, -1) != LUA_TTABLE) {
// 找不到直接运行,有的脚本不需要依赖
std::cout << "can not find global table: package"<< std::endl;
}
else {
// 当前exe路径的./clibs查找.dll后缀的
std::string cpath = ".\\clibs\\?.dll;";
// 当前exe路径的./或./lua查找.lua后缀的
std::string path = ".\\?.lua;.\\lua\\?.lua;";
// 修改package.cpath
lua_pushstring(m_state, cpath.data());
lua_setfield(m_state, -2, "cpath");
// 修改package.path
lua_pushstring(m_state, path.data());
lua_setfield(m_state, -2, "path");
// package出栈
lua_pop(m_state, 1);
}