Finally phase out LuaOldStack entirely.

This commit is contained in:
2023-04-14 17:50:06 -04:00
parent 2c2dabc127
commit 669a5a50ab
2 changed files with 143 additions and 285 deletions

View File

@@ -172,7 +172,6 @@ public:
}
friend class LuaCoreStack;
friend class LuaOldStack;
friend class LuaDefStack;
friend class LuaExtStack;
};
@@ -529,125 +528,15 @@ public:
static bool int64_storable(int64_t v) { return (v <= MAXINT) && (v >= -MAXINT); }
};
////////////////////////////////////////////////////////////////////
//
// LuaOldStack
//
// This class is deprecated, we're phasing it out. This was the first piece of
// code we wrote to allocate stack slots, and it has issues. It is
// still in use in the difference transmitter and the source database,
// and a few other small spots.
//
////////////////////////////////////////////////////////////////////
class LuaOldStack : 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_slotsx(LuaArg &v, SS & ... stackslots)
{
count_slotsx<NARG+1, NVAR, NRET>(stackslots...);
}
template<int NARG, int NVAR, int NRET, class... SS>
void count_slotsx(LuaVar &v, SS & ... stackslots)
{
count_slotsx<NARG, NVAR+1, NRET>(stackslots...);
}
template<int NARG, int NVAR, int NRET, class... SS>
void count_slotsx(LuaRet &v, SS & ... stackslots)
{
count_slotsx<NARG, NVAR, NRET+1>(stackslots...);
}
template<int NARG, int NVAR, int NRET>
void count_slotsx() {
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>
LuaOldStack(lua_State *L, SS & ... stackslots) : LuaCoreStack(L) {
count_slotsx<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_;
}
~LuaOldStack() {};
};
////////////////////////////////////////////////////////////////////
//
// LuaDefStack
//
// This version of LuaStack is meant to be used at the top level of a LuaDefine.
// It can assign stack slots to LuaArg, LuaRet, and LuaVar locals. It arranges
// for the arguments to be in the LuaArg variables, and it arranges for the
// LuaRet variables to be returned. It also makes sure that the function has
// the correct number of arguments.
// This version of LuaStack should only be used inside a LuaDefine. It can
// assign stack slots to LuaArg, LuaRet, LuaVar, and LuaExtraArgs. It
// arranges for the arguments to be in the LuaArg variables, and it arranges for
// the LuaRet variables to be returned. It also makes sure that the function
// has the correct number of arguments.
//
// At the end of the LuaDefine function, you're supposed to return LS.result().
// LS.result causes the allocated stack slots to be freed except for the LuaRet
@@ -659,11 +548,11 @@ public:
// The lua interpreter will clean up after an error or yield.
//
// Implementation note: LuaDefStack doesn't have a destructor to deallocate
// stack slots. That's deliberate: you shouldn't expect this class to clean
// up its stack frame, because after all, it has to leave return values on
// the stack. It would be deceptive to put a destructor, which then doesn't
// actually clean up anyway. Better to just let it be known that this
// class doesn't clean up its stack frame.
// stack slots. That's deliberate: you shouldn't expect this class to clean up
// its stack frame, because after all, it has to leave return values on the
// stack. It would be deceptive to put a destructor, which then doesn't
// actually clean up anyway. Better to just let it be known that this class
// doesn't clean up its stack frame.
//
////////////////////////////////////////////////////////////////////
@@ -742,7 +631,6 @@ public:
//
////////////////////////////////////////////////////////////////////
class LuaExtStack : public LuaCoreStack {
private:
int oldtop_;