trying env args; but unecessary?

This commit is contained in:
Ben Burdette 2021-08-09 14:30:47 -06:00
parent 89264d20e6
commit 030271184f
2 changed files with 74 additions and 77 deletions

View file

@ -96,7 +96,8 @@ EvalCommand::EvalCommand()
.handler = {&startReplOnEvalErrors, true}, .handler = {&startReplOnEvalErrors, true},
}); });
} }
extern std::function<void(const Error & error, const std::map<std::string, Value *> & env)> debuggerHook; // extern std::function<void(const Error & error, const std::map<std::string, Value *> & env)> debuggerHook;
extern std::function<void(const Error & error, const Env & env)> debuggerHook;
ref<EvalState> EvalCommand::getEvalState() ref<EvalState> EvalCommand::getEvalState()
{ {

View file

@ -35,7 +35,8 @@
namespace nix { namespace nix {
std::function<void(const Error & error, const std::map<std::string, Value *> & env)> debuggerHook; // std::function<void(const Error & error, const std::map<std::string, Value *> & env)> debuggerHook;
std::function<void(const Error & error, const Env & env)> debuggerHook;
static char * dupString(const char * s) static char * dupString(const char * s)
{ {
@ -667,189 +668,189 @@ LocalNoInline(void addBindings(string prefix, Bindings &b, valmap &valmap))
} }
} }
LocalNoInline(valmap * mapEnvBindings(Env &env)) // LocalNoInline(valmap * mapEnvBindings(Env &env))
{ // {
// NOT going to use this // // NOT going to use this
if (env.staticEnv) { // if (env.valuemap) {
std::cout << "got static env" << std::endl; // std::cout << "got static env" << std::endl;
} // }
// std::cout << "envsize: " << env.values.size() << std::endl; // // std::cout << "envsize: " << env.values.size() << std::endl;
// std::cout << "size_t size: " << sizeof(size_t) << std::endl; // // std::cout << "size_t size: " << sizeof(size_t) << std::endl;
// std::cout << "envsize: " << env.size << std::endl; // // std::cout << "envsize: " << env.size << std::endl;
// std::cout << "envup: " << env.up << std::endl; // // std::cout << "envup: " << env.up << std::endl;
valmap *vm = env.up ? mapEnvBindings(*env.up) : new valmap(); // valmap *vm = env.up ? mapEnvBindings(*env.up) : new valmap();
/* // /*
size_t i=0; // size_t i=0;
do { // do {
std::cout << "env: " << i << " value: " << showType(*env.values[i]) << std::endl; // std::cout << "env: " << i << " value: " << showType(*env.values[i]) << std::endl;
// std::cout << *env.values[i] << std::endl; // // std::cout << *env.values[i] << std::endl;
++i; // ++i;
} while(i < (std::min(env.size, (size_t)100))); // } while(i < (std::min(env.size, (size_t)100)));
if (env.values[0]->type() == nAttrs) // if (env.values[0]->type() == nAttrs)
addBindings(std::to_string((int)env.size), *env.values[0]->attrs, *vm); // addBindings(std::to_string((int)env.size), *env.values[0]->attrs, *vm);
*/ // */
return vm; // return vm;
} // }
/* Every "format" object (even temporary) takes up a few hundred bytes /* Every "format" object (even temporary) takes up a few hundred bytes
of stack space, which is a real killer in the recursive of stack space, which is a real killer in the recursive
evaluator. So here are some helper functions for throwing evaluator. So here are some helper functions for throwing
exceptions. */ exceptions. */
LocalNoInlineNoReturn(void throwEvalError(const char * s, const string & s2, valmap * env)) LocalNoInlineNoReturn(void throwEvalError(const char * s, const string & s2, Env & env))
{ {
auto delenv = std::unique_ptr<valmap>(env); // auto delenv = std::unique_ptr<valmap>(env);
auto error = EvalError(s, s2); auto error = EvalError(s, s2);
if (debuggerHook) if (debuggerHook)
debuggerHook(error, *env); debuggerHook(error, env);
throw error; throw error;
} }
LocalNoInlineNoReturn(void throwEvalError(const Pos & pos, const char * s, const string & s2, valmap * env)) LocalNoInlineNoReturn(void throwEvalError(const Pos & pos, const char * s, const string & s2, Env & env))
{ {
auto delenv = std::unique_ptr<valmap>(env); // auto delenv = std::unique_ptr<valmap>(env);
auto error = EvalError({ auto error = EvalError({
.msg = hintfmt(s, s2), .msg = hintfmt(s, s2),
.errPos = pos .errPos = pos
}); });
if (debuggerHook) if (debuggerHook)
debuggerHook(error, *env); debuggerHook(error, env);
throw error; throw error;
} }
LocalNoInlineNoReturn(void throwEvalError(const char * s, const string & s2, const string & s3, valmap * env)) LocalNoInlineNoReturn(void throwEvalError(const char * s, const string & s2, const string & s3, Env & env))
{ {
auto delenv = std::unique_ptr<valmap>(env); // auto delenv = std::unique_ptr<valmap>(env);
auto error = EvalError(s, s2, s3); auto error = EvalError(s, s2, s3);
if (debuggerHook) if (debuggerHook)
debuggerHook(error, *env); debuggerHook(error, env);
throw error; throw error;
} }
LocalNoInlineNoReturn(void throwEvalError(const Pos & pos, const char * s, const string & s2, const string & s3, valmap * env)) LocalNoInlineNoReturn(void throwEvalError(const Pos & pos, const char * s, const string & s2, const string & s3, Env & env))
{ {
auto delenv = std::unique_ptr<valmap>(env); // auto delenv = std::unique_ptr<valmap>(env);
auto error = EvalError({ auto error = EvalError({
.msg = hintfmt(s, s2, s3), .msg = hintfmt(s, s2, s3),
.errPos = pos .errPos = pos
}); });
if (debuggerHook) if (debuggerHook)
debuggerHook(error, *env); debuggerHook(error, env);
throw error; throw error;
} }
LocalNoInlineNoReturn(void throwEvalError(const Pos & p1, const char * s, const Symbol & sym, const Pos & p2, valmap * env)) LocalNoInlineNoReturn(void throwEvalError(const Pos & p1, const char * s, const Symbol & sym, const Pos & p2, Env & env))
{ {
// p1 is where the error occurred; p2 is a position mentioned in the message. // p1 is where the error occurred; p2 is a position mentioned in the message.
auto delenv = std::unique_ptr<valmap>(env); // auto delenv = std::unique_ptr<valmap>(env);
auto error = EvalError({ auto error = EvalError({
.msg = hintfmt(s, sym, p2), .msg = hintfmt(s, sym, p2),
.errPos = p1 .errPos = p1
}); });
if (debuggerHook) if (debuggerHook)
debuggerHook(error, *env); debuggerHook(error, env);
throw error; throw error;
} }
LocalNoInlineNoReturn(void throwTypeError(const Pos & pos, const char * s, valmap * env)) LocalNoInlineNoReturn(void throwTypeError(const Pos & pos, const char * s, Env & env))
{ {
auto delenv = std::unique_ptr<valmap>(env); // auto delenv = std::unique_ptr<valmap>(env);
auto error = TypeError({ auto error = TypeError({
.msg = hintfmt(s), .msg = hintfmt(s),
.errPos = pos .errPos = pos
}); });
if (debuggerHook) if (debuggerHook)
debuggerHook(error, *env); debuggerHook(error, env);
throw error; throw error;
} }
LocalNoInlineNoReturn(void throwTypeError(const Pos & pos, const char * s, const Value & v, valmap * env)) LocalNoInlineNoReturn(void throwTypeError(const Pos & pos, const char * s, const Value & v, Env & env))
{ {
auto delenv = std::unique_ptr<valmap>(env); // auto delenv = std::unique_ptr<valmap>(env);
auto error = TypeError({ auto error = TypeError({
.msg = hintfmt(s, v), .msg = hintfmt(s, v),
.errPos = pos .errPos = pos
}); });
if (debuggerHook) if (debuggerHook)
debuggerHook(error, *env); debuggerHook(error, env);
throw error; throw error;
} }
LocalNoInlineNoReturn(void throwTypeError(const Pos & pos, const char * s, const string &s2, valmap * env)) LocalNoInlineNoReturn(void throwTypeError(const Pos & pos, const char * s, const string &s2, Env & env))
{ {
auto delenv = std::unique_ptr<valmap>(env); // auto delenv = std::unique_ptr<valmap>(env);
auto error = TypeError({ auto error = TypeError({
.msg = hintfmt(s, s2), .msg = hintfmt(s, s2),
.errPos = pos .errPos = pos
}); });
if (debuggerHook) if (debuggerHook)
debuggerHook(error, *env); debuggerHook(error, env);
throw error; throw error;
} }
LocalNoInlineNoReturn(void throwTypeError(const Pos & pos, const char * s, const ExprLambda & fun, const Symbol & s2, valmap * env)) LocalNoInlineNoReturn(void throwTypeError(const Pos & pos, const char * s, const ExprLambda & fun, const Symbol & s2, Env & env))
{ {
auto delenv = std::unique_ptr<valmap>(env); // auto delenv = std::unique_ptr<valmap>(env);
auto error = TypeError({ auto error = TypeError({
.msg = hintfmt(s, fun.showNamePos(), s2), .msg = hintfmt(s, fun.showNamePos(), s2),
.errPos = pos .errPos = pos
}); });
if (debuggerHook) if (debuggerHook)
debuggerHook(error, *env); debuggerHook(error, env);
throw error; throw error;
} }
LocalNoInlineNoReturn(void throwAssertionError(const Pos & pos, const char * s, const string & s1, valmap * env)) LocalNoInlineNoReturn(void throwAssertionError(const Pos & pos, const char * s, const string & s1, Env & env))
{ {
auto delenv = std::unique_ptr<valmap>(env); // auto delenv = std::unique_ptr<valmap>(env);
auto error = AssertionError({ auto error = AssertionError({
.msg = hintfmt(s, s1), .msg = hintfmt(s, s1),
.errPos = pos .errPos = pos
}); });
if (debuggerHook) if (debuggerHook)
debuggerHook(error, *env); debuggerHook(error, env);
throw error; throw error;
} }
LocalNoInlineNoReturn(void throwUndefinedVarError(const Pos & pos, const char * s, const string & s1, valmap * env)) LocalNoInlineNoReturn(void throwUndefinedVarError(const Pos & pos, const char * s, const string & s1, Env & env))
{ {
auto delenv = std::unique_ptr<valmap>(env); // auto delenv = std::unique_ptr<valmap>(env);
auto error = UndefinedVarError({ auto error = UndefinedVarError({
.msg = hintfmt(s, s1), .msg = hintfmt(s, s1),
.errPos = pos .errPos = pos
}); });
if (debuggerHook) if (debuggerHook)
debuggerHook(error, *env); debuggerHook(error, env);
throw error; throw error;
} }
LocalNoInlineNoReturn(void throwMissingArgumentError(const Pos & pos, const char * s, const string & s1, valmap * env)) LocalNoInlineNoReturn(void throwMissingArgumentError(const Pos & pos, const char * s, const string & s1, Env & env))
{ {
auto delenv = std::unique_ptr<valmap>(env); // auto delenv = std::unique_ptr<valmap>(env);
auto error = MissingArgumentError({ auto error = MissingArgumentError({
.msg = hintfmt(s, s1), .msg = hintfmt(s, s1),
.errPos = pos .errPos = pos
}); });
if (debuggerHook) if (debuggerHook)
debuggerHook(error, *env); debuggerHook(error, env);
throw error; throw error;
} }
@ -909,8 +910,7 @@ inline Value * EvalState::lookupVar(Env * env, const ExprVar & var, bool noEval)
return j->value; return j->value;
} }
if (!env->prevWith) if (!env->prevWith)
throwUndefinedVarError(var.pos, "undefined variable '%1%'", var.name, throwUndefinedVarError(var.pos, "undefined variable '%1%'", var.name, *env);
mapEnvBindings(*env));
for (size_t l = env->prevWith; l; --l, env = env->up) ; for (size_t l = env->prevWith; l; --l, env = env->up) ;
} }
} }
@ -1238,7 +1238,7 @@ void ExprAttrs::eval(EvalState & state, Env & env, Value & v)
Bindings::iterator j = v.attrs->find(nameSym); Bindings::iterator j = v.attrs->find(nameSym);
if (j != v.attrs->end()) if (j != v.attrs->end())
throwEvalError(i.pos, "dynamic attribute '%1%' already defined at %2%", nameSym, *j->pos, throwEvalError(i.pos, "dynamic attribute '%1%' already defined at %2%", nameSym, *j->pos,
mapEnvBindings(env)); env);
// map1("value", &v)); // TODO dynamicAttrs to env? // map1("value", &v)); // TODO dynamicAttrs to env?
i.valueExpr->setName(nameSym); i.valueExpr->setName(nameSym);
@ -1332,8 +1332,7 @@ void ExprSelect::eval(EvalState & state, Env & env, Value & v)
} else { } else {
state.forceAttrs(*vAttrs, pos); state.forceAttrs(*vAttrs, pos);
if ((j = vAttrs->attrs->find(name)) == vAttrs->attrs->end()) if ((j = vAttrs->attrs->find(name)) == vAttrs->attrs->end())
throwEvalError(pos, "attribute '%1%' missing", name, throwEvalError(pos, "attribute '%1%' missing", name, env);
mapEnvBindings(env));
// mapBindings(*vAttrs->attrs)); // mapBindings(*vAttrs->attrs));
} }
vAttrs = j->value; vAttrs = j->value;
@ -1622,7 +1621,8 @@ this case it must have its arguments supplied either by default
values, or passed explicitly with '--arg' or '--argstr'. See values, or passed explicitly with '--arg' or '--argstr'. See
https://nixos.org/manual/nix/stable/#ss-functions.)", https://nixos.org/manual/nix/stable/#ss-functions.)",
i.name, i.name,
mapBindings(args)); fun.lambda.env);
// mapBindings(args));
// map1("fun", &fun)); // todo add bindings + fun // map1("fun", &fun)); // todo add bindings + fun
} }
} }
@ -1642,7 +1642,7 @@ void ExprWith::eval(EvalState & state, Env & env, Value & v)
env2.type = Env::HasWithExpr; env2.type = Env::HasWithExpr;
env2.values[0] = (Value *) attrs; env2.values[0] = (Value *) attrs;
env2.valuemap = mapBindings(attrs) env2.valuemap = mapBindings(*attrs)
body->eval(state, env2, v); body->eval(state, env2, v);
} }
@ -1813,8 +1813,7 @@ void ExprConcatStrings::eval(EvalState & state, Env & env, Value & v)
} else { } else {
std::cerr << "envtype: " << showType(env.values[0]->type()) << std::endl; std::cerr << "envtype: " << showType(env.values[0]->type()) << std::endl;
throwEvalError(pos, "cannot add %1% to an integer", showType(vTmp), throwEvalError(pos, "cannot add %1% to an integer", showType(vTmp), env);
mapEnvBindings(env));
} }
} else if (firstType == nFloat) { } else if (firstType == nFloat) {
if (vTmp.type() == nInt) { if (vTmp.type() == nInt) {
@ -1822,8 +1821,7 @@ void ExprConcatStrings::eval(EvalState & state, Env & env, Value & v)
} else if (vTmp.type() == nFloat) { } else if (vTmp.type() == nFloat) {
nf += vTmp.fpoint; nf += vTmp.fpoint;
} else } else
throwEvalError(pos, "cannot add %1% to a float", showType(vTmp), throwEvalError(pos, "cannot add %1% to a float", showType(vTmp), env);
mapEnvBindings(env));
} else } else
s << state.coerceToString(pos, vTmp, context, false, firstType == nString); s << state.coerceToString(pos, vTmp, context, false, firstType == nString);
} }
@ -2061,8 +2059,7 @@ string EvalState::coerceToString(const Pos & pos, Value & v, PathSet & context,
} }
auto i = v.attrs->find(sOutPath); auto i = v.attrs->find(sOutPath);
if (i == v.attrs->end()) if (i == v.attrs->end())
throwTypeError(pos, "cannot coerce a set to a string", throwTypeError(pos, "cannot coerce a set to a string", map1("value", &v));
map1("value", &v));
return coerceToString(pos, *i->value, context, coerceMore, copyToStore); return coerceToString(pos, *i->value, context, coerceMore, copyToStore);
} }
@ -2093,12 +2090,11 @@ string EvalState::coerceToString(const Pos & pos, Value & v, PathSet & context,
} }
} }
throwTypeError(pos, "cannot coerce %1% to a string", v, throwTypeError(pos, "cannot coerce %1% to a string", v, map1("value", &v));
map1("value", &v));
} }
string EvalState::copyPathToStore(PathSet & context, const Path & path) string EvalState::copyPathToStore(Env &env, PathSet & context, const Path & path)
{ {
if (nix::isDerivation(path)) if (nix::isDerivation(path))
throwEvalError("file names are not allowed to end in '%1%'", throwEvalError("file names are not allowed to end in '%1%'",