nix_api_expr: merge nix_parse_expr and nix_expr_eval, remove Expr

This commit is contained in:
Yorick van Pelt 2023-07-27 15:57:48 +02:00 committed by José Luis Lafuente
parent 1777e4a5bb
commit aa85f7d917
No known key found for this signature in database
GPG key ID: 8A3455EBE455489A
4 changed files with 11 additions and 66 deletions

View file

@ -39,27 +39,15 @@ nix_err nix_libexpr_init(nix_c_context *context) {
NIXC_CATCH_ERRS NIXC_CATCH_ERRS
} }
Expr *nix_parse_expr_from_string(nix_c_context *context, State *state, nix_err nix_expr_eval_from_string(nix_c_context *context, State *state,
const char *expr, const char *path, const char *expr, const char *path,
GCRef *ref) {
if (context)
context->last_err_code = NIX_OK;
try {
Expr *result = state->state.parseExprFromString(
expr, state->state.rootPath(nix::CanonPath(path)));
if (ref)
ref->ptr = result;
return result;
}
NIXC_CATCH_ERRS_NULL
}
nix_err nix_expr_eval(nix_c_context *context, State *state, Expr *expr,
Value *value) { Value *value) {
if (context) if (context)
context->last_err_code = NIX_OK; context->last_err_code = NIX_OK;
try { try {
state->state.eval((nix::Expr *)expr, *(nix::Value *)value); nix::Expr *parsedExpr = state->state.parseExprFromString(
expr, state->state.rootPath(nix::CanonPath(path)));
state->state.eval(parsedExpr, *(nix::Value *)value);
} }
NIXC_CATCH_ERRS NIXC_CATCH_ERRS
} }

View file

@ -13,12 +13,6 @@ extern "C" {
// cffi start // cffi start
// Type definitions // Type definitions
/**
* @brief Represents a parsed nix Expression, can be evaluated into a Value.
*
* Owned by the GC.
*/
typedef void Expr; // nix::Expr
/** /**
* @brief Represents a nix evaluator state. * @brief Represents a nix evaluator state.
* *
@ -54,33 +48,18 @@ typedef struct GCRef GCRef; // void*
nix_err nix_libexpr_init(nix_c_context *context); nix_err nix_libexpr_init(nix_c_context *context);
/** /**
* @brief Parses a Nix expression from a string. * @brief Parses and evaluates a Nix expression from a string.
*
* The returned expression is owned by the garbage collector.
* Pass a gcref to keep a reference.
*
* @param[out] context Optional, stores error information
* @param[in] state Evaluator state.
* @param[in] expr The Nix expression to parse.
* @param[in] path The file path to associate with the expression.
* @param[out] ref Optional, will store a reference to the returned value.
* @return A parsed expression or NULL on failure.
*/
Expr *nix_parse_expr_from_string(nix_c_context *context, State *state,
const char *expr, const char *path,
GCRef *ref);
/**
* @brief Evaluates a parsed Nix expression.
* *
* @param[out] context Optional, stores error information * @param[out] context Optional, stores error information
* @param[in] state The state of the evaluation. * @param[in] state The state of the evaluation.
* @param[in] expr The Nix expression to evaluate. * @param[in] expr The Nix expression to parse.
* @param[in] path The file path to associate with the expression.
* @param[out] value The result of the evaluation. You should allocate this * @param[out] value The result of the evaluation. You should allocate this
* yourself. * yourself.
* @return NIX_OK if the evaluation was successful, an error code otherwise. * @return NIX_OK if the evaluation was successful, an error code otherwise.
*/ */
nix_err nix_expr_eval(nix_c_context *context, State *state, Expr *expr, nix_err nix_expr_eval_from_string(nix_c_context *context, State *state,
const char *expr, const char *path,
Value *value); Value *value);
/** /**

View file

@ -386,17 +386,6 @@ nix_err nix_copy_value(nix_c_context *context, Value *value, Value *source) {
NIXC_CATCH_ERRS NIXC_CATCH_ERRS
} }
nix_err nix_set_thunk(nix_c_context *context, State *s, Value *value,
Expr *expr) {
if (context)
context->last_err_code = NIX_OK;
try {
auto &v = check_value_not_null(value);
s->state.mkThunk_(v, (nix::Expr *)expr);
}
NIXC_CATCH_ERRS
}
typedef std::shared_ptr<nix::BindingsBuilder> BindingsBuilder_Inner; typedef std::shared_ptr<nix::BindingsBuilder> BindingsBuilder_Inner;
nix_err nix_make_attrs(nix_c_context *context, Value *value, nix_err nix_make_attrs(nix_c_context *context, Value *value,

View file

@ -32,7 +32,6 @@ typedef enum {
// forward declarations // forward declarations
typedef void Value; typedef void Value;
typedef void Expr;
typedef struct State State; typedef struct State State;
typedef struct GCRef GCRef; typedef struct GCRef GCRef;
// type defs // type defs
@ -307,16 +306,6 @@ nix_err nix_set_primop(nix_c_context *context, Value *value, PrimOp *op);
* @return error code, NIX_OK on success. * @return error code, NIX_OK on success.
*/ */
nix_err nix_copy_value(nix_c_context *context, Value *value, Value *source); nix_err nix_copy_value(nix_c_context *context, Value *value, Value *source);
/** @brief Make a thunk from an expr.
*
* Expr will be evaluated when the value is forced
* @param[out] context Optional, stores error information
* @param[out] value Nix value to modify
* @param[in] expr the expr to thunk
* @return error code, NIX_OK on success.
*/
nix_err nix_set_thunk(nix_c_context *context, State *s, Value *value,
Expr *expr);
/**@}*/ /**@}*/
/** @brief Create a bindings builder /** @brief Create a bindings builder