2020-11-27 13:21:07 -05:00
|
|
|
#include "luastack.hpp"
|
2021-02-07 15:35:31 -05:00
|
|
|
#include "util.hpp"
|
2021-01-06 15:10:21 -05:00
|
|
|
#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
|
|
|
|
2021-01-06 15:10:21 -05:00
|
|
|
LuaFunctionReg *LuaFunctionReg::LuaFunctionRegistry;
|
|
|
|
|
|
2021-03-30 19:30:58 -04:00
|
|
|
bool LuaStack::ckboolean(LuaSlot s) const {
|
|
|
|
|
luaL_checktype(L_, s, LUA_TBOOLEAN);
|
|
|
|
|
return lua_toboolean(L_, s) ? true:false;
|
|
|
|
|
}
|
2021-01-06 15:10:21 -05:00
|
|
|
|
2021-02-28 14:01:59 -05:00
|
|
|
lua_Integer LuaStack::ckinteger(LuaSlot s) const {
|
|
|
|
|
luaL_checktype(L_, s, LUA_TNUMBER);
|
|
|
|
|
return 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);
|
2021-02-28 14:01:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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_;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-28 14:01:59 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2021-02-28 14:01:59 -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
|
|
|
}
|
2021-02-28 14:01:59 -05:00
|
|
|
|
2020-11-27 13:21:07 -05:00
|
|
|
|
2021-02-28 14:01:59 -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-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;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
2021-07-03 19:49:55 -04: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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-19 13:41:41 -04:00
|
|
|
void LuaStack::getclass(LuaSlot classtab, LuaSlot classname) const {
|
|
|
|
|
lua_checkstack(L_, 20);
|
|
|
|
|
LuaVar globtab, cname;
|
|
|
|
|
LuaStack LS(L_, globtab, cname);
|
|
|
|
|
|
|
|
|
|
// Convert class name to a table.
|
|
|
|
|
if (LS.isstring(classname)) {
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
LS.set(classtab, classname);
|
|
|
|
|
if (!LS.istable(classtab)) {
|
2021-03-28 15:12:09 -04:00
|
|
|
luaL_error(L_, "class can be a string or a table");
|
2021-03-19 13:41:41 -04:00
|
|
|
}
|
|
|
|
|
LS.rawget(cname, classtab, "__class");
|
|
|
|
|
if (!LS.isstring(cname)) {
|
|
|
|
|
luaL_error(L_, "table is not a class");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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.
|
2021-03-19 13:41:41 -04:00
|
|
|
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.
|
2021-03-19 13:41:41 -04:00
|
|
|
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-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-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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-19 13:41:41 -04:00
|
|
|
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());
|
2021-03-19 13:41:41 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-03 19:49:55 -04:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|