A minor refactor to precede a major LuaStack refactor

This commit is contained in:
2023-04-06 14:05:12 -04:00
parent f0f4ad8609
commit dc4a5f6198
2 changed files with 148 additions and 158 deletions

View File

@@ -179,7 +179,9 @@ public:
return index_;
}
friend class LuaCoreStack;
friend class LuaStack;
friend class LuaExtStack;
};
class LuaArg : public LuaSlot {};
@@ -269,67 +271,9 @@ public:
eng::string str() const;
};
class LuaStack : public eng::nevernew {
private:
int narg_;
int ngap_;
int nvar_;
int nret_;
int argpos_;
int gappos_;
int varpos_;
int retpos_;
int rettop_;
int finaltop_;
class LuaCoreStack : public eng::nevernew {
protected:
lua_State *L_;
template<int NARG, int NVAR, int NRET, class... SS>
void count_slots(LuaArg &v, SS & ... stackslots)
{
count_slots<NARG+1, NVAR, NRET>(stackslots...);
}
template<int NARG, int NVAR, int NRET, class... SS>
void count_slots(LuaVar &v, SS & ... stackslots)
{
count_slots<NARG, NVAR+1, NRET>(stackslots...);
}
template<int NARG, int NVAR, int NRET, class... SS>
void count_slots(LuaRet &v, SS & ... stackslots)
{
count_slots<NARG, NVAR, NRET+1>(stackslots...);
}
template<int NARG, int NVAR, int NRET>
void count_slots() {
count_slots_finalize(NARG, NVAR, NRET);
}
void count_slots_finalize(int narg, int nvar, int nret);
template<class... SS>
void assign_slots(int argp, int varp, int retp, LuaArg &v, SS & ... stackslots) {
v.index_ = argp;
assign_slots(argp + 1, varp, retp, stackslots...);
}
template<class... SS>
void assign_slots(int argp, int varp, int retp, LuaVar &v, SS & ... stackslots) {
v.index_ = varp;
assign_slots(argp, varp+1, retp, stackslots...);
}
template<class... SS>
void assign_slots(int argp, int varp, int retp, LuaRet &v, SS & ... stackslots) {
v.index_ = retp;
assign_slots(argp, varp, retp+1, stackslots...);
}
void assign_slots(int argp, int varp, int retp) {}
void clear_frame();
private:
// Push any value on the stack, by type.
@@ -356,37 +300,17 @@ private:
void push_any_values() {
}
template<typename T>
static void delete_pointer(void *p) { delete (T*)p; }
static void do_not_delete(void *p) { }
void argerr(const char *arg, const char *tp) const;
public:
template<class... SS>
LuaStack(lua_State *L, SS & ... stackslots) {
L_ = L;
count_slots<0, 0, 0>(stackslots...);
if (lua_gettop(L) < narg_) {
luaL_error(L, "not enough arguments to function");
}
assign_slots(argpos_, varpos_, retpos_, stackslots...);
clear_frame();
}
lua_State *state() const { return L_; }
~LuaStack() {};
int result();
public:
// This is the largest integer that can be stored in a lua_Number.
// In other words, any 53-bit number can be stored.
static const int64_t MAXINT = 0x001FFFFFFFFFFFFF;
static lua_State *newstate (lua_Alloc allocf);
lua_State *state() const { return L_; }
int type(LuaSlot s) const { return lua_type(L_, s); }
void checktype(LuaSlot s, int type) const { luaL_checktype(L_, s, type); }
@@ -480,7 +404,7 @@ public:
// Move a sortable key (string, number, or boolean) from one lua
// environment to another lua environment. WARNING: this assert-fails
// if the value is not a sortable key.
void movesortablekey(LuaSlot val, LuaStack &other, LuaSlot otherslot);
void movesortablekey(LuaSlot val, LuaCoreStack &other, LuaSlot otherslot);
bool rawequal(LuaSlot v1, LuaSlot v2) const {
return lua_rawequal(L_, v1, v2);
@@ -552,6 +476,107 @@ public:
static bool int64_storable(int64_t v) { return (v <= MAXINT) && (v >= -MAXINT); }
};
class LuaStack : public LuaCoreStack {
private:
int narg_;
int ngap_;
int nvar_;
int nret_;
int argpos_;
int gappos_;
int varpos_;
int retpos_;
int rettop_;
int finaltop_;
private:
template<class... SS>
void assign_slots(int argp, int varp, int retp, LuaArg &v, SS & ... stackslots) {
v.index_ = argp;
assign_slots(argp + 1, varp, retp, stackslots...);
}
template<class... SS>
void assign_slots(int argp, int varp, int retp, LuaVar &v, SS & ... stackslots) {
v.index_ = varp;
assign_slots(argp, varp+1, retp, stackslots...);
}
template<class... SS>
void assign_slots(int argp, int varp, int retp, LuaRet &v, SS & ... stackslots) {
v.index_ = retp;
assign_slots(argp, varp, retp+1, stackslots...);
}
void assign_slots(int argp, int varp, int retp) {}
template<int NARG, int NVAR, int NRET, class... SS>
void count_slots(LuaArg &v, SS & ... stackslots)
{
count_slots<NARG+1, NVAR, NRET>(stackslots...);
}
template<int NARG, int NVAR, int NRET, class... SS>
void count_slots(LuaVar &v, SS & ... stackslots)
{
count_slots<NARG, NVAR+1, NRET>(stackslots...);
}
template<int NARG, int NVAR, int NRET, class... SS>
void count_slots(LuaRet &v, SS & ... stackslots)
{
count_slots<NARG, NVAR, NRET+1>(stackslots...);
}
template<int NARG, int NVAR, int NRET>
void count_slots() {
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;
}
public:
template<class... SS>
LuaStack(lua_State *L, SS & ... stackslots) {
L_ = L;
count_slots<0, 0, 0>(stackslots...);
if (lua_gettop(L) < narg_) {
luaL_error(L, "not enough arguments to function");
}
assign_slots(argpos_, varpos_, retpos_, stackslots...);
lua_settop(L_, varpos_ - 1);
for (int i = 0; i < nvar_ + nret_; i++) {
lua_pushnil(L_);
}
}
int 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_;
}
~LuaStack() {};
};
// This is a helper class to help parse tables full of keywords.
class LuaKeywordParser {
struct cmp_char {