Files
integration/luprex/core/cpp/luastack.cpp

317 lines
8.1 KiB
C++
Raw Normal View History

2020-11-27 13:21:07 -05:00
#include "luastack.hpp"
#include <iostream>
2020-11-27 13:21:07 -05:00
2020-12-05 18:57:53 -05:00
LuaSpecial LuaRegistry(LUA_REGISTRYINDEX);
LuaNilMarker LuaNil;
2021-01-02 13:31:18 -05:00
LuaNewTableMarker LuaNewTable;
LuaFunctionReg::LuaFunctionReg(const char *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;
}
2020-11-27 13:21:07 -05:00
LuaFunctionReg *LuaFunctionReg::LuaFunctionRegistry;
2021-08-13 17:02:35 -04:00
bool LuaStack::issortablekey(LuaSlot s) const {
int type = lua_type(L_, s);
return (type == LUA_TBOOLEAN) || (type == LUA_TNUMBER) || (type == LUA_TSTRING);
}
bool LuaStack::ckboolean(LuaSlot s) const {
luaL_checktype(L_, s, LUA_TBOOLEAN);
return lua_toboolean(L_, s) ? true:false;
}
lua_Integer LuaStack::ckinteger(LuaSlot s) const {
luaL_checktype(L_, s, LUA_TNUMBER);
return lua_tointeger(L_, s);
}
2021-07-09 18:07:06 -04:00
int LuaStack::ckint(LuaSlot s) const {
luaL_checktype(L_, s, LUA_TNUMBER);
return (int)lua_tointeger(L_, s);
}
lua_Number LuaStack::cknumber(LuaSlot s) const {
luaL_checktype(L_, s, LUA_TNUMBER);
return lua_tonumber(L_, s);
}
std::string LuaStack::ckstring(LuaSlot s) const {
luaL_checktype(L_, s, LUA_TSTRING);
size_t len;
2021-02-28 14:24:59 -05:00
const char *str = lua_tolstring(L_, s, &len);
return std::string(str, len);
}
lua_State *LuaStack::ckthread(LuaSlot s) const {
luaL_checktype(L_, s, LUA_TTHREAD);
return lua_tothread(L_, s);
}
2020-11-27 13:21:07 -05:00
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::pop_any_value(lua_Integer &s) const {
luaL_checktype(L_, -1, LUA_TNUMBER);
s = lua_tointeger(L_, -1);
2021-01-02 13:31:18 -05:00
lua_pop(L_, 1);
2020-11-27 13:21:07 -05:00
}
void LuaStack::pop_any_value(lua_Number &s) const {
luaL_checktype(L_, -1, LUA_TNUMBER);
s = lua_tonumber(L_, -1);
2021-01-02 13:31:18 -05:00
lua_pop(L_, 1);
2020-11-27 13:21:07 -05:00
}
2020-11-27 13:21:07 -05:00
void LuaStack::pop_any_value(std::string &s) const {
luaL_checktype(L_, -1, LUA_TSTRING);
size_t len;
const char *str = lua_tolstring(L_, -1, &len);
s = std::string(str, len);
2021-01-02 13:31:18 -05:00
lua_pop(L_, 1);
2020-11-27 13:21:07 -05:00
}
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);
}
2021-08-13 17:02:35 -04:00
bool LuaStack::getmetatable(LuaSlot mt, LuaSlot tab) const {
if (lua_getmetatable(L_, tab)) {
lua_replace(L_, mt);
return true;
} else {
lua_pushnil(L_);
lua_replace(L_, mt);
return false;
}
2021-01-02 13:31:18 -05:00
}
2020-11-27 14:24:37 -05:00
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;
}
2021-02-28 16:32:42 -05:00
void LuaStack::getglobaltable(LuaSlot target) const {
lua_pushglobaltable(L_);
lua_replace(L_, target);
}
2020-11-27 14:24:37 -05:00
void LuaStack::newtable(LuaSlot target) const {
lua_newtable(L_);
lua_replace(L_, target);
}
2020-12-05 18:57:53 -05:00
void LuaStack::createtable(LuaSlot target, int narr, int nrec) const {
lua_createtable(L_, narr, nrec);
lua_replace(L_, target);
}
2021-02-17 13:38:22 -05:00
lua_State *LuaStack::newthread(LuaSlot target) const {
lua_State *result = lua_newthread(L_);
lua_replace(L_, target);
return result;
}
void LuaStack::getclass(LuaSlot classtab, LuaSlot classname) const {
lua_checkstack(L_, 20);
LuaVar globtab, cname;
LuaStack LS(L_, globtab, cname);
2021-08-10 10:41:06 -04:00
if (!LS.isstring(classname)) {
luaL_error(L_, "Not a class name: %s", lua_tostring(L_, classname));
}
// Convert class name to a table.
2021-08-10 10:41:06 -04:00
if (LS.rawequal(classname, "_G")) {
luaL_error(L_, "_G is explicitly not allowed as a class name");
}
LS.getglobaltable(globtab);
LS.rawget(classtab, globtab, classname);
if (!LS.istable(classtab)) {
luaL_error(L_, "Not a class: %s", lua_tostring(L_, classname));
}
LS.rawget(cname, classtab, "__class");
if (!LS.rawequal(cname, classname)) {
luaL_error(L_, "Not a class: %s", lua_tostring(L_, classname));
}
// OK, we're done.
LS.result();
}
2021-02-09 17:15:54 -05:00
void LuaStack::makeclass(LuaSlot classtab, LuaSlot classname) const {
2021-02-28 16:32:42 -05:00
lua_checkstack(L_, 20);
2021-02-28 14:24:59 -05:00
LuaVar globtab;
LuaStack LS(L_, globtab);
2021-02-09 17:15:54 -05:00
2021-02-28 14:24:59 -05:00
// Validate the class name.
if (!LS.isstring(classname)) {
luaL_error(L_, "Not a class name: %s", lua_tostring(L_, classname));
}
2021-02-28 14:45:09 -05:00
if (LS.rawequal(classname, "_G")) {
2021-02-10 16:22:24 -05:00
luaL_error(L_, "_G is explicitly not allowed as a class name");
2021-02-09 17:15:54 -05:00
}
2021-02-28 14:24:59 -05:00
// Fetch the global environment from the registry.
LS.getglobaltable(globtab);
2021-02-09 17:15:54 -05:00
// Get the classtab from the global environment.
// Create it if it doesn't exist.
2021-02-28 14:24:59 -05:00
LS.rawget(classtab, globtab, classname);
if (LS.isnil(classtab)) {
LS.newtable(classtab);
LS.rawset(globtab, classname, classtab);
2021-02-09 17:15:54 -05:00
}
// If the name isn't bound to a table, abort.
2021-02-28 14:24:59 -05:00
if (!LS.istable(classtab)) {
2021-02-09 17:15:54 -05:00
luaL_error(L_, "%s is not a class", ckstring(classname).c_str());
}
// Repair the special fields.
2021-08-10 10:41:06 -04:00
LS.settabletype(classtab, TAB_CLASS);
2021-02-28 14:24:59 -05:00
LS.rawset(classtab, "__index", classtab);
LS.rawset(classtab, "__class", classname);
// Put the stack back.
LS.result();
2021-02-09 17:15:54 -05:00
}
2021-08-13 17:02:35 -04:00
void LuaStack::movesortablekey(LuaSlot key, lua_State *L) {
int type = lua_type(L_, key);
switch (type) {
case LUA_TBOOLEAN:
lua_pushboolean(L, lua_toboolean(L_, key));
break;
case LUA_TNUMBER:
lua_pushnumber(L, lua_tonumber(L_, key));
break;
case LUA_TSTRING: {
size_t len;
const char *str = lua_tolstring(L_, key, &len);
lua_pushlstring(L, str, len);
break;
}
default:
luaL_error(L, "movesortablekey: not a sortable key");
}
}
2021-02-10 16:22:24 -05:00
void LuaStack::makesubtable(LuaSlot sub, LuaSlot tab, const char *name) const {
rawget(sub, tab, name);
if (istable(sub)) {
return;
} else if (isnil(sub)) {
newtable(sub);
rawset(tab, name, sub);
return;
} else {
luaL_error(L_, "%s is not a table", name);
}
}
void LuaStack::cleartable(LuaSlot tab) const {
checktable(tab);
lua_pushnil(L_);
while (lua_next(L_, tab.index()) != 0) {
lua_pop(L_, 1); // Pop the old value.
lua_pushvalue(L_, -1); // Clone the key
lua_pushnil(L_); // Push the new value.
2021-07-09 13:52:03 -04:00
lua_rawset(L_, tab.index());
}
}
int LuaStack::rawlen(LuaSlot obj) const {
return lua_rawlen(L_, obj.index());
}
2020-12-05 18:57:53 -05:00
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);
}
}
2021-08-10 10:41:06 -04:00
LuaStack::TableType LuaStack::gettabletype(LuaSlot tab) {
uint16_t bits = lua_getflagbits(L_, tab.index());
return TableType(bits & 0x000F);
}
void LuaStack::settabletype(LuaSlot tab, TableType t) {
lua_modflagbits(L_, tab.index(), 0x000F, t);
}
bool LuaStack::getvisited(LuaSlot tab) {
uint16_t bits = lua_getflagbits(L_, tab.index());
return (bits & 0x0010);
}
void LuaStack::setvisited(LuaSlot tab, bool visited) {
lua_modflagbits(L_, tab.index(), 0x0010, visited ? 0x0010 : 0);
}