2013-11-18 23:33:06 +00:00
|
|
|
#include "value-to-json.hh"
|
2013-11-18 23:03:11 +00:00
|
|
|
#include "eval-inline.hh"
|
2024-03-10 06:36:47 +00:00
|
|
|
#include "signals.hh"
|
2022-12-20 13:58:39 +00:00
|
|
|
#include "store-api.hh"
|
2013-11-18 23:03:11 +00:00
|
|
|
|
|
|
|
#include <cstdlib>
|
2014-09-29 22:41:18 +00:00
|
|
|
#include <iomanip>
|
2022-11-16 15:49:49 +00:00
|
|
|
#include <nlohmann/json.hpp>
|
2013-11-18 23:03:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace nix {
|
2022-11-16 15:49:49 +00: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 01:31:10 +00:00
|
|
|
Value & v, const PosIdx pos, NixStringContext & context, bool copyToStore)
|
2013-11-18 23:03:11 +00:00
|
|
|
{
|
|
|
|
checkInterrupt();
|
|
|
|
|
2021-10-25 21:13:35 +00:00
|
|
|
if (strict) state.forceValue(v, pos);
|
2013-11-18 23:03:11 +00:00
|
|
|
|
2022-11-16 15:49:49 +00:00
|
|
|
json out;
|
|
|
|
|
2020-12-17 13:45:45 +00:00
|
|
|
switch (v.type()) {
|
2013-11-18 23:03:11 +00:00
|
|
|
|
2020-12-12 01:09:10 +00:00
|
|
|
case nInt:
|
2022-11-16 15:49:49 +00:00
|
|
|
out = v.integer;
|
2013-11-18 23:03:11 +00:00
|
|
|
break;
|
|
|
|
|
2020-12-12 01:09:10 +00:00
|
|
|
case nBool:
|
2022-11-16 15:49:49 +00:00
|
|
|
out = v.boolean;
|
2013-11-18 23:03:11 +00:00
|
|
|
break;
|
|
|
|
|
2020-12-12 01:09:10 +00:00
|
|
|
case nString:
|
2013-11-18 23:03:11 +00:00
|
|
|
copyContext(v, context);
|
2022-11-16 15:49:49 +00:00
|
|
|
out = v.string.s;
|
2013-11-18 23:03:11 +00:00
|
|
|
break;
|
|
|
|
|
2020-12-12 01:09:10 +00:00
|
|
|
case nPath:
|
2022-08-16 10:23:37 +00:00
|
|
|
if (copyToStore)
|
2023-04-06 11:15:50 +00:00
|
|
|
out = state.store->printStorePath(
|
|
|
|
state.copyPathToStore(context, v.path()));
|
2022-08-16 10:23:37 +00:00
|
|
|
else
|
2023-04-06 11:15:50 +00:00
|
|
|
out = v.path().path.abs();
|
2013-11-18 23:03:11 +00:00
|
|
|
break;
|
|
|
|
|
2020-12-12 01:09:10 +00:00
|
|
|
case nNull:
|
2023-06-29 23:29:11 +00:00
|
|
|
// already initialized as null
|
2013-11-18 23:03:11 +00:00
|
|
|
break;
|
|
|
|
|
2020-12-12 01:09:10 +00:00
|
|
|
case nAttrs: {
|
2021-10-27 19:48:48 +00:00
|
|
|
auto maybeString = state.tryAttrsToString(pos, v, context, false, false);
|
2019-10-27 09:15:51 +00:00
|
|
|
if (maybeString) {
|
2022-11-16 15:49:49 +00:00
|
|
|
out = *maybeString;
|
2019-10-27 09:15:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
auto i = v.attrs->find(state.sOutPath);
|
2013-11-18 23:03:11 +00:00
|
|
|
if (i == v.attrs->end()) {
|
2022-11-16 15:49:49 +00:00
|
|
|
out = json::object();
|
2013-11-18 23:03:11 +00:00
|
|
|
StringSet names;
|
2015-07-17 17:24:28 +00:00
|
|
|
for (auto & j : *v.attrs)
|
2022-03-05 13:40:24 +00:00
|
|
|
names.emplace(state.symbols[j.name]);
|
2015-07-17 17:24:28 +00:00
|
|
|
for (auto & j : names) {
|
|
|
|
Attr & a(*v.attrs->find(state.symbols.create(j)));
|
2023-06-29 23:29:11 +00:00
|
|
|
try {
|
|
|
|
out[j] = printValueAsJSON(state, strict, *a.value, a.pos, context, copyToStore);
|
|
|
|
} catch (Error & e) {
|
|
|
|
e.addTrace(state.positions[a.pos],
|
2024-03-08 07:10:05 +00:00
|
|
|
HintFmt("while evaluating attribute '%1%'", j));
|
2023-06-29 23:29:11 +00:00
|
|
|
throw;
|
|
|
|
}
|
2013-11-18 23:03:11 +00:00
|
|
|
}
|
|
|
|
} else
|
2022-11-16 15:49:49 +00:00
|
|
|
return printValueAsJSON(state, strict, *i->value, i->pos, context, copyToStore);
|
2013-11-18 23:03:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-12-12 01:09:10 +00:00
|
|
|
case nList: {
|
2022-11-16 15:49:49 +00:00
|
|
|
out = json::array();
|
2023-06-29 23:29:11 +00: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`.
(cherry picked from commit c6a89c1a1659b31694c0fbcd21d78a6dd521c732)
Change-Id: Iced91ba4e00ca9e801518071fb43798936cbd05a
2024-03-08 06:09:48 +00:00
|
|
|
e.addTrace(state.positions[pos],
|
2024-03-08 07:10:05 +00:00
|
|
|
HintFmt("while evaluating list element at index %1%", i));
|
2023-06-29 23:29:11 +00:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
2013-11-18 23:03:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-12-12 01:09:10 +00:00
|
|
|
case nExternal:
|
2022-11-16 15:49:49 +00:00
|
|
|
return v.external->printValueAsJSON(state, strict, context, copyToStore);
|
2014-11-30 18:16:19 +00:00
|
|
|
break;
|
|
|
|
|
2020-12-12 01:09:10 +00:00
|
|
|
case nFloat:
|
2022-11-16 15:49:49 +00:00
|
|
|
out = v.fpoint;
|
2016-01-04 23:40:40 +00:00
|
|
|
break;
|
|
|
|
|
2020-12-12 01:09:10 +00: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`.
(cherry picked from commit c6a89c1a1659b31694c0fbcd21d78a6dd521c732)
Change-Id: Iced91ba4e00ca9e801518071fb43798936cbd05a
2024-03-08 06:09:48 +00:00
|
|
|
state.error<TypeError>(
|
|
|
|
"cannot convert %1% to JSON",
|
|
|
|
showType(v)
|
|
|
|
)
|
|
|
|
.atPos(v.determinePos(pos))
|
|
|
|
.debugThrow();
|
2013-11-18 23:03:11 +00:00
|
|
|
}
|
2022-11-16 15:49:49 +00:00
|
|
|
return out;
|
2013-11-18 23:03:11 +00:00
|
|
|
}
|
|
|
|
|
2016-08-26 16:55:55 +00: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 01:31:10 +00:00
|
|
|
Value & v, const PosIdx pos, std::ostream & str, NixStringContext & context, bool copyToStore)
|
2016-08-26 16:55:55 +00:00
|
|
|
{
|
2022-11-16 15:49:49 +00:00
|
|
|
str << printValueAsJSON(state, strict, v, pos, context, copyToStore);
|
2016-08-26 16:55:55 +00:00
|
|
|
}
|
2013-11-18 23:03:11 +00:00
|
|
|
|
2022-11-16 15:49:49 +00: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 01:31:10 +00:00
|
|
|
NixStringContext & context, bool copyToStore) const
|
2014-11-30 18:16:19 +00: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`.
(cherry picked from commit c6a89c1a1659b31694c0fbcd21d78a6dd521c732)
Change-Id: Iced91ba4e00ca9e801518071fb43798936cbd05a
2024-03-08 06:09:48 +00:00
|
|
|
state.error<TypeError>("cannot convert %1% to JSON", showType())
|
|
|
|
.debugThrow();
|
2014-11-30 18:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-18 23:03:11 +00:00
|
|
|
}
|