2013-11-19 01:33:06 +02:00
|
|
|
#include "value-to-json.hh"
|
2013-11-19 01:03:11 +02:00
|
|
|
#include "eval-inline.hh"
|
2022-12-20 15:58:39 +02:00
|
|
|
#include "store-api.hh"
|
2023-10-25 07:43:36 +03:00
|
|
|
#include "signals.hh"
|
2013-11-19 01:03:11 +02:00
|
|
|
|
|
|
|
#include <cstdlib>
|
2014-09-30 01:41:18 +03:00
|
|
|
#include <iomanip>
|
2022-11-16 17:49:49 +02:00
|
|
|
#include <nlohmann/json.hpp>
|
2013-11-19 01:03:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
namespace nix {
|
2022-11-16 17:49:49 +02:00
|
|
|
using json = nlohmann::json;
|
|
|
|
json printValueAsJSON(EvalState & state, bool strict,
|
Use `std::set<StringContextElem>` not `PathSet` for string contexts
Motivation
`PathSet` is not correct because string contexts have other forms
(`Built` and `DrvDeep`) that are not rendered as plain store paths.
Instead of wrongly using `PathSet`, or "stringly typed" using
`StringSet`, use `std::std<StringContextElem>`.
-----
In support of this change, `NixStringContext` is now defined as
`std::std<StringContextElem>` not `std:vector<StringContextElem>`. The
old definition was just used by a `getContext` method which was only
used by the eval cache. It can be deleted altogether since the types are
now unified and the preexisting `copyContext` function already suffices.
Summarizing the previous paragraph:
Old:
- `value/context.hh`: `NixStringContext = std::vector<StringContextElem>`
- `value.hh`: `NixStringContext Value::getContext(...)`
- `value.hh`: `copyContext(...)`
New:
- `value/context.hh`: `NixStringContext = std::set<StringContextElem>`
- `value.hh`: `copyContext(...)`
----
The string representation of string context elements no longer contains
the store dir. The diff of `src/libexpr/tests/value/context.cc` should
make clear what the new representation is, so we recommend reviewing
that file first. This was done for two reasons:
Less API churn:
`Value::mkString` and friends did not take a `Store` before. But if
`NixStringContextElem::{parse, to_string}` *do* take a store (as they
did before), then we cannot have the `Value` functions use them (in
order to work with the fully-structured `NixStringContext`) without
adding that argument.
That would have been a lot of churn of threading the store, and this
diff is already large enough, so the easier and less invasive thing to
do was simply make the element `parse` and `to_string` functions not
take the `Store` reference, and the easiest way to do that was to simply
drop the store dir.
Space usage:
Dropping the `/nix/store/` (or similar) from the internal representation
will safe space in the heap of the Nix programming being interpreted. If
the heap contains many strings with non-trivial contexts, the saving
could add up to something significant.
----
The eval cache version is bumped.
The eval cache serialization uses `NixStringContextElem::{parse,
to_string}`, and since those functions are changed per the above, that
means the on-disk representation is also changed.
This is simply done by changing the name of the used for the eval cache
from `eval-cache-v4` to eval-cache-v5`.
----
To avoid some duplication `EvalCache::mkPathString` is added to abstract
over the simple case of turning a store path to a string with just that
string in the context.
Context
This PR picks up where #7543 left off. That one introduced the fully
structured `NixStringContextElem` data type, but kept `PathSet context`
as an awkward middle ground between internal `char[][]` interpreter heap
string contexts and `NixStringContext` fully parsed string contexts.
The infelicity of `PathSet context` was specifically called out during
Nix team group review, but it was agreeing that fixing it could be left
as future work. This is that future work.
A possible follow-up step would be to get rid of the `char[][]`
evaluator heap representation, too, but it is not yet clear how to do
that. To use `NixStringContextElem` there we would need to get the STL
containers to GC pointers in the GC build, and I am not sure how to do
that.
----
PR #7543 effectively is writing the inverse of a `mkPathString`,
`mkOutputString`, and one more such function for the `DrvDeep` case. I
would like that PR to have property tests ensuring it is actually the
inverse as expected.
This PR sets things up nicely so that reworking that PR to be in that
more elegant and better tested way is possible.
Co-authored-by: Théophane Hufschmitt <7226587+thufschmitt@users.noreply.github.com>
2023-01-29 03:31:10 +02:00
|
|
|
Value & v, const PosIdx pos, NixStringContext & context, bool copyToStore)
|
2013-11-19 01:03:11 +02:00
|
|
|
{
|
|
|
|
checkInterrupt();
|
|
|
|
|
2021-10-26 00:13:35 +03:00
|
|
|
if (strict) state.forceValue(v, pos);
|
2013-11-19 01:03:11 +02:00
|
|
|
|
2022-11-16 17:49:49 +02:00
|
|
|
json out;
|
|
|
|
|
2020-12-17 15:45:45 +02:00
|
|
|
switch (v.type()) {
|
2013-11-19 01:03:11 +02:00
|
|
|
|
2020-12-12 03:09:10 +02:00
|
|
|
case nInt:
|
2022-11-16 17:49:49 +02:00
|
|
|
out = v.integer;
|
2013-11-19 01:03:11 +02:00
|
|
|
break;
|
|
|
|
|
2020-12-12 03:09:10 +02:00
|
|
|
case nBool:
|
2022-11-16 17:49:49 +02:00
|
|
|
out = v.boolean;
|
2013-11-19 01:03:11 +02:00
|
|
|
break;
|
|
|
|
|
2020-12-12 03:09:10 +02:00
|
|
|
case nString:
|
2013-11-19 01:03:11 +02:00
|
|
|
copyContext(v, context);
|
2023-09-26 04:30:41 +03:00
|
|
|
out = v.c_str();
|
2013-11-19 01:03:11 +02:00
|
|
|
break;
|
|
|
|
|
2020-12-12 03:09:10 +02:00
|
|
|
case nPath:
|
2022-08-16 13:23:37 +03:00
|
|
|
if (copyToStore)
|
2023-04-06 14:15:50 +03:00
|
|
|
out = state.store->printStorePath(
|
|
|
|
state.copyPathToStore(context, v.path()));
|
2022-08-16 13:23:37 +03:00
|
|
|
else
|
2023-04-06 14:15:50 +03:00
|
|
|
out = v.path().path.abs();
|
2013-11-19 01:03:11 +02:00
|
|
|
break;
|
|
|
|
|
2020-12-12 03:09:10 +02:00
|
|
|
case nNull:
|
2023-06-30 02:29:11 +03:00
|
|
|
// already initialized as null
|
2013-11-19 01:03:11 +02:00
|
|
|
break;
|
|
|
|
|
2020-12-12 03:09:10 +02:00
|
|
|
case nAttrs: {
|
2021-10-27 22:48:48 +03:00
|
|
|
auto maybeString = state.tryAttrsToString(pos, v, context, false, false);
|
2019-10-27 11:15:51 +02:00
|
|
|
if (maybeString) {
|
2022-11-16 17:49:49 +02:00
|
|
|
out = *maybeString;
|
2019-10-27 11:15:51 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
auto i = v.attrs->find(state.sOutPath);
|
2013-11-19 01:03:11 +02:00
|
|
|
if (i == v.attrs->end()) {
|
2022-11-16 17:49:49 +02:00
|
|
|
out = json::object();
|
2013-11-19 01:03:11 +02:00
|
|
|
StringSet names;
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & j : *v.attrs)
|
2022-03-05 15:40:24 +02:00
|
|
|
names.emplace(state.symbols[j.name]);
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & j : names) {
|
|
|
|
Attr & a(*v.attrs->find(state.symbols.create(j)));
|
2023-06-30 02:29:11 +03:00
|
|
|
try {
|
|
|
|
out[j] = printValueAsJSON(state, strict, *a.value, a.pos, context, copyToStore);
|
|
|
|
} catch (Error & e) {
|
|
|
|
e.addTrace(state.positions[a.pos],
|
2024-02-04 06:35:19 +02:00
|
|
|
HintFmt("while evaluating attribute '%1%'", j));
|
2023-06-30 02:29:11 +03:00
|
|
|
throw;
|
|
|
|
}
|
2013-11-19 01:03:11 +02:00
|
|
|
}
|
|
|
|
} else
|
2022-11-16 17:49:49 +02:00
|
|
|
return printValueAsJSON(state, strict, *i->value, i->pos, context, copyToStore);
|
2013-11-19 01:03:11 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-12-12 03:09:10 +02:00
|
|
|
case nList: {
|
2022-11-16 17:49:49 +02:00
|
|
|
out = json::array();
|
2023-06-30 02:29:11 +03:00
|
|
|
int i = 0;
|
|
|
|
for (auto elem : v.listItems()) {
|
|
|
|
try {
|
|
|
|
out.push_back(printValueAsJSON(state, strict, *elem, pos, context, copyToStore));
|
|
|
|
} catch (Error & e) {
|
libexpr: Support structured error classes
While preparing PRs like #9753, I've had to change error messages in
dozens of code paths. It would be nice if instead of
EvalError("expected 'boolean' but found '%1%'", showType(v))
we could write
TypeError(v, "boolean")
or similar. Then, changing the error message could be a mechanical
refactor with the compiler pointing out places the constructor needs to
be changed, rather than the error-prone process of grepping through the
codebase. Structured errors would also help prevent the "same" error
from having multiple slightly different messages, and could be a first
step towards error codes / an error index.
This PR reworks the exception infrastructure in `libexpr` to
support exception types with different constructor signatures than
`BaseError`. Actually refactoring the exceptions to use structured data
will come in a future PR (this one is big enough already, as it has to
touch every exception in `libexpr`).
The core design is in `eval-error.hh`. Generally, errors like this:
state.error("'%s' is not a string", getAttrPathStr())
.debugThrow<TypeError>()
are transformed like this:
state.error<TypeError>("'%s' is not a string", getAttrPathStr())
.debugThrow()
The type annotation has moved from `ErrorBuilder::debugThrow` to
`EvalState::error`.
2024-01-23 03:08:29 +02:00
|
|
|
e.addTrace(state.positions[pos],
|
2024-02-04 06:35:19 +02:00
|
|
|
HintFmt("while evaluating list element at index %1%", i));
|
2023-06-30 02:29:11 +03:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
2013-11-19 01:03:11 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-12-12 03:09:10 +02:00
|
|
|
case nExternal:
|
2022-11-16 17:49:49 +02:00
|
|
|
return v.external->printValueAsJSON(state, strict, context, copyToStore);
|
2014-11-30 20:16:19 +02:00
|
|
|
break;
|
|
|
|
|
2020-12-12 03:09:10 +02:00
|
|
|
case nFloat:
|
2022-11-16 17:49:49 +02:00
|
|
|
out = v.fpoint;
|
2016-01-05 01:40:40 +02:00
|
|
|
break;
|
|
|
|
|
2020-12-12 03:09:10 +02:00
|
|
|
case nThunk:
|
|
|
|
case nFunction:
|
libexpr: Support structured error classes
While preparing PRs like #9753, I've had to change error messages in
dozens of code paths. It would be nice if instead of
EvalError("expected 'boolean' but found '%1%'", showType(v))
we could write
TypeError(v, "boolean")
or similar. Then, changing the error message could be a mechanical
refactor with the compiler pointing out places the constructor needs to
be changed, rather than the error-prone process of grepping through the
codebase. Structured errors would also help prevent the "same" error
from having multiple slightly different messages, and could be a first
step towards error codes / an error index.
This PR reworks the exception infrastructure in `libexpr` to
support exception types with different constructor signatures than
`BaseError`. Actually refactoring the exceptions to use structured data
will come in a future PR (this one is big enough already, as it has to
touch every exception in `libexpr`).
The core design is in `eval-error.hh`. Generally, errors like this:
state.error("'%s' is not a string", getAttrPathStr())
.debugThrow<TypeError>()
are transformed like this:
state.error<TypeError>("'%s' is not a string", getAttrPathStr())
.debugThrow()
The type annotation has moved from `ErrorBuilder::debugThrow` to
`EvalState::error`.
2024-01-23 03:08:29 +02:00
|
|
|
state.error<TypeError>(
|
|
|
|
"cannot convert %1% to JSON",
|
|
|
|
showType(v)
|
|
|
|
)
|
|
|
|
.atPos(v.determinePos(pos))
|
|
|
|
.debugThrow();
|
2013-11-19 01:03:11 +02:00
|
|
|
}
|
2022-11-16 17:49:49 +02:00
|
|
|
return out;
|
2013-11-19 01:03:11 +02:00
|
|
|
}
|
|
|
|
|
2016-08-26 19:55:55 +03:00
|
|
|
void printValueAsJSON(EvalState & state, bool strict,
|
Use `std::set<StringContextElem>` not `PathSet` for string contexts
Motivation
`PathSet` is not correct because string contexts have other forms
(`Built` and `DrvDeep`) that are not rendered as plain store paths.
Instead of wrongly using `PathSet`, or "stringly typed" using
`StringSet`, use `std::std<StringContextElem>`.
-----
In support of this change, `NixStringContext` is now defined as
`std::std<StringContextElem>` not `std:vector<StringContextElem>`. The
old definition was just used by a `getContext` method which was only
used by the eval cache. It can be deleted altogether since the types are
now unified and the preexisting `copyContext` function already suffices.
Summarizing the previous paragraph:
Old:
- `value/context.hh`: `NixStringContext = std::vector<StringContextElem>`
- `value.hh`: `NixStringContext Value::getContext(...)`
- `value.hh`: `copyContext(...)`
New:
- `value/context.hh`: `NixStringContext = std::set<StringContextElem>`
- `value.hh`: `copyContext(...)`
----
The string representation of string context elements no longer contains
the store dir. The diff of `src/libexpr/tests/value/context.cc` should
make clear what the new representation is, so we recommend reviewing
that file first. This was done for two reasons:
Less API churn:
`Value::mkString` and friends did not take a `Store` before. But if
`NixStringContextElem::{parse, to_string}` *do* take a store (as they
did before), then we cannot have the `Value` functions use them (in
order to work with the fully-structured `NixStringContext`) without
adding that argument.
That would have been a lot of churn of threading the store, and this
diff is already large enough, so the easier and less invasive thing to
do was simply make the element `parse` and `to_string` functions not
take the `Store` reference, and the easiest way to do that was to simply
drop the store dir.
Space usage:
Dropping the `/nix/store/` (or similar) from the internal representation
will safe space in the heap of the Nix programming being interpreted. If
the heap contains many strings with non-trivial contexts, the saving
could add up to something significant.
----
The eval cache version is bumped.
The eval cache serialization uses `NixStringContextElem::{parse,
to_string}`, and since those functions are changed per the above, that
means the on-disk representation is also changed.
This is simply done by changing the name of the used for the eval cache
from `eval-cache-v4` to eval-cache-v5`.
----
To avoid some duplication `EvalCache::mkPathString` is added to abstract
over the simple case of turning a store path to a string with just that
string in the context.
Context
This PR picks up where #7543 left off. That one introduced the fully
structured `NixStringContextElem` data type, but kept `PathSet context`
as an awkward middle ground between internal `char[][]` interpreter heap
string contexts and `NixStringContext` fully parsed string contexts.
The infelicity of `PathSet context` was specifically called out during
Nix team group review, but it was agreeing that fixing it could be left
as future work. This is that future work.
A possible follow-up step would be to get rid of the `char[][]`
evaluator heap representation, too, but it is not yet clear how to do
that. To use `NixStringContextElem` there we would need to get the STL
containers to GC pointers in the GC build, and I am not sure how to do
that.
----
PR #7543 effectively is writing the inverse of a `mkPathString`,
`mkOutputString`, and one more such function for the `DrvDeep` case. I
would like that PR to have property tests ensuring it is actually the
inverse as expected.
This PR sets things up nicely so that reworking that PR to be in that
more elegant and better tested way is possible.
Co-authored-by: Théophane Hufschmitt <7226587+thufschmitt@users.noreply.github.com>
2023-01-29 03:31:10 +02:00
|
|
|
Value & v, const PosIdx pos, std::ostream & str, NixStringContext & context, bool copyToStore)
|
2016-08-26 19:55:55 +03:00
|
|
|
{
|
2022-11-16 17:49:49 +02:00
|
|
|
str << printValueAsJSON(state, strict, v, pos, context, copyToStore);
|
2016-08-26 19:55:55 +03:00
|
|
|
}
|
2013-11-19 01:03:11 +02:00
|
|
|
|
2022-11-16 17:49:49 +02:00
|
|
|
json ExternalValueBase::printValueAsJSON(EvalState & state, bool strict,
|
Use `std::set<StringContextElem>` not `PathSet` for string contexts
Motivation
`PathSet` is not correct because string contexts have other forms
(`Built` and `DrvDeep`) that are not rendered as plain store paths.
Instead of wrongly using `PathSet`, or "stringly typed" using
`StringSet`, use `std::std<StringContextElem>`.
-----
In support of this change, `NixStringContext` is now defined as
`std::std<StringContextElem>` not `std:vector<StringContextElem>`. The
old definition was just used by a `getContext` method which was only
used by the eval cache. It can be deleted altogether since the types are
now unified and the preexisting `copyContext` function already suffices.
Summarizing the previous paragraph:
Old:
- `value/context.hh`: `NixStringContext = std::vector<StringContextElem>`
- `value.hh`: `NixStringContext Value::getContext(...)`
- `value.hh`: `copyContext(...)`
New:
- `value/context.hh`: `NixStringContext = std::set<StringContextElem>`
- `value.hh`: `copyContext(...)`
----
The string representation of string context elements no longer contains
the store dir. The diff of `src/libexpr/tests/value/context.cc` should
make clear what the new representation is, so we recommend reviewing
that file first. This was done for two reasons:
Less API churn:
`Value::mkString` and friends did not take a `Store` before. But if
`NixStringContextElem::{parse, to_string}` *do* take a store (as they
did before), then we cannot have the `Value` functions use them (in
order to work with the fully-structured `NixStringContext`) without
adding that argument.
That would have been a lot of churn of threading the store, and this
diff is already large enough, so the easier and less invasive thing to
do was simply make the element `parse` and `to_string` functions not
take the `Store` reference, and the easiest way to do that was to simply
drop the store dir.
Space usage:
Dropping the `/nix/store/` (or similar) from the internal representation
will safe space in the heap of the Nix programming being interpreted. If
the heap contains many strings with non-trivial contexts, the saving
could add up to something significant.
----
The eval cache version is bumped.
The eval cache serialization uses `NixStringContextElem::{parse,
to_string}`, and since those functions are changed per the above, that
means the on-disk representation is also changed.
This is simply done by changing the name of the used for the eval cache
from `eval-cache-v4` to eval-cache-v5`.
----
To avoid some duplication `EvalCache::mkPathString` is added to abstract
over the simple case of turning a store path to a string with just that
string in the context.
Context
This PR picks up where #7543 left off. That one introduced the fully
structured `NixStringContextElem` data type, but kept `PathSet context`
as an awkward middle ground between internal `char[][]` interpreter heap
string contexts and `NixStringContext` fully parsed string contexts.
The infelicity of `PathSet context` was specifically called out during
Nix team group review, but it was agreeing that fixing it could be left
as future work. This is that future work.
A possible follow-up step would be to get rid of the `char[][]`
evaluator heap representation, too, but it is not yet clear how to do
that. To use `NixStringContextElem` there we would need to get the STL
containers to GC pointers in the GC build, and I am not sure how to do
that.
----
PR #7543 effectively is writing the inverse of a `mkPathString`,
`mkOutputString`, and one more such function for the `DrvDeep` case. I
would like that PR to have property tests ensuring it is actually the
inverse as expected.
This PR sets things up nicely so that reworking that PR to be in that
more elegant and better tested way is possible.
Co-authored-by: Théophane Hufschmitt <7226587+thufschmitt@users.noreply.github.com>
2023-01-29 03:31:10 +02:00
|
|
|
NixStringContext & context, bool copyToStore) const
|
2014-11-30 20:16:19 +02:00
|
|
|
{
|
libexpr: Support structured error classes
While preparing PRs like #9753, I've had to change error messages in
dozens of code paths. It would be nice if instead of
EvalError("expected 'boolean' but found '%1%'", showType(v))
we could write
TypeError(v, "boolean")
or similar. Then, changing the error message could be a mechanical
refactor with the compiler pointing out places the constructor needs to
be changed, rather than the error-prone process of grepping through the
codebase. Structured errors would also help prevent the "same" error
from having multiple slightly different messages, and could be a first
step towards error codes / an error index.
This PR reworks the exception infrastructure in `libexpr` to
support exception types with different constructor signatures than
`BaseError`. Actually refactoring the exceptions to use structured data
will come in a future PR (this one is big enough already, as it has to
touch every exception in `libexpr`).
The core design is in `eval-error.hh`. Generally, errors like this:
state.error("'%s' is not a string", getAttrPathStr())
.debugThrow<TypeError>()
are transformed like this:
state.error<TypeError>("'%s' is not a string", getAttrPathStr())
.debugThrow()
The type annotation has moved from `ErrorBuilder::debugThrow` to
`EvalState::error`.
2024-01-23 03:08:29 +02:00
|
|
|
state.error<TypeError>("cannot convert %1% to JSON", showType())
|
|
|
|
.debugThrow();
|
2014-11-30 20:16:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-19 01:03:11 +02:00
|
|
|
}
|