2020-11-27 13:21:07 -05:00
|
|
|
#include "luastack.hpp"
|
2021-08-23 23:34:30 -04:00
|
|
|
#include <cassert>
|
2022-02-25 19:57:23 -05:00
|
|
|
#include <cstdio>
|
2022-06-06 23:03:26 -04:00
|
|
|
#include <climits>
|
2022-07-22 16:00:37 -04:00
|
|
|
#include "wrap-string.hpp"
|
|
|
|
|
#include "wrap-set.hpp"
|
2022-07-22 17:07:40 -04:00
|
|
|
#include "wrap-sstream.hpp"
|
2024-03-13 15:39:09 -04:00
|
|
|
#include "util.hpp"
|
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;
|
|
|
|
|
|
2022-03-16 17:05:20 -04:00
|
|
|
LuaFunctionReg::LuaFunctionReg(const char *n, const char *a, const char *d, bool s, lua_CFunction f) {
|
2021-01-02 13:31:18 -05:00
|
|
|
name_ = n;
|
2021-12-15 23:03:43 -05:00
|
|
|
args_ = a;
|
|
|
|
|
docs_ = d;
|
2021-01-02 13:31:18 -05:00
|
|
|
func_ = f;
|
2022-03-16 17:05:20 -04:00
|
|
|
sandbox_ = s;
|
2022-02-25 19:57:23 -05:00
|
|
|
next_ = All;
|
|
|
|
|
All = this;
|
2021-01-02 13:31:18 -05:00
|
|
|
}
|
2020-11-27 13:21:07 -05:00
|
|
|
|
2022-06-06 23:03:26 -04:00
|
|
|
LuaConstantReg::LuaConstantReg(const char *n, const char *d, LuaToken tokenvalue, lua_Number numbervalue) {
|
|
|
|
|
name_ = n;
|
|
|
|
|
docs_ = d;
|
|
|
|
|
tokenvalue_ = tokenvalue;
|
|
|
|
|
numbervalue_ = numbervalue;
|
|
|
|
|
next_ = All;
|
|
|
|
|
All = this;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-15 23:03:43 -05:00
|
|
|
const LuaFunctionReg *LuaFunctionReg::lookup(lua_CFunction fn) {
|
2022-02-25 19:57:23 -05:00
|
|
|
for (const LuaFunctionReg *r = All; r != 0; r = r->next_) {
|
2021-12-15 23:03:43 -05:00
|
|
|
if (r->func_ == fn) {
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-25 19:57:23 -05:00
|
|
|
LuaFunctionReg *LuaFunctionReg::All;
|
2022-06-06 23:03:26 -04:00
|
|
|
LuaConstantReg *LuaConstantReg::All;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
eng::string LuaToken::str() const {
|
2025-02-04 18:35:26 -05:00
|
|
|
uint64_t n = (uint64_t)value;
|
|
|
|
|
char buffer[20];
|
|
|
|
|
int pos = 19;
|
|
|
|
|
buffer[pos] = 0;
|
|
|
|
|
while (n > 0) {
|
|
|
|
|
uint64_t digit = (n % 36);
|
|
|
|
|
n /= 36;
|
|
|
|
|
if (digit < 10) {
|
|
|
|
|
buffer[--pos] = '0' + digit;
|
|
|
|
|
} else {
|
|
|
|
|
buffer[--pos] = 'a' + (digit - 10);
|
|
|
|
|
}
|
2022-06-06 23:03:26 -04:00
|
|
|
}
|
2025-02-04 18:35:26 -05:00
|
|
|
return eng::string(buffer + pos, 19 - pos);
|
2022-06-06 23:03:26 -04:00
|
|
|
}
|
2022-02-25 19:57:23 -05:00
|
|
|
|
|
|
|
|
static int panicf(lua_State *L) {
|
|
|
|
|
const char *p = lua_tostring(L, -1);
|
|
|
|
|
fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n", p);
|
|
|
|
|
fflush(stderr);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
2021-01-06 15:10:21 -05:00
|
|
|
|
2022-02-28 21:57:54 -05:00
|
|
|
// An allocator for lua states that uses system malloc and system free.
|
|
|
|
|
static void *l_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
|
|
|
|
|
if (nsize == 0) {
|
|
|
|
|
free(ptr);
|
|
|
|
|
return NULL;
|
|
|
|
|
} else {
|
|
|
|
|
return realloc(ptr, nsize);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-13 17:02:35 -04:00
|
|
|
|
2023-04-06 14:05:12 -04:00
|
|
|
lua_State *LuaCoreStack::newstate (lua_Alloc allocf) {
|
2022-02-28 21:57:54 -05:00
|
|
|
if (allocf == nullptr) allocf = l_alloc;
|
|
|
|
|
lua_State *L = lua_newstate(allocf, NULL);
|
2023-03-17 13:31:10 -04:00
|
|
|
assert(L != nullptr);
|
|
|
|
|
lua_atpanic(L, &panicf);
|
|
|
|
|
|
|
|
|
|
// We want all states to have a classes table and
|
|
|
|
|
// a tangibles table so that LS.makeclass and LS.maketan
|
|
|
|
|
// work out-of-the-box.
|
|
|
|
|
|
|
|
|
|
lua_pushstring(L, "classes");
|
|
|
|
|
lua_newtable(L);
|
|
|
|
|
lua_rawset(L, LUA_REGISTRYINDEX);
|
2024-09-18 16:09:05 -04:00
|
|
|
lua_pushstring(L, "classnames");
|
|
|
|
|
lua_newtable(L);
|
|
|
|
|
lua_rawset(L, LUA_REGISTRYINDEX);
|
2023-03-17 13:31:10 -04:00
|
|
|
lua_pushstring(L, "tangibles");
|
|
|
|
|
lua_newtable(L);
|
|
|
|
|
lua_rawset(L, LUA_REGISTRYINDEX);
|
2024-03-20 14:29:20 -04:00
|
|
|
lua_pushstring(L, "worldtype");
|
|
|
|
|
lua_pushnumber(L, WORLD_TYPE_MASTER);
|
|
|
|
|
lua_rawset(L, LUA_REGISTRYINDEX);
|
2023-03-17 13:31:10 -04:00
|
|
|
|
2022-02-28 21:57:54 -05:00
|
|
|
return L;
|
2022-02-25 19:57:23 -05:00
|
|
|
}
|
|
|
|
|
|
2023-04-06 14:05:12 -04:00
|
|
|
void LuaCoreStack::argerr(const char *nm, const char *tp) const {
|
2022-07-22 16:00:37 -04:00
|
|
|
luaL_error(L_, "'%s' should be %s", nm, tp);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-13 17:46:26 -04:00
|
|
|
std::optional<bool> LuaCoreStack::tryboolean(LuaSlot s) const {
|
|
|
|
|
if (lua_type(L_, s) == LUA_TBOOLEAN) {
|
|
|
|
|
return lua_toboolean(L_, s);
|
2022-04-15 17:24:07 -04:00
|
|
|
}
|
2024-03-13 17:46:26 -04:00
|
|
|
return std::nullopt;
|
2022-04-15 17:24:07 -04:00
|
|
|
}
|
|
|
|
|
|
2024-03-13 17:46:26 -04:00
|
|
|
std::optional<lua_Integer> LuaCoreStack::tryinteger(LuaSlot s) const {
|
2022-04-15 17:24:07 -04:00
|
|
|
if (lua_type(L_, s) == LUA_TNUMBER) {
|
|
|
|
|
lua_Number result = lua_tonumber(L_, s);
|
2024-03-13 17:46:26 -04:00
|
|
|
if (lua_Integer(result) == result) {
|
|
|
|
|
return lua_Integer(result);
|
|
|
|
|
}
|
2022-04-15 17:24:07 -04:00
|
|
|
}
|
2024-03-13 17:46:26 -04:00
|
|
|
return std::nullopt;
|
2022-04-15 17:24:07 -04:00
|
|
|
}
|
|
|
|
|
|
2024-03-13 17:46:26 -04:00
|
|
|
std::optional<int> LuaCoreStack::tryint(LuaSlot s) const {
|
|
|
|
|
if (lua_type(L_, s) == LUA_TNUMBER) {
|
|
|
|
|
lua_Number result = lua_tonumber(L_, s);
|
|
|
|
|
if (int(result) == result) {
|
|
|
|
|
return int(result);
|
2024-03-13 15:39:09 -04:00
|
|
|
}
|
|
|
|
|
}
|
2024-03-13 17:46:26 -04:00
|
|
|
return std::nullopt;
|
2021-03-30 19:30:58 -04:00
|
|
|
}
|
2021-01-06 15:10:21 -05:00
|
|
|
|
2024-03-13 17:46:26 -04:00
|
|
|
std::optional<lua_Number> LuaCoreStack::trynumber(LuaSlot s) const {
|
|
|
|
|
if (lua_type(L_, s) == LUA_TNUMBER) {
|
|
|
|
|
return lua_tonumber(L_, s);
|
2022-04-15 17:24:07 -04:00
|
|
|
}
|
2024-03-13 17:46:26 -04:00
|
|
|
return std::nullopt;
|
2021-02-28 14:01:59 -05:00
|
|
|
}
|
|
|
|
|
|
2024-03-13 17:46:26 -04:00
|
|
|
std::optional<eng::string> LuaCoreStack::trystring(LuaSlot s) const {
|
|
|
|
|
if (lua_type(L_, s) == LUA_TSTRING) {
|
|
|
|
|
size_t len;
|
|
|
|
|
const char *str = lua_tolstring(L_, s, &len);
|
|
|
|
|
return eng::string(str, len);
|
2022-04-15 17:24:07 -04:00
|
|
|
}
|
2024-03-13 17:46:26 -04:00
|
|
|
return std::nullopt;
|
2021-07-09 18:07:06 -04:00
|
|
|
}
|
|
|
|
|
|
2024-03-13 17:46:26 -04:00
|
|
|
std::optional<std::string_view> LuaCoreStack::trystringview(LuaSlot s) const {
|
|
|
|
|
if (lua_type(L_, s) == LUA_TSTRING) {
|
|
|
|
|
size_t len;
|
|
|
|
|
const char *str = lua_tolstring(L_, s, &len);
|
|
|
|
|
return std::string_view(str, len);
|
|
|
|
|
}
|
|
|
|
|
return std::nullopt;
|
2022-06-06 23:03:26 -04:00
|
|
|
}
|
|
|
|
|
|
2024-03-13 17:46:26 -04:00
|
|
|
std::optional<lua_State*> LuaCoreStack::trythread(LuaSlot s) const {
|
|
|
|
|
if (lua_type(L_, s) == LUA_TTHREAD) {
|
|
|
|
|
return lua_tothread(L_, s);
|
|
|
|
|
}
|
|
|
|
|
return std::nullopt;
|
2021-02-28 14:01:59 -05:00
|
|
|
}
|
|
|
|
|
|
2024-03-13 17:46:26 -04:00
|
|
|
std::optional<LuaToken> LuaCoreStack::trytoken(LuaSlot s) const {
|
|
|
|
|
if (lua_type(L_, s) == LUA_TLIGHTUSERDATA) {
|
|
|
|
|
return LuaToken(lua_touserdata(L_, s));
|
|
|
|
|
}
|
|
|
|
|
return std::nullopt;
|
2022-06-06 23:03:26 -04:00
|
|
|
}
|
|
|
|
|
|
2024-03-13 17:46:26 -04:00
|
|
|
std::optional<util::DXYZ> LuaCoreStack::tryxyz(LuaSlot s) const {
|
2024-03-13 15:39:09 -04:00
|
|
|
if (lua_istable(L_, s) && (lua_nkeys(L_, s) == 3)) {
|
|
|
|
|
int top = lua_gettop(L_);
|
|
|
|
|
lua_rawgeti(L_, s, 3);
|
|
|
|
|
lua_rawgeti(L_, s, 2);
|
|
|
|
|
lua_rawgeti(L_, s, 1);
|
2024-09-01 19:43:00 -04:00
|
|
|
if ((lua_type(L_, -1)==LUA_TNUMBER) && (lua_type(L_, -2)==LUA_TNUMBER) && (lua_type(L_, -3)==LUA_TNUMBER)) {
|
2024-03-13 15:39:09 -04:00
|
|
|
util::DXYZ result;
|
|
|
|
|
result.x = lua_tonumber(L_, -1);
|
|
|
|
|
result.y = lua_tonumber(L_, -2);
|
|
|
|
|
result.z = lua_tonumber(L_, -3);
|
|
|
|
|
lua_settop(L_, top);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
lua_settop(L_, top);
|
|
|
|
|
}
|
2024-03-13 17:46:26 -04:00
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool LuaCoreStack::trytable(LuaSlot s) const {
|
|
|
|
|
return lua_istable(L_, s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool LuaCoreStack::trynil(LuaSlot s) const {
|
|
|
|
|
return lua_isnil(L_, s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool LuaCoreStack::tryfunction(LuaSlot s) const {
|
|
|
|
|
return lua_isfunction(L_, s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool LuaCoreStack::trycfunction(LuaSlot s) const {
|
|
|
|
|
return lua_iscfunction(L_, s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool LuaCoreStack::trytangible(LuaSlot s) const {
|
|
|
|
|
return (lua_istable(L_, s) && gettabletype(s) == LUA_TT_TANGIBLE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool LuaCoreStack::ckboolean(LuaSlot s, const char *argname) const {
|
|
|
|
|
auto result = tryboolean(s);
|
|
|
|
|
if (!result) argerr(argname, "boolean");
|
|
|
|
|
return *result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lua_Integer LuaCoreStack::ckinteger(LuaSlot s, const char *argname) const {
|
|
|
|
|
auto result = tryinteger(s);
|
|
|
|
|
if (!result) argerr(argname, "integer");
|
|
|
|
|
return *result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int LuaCoreStack::ckint(LuaSlot s, const char *argname) const {
|
|
|
|
|
auto result = tryint(s);
|
|
|
|
|
if (!result) argerr(argname, "int");
|
|
|
|
|
return *result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lua_Number LuaCoreStack::cknumber(LuaSlot s, const char *argname) const {
|
|
|
|
|
auto result = trynumber(s);
|
|
|
|
|
if (!result) argerr(argname, "number");
|
|
|
|
|
return *result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
eng::string LuaCoreStack::ckstring(LuaSlot s, const char *argname) const {
|
|
|
|
|
auto result = trystring(s);
|
|
|
|
|
if (!result) argerr(argname, "string");
|
|
|
|
|
return *result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string_view LuaCoreStack::ckstringview(LuaSlot s, const char *argname) const {
|
|
|
|
|
auto result = trystringview(s);
|
|
|
|
|
if (!result) argerr(argname, "string");
|
|
|
|
|
return *result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lua_State * LuaCoreStack::ckthread(LuaSlot s, const char *argname) const {
|
|
|
|
|
auto result = trythread(s);
|
|
|
|
|
if (!result) argerr(argname, "thread");
|
|
|
|
|
return *result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LuaToken LuaCoreStack::cktoken(LuaSlot s, const char *argname) const {
|
|
|
|
|
auto result = trytoken(s);
|
|
|
|
|
if (!result) argerr(argname, "token");
|
|
|
|
|
return *result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
util::DXYZ LuaCoreStack::ckxyz(LuaSlot s, const char *argname) const {
|
|
|
|
|
auto result = tryxyz(s);
|
|
|
|
|
if (!result) argerr(argname, "xyz");
|
|
|
|
|
return *result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaCoreStack::cktable(LuaSlot s, const char *argname) const {
|
|
|
|
|
if (!trytable(s)) argerr(argname, "table");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaCoreStack::cknil(LuaSlot s, const char *argname) const {
|
|
|
|
|
if (!trynil(s)) argerr(argname, "nil");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaCoreStack::ckfunction(LuaSlot s, const char *argname) const {
|
|
|
|
|
if (!tryfunction(s)) argerr(argname, "function");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaCoreStack::ckcfunction(LuaSlot s, const char *argname) const {
|
|
|
|
|
if (!trycfunction(s)) argerr(argname, "c-function");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LuaCoreStack::cktangible(LuaSlot s, const char *argname) const {
|
|
|
|
|
if (!trytangible(s)) argerr(argname, "tangible");
|
2024-03-13 15:39:09 -04:00
|
|
|
}
|
|
|
|
|
|
2024-03-13 17:46:26 -04:00
|
|
|
|
2023-04-06 14:05:12 -04:00
|
|
|
void LuaCoreStack::clearmetatable(LuaSlot tab) const {
|
2020-11-27 14:24:37 -05:00
|
|
|
lua_pushnil(L_);
|
|
|
|
|
lua_setmetatable(L_, tab);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 14:05:12 -04:00
|
|
|
void LuaCoreStack::setmetatable(LuaSlot tab, LuaSlot mt) const {
|
2020-11-27 14:24:37 -05:00
|
|
|
lua_pushvalue(L_, mt);
|
|
|
|
|
lua_setmetatable(L_, tab);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 14:05:12 -04:00
|
|
|
bool LuaCoreStack::getmetatable(LuaSlot mt, LuaSlot tab) const {
|
2021-08-13 17:02:35 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2024-03-20 14:29:20 -04:00
|
|
|
bool LuaCoreStack::next(LuaSlot tab, LuaSlot key, LuaSlot value) const {
|
2020-11-27 14:24:37 -05:00
|
|
|
lua_pushvalue(L_, key);
|
|
|
|
|
int ret = lua_next(L_, tab);
|
|
|
|
|
if (ret != 0) {
|
|
|
|
|
lua_replace(L_, value);
|
|
|
|
|
lua_replace(L_, key);
|
|
|
|
|
}
|
2024-03-20 14:29:20 -04:00
|
|
|
return (ret != 0);
|
2020-11-27 14:24:37 -05:00
|
|
|
}
|
|
|
|
|
|
2023-04-06 14:05:12 -04:00
|
|
|
void LuaCoreStack::getglobaltable(LuaSlot target) const {
|
2021-02-28 16:32:42 -05:00
|
|
|
lua_pushglobaltable(L_);
|
|
|
|
|
lua_replace(L_, target);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 14:05:12 -04:00
|
|
|
void LuaCoreStack::newtable(LuaSlot target) const {
|
2020-11-27 14:24:37 -05:00
|
|
|
lua_newtable(L_);
|
|
|
|
|
lua_replace(L_, target);
|
|
|
|
|
}
|
2020-12-05 18:57:53 -05:00
|
|
|
|
2023-04-06 14:05:12 -04:00
|
|
|
void LuaCoreStack::createtable(LuaSlot target, int narr, int nrec) const {
|
2021-07-03 19:49:55 -04:00
|
|
|
lua_createtable(L_, narr, nrec);
|
|
|
|
|
lua_replace(L_, target);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 14:05:12 -04:00
|
|
|
lua_State *LuaCoreStack::newthread(LuaSlot target) const {
|
2021-02-17 13:38:22 -05:00
|
|
|
lua_State *result = lua_newthread(L_);
|
|
|
|
|
lua_replace(L_, target);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 16:09:05 -04:00
|
|
|
eng::string LuaCoreStack::classname(LuaSlot input) const {
|
|
|
|
|
LuaVar lookup, classtab, classname;
|
|
|
|
|
LuaExtStack LS(L_, lookup, classtab, classname);
|
2023-03-17 13:31:10 -04:00
|
|
|
|
2024-09-18 16:09:05 -04:00
|
|
|
int xt = xtype(input);
|
|
|
|
|
if (xt == LUA_TSTRING) {
|
|
|
|
|
LS.rawget(lookup, LuaRegistry, "classes");
|
|
|
|
|
LS.rawget(classtab, lookup, input);
|
|
|
|
|
if (xtype(classtab) != LUA_TT_CLASS) {
|
|
|
|
|
return "";
|
2023-03-17 13:31:10 -04:00
|
|
|
}
|
2024-09-18 16:09:05 -04:00
|
|
|
return LS.ckstring(input);
|
|
|
|
|
} else if (xt == LUA_TT_TANGIBLE) {
|
|
|
|
|
if (!LS.getmetatable(lookup, input)) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
LS.rawget(classtab, lookup, "__index");
|
|
|
|
|
if (xtype(classtab) != LUA_TT_CLASS) {
|
|
|
|
|
return "";
|
2021-12-27 16:44:12 -05:00
|
|
|
}
|
2024-09-18 16:09:05 -04:00
|
|
|
LS.rawget(lookup, LuaRegistry, "classnames");
|
|
|
|
|
LS.rawget(classname, lookup, classtab);
|
|
|
|
|
if (!LS.isstring(classname)) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
return LS.ckstring(classname);
|
|
|
|
|
} else if (xt == LUA_TT_CLASS) {
|
|
|
|
|
LS.rawget(lookup, LuaRegistry, "classnames");
|
|
|
|
|
LS.rawget(classname, lookup, input);
|
|
|
|
|
if (!LS.isstring(classname)) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
return LS.ckstring(classname);
|
|
|
|
|
} else {
|
|
|
|
|
return "";
|
2021-12-27 16:44:12 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 16:09:05 -04:00
|
|
|
static eng::string nonexistentclass(eng::string name) {
|
|
|
|
|
eng::string err;
|
|
|
|
|
if (!sv::is_lua_classname(name)) {
|
|
|
|
|
err = "invalid class name: " + name;
|
|
|
|
|
} else {
|
|
|
|
|
err = "not a class: " + name;
|
|
|
|
|
}
|
|
|
|
|
return err;
|
|
|
|
|
}
|
2021-03-19 13:41:41 -04:00
|
|
|
|
2024-09-18 16:09:05 -04:00
|
|
|
eng::string LuaCoreStack::getclass(LuaSlot classtab, LuaSlot input) const {
|
|
|
|
|
LuaVar lookup;
|
|
|
|
|
LuaExtStack LS(L_, lookup);
|
|
|
|
|
|
|
|
|
|
int xt = xtype(input);
|
|
|
|
|
if (xt == LUA_TSTRING) {
|
|
|
|
|
LS.rawget(lookup, LuaRegistry, "classes");
|
|
|
|
|
LS.rawget(classtab, lookup, input);
|
|
|
|
|
if (xtype(classtab) != LUA_TT_CLASS) {
|
|
|
|
|
eng::string classname = LS.ckstring(input);
|
|
|
|
|
return nonexistentclass(LS.ckstring(input));
|
2021-12-27 16:44:12 -05:00
|
|
|
}
|
|
|
|
|
return "";
|
2024-09-18 16:09:05 -04:00
|
|
|
} else if (xt == LUA_TT_TANGIBLE) {
|
|
|
|
|
if (!LS.getmetatable(lookup, input)) {
|
|
|
|
|
eng::string err = "inactive tangible has no class";
|
2021-12-27 16:44:12 -05:00
|
|
|
return err;
|
|
|
|
|
}
|
2024-09-18 16:09:05 -04:00
|
|
|
LS.rawget(classtab, lookup, "__index");
|
|
|
|
|
if (xtype(classtab) != LUA_TT_CLASS) {
|
|
|
|
|
eng::string err = "tangible has invalid class";
|
2021-12-27 16:44:12 -05:00
|
|
|
}
|
|
|
|
|
return "";
|
2024-09-18 16:09:05 -04:00
|
|
|
} else if (xt == LUA_TT_CLASS) {
|
|
|
|
|
LS.set(classtab, input);
|
|
|
|
|
return "";
|
2021-12-27 16:44:12 -05:00
|
|
|
} else {
|
2024-09-18 16:09:05 -04:00
|
|
|
eng::string err = "getclass must be passed a string, class, or tangible";
|
2021-12-27 16:44:12 -05:00
|
|
|
return err;
|
2021-08-10 10:41:06 -04:00
|
|
|
}
|
2021-12-27 16:44:12 -05:00
|
|
|
}
|
2021-08-10 10:41:06 -04:00
|
|
|
|
2024-09-18 16:09:05 -04:00
|
|
|
eng::string LuaCoreStack::getclass(LuaSlot classtab, std::string_view classname) const {
|
|
|
|
|
LuaVar lookup;
|
|
|
|
|
LuaExtStack LS(L_, lookup);
|
|
|
|
|
LS.rawget(lookup, LuaRegistry, "classes");
|
|
|
|
|
LS.rawget(classtab, lookup, classname);
|
|
|
|
|
if (xtype(classtab) != LUA_TT_CLASS) {
|
|
|
|
|
return nonexistentclass(eng::string(classname));
|
|
|
|
|
}
|
|
|
|
|
return "";
|
2021-03-19 13:41:41 -04:00
|
|
|
}
|
|
|
|
|
|
2023-04-06 14:05:12 -04:00
|
|
|
void LuaCoreStack::makeclass(LuaSlot classtab, LuaSlot classname) const {
|
2024-09-18 16:09:05 -04:00
|
|
|
LuaVar classes, classnames, globtab, cname;
|
|
|
|
|
LuaExtStack LS(L_, classes, classnames, globtab, cname);
|
2021-02-09 17:15:54 -05:00
|
|
|
|
2021-02-28 14:24:59 -05:00
|
|
|
// Validate the class name.
|
2024-09-18 16:09:05 -04:00
|
|
|
assert(LS.isstring(classname));
|
|
|
|
|
assert(sv::is_lua_classname(LS.ckstring(classname)));
|
2021-12-27 16:44:12 -05:00
|
|
|
|
2023-03-17 13:31:10 -04:00
|
|
|
// Fetch the classes table from the registry.
|
|
|
|
|
LS.rawget(classes, LuaRegistry, "classes");
|
|
|
|
|
assert(LS.istable(classes));
|
|
|
|
|
|
2024-09-18 16:09:05 -04:00
|
|
|
// Fetch the classnames table from the registry.
|
|
|
|
|
LS.rawget(classnames, LuaRegistry, "classnames");
|
|
|
|
|
assert(LS.istable(classnames));
|
|
|
|
|
|
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
|
|
|
|
2023-03-17 13:31:10 -04:00
|
|
|
// Get the classtab from the classes table.
|
|
|
|
|
LS.rawget(classtab, classes, classname);
|
2021-02-09 17:15:54 -05:00
|
|
|
|
2021-12-27 16:44:12 -05:00
|
|
|
// Make a new table if necessary.
|
2021-12-28 13:56:16 -05:00
|
|
|
if (!LS.istable(classtab)) {
|
2021-12-27 16:44:12 -05:00
|
|
|
LS.newtable(classtab);
|
2023-03-17 13:31:10 -04:00
|
|
|
LS.rawset(classes, classname, classtab);
|
2024-09-18 16:09:05 -04:00
|
|
|
LS.rawset(classnames, classtab, classname);
|
2021-12-27 16:44:12 -05:00
|
|
|
}
|
|
|
|
|
|
2023-03-17 13:31:10 -04:00
|
|
|
// Put the table into the global environment.
|
|
|
|
|
LS.rawset(globtab, classname, classtab);
|
|
|
|
|
|
2021-02-09 17:15:54 -05:00
|
|
|
// Repair the special fields.
|
2021-08-23 23:34:30 -04:00
|
|
|
LS.settabletype(classtab, LUA_TT_CLASS);
|
2021-02-28 14:24:59 -05:00
|
|
|
LS.rawset(classtab, "__index", classtab);
|
2021-02-09 17:15:54 -05:00
|
|
|
}
|
|
|
|
|
|
2023-04-06 14:05:12 -04:00
|
|
|
void LuaCoreStack::makeclass(LuaSlot tab, std::string_view name) const {
|
2021-08-23 23:34:30 -04:00
|
|
|
push_any_value(name);
|
|
|
|
|
LuaSpecial classname(lua_gettop(L_));
|
|
|
|
|
makeclass(tab, classname);
|
|
|
|
|
lua_pop(L_, 1);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 14:05:12 -04:00
|
|
|
void LuaCoreStack::maketan(LuaSlot tab, int64_t id) const {
|
2024-03-20 14:29:20 -04:00
|
|
|
assert(validpositiveinteger(id));
|
2024-02-28 14:19:59 -05:00
|
|
|
|
2023-03-16 23:31:29 -04:00
|
|
|
LuaVar tangibles, metatab;
|
2023-04-11 16:51:40 -04:00
|
|
|
LuaExtStack LS(L_, tangibles, metatab);
|
2023-03-16 23:31:29 -04:00
|
|
|
|
|
|
|
|
// Try to get the existing tangible.
|
|
|
|
|
LS.rawget(tangibles, LuaRegistry, "tangibles");
|
|
|
|
|
LS.rawget(tab, tangibles, id);
|
|
|
|
|
|
|
|
|
|
// If we succeeded, return it.
|
|
|
|
|
if (LS.istable(tab)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create the tangible's database and metatable.
|
|
|
|
|
LS.set(tab, LuaNewTable);
|
|
|
|
|
LS.set(metatab, LuaNewTable);
|
|
|
|
|
LS.setmetatable(tab, metatab);
|
|
|
|
|
|
|
|
|
|
// Mark the tangible using the tabletype field.
|
|
|
|
|
LS.settabletype(tab, LUA_TT_TANGIBLE);
|
|
|
|
|
LS.settabletype(metatab, LUA_TT_TANGIBLEMETA);
|
|
|
|
|
|
|
|
|
|
// Store the tangible ID and lock the metatable.
|
|
|
|
|
LS.rawset(metatab, "id", id);
|
|
|
|
|
LS.rawset(metatab, "__metatable", false);
|
|
|
|
|
|
|
|
|
|
// Store the database into the tangibles table.
|
|
|
|
|
LS.rawset(tangibles, id, tab);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-04-06 14:05:12 -04:00
|
|
|
bool LuaCoreStack::tanblank(LuaSlot tab) const {
|
2023-03-16 23:31:29 -04:00
|
|
|
bool result = true;
|
|
|
|
|
if (istable(tab) && gettabletype(tab) == LUA_TT_TANGIBLE) {
|
|
|
|
|
if (lua_getmetatable(L_, tab.index())) {
|
|
|
|
|
lua_pushstring(L_, "threads");
|
|
|
|
|
lua_rawget(L_, -2);
|
|
|
|
|
if (lua_type(L_, -1) == LUA_TTABLE) {
|
|
|
|
|
result = false;
|
|
|
|
|
}
|
|
|
|
|
lua_pop(L_, 2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2021-08-23 23:34:30 -04:00
|
|
|
|
2023-04-06 14:05:12 -04:00
|
|
|
int64_t LuaCoreStack::tanid(LuaSlot tab) const {
|
2021-11-26 12:28:59 -05:00
|
|
|
int64_t result = 0;
|
|
|
|
|
if (istable(tab) && gettabletype(tab) == LUA_TT_TANGIBLE) {
|
|
|
|
|
if (lua_getmetatable(L_, tab.index())) {
|
|
|
|
|
lua_pushstring(L_, "id");
|
|
|
|
|
lua_rawget(L_, -2);
|
|
|
|
|
if (lua_type(L_, -1) == LUA_TNUMBER) {
|
|
|
|
|
result = int64_t(lua_tonumber(L_, -1));
|
|
|
|
|
}
|
|
|
|
|
lua_pop(L_, 2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-28 14:19:59 -05:00
|
|
|
bool LuaCoreStack::tangetclass(LuaSlot classobj, LuaSlot tab) {
|
|
|
|
|
if (istable(tab) && (gettabletype(tab) == LUA_TT_TANGIBLE) && lua_getmetatable(L_, tab.index())) {
|
|
|
|
|
lua_pushstring(L_, "__index");
|
|
|
|
|
lua_rawget(L_, -2);
|
|
|
|
|
lua_replace(L_, classobj);
|
|
|
|
|
lua_pop(L_, 1);
|
|
|
|
|
if (istable(classobj) && (gettabletype(classobj) == LUA_TT_CLASS)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
set(classobj, LuaNil);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 14:05:12 -04:00
|
|
|
bool LuaCoreStack::issortablekey(LuaSlot s) const {
|
2021-12-27 17:03:42 -05:00
|
|
|
int type = lua_type(L_, s);
|
|
|
|
|
return (type == LUA_TBOOLEAN) || (type == LUA_TNUMBER) || (type == LUA_TSTRING);
|
|
|
|
|
}
|
2021-11-26 12:28:59 -05:00
|
|
|
|
2023-04-06 14:05:12 -04:00
|
|
|
void LuaCoreStack::movesortablekey(LuaSlot key, LuaCoreStack &otherstack, LuaSlot otherslot) {
|
2021-08-13 17:02:35 -04:00
|
|
|
int type = lua_type(L_, key);
|
|
|
|
|
switch (type) {
|
|
|
|
|
case LUA_TBOOLEAN:
|
2021-08-23 23:34:30 -04:00
|
|
|
lua_pushboolean(otherstack.L_, lua_toboolean(L_, key));
|
|
|
|
|
lua_replace(otherstack.L_, otherslot);
|
2021-08-13 17:02:35 -04:00
|
|
|
break;
|
|
|
|
|
case LUA_TNUMBER:
|
2021-08-23 23:34:30 -04:00
|
|
|
lua_pushnumber(otherstack.L_, lua_tonumber(L_, key));
|
|
|
|
|
lua_replace(otherstack.L_, otherslot);
|
2021-08-13 17:02:35 -04:00
|
|
|
break;
|
|
|
|
|
case LUA_TSTRING: {
|
|
|
|
|
size_t len;
|
|
|
|
|
const char *str = lua_tolstring(L_, key, &len);
|
2021-08-23 23:34:30 -04:00
|
|
|
lua_pushlstring(otherstack.L_, str, len);
|
|
|
|
|
lua_replace(otherstack.L_, otherslot);
|
2021-08-13 17:02:35 -04:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
2021-12-27 17:03:42 -05:00
|
|
|
assert(false && "movesortablekey: not a sortable key");
|
2021-08-13 17:02:35 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 14:05:12 -04:00
|
|
|
void LuaCoreStack::cleartable(LuaSlot tab, bool clearmeta) const {
|
2021-12-27 17:03:42 -05:00
|
|
|
assert(istable(tab));
|
2021-03-19 13:41:41 -04:00
|
|
|
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-10-25 14:47:37 -04:00
|
|
|
if (clearmeta) {
|
|
|
|
|
lua_pushnil(L_);
|
|
|
|
|
lua_setmetatable(L_, tab.index());
|
|
|
|
|
}
|
2021-03-19 13:41:41 -04:00
|
|
|
}
|
|
|
|
|
|
2023-04-06 14:05:12 -04:00
|
|
|
int LuaCoreStack::rawlen(LuaSlot obj) const {
|
2021-07-03 19:49:55 -04:00
|
|
|
return lua_rawlen(L_, obj.index());
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-13 16:55:48 -04:00
|
|
|
int LuaCoreStack::nkeys(LuaSlot obj) const {
|
|
|
|
|
return lua_nkeys(L_, obj.index());
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 14:05:12 -04:00
|
|
|
int LuaCoreStack::gettabletype(LuaSlot tab) const {
|
2021-08-10 10:41:06 -04:00
|
|
|
uint16_t bits = lua_getflagbits(L_, tab.index());
|
2021-08-23 23:34:30 -04:00
|
|
|
return LUA_TT_GENERAL + (bits & 0x000F);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 14:05:12 -04:00
|
|
|
void LuaCoreStack::settabletype(LuaSlot tab, int t) const {
|
2021-08-23 23:34:30 -04:00
|
|
|
assert((t >= LUA_TT_GENERAL) && (t <= LUA_TT_CLASS));
|
|
|
|
|
int offset = (t - LUA_TT_GENERAL);
|
|
|
|
|
lua_modflagbits(L_, tab.index(), 0x000F, offset);
|
2021-08-10 10:41:06 -04:00
|
|
|
}
|
|
|
|
|
|
2023-04-06 14:05:12 -04:00
|
|
|
int LuaCoreStack::xtype(LuaSlot slot) const {
|
2021-08-23 23:34:30 -04:00
|
|
|
int t = lua_type(L_, slot);
|
|
|
|
|
if (t != LUA_TTABLE) return t;
|
|
|
|
|
uint16_t bits = lua_getflagbits(L_, slot);
|
|
|
|
|
return LUA_TT_GENERAL + (bits & 0x000F);
|
2021-08-10 10:41:06 -04:00
|
|
|
}
|
|
|
|
|
|
2023-04-06 14:05:12 -04:00
|
|
|
bool LuaCoreStack::getvisited(LuaSlot tab) const {
|
2021-08-10 10:41:06 -04:00
|
|
|
uint16_t bits = lua_getflagbits(L_, tab.index());
|
|
|
|
|
return (bits & 0x0010);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 14:05:12 -04:00
|
|
|
void LuaCoreStack::setvisited(LuaSlot tab, bool visited) const {
|
2021-08-10 10:41:06 -04:00
|
|
|
lua_modflagbits(L_, tab.index(), 0x0010, visited ? 0x0010 : 0);
|
|
|
|
|
}
|
2022-07-22 16:00:37 -04:00
|
|
|
|
2024-03-20 14:29:20 -04:00
|
|
|
WorldType LuaCoreStack::get_world_type() const {
|
2023-03-01 16:07:13 -05:00
|
|
|
lua_pushstring(L_, "worldtype");
|
|
|
|
|
lua_rawget(L_, LUA_REGISTRYINDEX);
|
|
|
|
|
lua_Integer n = lua_tointeger(L_, -1);
|
|
|
|
|
lua_pop(L_, 1);
|
|
|
|
|
assert(n != 0);
|
|
|
|
|
return (WorldType)n;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-20 14:29:20 -04:00
|
|
|
void LuaCoreStack::set_world_type(WorldType t) const {
|
|
|
|
|
lua_pushstring(L_, "worldtype");
|
|
|
|
|
lua_pushnumber(L_, (int)t);
|
|
|
|
|
lua_rawset(L_, LUA_REGISTRYINDEX);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 14:05:12 -04:00
|
|
|
void LuaCoreStack::guard_nopredict(const char *fn) {
|
2023-03-01 16:07:13 -05:00
|
|
|
if (lua_isyieldable(L_)) {
|
|
|
|
|
if (!is_authoritative()) {
|
|
|
|
|
lua_yield(L_, 0);
|
|
|
|
|
luaL_error(L_, "unexplained nopredict failure in %s", fn);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-22 16:00:37 -04:00
|
|
|
LuaKeywordParser::LuaKeywordParser(lua_State *L, int slot) {
|
|
|
|
|
L_ = L;
|
|
|
|
|
slot_ = slot;
|
2022-07-22 17:07:40 -04:00
|
|
|
not_table_ = !lua_istable(L_, slot_);
|
|
|
|
|
if (not_table_) {
|
|
|
|
|
lua_newtable(L_);
|
|
|
|
|
lua_replace(L_, slot_);
|
2022-07-22 16:00:37 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool LuaKeywordParser::parse(LuaSlot out, const char *kw) {
|
|
|
|
|
lua_pushstring(L_, kw);
|
|
|
|
|
lua_rawget(L_, slot_);
|
|
|
|
|
lua_replace(L_, out.index());
|
|
|
|
|
if (!lua_isnil(L_, out.index())) {
|
|
|
|
|
parsed_.insert(kw);
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-07-22 17:07:40 -04:00
|
|
|
eng::string LuaKeywordParser::final_check() {
|
|
|
|
|
if (not_table_) {
|
|
|
|
|
return "expected a keyword table";
|
|
|
|
|
}
|
2024-03-12 11:46:48 -04:00
|
|
|
lua_pushnil(L_);
|
|
|
|
|
while (lua_next(L_, slot_) != 0) {
|
|
|
|
|
lua_pop(L_, 1); // Don't need the value.
|
|
|
|
|
if (!lua_isstring(L_, -1)) {
|
|
|
|
|
return "keyword table contains non-string key";
|
|
|
|
|
}
|
|
|
|
|
const char *kw = lua_tostring(L_, -1);
|
|
|
|
|
if (parsed_.find(kw) == parsed_.end()) {
|
|
|
|
|
eng::ostringstream oss;
|
|
|
|
|
oss << "keyword " << kw << " not known";
|
|
|
|
|
return oss.str();
|
2022-07-22 16:00:37 -04:00
|
|
|
}
|
|
|
|
|
}
|
2022-07-22 17:07:40 -04:00
|
|
|
return "";
|
2022-07-22 16:00:37 -04:00
|
|
|
}
|
|
|
|
|
|
2022-07-22 17:07:40 -04:00
|
|
|
void LuaKeywordParser::final_check_throw() {
|
|
|
|
|
eng::string err = final_check();
|
|
|
|
|
if (!err.empty()) {
|
|
|
|
|
luaL_error(L_, "%s", err.c_str());
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-18 14:03:05 -04:00
|
|
|
|
|
|
|
|
const char *LuaByteReader::lua_reader(lua_State *L, void *ud, size_t *size) {
|
|
|
|
|
LuaByteReader *reader = (LuaByteReader*)ud;
|
|
|
|
|
*size = reader->size_;
|
|
|
|
|
const char *data = reader->data_;
|
|
|
|
|
reader->data_ = 0;
|
|
|
|
|
reader->size_ = 0;
|
|
|
|
|
return data;
|
|
|
|
|
}
|