#include "luastack.hpp" LuaSpecial LuaRegistry(LUA_REGISTRYINDEX); LuaSpecial LuaGlobals(LUA_GLOBALSINDEX); LuaNilMarker LuaNil; void LuaStack::count_slots_finalize(int narg, int nvar, int nret) { narg_ = narg; nret_ = nret; nvar_ = nvar; ngap_ = nret - nvar - narg; if (ngap_ < 0) ngap_ = 0; int argtop = lua_gettop(L_); argpos_ = argtop + 1 - narg_; gappos_ = argpos_ + narg_; varpos_ = gappos_ + ngap_; retpos_ = varpos_ + nvar_; rettop_ = retpos_ + nret_ - 1; finaltop_ = argpos_ + nret_ - 1; } void LuaStack::clear_frame() { lua_settop(L_, varpos_ - 1); for (int i = 0; i < nvar_ + nret_; i++) { lua_pushnil(L_); } } int LuaStack::result() { lua_settop(L_, rettop_); int i = finaltop_; for (int j = 0; j < nret_; j++) { lua_replace(L_, i); i -= 1; } lua_settop(L_, finaltop_); return nret_; } LuaFunctionReg *LuaFunctionReg::LuaFunctionRegistry; LuaFunctionReg::LuaFunctionReg(int m, const char *n, lua_CFunction f) { mode_ = m; name_ = n; func_ = f; next_ = LuaFunctionRegistry; LuaFunctionRegistry = this; } LuaFunctionReg::List LuaFunctionReg::all() { LuaFunctionReg::List result; for (const LuaFunctionReg *r = LuaFunctionRegistry; r != 0; r = r->next_) { result.push_back(r); } return result; } std::string LuaStack::tostring(LuaSlot s) const { size_t len; const char *str = lua_tolstring(L_, s, &len); return std::string(str, len); } std::string LuaStack::checkstring(LuaSlot s) const { size_t len; const char *str = luaL_checklstring(L_, s, &len); return std::string(str, len); } void LuaStack::clearmetatable(LuaSlot tab) const { lua_pushnil(L_); lua_setmetatable(L_, tab); } void LuaStack::setmetatable(LuaSlot tab, LuaSlot mt) const { lua_pushvalue(L_, mt); lua_setmetatable(L_, tab); } void LuaStack::checknometa(LuaSlot index) const { if (lua_istable(L_, index)) { if (!lua_getmetatable(L_, index)) { return; } } luaL_error(L_, "expected simple table with no metatable"); } int LuaStack::next(LuaSlot tab, LuaSlot key, LuaSlot value) const { lua_pushvalue(L_, key); int ret = lua_next(L_, tab); if (ret != 0) { lua_replace(L_, value); lua_replace(L_, key); } return ret; } void LuaStack::newtable(LuaSlot target) const { lua_newtable(L_); lua_replace(L_, target); } void LuaStack::check_nret(int xnret, int otop, int nret) const { int ntop = lua_gettop(L_); if ((nret != xnret)||(ntop != otop + xnret)) { luaL_error(L_, "expected %d return values", xnret); } }