primops.cc: cleanup
Change-Id: Iae0580e8f44988b8452aef77e67ca2495c342bf6
This commit is contained in:
parent
8035abdc37
commit
09139554a8
1 changed files with 105 additions and 194 deletions
|
@ -1,20 +1,10 @@
|
|||
#include "archive.hh"
|
||||
#include "derivations.hh"
|
||||
#include "downstream-placeholder.hh"
|
||||
#include "eval-inline.hh"
|
||||
#include "eval.hh"
|
||||
#include "eval-settings.hh"
|
||||
#include "gc-small-vector.hh"
|
||||
#include "globals.hh"
|
||||
#include "json-to-value.hh"
|
||||
#include "names.hh"
|
||||
#include "path-references.hh"
|
||||
#include "store-api.hh"
|
||||
#include "util.hh"
|
||||
#include "value-to-json.hh"
|
||||
#include "value-to-xml.hh"
|
||||
#include "primops.hh"
|
||||
#include "fetch-to-store.hh"
|
||||
|
||||
#include <boost/container/small_vector.hpp>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
@ -24,147 +14,20 @@
|
|||
#include <unistd.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <regex>
|
||||
#include <dlfcn.h>
|
||||
|
||||
#include <cmath>
|
||||
|
||||
namespace nix {
|
||||
|
||||
|
||||
/*************************************************************
|
||||
* Miscellaneous
|
||||
*************************************************************/
|
||||
|
||||
|
||||
/* Execute a program and parse its output */
|
||||
|
||||
|
||||
|
||||
template<typename Callable>
|
||||
static inline void withExceptionContext(Trace trace, Callable&& func)
|
||||
{
|
||||
try
|
||||
{
|
||||
func();
|
||||
}
|
||||
catch(Error & e)
|
||||
{
|
||||
e.pushTrace(trace);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* Try evaluating the argument. Success => {success=true; value=something;},
|
||||
* else => {success=false; value=false;} */
|
||||
|
||||
/* Return an environment variable. Use with care. */
|
||||
/* Evaluate the first argument, then return the second argument. */
|
||||
|
||||
|
||||
|
||||
/*************************************************************
|
||||
* Derivations
|
||||
*************************************************************/
|
||||
|
||||
static void derivationStrictInternal(EvalState & state, const std::string & name, Bindings * attrs, Value & v);
|
||||
|
||||
|
||||
|
||||
|
||||
/*************************************************************
|
||||
* Paths
|
||||
*************************************************************/
|
||||
|
||||
/*************************************************************
|
||||
* Creating files
|
||||
*************************************************************/
|
||||
|
||||
|
||||
/* Convert the argument (which can be any Nix expression) to an XML
|
||||
representation returned in a string. Not all Nix expressions can
|
||||
be sensibly or completely represented (e.g., functions). */
|
||||
|
||||
|
||||
/* Store a string in the Nix store as a source file that can be used
|
||||
as an input by derivations. */
|
||||
|
||||
|
||||
|
||||
/*************************************************************
|
||||
* Sets
|
||||
*************************************************************/
|
||||
|
||||
|
||||
/* Determine whether the argument is a set. */
|
||||
|
||||
|
||||
/* Builds a set from a list specifying (name, value) pairs. To be
|
||||
precise, a list [{name = "name1"; value = value1;} ... {name =
|
||||
"nameN"; value = valueN;}] is transformed to {name1 = value1;
|
||||
... nameN = valueN;}. In case of duplicate occurrences of the same
|
||||
name, the first takes precedence. */
|
||||
|
||||
|
||||
/* */
|
||||
|
||||
|
||||
|
||||
/*************************************************************
|
||||
* Lists
|
||||
*************************************************************/
|
||||
|
||||
|
||||
|
||||
/* Return the first element of a list. */
|
||||
|
||||
|
||||
/* Apply a function to every element of a list. */
|
||||
|
||||
/* Return the length of a list. This is an O(1) time operation. */
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*************************************************************
|
||||
* Integer arithmetic
|
||||
*************************************************************/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*************************************************************
|
||||
* String manipulation
|
||||
*************************************************************/
|
||||
|
||||
|
||||
/*************************************************************
|
||||
* Versions
|
||||
*************************************************************/
|
||||
|
||||
|
||||
|
||||
/*************************************************************
|
||||
* Primop registration
|
||||
*************************************************************/
|
||||
|
||||
|
||||
RegisterPrimOp::PrimOps * RegisterPrimOp::primOps;
|
||||
|
||||
|
||||
RegisterPrimOp::RegisterPrimOp(PrimOp && primOp)
|
||||
{
|
||||
if (!primOps) primOps = new PrimOps;
|
||||
if (!primOps) {
|
||||
primOps = new PrimOps;
|
||||
}
|
||||
primOps->push_back(std::move(primOp));
|
||||
}
|
||||
|
||||
|
||||
void EvalState::createBaseEnv()
|
||||
{
|
||||
baseEnv.up = 0;
|
||||
|
@ -174,7 +37,10 @@ void EvalState::createBaseEnv()
|
|||
|
||||
/* `builtins' must be first! */
|
||||
v.mkAttrs(buildBindings(128).finish());
|
||||
addConstant("builtins", v, {
|
||||
addConstant(
|
||||
"builtins",
|
||||
v,
|
||||
{
|
||||
.type = nAttrs,
|
||||
.doc = R"(
|
||||
Contains all the [built-in functions](@docroot@/language/builtins.md) and values.
|
||||
|
@ -186,10 +52,14 @@ void EvalState::createBaseEnv()
|
|||
if builtins ? hasContext then builtins.hasContext s else true
|
||||
```
|
||||
)",
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
v.mkBool(true);
|
||||
addConstant("true", v, {
|
||||
addConstant(
|
||||
"true",
|
||||
v,
|
||||
{
|
||||
.type = nBool,
|
||||
.doc = R"(
|
||||
Primitive value.
|
||||
|
@ -206,10 +76,14 @@ void EvalState::createBaseEnv()
|
|||
1
|
||||
```
|
||||
)",
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
v.mkBool(false);
|
||||
addConstant("false", v, {
|
||||
addConstant(
|
||||
"false",
|
||||
v,
|
||||
{
|
||||
.type = nBool,
|
||||
.doc = R"(
|
||||
Primitive value.
|
||||
|
@ -226,10 +100,14 @@ void EvalState::createBaseEnv()
|
|||
1
|
||||
```
|
||||
)",
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
v.mkNull();
|
||||
addConstant("null", v, {
|
||||
addConstant(
|
||||
"null",
|
||||
v,
|
||||
{
|
||||
.type = nNull,
|
||||
.doc = R"(
|
||||
Primitive value.
|
||||
|
@ -241,12 +119,16 @@ void EvalState::createBaseEnv()
|
|||
1
|
||||
```
|
||||
)",
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
if (!evalSettings.pureEval) {
|
||||
v.mkInt(time(0));
|
||||
}
|
||||
addConstant("__currentTime", v, {
|
||||
addConstant(
|
||||
"__currentTime",
|
||||
v,
|
||||
{
|
||||
.type = nInt,
|
||||
.doc = R"(
|
||||
Return the [Unix time](https://en.wikipedia.org/wiki/Unix_time) at first evaluation.
|
||||
|
@ -268,11 +150,16 @@ void EvalState::createBaseEnv()
|
|||
The [store path](@docroot@/glossary.md#gloss-store-path) of a derivation depending on `currentTime` will differ for each evaluation, unless both evaluate `builtins.currentTime` in the same second.
|
||||
)",
|
||||
.impureOnly = true,
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
if (!evalSettings.pureEval)
|
||||
if (!evalSettings.pureEval) {
|
||||
v.mkString(evalSettings.getCurrentSystem());
|
||||
addConstant("__currentSystem", v, {
|
||||
}
|
||||
addConstant(
|
||||
"__currentSystem",
|
||||
v,
|
||||
{
|
||||
.type = nString,
|
||||
.doc = R"(
|
||||
The value of the
|
||||
|
@ -298,10 +185,14 @@ void EvalState::createBaseEnv()
|
|||
```
|
||||
)",
|
||||
.impureOnly = true,
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
v.mkString("2.18.3-lix");
|
||||
addConstant("__nixVersion", v, {
|
||||
addConstant(
|
||||
"__nixVersion",
|
||||
v,
|
||||
{
|
||||
.type = nString,
|
||||
.doc = R"(
|
||||
Legacy version of Nix. Always returns "2.18.3-lix" on Lix.
|
||||
|
@ -314,10 +205,14 @@ void EvalState::createBaseEnv()
|
|||
If the feature you want to write compatibility code for cannot be
|
||||
detected by any means, please file a Lix bug.
|
||||
)",
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
v.mkString(store->storeDir);
|
||||
addConstant("__storeDir", v, {
|
||||
addConstant(
|
||||
"__storeDir",
|
||||
v,
|
||||
{
|
||||
.type = nString,
|
||||
.doc = R"(
|
||||
Logical file system location of the [Nix store](@docroot@/glossary.md#gloss-store) currently in use.
|
||||
|
@ -329,13 +224,17 @@ void EvalState::createBaseEnv()
|
|||
"/blah"
|
||||
```
|
||||
)",
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
/* Legacy language version.
|
||||
* This is fixed at 6, and will never change in the future on Lix.
|
||||
* A better language versioning construct needs to be built instead. */
|
||||
v.mkInt(6);
|
||||
addConstant("__langVersion", v, {
|
||||
addConstant(
|
||||
"__langVersion",
|
||||
v,
|
||||
{
|
||||
.type = nInt,
|
||||
.doc = R"(
|
||||
The legacy version of the Nix language. Always is `6` on Lix,
|
||||
|
@ -349,7 +248,8 @@ void EvalState::createBaseEnv()
|
|||
If the feature you want to write compatibility code for cannot be
|
||||
detected by any means, please file a Lix bug.
|
||||
)",
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
// Miscellaneous
|
||||
if (evalSettings.enableNativeCode) {
|
||||
|
@ -386,7 +286,10 @@ void EvalState::createBaseEnv()
|
|||
attrs.alloc("prefix").mkString(i.prefix.s);
|
||||
(v.listElems()[n++] = allocValue())->mkAttrs(attrs);
|
||||
}
|
||||
addConstant("__nixPath", v, {
|
||||
addConstant(
|
||||
"__nixPath",
|
||||
v,
|
||||
{
|
||||
.type = nList,
|
||||
.doc = R"(
|
||||
The search path used to resolve angle bracket path lookups.
|
||||
|
@ -406,16 +309,18 @@ void EvalState::createBaseEnv()
|
|||
builtins.findFile builtins.nixPath "nixpkgs"
|
||||
```
|
||||
)",
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
if (RegisterPrimOp::primOps)
|
||||
for (auto & primOp : *RegisterPrimOp::primOps)
|
||||
if (experimentalFeatureSettings.isEnabled(primOp.experimentalFeature))
|
||||
{
|
||||
if (RegisterPrimOp::primOps) {
|
||||
for (auto & primOp : *RegisterPrimOp::primOps) {
|
||||
if (experimentalFeatureSettings.isEnabled(primOp.experimentalFeature)) {
|
||||
auto primOpAdjusted = primOp;
|
||||
primOpAdjusted.arity = std::max(primOp.args.size(), primOp.arity);
|
||||
addPrimOp(std::move(primOpAdjusted));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Add a wrapper around the derivation primop that computes the
|
||||
`drvPath' and `outPath' attributes lazily.
|
||||
|
@ -423,9 +328,13 @@ void EvalState::createBaseEnv()
|
|||
Null docs because it is documented separately.
|
||||
*/
|
||||
auto vDerivation = allocValue();
|
||||
addConstant("derivation", vDerivation, {
|
||||
addConstant(
|
||||
"derivation",
|
||||
vDerivation,
|
||||
{
|
||||
.type = nFunction,
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
/* Now that we've added all primops, sort the `builtins' set,
|
||||
because attribute lookups expect it to be sorted. */
|
||||
|
@ -440,8 +349,10 @@ void EvalState::createBaseEnv()
|
|||
// the parser needs two NUL bytes as terminators; one of them
|
||||
// is implied by being a C string.
|
||||
"\0";
|
||||
eval(parse(code, sizeof(code), derivationInternal, {CanonPath::root}, staticBaseEnv), *vDerivation);
|
||||
eval(
|
||||
parse(code, sizeof(code), derivationInternal, {CanonPath::root}, staticBaseEnv),
|
||||
*vDerivation
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue