From 884d59178735bcb5d5db7db97a05ad62a175493b Mon Sep 17 00:00:00 2001 From: Ben Burdette Date: Fri, 20 May 2022 10:33:50 -0600 Subject: [PATCH] debugRepl ftn pointer --- src/libcmd/command.cc | 27 +++++++++++++++++++++++++++ src/libexpr/eval.cc | 12 ++++++++---- src/libexpr/eval.hh | 10 +++++----- src/libexpr/primops.cc | 2 +- 4 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/libcmd/command.cc b/src/libcmd/command.cc index a4ea5bc33..b61b9b61d 100644 --- a/src/libcmd/command.cc +++ b/src/libcmd/command.cc @@ -120,7 +120,34 @@ ref EvalCommand::getEvalState() ; evalState->debugMode = startReplOnEvalErrors; + // TODO move this somewhere else. Its only here to get the evalState ptr! + if (startReplOnEvalErrors) { + evalState->debugRepl = &runRepl; + }; + // // debuggerHook = [evalState{ref(evalState)}](const Error * error, const Env & env, const Expr & expr) { + // debuggerHook = [](EvalState & evalState, const Error * error, const Env & env, const Expr & expr) { + // auto dts = + // error && expr.getPos() + // ? std::make_unique( + // evalState, + // DebugTrace { + // .pos = error->info().errPos ? *error->info().errPos : evalState.positions[expr.getPos()], + // .expr = expr, + // .env = env, + // .hint = error->info().msg, + // .isError = true + // }) + // : nullptr; + + // if (error) + // printError("%s\n\n" ANSI_BOLD "Starting REPL to allow you to inspect the current state of the evaluator.\n" ANSI_NORMAL, error->what()); + + // auto se = evalState.getStaticEnv(expr); + // if (se) { + // auto vm = mapStaticEnvBindings(evalState.symbols, *se.get(), env); + // runRepl(evalState, *vm); + // } // if (startReplOnEvalErrors) // // debuggerHook = [evalState{ref(evalState)}](const Error * error, const Env & env, const Expr & expr) { diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index 1cde4a9ab..5faecdbe3 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -463,7 +463,7 @@ EvalState::EvalState( , emptyBindings(0) , store(store) , buildStore(buildStore ? buildStore : store) - , debugMode(false) + , debugRepl(0) , debugStop(false) , debugQuit(false) , regexCache(makeRegexCache()) @@ -811,8 +811,12 @@ std::unique_ptr mapStaticEnvBindings(const SymbolTable & st, const Stati return vm; } -void EvalState::debugRepl(const Error * error, const Env & env, const Expr & expr) +void EvalState::runDebugRepl(const Error * error, const Env & env, const Expr & expr) { + // double check we've got the debugRepl ftn pointer. + if (!debugRepl) + return; + auto dts = error && expr.getPos() ? std::make_unique( @@ -832,7 +836,7 @@ void EvalState::debugRepl(const Error * error, const Env & env, const Expr & exp auto se = getStaticEnv(expr); if (se) { auto vm = mapStaticEnvBindings(symbols, *se.get(), env); - runRepl(*this, *vm); + (debugRepl)(*this, *vm); } } @@ -1070,7 +1074,7 @@ DebugTraceStacker::DebugTraceStacker(EvalState & evalState, DebugTrace t) { evalState.debugTraces.push_front(trace); if (evalState.debugStop && evalState.debugMode) - evalState.debugRepl(nullptr, trace.env, trace.expr); + evalState.runDebugRepl(nullptr, trace.env, trace.expr); } void Value::mkString(std::string_view s) diff --git a/src/libexpr/eval.hh b/src/libexpr/eval.hh index 711a4d6be..90bd5497b 100644 --- a/src/libexpr/eval.hh +++ b/src/libexpr/eval.hh @@ -48,8 +48,6 @@ struct Env Value * values[0]; }; -extern void runRepl(ref evalState, const ValMap & extraEnv); - void printEnvBindings(const EvalState &es, const Expr & expr, const Env & env); void printEnvBindings(const SymbolTable & st, const StaticEnv & se, const Env & env, int lvl = 0); @@ -129,6 +127,8 @@ public: RootValue vImportedDrvToDerivation = nullptr; /* Debugger */ + void (* debugRepl)(EvalState & es, const ValMap & extraEnv); + bool debugMode; bool debugStop; bool debugQuit; @@ -143,14 +143,14 @@ public: return std::shared_ptr();; } - void debugRepl(const Error * error, const Env & env, const Expr & expr); + void runDebugRepl(const Error * error, const Env & env, const Expr & expr); template [[gnu::noinline, gnu::noreturn]] void debugThrow(const E &error, const Env & env, const Expr & expr) { if (debugMode) - debugRepl(&error, env, expr); + runDebugRepl(&error, env, expr); throw error; } @@ -164,7 +164,7 @@ public: // DebugTrace stack. if (debugMode && !debugTraces.empty()) { const DebugTrace & last = debugTraces.front(); - debugRepl(&e, last.env, last.expr); + runDebugRepl(&e, last.env, last.expr); } throw e; diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index f7429197a..3ca377b7c 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -765,7 +765,7 @@ static RegisterPrimOp primop_break({ }); auto & dt = state.debugTraces.front(); - state.debugRepl(&error, dt.env, dt.expr); + state.runDebugRepl(&error, dt.env, dt.expr); if (state.debugQuit) { // If the user elects to quit the repl, throw an exception.