From d4fcbe1687c3d212966e43118341622cf69612f3 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Sat, 4 Oct 2014 16:41:24 +0200 Subject: [PATCH] =?UTF-8?q?Add=20primop=20=E2=80=98attrValues=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/libexpr/primops.cc | 24 +++++++++++++++++++++++- tests/lang/eval-okay-attrnames.nix | 2 +- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 072866a3..fb37e21a 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -974,7 +974,28 @@ static void prim_attrNames(EvalState & state, const Pos & pos, Value * * args, V for (auto & i : *args[0]->attrs) mkString(*(v.list.elems[n++] = state.allocValue()), i.name); - std::sort(v.list.elems, v.list.elems + n, [](Value * v1, Value * v2) { return strcmp(v1->string.s, v2->string.s) < 0; }); + std::sort(v.list.elems, v.list.elems + n, + [](Value * v1, Value * v2) { return strcmp(v1->string.s, v2->string.s) < 0; }); +} + + +/* Return the values of the attributes in a set as a list, in the same + order as attrNames. */ +static void prim_attrValues(EvalState & state, const Pos & pos, Value * * args, Value & v) +{ + state.forceAttrs(*args[0], pos); + + state.mkList(v, args[0]->attrs->size()); + + unsigned int n = 0; + for (auto & i : *args[0]->attrs) + v.list.elems[n++] = (Value *) &i; + + std::sort(v.list.elems, v.list.elems + n, + [](Value * v1, Value * v2) { return (string) ((Attr *) v1)->name < (string) ((Attr *) v2)->name; }); + + for (unsigned int i = 0; i < n; ++i) + v.list.elems[i] = ((Attr *) v.list.elems[i])->value; } @@ -1501,6 +1522,7 @@ void EvalState::createBaseEnv() // Sets addPrimOp("__attrNames", 1, prim_attrNames); + addPrimOp("__attrValues", 1, prim_attrValues); addPrimOp("__getAttr", 2, prim_getAttr); addPrimOp("__unsafeGetAttrPos", 2, prim_unsafeGetAttrPos); addPrimOp("__hasAttr", 2, prim_hasAttr); diff --git a/tests/lang/eval-okay-attrnames.nix b/tests/lang/eval-okay-attrnames.nix index 978138f0..e5b26e9f 100644 --- a/tests/lang/eval-okay-attrnames.nix +++ b/tests/lang/eval-okay-attrnames.nix @@ -8,4 +8,4 @@ let values = map (name: builtins.getAttr name attrs) names; -in concat values +in assert values == builtins.attrValues attrs; concat values