Lots of work on LuaStack
This commit is contained in:
92
luprex/syscpp/luastack.cpp
Normal file
92
luprex/syscpp/luastack.cpp
Normal file
@@ -0,0 +1,92 @@
|
||||
#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);
|
||||
}
|
||||
|
||||
std::string LuaStack::tostring(LuaSlot s) {
|
||||
size_t len;
|
||||
const char *str = lua_tolstring(L_, s, &len);
|
||||
return std::string(str, len);
|
||||
}
|
||||
|
||||
std::string LuaStack::checkstring(LuaSlot s) {
|
||||
size_t len;
|
||||
const char *str = luaL_checklstring(L_, s, &len);
|
||||
return std::string(str, len);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user