Skip to content

Commit

Permalink
Implement repl-init-file
Browse files Browse the repository at this point in the history
Closes #9940
  • Loading branch information
9999years committed Mar 9, 2024
1 parent e80cb0e commit 0d21dcd
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/libcmd/repl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ extern "C" {
#include "markdown.hh"
#include "local-fs-store.hh"
#include "print.hh"
#include "gc-small-vector.hh"
#include "print-options.hh"

#if HAVE_BOEHMGC
#define GC_INCLUDE_NEW
Expand Down Expand Up @@ -887,6 +889,49 @@ void NixRepl::loadFiles()
notice("Loading installable '%1%'...", what);
addAttrsToScope(*i);
}

if (evalSettings.replInitFile) {
notice("Loading '%1%'...", *evalSettings.replInitFile.get());
Value replInit;
state->evalFile(evalSettings.replInitFile, replInit);
// // Doesn't help:
// state->forceValue(replInit, replInit.determinePos(noPos));

auto infoBuilder = state->buildBindings(1);
Value currentSystem;
currentSystem.mkString(evalSettings.getCurrentSystem());
infoBuilder.insert(state->symbols.create("currentSystem"), &currentSystem);
Value info;
info.mkAttrs(infoBuilder.finish());

// // This doesn't work either:
// auto map = mapStaticEnvBindings(state->symbols, *staticEnv, *env);
// auto currentBuilder = state->buildBindings(map->size());
// for (auto & [name, value] : *map) {
// currentBuilder.insert(state->symbols.create(name), value);
// }

auto currentBuilder = state->buildBindings(staticEnv->vars.size());
for (auto & [symbol, displacement] : staticEnv->vars) {
currentBuilder.insert(symbol, env->values[displacement]);
}
Value currentAttrs;
currentAttrs.mkAttrs(currentBuilder.finish());

Value newAttrs;
SmallValueVector<2> args = {&info, &currentAttrs};
state->callFunction(replInit, args.size(), args.data(), newAttrs, replInit.determinePos(noPos));
std::cout << ValuePrinter(*state, newAttrs, errorPrintOptions) << std::endl;

addAttrsToScope(newAttrs);

// This prints the values of the variables fine:
for (auto & [symbol, displacement] : staticEnv->vars) {
auto value = *env->values[displacement];
state->forceValue(value, value.determinePos(noPos));
std::cout << state->symbols[symbol] << " = " << ValuePrinter(*state, value, errorPrintOptions) << std::endl;
}
}
}


Expand Down

0 comments on commit 0d21dcd

Please sign in to comment.