libexpr: extract persistent eval state to new struct

"persistent" here means state that outlives any particular evaluation,
i.e. everything that exist outside of a call to eval() and friends. do
note that this leave EvalState nearly empty, containing only things of
importance to evaluation *itself*, e.g. the call depth tracker. do not
worry about the self-reference ctx member. nothing to see here, go on.

Change-Id: Ie8d0af3f09d33902f81e0c36a1096dd9f334a537
This commit is contained in:
eldritch horrors 2024-12-03 20:38:41 +01:00
parent ce698198f0
commit 8e74d1ceeb
2 changed files with 34 additions and 7 deletions

View file

@ -305,11 +305,13 @@ EvalPaths::EvalPaths(
}
}
EvalState::EvalState(
EvalContext::EvalContext(
EvalState & parent,
const SearchPath & _searchPath,
ref<Store> store,
std::shared_ptr<Store> buildStore,
std::function<ReplExitStatus(EvalState & es, ValMap const & extraEnv)> debugRepl)
std::function<ReplExitStatus(EvalState & es, ValMap const & extraEnv)> debugRepl
)
: s(symbols)
, paths(store, buildStore ? ref(buildStore) : store, [&] {
SearchPath searchPath;
@ -328,7 +330,7 @@ EvalState::EvalState(
debugRepl ? std::make_unique<DebugState>(
positions,
symbols,
[this, debugRepl](const ValMap & extraEnv) { return debugRepl(*this, extraEnv); }
[&parent, debugRepl](const ValMap & extraEnv) { return debugRepl(parent, extraEnv); }
)
: nullptr
}
@ -339,6 +341,14 @@ EvalState::EvalState(
static_assert(sizeof(Env) <= 16, "environment must be <= 16 bytes");
}
EvalState::EvalState(
const SearchPath & _searchPath,
ref<Store> store,
std::shared_ptr<Store> buildStore,
std::function<ReplExitStatus(EvalState & es, ValMap const & extraEnv)> debugRepl)
: EvalContext(*this, _searchPath, store, buildStore, debugRepl)
{
}
EvalState::~EvalState()
{

View file

@ -484,11 +484,8 @@ struct EvalStatistics
void addCall(ExprLambda & fun);
};
class EvalState
class EvalContext
{
friend class EvalBuiltins;
public:
SymbolTable symbols;
PosTable positions;
@ -513,7 +510,27 @@ public:
std::unique_ptr<DebugState> debug;
EvalErrorContext errors;
EvalContext(
EvalState & parent,
const SearchPath & _searchPath,
ref<Store> store,
std::shared_ptr<Store> buildStore = nullptr,
std::function<ReplExitStatus(EvalState & es, ValMap const & extraEnv)> debugRepl = nullptr
);
EvalContext(const EvalContext &) = delete;
EvalContext(EvalContext &&) = delete;
EvalContext & operator=(const EvalContext &) = delete;
EvalContext & operator=(EvalContext &&) = delete;
};
class EvalState : public EvalContext
{
friend class EvalBuiltins;
public:
EvalState & ctx{*this};
EvalState(
const SearchPath & _searchPath,