int lua_getglobal (lua_State *L, const char *name); Pushes onto the stack the value of the global name. Returns the type of that value.
void lua_setglobal (lua_State *L, const char *name); Pops a value from the stack and sets it as the new value of global name.
cpp 读取 lua 表
if (lua_istable(m_luaState,-1)) { // Push another reference to the table on top of the stack (so we know // where it is, and this function can work for negative, positive and // pseudo indices lua_pushvalue(m_luaState, -1); // stack now contains: -1 => table lua_pushnil(m_luaState); // stack now contains: -1 => nil; -2 => table while (lua_next(m_luaState, -2)) { // stack now contains: -1 => value; -2 => key; -3 => table // copy the key so that lua_tostring does not modify the original lua_pushvalue(m_luaState, -2); // stack now contains: -1 => key; -2 => value; -3 => key; -4 => table string strKey = lua_tostring(m_luaState, -1); retVar = lua_tonumber(m_luaState, -2); // pop value + copy of key, leaving original key lua_pop(m_luaState, 2); // stack now contains: -1 => key; -2 => table } }
栈操作
默认栈空间是 20 个。一般情况下都可以用。
可以使用下面函数检查栈空间是否可用。
-- 返回错误码 int lua_checkstack (lua_State *L, int sz); -- 返回错误信息 void luaL_checkstack (lua_State *L, int sz, const char *msg);
// 建一个新表 栈高度+1, 栈顶元素是新table // 创建一个新的table, 并把它放在栈顶. narr和nrec分别指定该table的array部分和hash部分的预分配元素数量 voidlua_createtable(lua_State *L, int narr, int nrec) // 用于设置table元素的方法 voidlua_settable(lua_State *L, int index); voidlua_rawset(lua_State *L, int index); voidlua_setfield(lua_State *L, int index, constchar *k); voidlua_rawseti(lua_State *L, int index, int n);
类型判断
函数原型:lua_is*
int lua_isstring(lua_State L, int index) 判断index指明位置上的元素是为对应的类型,这里是string. 如果是则返回1,否则返回0;不会改变栈的大小,内容
获取数据
使用 lua_to* 函数。
intlua_toboolean(lua_State *L, int index); constchar *lua_tolstring(lua_State *L, int index, size_t *len); lua_State *lua_tothread(lua_State *L, int index); lua_Number lua_tonumber(lua_State *L, int index); lua_Integer lua_tointeger(lua_State *L, int index);
lua_tolstring and lua_tothread return NULL for values with incorrect types. The numeric functions, however, have no way to signal a wrong type, so they simply return zero.
引入的新函数: lua_Number lua_tonumberx (lua_State *L, int idx, int *isnum); lua_Integer lua_tointegerx (lua_State *L, int idx, int *isnum);
The out parameter isnum returns a Boolean that indicates whether the Lua value was successfully coerced to the desired type.
intlua_gettop(lua_State *L); voidlua_settop(lua_State *L, int index); voidlua_pushvalue(lua_State *L, int index); voidlua_rotate(lua_State *L, int index, int n); voidlua_remove(lua_State *L, int index); voidlua_insert(lua_State *L, int index); voidlua_replace(lua_State *L, int index); voidlua_copy(lua_State *L, int fromidx, int toidx);
doubleLuaHelper::test_var() { double result = 0; string strErr;
/* opens Lua */ lua_State* m_luaState = luaL_newstate(); /* opens the standard libraries */ luaL_openlibs(m_luaState); if (luaL_loadfile(m_luaState, "sum.lua") || lua_pcall(m_luaState, 0, 0, 0)) { strErr = lua_tostring(m_luaState, -1); lua_pop(m_luaState, 1); /* pop error message from the stack */ } else { double up_right1_min = 15.3456; double up_left1_max = 10.1324; double up_left1_mean = 10.12; double down_left1_mean = 7.12;