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

263 lines
6.8 KiB
C++
Raw Normal View History

2020-11-27 13:21:07 -05:00
#include "luastack.hpp"
2021-02-07 15:35:31 -05:00
#include "util.hpp"
#include <iostream>
2020-11-27 13:21:07 -05:00
2020-12-05 18:57:53 -05:00
LuaSpecial LuaRegistry(LUA_REGISTRYINDEX);
LuaSpecial LuaGlobals(LUA_GLOBALSINDEX);
LuaNilMarker LuaNil;
2021-01-02 13:31:18 -05:00
LuaNewTableMarker LuaNewTable;
LuaDiscardMarker LuaDiscard;
2021-02-07 17:26:48 -05:00
void LuaStack::clear_tagged_pointer(LuaSlot target) {
TaggedPointer *tp = (TaggedPointer*)lua_touserdata(L_, target.index());
tp->ptr = 0;
}
void LuaStack::make_tagged_pointer(LuaSlot target, void *ptr, LuaTypeTag tag, LuaDeleterFn del) {
TaggedPointer *tp = (TaggedPointer*)lua_newuserdata(L_, sizeof(TaggedPointer));
tp->ptr = ptr;
tp->tag = tag;
tp->del = del;
lua_pushlightuserdata(L_, (void*)tag);
lua_rawget(L_, LUA_REGISTRYINDEX);
if (lua_isnil(L_, -1)) luaL_error(L_, "type not registered with LuaDefineType");
lua_setmetatable(L_, -2);
lua_replace(L_, target);
}
int LuaStack::collect_tagged_pointer(lua_State *L) {
2021-02-07 17:26:48 -05:00
TaggedPointer *p = (TaggedPointer*)lua_touserdata(L, 1);
if (p==0) {
luaL_error(L, "lua deleter function received a non-userdata");
}
2021-02-07 15:35:31 -05:00
if (p->ptr != 0) {
p->del(p->ptr);
p->ptr = 0;
}
return 0;
}
void LuaStack::register_all_userdata(lua_State *L) {
2021-02-09 17:15:54 -05:00
LuaVar tab, lud, classtab, classname;
LuaStack LS(L, tab, lud, classtab, classname);
auto regs = LuaFunctionReg::all();
for (const LuaFunctionReg *r : regs) {
2021-02-07 15:35:31 -05:00
const std::string &name = util::tolower(r->get_name());
lua_CFunction tag = r->get_func();
std::string mode = r->get_mode();
2021-02-09 17:15:54 -05:00
LS.set(classname, name);
if (mode.find('t') != std::string::npos) { // Register type
LS.newtable(tab);
LS.setfield(tab, "type", name);
LS.setfield(tab, "__gc", collect_tagged_pointer);
2021-02-09 17:15:54 -05:00
LS.makeclass(classtab, classname);
LS.setfield(tab, "__index", classtab);
LS.setlightuserdata(lud, (void *)tag);
LS.rawset(LuaRegistry, lud, tab);
}
}
2021-01-23 16:31:16 -05:00
LS.result();
}
2021-01-02 13:31:18 -05:00
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;
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_;
}
2021-01-02 13:31:18 -05:00
void LuaStack::pop_any_value(std::string &s) const {
size_t len;
const char *str = luaL_cklstring(L_, -1, &len);
s = std::string(str, len);
lua_pop(L_, 1);
}
2020-11-27 13:21:07 -05:00
2021-01-02 13:31:18 -05:00
void LuaStack::pop_any_value(LuaAcceptNilNumber &s) const {
if (lua_isnil(L_, -1)) {
s.v = 0.0;
} else {
s.v = luaL_cknumber(L_, -1);
}
lua_pop(L_, 1);
2020-11-27 13:21:07 -05:00
}
2021-01-02 13:31:18 -05:00
void LuaStack::pop_any_value(LuaAcceptNilInteger &s) const {
if (lua_isnil(L_, -1)) {
s.v = 0;
} else {
s.v = luaL_ckinteger(L_, -1);
2020-12-05 18:57:53 -05:00
}
2021-01-02 13:31:18 -05:00
lua_pop(L_, 1);
2020-11-27 13:21:07 -05:00
}
2021-01-02 13:31:18 -05:00
void LuaStack::pop_any_value(LuaAcceptNilString &s) const {
if (lua_isnil(L_, -1)) {
s.v = "";
} else {
size_t len;
const char *str = luaL_cklstring(L_, -1, &len);
s.v = std::string(str, len);
}
lua_pop(L_, 1);
2020-11-27 13:21:07 -05:00
}
2021-01-02 13:31:18 -05:00
std::string LuaStack::ckstring(LuaSlot s) const {
2020-11-27 13:21:07 -05:00
size_t len;
2021-01-02 13:31:18 -05:00
const char *str = luaL_cklstring(L_, s, &len);
2020-11-27 13:21:07 -05:00
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);
}
2021-01-02 13:31:18 -05:00
void LuaStack::getmetatable(LuaSlot mt, LuaSlot tab) const {
lua_getmetatable(L_, tab);
lua_replace(L_, mt);
}
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;
}
void LuaStack::newtable(LuaSlot target) const {
lua_newtable(L_);
lua_replace(L_, target);
}
2020-12-05 18:57:53 -05:00
2021-02-09 17:15:54 -05:00
void LuaStack::makeclass(LuaSlot classtab, LuaSlot classname) const {
checkstring(classname);
2021-02-10 16:22:24 -05:00
// Special case: if the classname is _G, detect and error.
if (equal(classname, "_G")) {
luaL_error(L_, "_G is explicitly not allowed as a class name");
2021-02-09 17:15:54 -05:00
}
// Get the classtab from the global environment.
// Create it if it doesn't exist.
rawget(classtab, LuaGlobals, classname);
if (isnil(classtab)) {
newtable(classtab);
rawset(LuaGlobals, classname, classtab);
}
// If the name isn't bound to a table, abort.
if (!istable(classtab)) {
luaL_error(L_, "%s is not a class", ckstring(classname).c_str());
}
// Repair the special fields.
setfield(classtab, "__index", classtab);
setfield(classtab, "__class", classname);
}
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::setlightuserdata(LuaSlot target, void *p) const {
lua_pushlightuserdata(L_, p);
lua_replace(L_, target);
}
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-02-07 15:35:31 -05:00
LuaDefine(system_type, "f") {
LuaArg obj;
LuaRet tname;
LuaVar mt;
LuaStack LS(L, obj, tname, mt);
int type = LS.type(obj);
if (type == LUA_TUSERDATA) {
LS.getmetatable(mt, obj);
LS.getfield(tname, mt, "type");
} else {
LS.set(tname, lua_typename(L, type));
}
return LS.result();
}