[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
22/118: Share code between scopedImport and import
From: |
Ludovic Courtès |
Subject: |
22/118: Share code between scopedImport and import |
Date: |
Tue, 19 May 2015 14:45:20 +0000 |
civodul pushed a commit to branch nix
in repository guix.
commit a8fb575c98726f195d0cf5c7e6b7e51c75a0a9b3
Author: Shea Levy <address@hidden>
Date: Fri May 30 15:04:17 2014 -0400
Share code between scopedImport and import
In addition to reducing duplication, this fixes both import from
derivation and import of derivation for scopedImport
---
src/libexpr/primops.cc | 86 ++++++++++++++++++++++++-----------------------
1 files changed, 44 insertions(+), 42 deletions(-)
diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc
index feb0227..f270ca3 100644
--- a/src/libexpr/primops.cc
+++ b/src/libexpr/primops.cc
@@ -39,31 +39,34 @@ std::pair<string, string> decodeContext(const string & s)
/* Load and evaluate an expression from path specified by the
argument. */
-static void prim_import(EvalState & state, const Pos & pos, Value * * args,
Value & v)
+static void prim_scopedImport(EvalState & state, const Pos & pos, Value * *
args, Value & v)
{
PathSet context;
- Path path = state.coerceToPath(pos, *args[0], context);
+ Path path = state.coerceToPath(pos, *args[1], context);
- foreach (PathSet::iterator, i, context) {
- Path ctx = decodeContext(*i).first;
+ PathSet drvs;
+ for (auto & i : context) {
+ std::pair<string, string> decoded = decodeContext(i);
+ Path ctx = decoded.first;
assert(isStorePath(ctx));
if (!store->isValidPath(ctx))
throw EvalError(format("cannot import `%1%', since path `%2%' is
not valid, at %3%")
% path % ctx % pos);
if (isDerivation(ctx))
- try {
- /* For performance, prefetch all substitute info. */
- PathSet willBuild, willSubstitute, unknown;
- unsigned long long downloadSize, narSize;
- queryMissing(*store, singleton<PathSet>(ctx),
- willBuild, willSubstitute, unknown, downloadSize, narSize);
-
- /* !!! If using a substitute, we only need to fetch
- the selected output of this derivation. */
- store->buildPaths(singleton<PathSet>(ctx));
- } catch (Error & e) {
- throw ImportError(e.msg());
- }
+ drvs.insert(decoded.first + "!" + decoded.second);
+ }
+ if (!drvs.empty()) {
+ try {
+ /* For performance, prefetch all substitute info. */
+ PathSet willBuild, willSubstitute, unknown;
+ unsigned long long downloadSize, narSize;
+ queryMissing(*store, drvs,
+ willBuild, willSubstitute, unknown, downloadSize, narSize);
+
+ store->buildPaths(drvs);
+ } catch (Error & e) {
+ throw ImportError(e.msg());
+ }
}
if (isStorePath(path) && store->isValidPath(path) && isDerivation(path)) {
@@ -88,32 +91,27 @@ static void prim_import(EvalState & state, const Pos & pos,
Value * * args, Valu
mkApp(v, fun, w);
state.forceAttrs(v, pos);
} else {
- state.evalFile(path, v);
- }
-}
-
-
-static void prim_scopedImport(EvalState & state, const Pos & pos, Value * *
args, Value & v)
-{
- PathSet context;
- state.forceAttrs(*args[0]);
- Path path = resolveExprPath(state.coerceToPath(pos, *args[1], context));
-
- Env * env = &state.allocEnv(args[0]->attrs->size());
- env->up = &state.baseEnv;
+ state.forceAttrs(*args[0]);
+ if (args[0]->attrs->empty())
+ state.evalFile(path, v);
+ else {
+ Env * env = &state.allocEnv(args[0]->attrs->size());
+ env->up = &state.baseEnv;
+
+ StaticEnv staticEnv(false, &state.staticBaseEnv);
+
+ unsigned int displ = 0;
+ for (auto & attr : *args[0]->attrs) {
+ staticEnv.vars[attr.name] = displ;
+ env->values[displ++] = attr.value;
+ }
- StaticEnv staticEnv(false, &state.staticBaseEnv);
+ startNest(nest, lvlTalkative, format("evaluating file `%1%'") %
path);
+ Expr * e = state.parseExprFromFile(resolveExprPath(path),
staticEnv);
- unsigned int displ = 0;
- for (auto & attr : *args[0]->attrs) {
- staticEnv.vars[attr.name] = displ;
- env->values[displ++] = attr.value;
+ e->eval(state, *env, v);
+ }
}
-
- startNest(nest, lvlTalkative, format("evaluating file `%1%'") % path);
- Expr * e = state.parseExprFromFile(path, staticEnv);
-
- e->eval(state, *env, v);
}
@@ -1301,8 +1299,12 @@ void EvalState::createBaseEnv()
addConstant("__langVersion", v);
// Miscellaneous
- addPrimOp("import", 1, prim_import);
addPrimOp("scopedImport", 2, prim_scopedImport);
+ Value * v2 = allocValue();
+ mkAttrs(*v2, 0);
+ mkApp(v, *baseEnv.values[baseEnvDispl - 1], *v2);
+ forceValue(v);
+ addConstant("import", v);
addPrimOp("__typeOf", 1, prim_typeOf);
addPrimOp("isNull", 1, prim_isNull);
addPrimOp("__isFunction", 1, prim_isFunction);
@@ -1388,7 +1390,7 @@ void EvalState::createBaseEnv()
mkList(v, searchPath.size());
int n = 0;
for (auto & i : searchPath) {
- Value * v2 = v.list.elems[n++] = allocValue();
+ v2 = v.list.elems[n++] = allocValue();
mkAttrs(*v2, 2);
mkString(*allocAttr(*v2, symbols.create("path")), i.second);
mkString(*allocAttr(*v2, symbols.create("prefix")), i.first);
- 19/118: Report daemon OOM better, (continued)
- 19/118: Report daemon OOM better, Ludovic Courtès, 2015/05/19
- 06/118: Add primop ‘scopedImport’, Ludovic Courtès, 2015/05/19
- 21/118: Add autoloads, make code more concise & idiomatic, Ludovic Courtès, 2015/05/19
- 25/118: Fix bogus warnings about dumping large paths, Ludovic Courtès, 2015/05/19
- 20/118: == operator: Ignore string context, Ludovic Courtès, 2015/05/19
- 17/118: nix-env -qa --json: Generate valid JSON even if there are invalid meta attrs, Ludovic Courtès, 2015/05/19
- 23/118: findFile: Realise the context of the path attributes, Ludovic Courtès, 2015/05/19
- 11/118: Rephrase @ operator description, Ludovic Courtès, 2015/05/19
- 29/118: Merge branch 'shlevy-import-native', Ludovic Courtès, 2015/05/19
- 12/118: dev-shell is a bash script, not sh, Ludovic Courtès, 2015/05/19
- 22/118: Share code between scopedImport and import,
Ludovic Courtès <=
- 26/118: Don't use member initialisers, Ludovic Courtès, 2015/05/19
- 31/118: Add `--json` argument to `nix-instantiate`, Ludovic Courtès, 2015/05/19
- 18/118: Print a warning when loading a large path into memory, Ludovic Courtès, 2015/05/19
- 27/118: Add importNative primop, Ludovic Courtès, 2015/05/19
- 24/118: Drop ImportError and FindError, Ludovic Courtès, 2015/05/19
- 28/118: Only add the importNative primop if the allow-arbitrary-code-during-evaluation option is true (default false), Ludovic Courtès, 2015/05/19
- 30/118: allow-arbitrary-code-during-evaluation -> allow-unsafe-native-code-during-evaluation, Ludovic Courtès, 2015/05/19
- 34/118: Add builtin function ‘fromJSON’, Ludovic Courtès, 2015/05/19
- 36/118: Fix compilation error on some versions of GCC, Ludovic Courtès, 2015/05/19
- 32/118: Style fix, Ludovic Courtès, 2015/05/19