2020-11-27 13:21:07 -05:00
|
|
|
#include "luastack.hpp"
|
|
|
|
|
|
|
|
|
|
LuaSlot LuaRegistry(LUA_REGISTRYINDEX);
|
|
|
|
|
LuaSlot LuaGlobals(LUA_GLOBALSINDEX);
|
|
|
|
|
|
|
|
|
|
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_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaStack::reg(lua_State *L, const char *classname, const char *funcname, lua_CFunction fn) {
|
|
|
|
|
int top = lua_gettop(L);
|
|
|
|
|
lua_pushvalue(L, LUA_GLOBALSINDEX);
|
|
|
|
|
luaL_Reg reg;
|
|
|
|
|
reg.name = funcname;
|
|
|
|
|
reg.func = fn;
|
|
|
|
|
luaL_register(L, classname, ®);
|
|
|
|
|
lua_settop(L, top);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaStack::setnil(LuaSlot target) const {
|
|
|
|
|
lua_pushnil(L_);
|
|
|
|
|
lua_replace(L_, target);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaStack::setboolean(LuaSlot target, bool b) const {
|
|
|
|
|
lua_pushboolean(L_, b ? 1 : 0);
|
|
|
|
|
lua_replace(L_, target);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaStack::setboolean(LuaSlot target, int b) const {
|
|
|
|
|
lua_pushboolean(L_, b);
|
|
|
|
|
lua_replace(L_, target);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaStack::setstring(LuaSlot target, const char *str) const {
|
|
|
|
|
lua_pushstring(L_, str);
|
|
|
|
|
lua_replace(L_, target);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaStack::setstring(LuaSlot target, const std::string &str) const {
|
|
|
|
|
lua_pushlstring(L_, str.c_str(), str.size());
|
|
|
|
|
lua_replace(L_, target);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaStack::setnumber(LuaSlot target, double value) const {
|
|
|
|
|
lua_pushnumber(L_, value);
|
|
|
|
|
lua_replace(L_, target);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-27 14:24:37 -05:00
|
|
|
std::string LuaStack::tostring(LuaSlot s) const {
|
2020-11-27 13:21:07 -05:00
|
|
|
size_t len;
|
|
|
|
|
const char *str = lua_tolstring(L_, s, &len);
|
|
|
|
|
return std::string(str, len);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-27 14:24:37 -05:00
|
|
|
std::string LuaStack::checkstring(LuaSlot s) const {
|
2020-11-27 13:21:07 -05:00
|
|
|
size_t len;
|
|
|
|
|
const char *str = luaL_checklstring(L_, s, &len);
|
|
|
|
|
return std::string(str, len);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-27 14:24:37 -05:00
|
|
|
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);
|
|
|
|
|
}
|