diff options
author | Eelco Dolstra <eelco.dolstra@logicblox.com> | 2015-07-23T17·23+0200 |
---|---|---|
committer | Eelco Dolstra <eelco.dolstra@logicblox.com> | 2015-07-23T17·23+0200 |
commit | 14be783676adbb3517b2f73fee31c6f341575440 (patch) | |
tree | 759d4cec921a562e5c3f00ec9db251639ae5e526 /src/libexpr/primops.cc | |
parent | 39e27a04b8d1fc8251128b1f9f8f152b8c8a9b68 (diff) |
Add primops all and any
These are used thousands of times during NixOS evaluation, so it's useful to speed them up.
Diffstat (limited to 'src/libexpr/primops.cc')
-rw-r--r-- | src/libexpr/primops.cc | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index dd21bdc57e4f..d1ad75a286e7 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -1333,6 +1333,37 @@ static void prim_foldlStrict(EvalState & state, const Pos & pos, Value * * args, } +static void anyOrAll(bool any, EvalState & state, const Pos & pos, Value * * args, Value & v) +{ + state.forceFunction(*args[0], pos); + state.forceList(*args[1], pos); + + Value vTmp; + for (unsigned int n = 0; n < args[1]->list.length; ++n) { + state.callFunction(*args[0], *args[1]->list.elems[n], vTmp, pos); + bool res = state.forceBool(vTmp); + if (res == any) { + mkBool(v, any); + return; + } + } + + mkBool(v, !any); +} + + +static void prim_any(EvalState & state, const Pos & pos, Value * * args, Value & v) +{ + anyOrAll(true, state, pos, args, v); +} + + +static void prim_all(EvalState & state, const Pos & pos, Value * * args, Value & v) +{ + anyOrAll(false, state, pos, args, v); +} + + /************************************************************* * Integer arithmetic *************************************************************/ @@ -1671,6 +1702,8 @@ void EvalState::createBaseEnv() addPrimOp("__concatLists", 1, prim_concatLists); addPrimOp("__length", 1, prim_length); addPrimOp("__foldl'", 3, prim_foldlStrict); + addPrimOp("__any", 2, prim_any); + addPrimOp("__all", 2, prim_all); // Integer arithmetic addPrimOp("__add", 2, prim_add); |