Skip to content

Commit bd3d2a5

Browse files
committed
c api: add EvalState argument to nix_init_*
We will need this argument in the future as we put things in managed memory. see NixOS#14584 for an example.
1 parent 68d2292 commit bd3d2a5

File tree

5 files changed

+76
-68
lines changed

5 files changed

+76
-68
lines changed

src/libexpr-c/nix_api_value.cc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ const char * nix_get_attr_name_byidx(nix_c_context * context, nix_value * value,
485485
NIXC_CATCH_ERRS_NULL
486486
}
487487

488-
nix_err nix_init_bool(nix_c_context * context, nix_value * value, bool b)
488+
nix_err nix_init_bool(nix_c_context * context, EvalState * state, nix_value * value, bool b)
489489
{
490490
if (context)
491491
context->last_err_code = NIX_OK;
@@ -497,7 +497,7 @@ nix_err nix_init_bool(nix_c_context * context, nix_value * value, bool b)
497497
}
498498

499499
// todo string context
500-
nix_err nix_init_string(nix_c_context * context, nix_value * value, const char * str)
500+
nix_err nix_init_string(nix_c_context * context, EvalState * state, nix_value * value, const char * str)
501501
{
502502
if (context)
503503
context->last_err_code = NIX_OK;
@@ -508,18 +508,18 @@ nix_err nix_init_string(nix_c_context * context, nix_value * value, const char *
508508
NIXC_CATCH_ERRS
509509
}
510510

511-
nix_err nix_init_path_string(nix_c_context * context, EvalState * s, nix_value * value, const char * str)
511+
nix_err nix_init_path_string(nix_c_context * context, EvalState * state, nix_value * value, const char * str)
512512
{
513513
if (context)
514514
context->last_err_code = NIX_OK;
515515
try {
516516
auto & v = check_value_out(value);
517-
v.mkPath(s->state.rootPath(nix::CanonPath(str)));
517+
v.mkPath(state->state.rootPath(nix::CanonPath(str)));
518518
}
519519
NIXC_CATCH_ERRS
520520
}
521521

522-
nix_err nix_init_float(nix_c_context * context, nix_value * value, double d)
522+
nix_err nix_init_float(nix_c_context * context, EvalState * state, nix_value * value, double d)
523523
{
524524
if (context)
525525
context->last_err_code = NIX_OK;
@@ -530,7 +530,7 @@ nix_err nix_init_float(nix_c_context * context, nix_value * value, double d)
530530
NIXC_CATCH_ERRS
531531
}
532532

533-
nix_err nix_init_int(nix_c_context * context, nix_value * value, int64_t i)
533+
nix_err nix_init_int(nix_c_context * context, EvalState * state, nix_value * value, int64_t i)
534534
{
535535
if (context)
536536
context->last_err_code = NIX_OK;
@@ -541,7 +541,7 @@ nix_err nix_init_int(nix_c_context * context, nix_value * value, int64_t i)
541541
NIXC_CATCH_ERRS
542542
}
543543

544-
nix_err nix_init_null(nix_c_context * context, nix_value * value)
544+
nix_err nix_init_null(nix_c_context * context, EvalState * state, nix_value * value)
545545
{
546546
if (context)
547547
context->last_err_code = NIX_OK;
@@ -552,7 +552,7 @@ nix_err nix_init_null(nix_c_context * context, nix_value * value)
552552
NIXC_CATCH_ERRS
553553
}
554554

555-
nix_err nix_init_apply(nix_c_context * context, nix_value * value, nix_value * fn, nix_value * arg)
555+
nix_err nix_init_apply(nix_c_context * context, EvalState * state, nix_value * value, nix_value * fn, nix_value * arg)
556556
{
557557
if (context)
558558
context->last_err_code = NIX_OK;
@@ -565,7 +565,7 @@ nix_err nix_init_apply(nix_c_context * context, nix_value * value, nix_value * f
565565
NIXC_CATCH_ERRS
566566
}
567567

568-
nix_err nix_init_external(nix_c_context * context, nix_value * value, ExternalValue * val)
568+
nix_err nix_init_external(nix_c_context * context, EvalState * state, nix_value * value, ExternalValue * val)
569569
{
570570
if (context)
571571
context->last_err_code = NIX_OK;
@@ -624,7 +624,7 @@ nix_err nix_make_list(nix_c_context * context, ListBuilder * list_builder, nix_v
624624
NIXC_CATCH_ERRS
625625
}
626626

627-
nix_err nix_init_primop(nix_c_context * context, nix_value * value, PrimOp * p)
627+
nix_err nix_init_primop(nix_c_context * context, EvalState * state, nix_value * value, PrimOp * p)
628628
{
629629
if (context)
630630
context->last_err_code = NIX_OK;

src/libexpr-c/nix_api_value.h

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -507,55 +507,61 @@ const char * nix_get_attr_name_byidx(nix_c_context * context, nix_value * value,
507507
/** @brief Set boolean value
508508
* @ingroup value_create
509509
* @param[out] context Optional, stores error information
510+
* @param[in] state nix evaluator state
510511
* @param[out] value Nix value to modify
511512
* @param[in] b the boolean value
512513
* @return error code, NIX_OK on success.
513514
*/
514-
nix_err nix_init_bool(nix_c_context * context, nix_value * value, bool b);
515+
nix_err nix_init_bool(nix_c_context * context, EvalState * state, nix_value * value, bool b);
515516

516517
/** @brief Set a string
517518
* @ingroup value_create
518519
* @param[out] context Optional, stores error information
520+
* @param[in] state nix evaluator state
519521
* @param[out] value Nix value to modify
520522
* @param[in] str the string, copied
521523
* @return error code, NIX_OK on success.
522524
*/
523-
nix_err nix_init_string(nix_c_context * context, nix_value * value, const char * str);
525+
nix_err nix_init_string(nix_c_context * context, EvalState * state, nix_value * value, const char * str);
524526

525527
/** @brief Set a path
526528
* @ingroup value_create
527529
* @param[out] context Optional, stores error information
530+
* @param[in] state nix evaluator state
528531
* @param[out] value Nix value to modify
529532
* @param[in] str the path string, copied
530533
* @return error code, NIX_OK on success.
531534
*/
532-
nix_err nix_init_path_string(nix_c_context * context, EvalState * s, nix_value * value, const char * str);
535+
nix_err nix_init_path_string(nix_c_context * context, EvalState * state, nix_value * value, const char * str);
533536

534537
/** @brief Set a float
535538
* @ingroup value_create
536539
* @param[out] context Optional, stores error information
540+
* @param[in] state nix evaluator state
537541
* @param[out] value Nix value to modify
538542
* @param[in] d the float, 64-bits
539543
* @return error code, NIX_OK on success.
540544
*/
541-
nix_err nix_init_float(nix_c_context * context, nix_value * value, double d);
545+
nix_err nix_init_float(nix_c_context * context, EvalState * state, nix_value * value, double d);
542546

543547
/** @brief Set an int
544548
* @ingroup value_create
545549
* @param[out] context Optional, stores error information
550+
* @param[in] state nix evaluator state
546551
* @param[out] value Nix value to modify
547552
* @param[in] i the int
548553
* @return error code, NIX_OK on success.
549554
*/
550555

551-
nix_err nix_init_int(nix_c_context * context, nix_value * value, int64_t i);
556+
nix_err nix_init_int(nix_c_context * context, EvalState * state, nix_value * value, int64_t i);
552557
/** @brief Set null
553558
* @ingroup value_create
554559
* @param[out] context Optional, stores error information
560+
* @param[in] state nix evaluator state
555561
* @param[out] value Nix value to modify
556562
* @return error code, NIX_OK on success.
557563
*/
558-
nix_err nix_init_null(nix_c_context * context, nix_value * value);
564+
nix_err nix_init_null(nix_c_context * context, EvalState * state, nix_value * value);
559565

560566
/** @brief Set the value to a thunk that will perform a function application when needed.
561567
* @ingroup value_create
@@ -565,23 +571,25 @@ nix_err nix_init_null(nix_c_context * context, nix_value * value);
565571
* In such cases, you may use nix_value_call() instead (but note the different argument order).
566572
*
567573
* @param[out] context Optional, stores error information
574+
* @param[in] state nix evaluator state
568575
* @param[out] value Nix value to modify
569576
* @param[in] fn function to call
570577
* @param[in] arg argument to pass
571578
* @return error code, NIX_OK on successful initialization.
572579
* @see nix_value_call() for a similar function that performs the call immediately and only stores the return value.
573580
* Note the different argument order.
574581
*/
575-
nix_err nix_init_apply(nix_c_context * context, nix_value * value, nix_value * fn, nix_value * arg);
582+
nix_err nix_init_apply(nix_c_context * context, EvalState * state, nix_value * value, nix_value * fn, nix_value * arg);
576583

577584
/** @brief Set an external value
578585
* @ingroup value_create
579586
* @param[out] context Optional, stores error information
587+
* @param[in] state nix evaluator state
580588
* @param[out] value Nix value to modify
581589
* @param[in] val the external value to set. Will be GC-referenced by the value.
582590
* @return error code, NIX_OK on success.
583591
*/
584-
nix_err nix_init_external(nix_c_context * context, nix_value * value, ExternalValue * val);
592+
nix_err nix_init_external(nix_c_context * context, EvalState * state, nix_value * value, ExternalValue * val);
585593

586594
/** @brief Create a list from a list builder
587595
* @ingroup value_create
@@ -645,7 +653,7 @@ nix_err nix_make_attrs(nix_c_context * context, nix_value * value, BindingsBuild
645653
* @see nix_alloc_primop
646654
* @return error code, NIX_OK on success.
647655
*/
648-
nix_err nix_init_primop(nix_c_context * context, nix_value * value, PrimOp * op);
656+
nix_err nix_init_primop(nix_c_context * context, EvalState * s, nix_value * value, PrimOp * op);
649657
/** @brief Copy from another value
650658
* @ingroup value_create
651659
* @param[out] context Optional, stores error information

src/libexpr-tests/nix_api_expr.cc

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ primop_square(void * user_data, nix_c_context * context, EvalState * state, nix_
241241
assert(state);
242242
assert(user_data == SAMPLE_USER_DATA);
243243
auto i = nix_get_int(context, args[0]);
244-
nix_init_int(context, ret, i * i);
244+
nix_init_int(context, state, ret, i * i);
245245
}
246246

247247
TEST_F(nix_api_expr_test, nix_expr_primop)
@@ -251,12 +251,12 @@ TEST_F(nix_api_expr_test, nix_expr_primop)
251251
assert_ctx_ok();
252252
nix_value * primopValue = nix_alloc_value(ctx, state);
253253
assert_ctx_ok();
254-
nix_init_primop(ctx, primopValue, primop);
254+
nix_init_primop(ctx, state, primopValue, primop);
255255
assert_ctx_ok();
256256

257257
nix_value * three = nix_alloc_value(ctx, state);
258258
assert_ctx_ok();
259-
nix_init_int(ctx, three, 3);
259+
nix_init_int(ctx, state, three, 3);
260260
assert_ctx_ok();
261261

262262
nix_value * result = nix_alloc_value(ctx, state);
@@ -290,7 +290,7 @@ primop_repeat(void * user_data, nix_c_context * context, EvalState * state, nix_
290290
for (int i = 0; i < n; ++i)
291291
result += s;
292292

293-
nix_init_string(context, ret, result.c_str());
293+
nix_init_string(context, state, ret, result.c_str());
294294
}
295295

296296
TEST_F(nix_api_expr_test, nix_expr_primop_arity_2_multiple_calls)
@@ -300,17 +300,17 @@ TEST_F(nix_api_expr_test, nix_expr_primop_arity_2_multiple_calls)
300300
assert_ctx_ok();
301301
nix_value * primopValue = nix_alloc_value(ctx, state);
302302
assert_ctx_ok();
303-
nix_init_primop(ctx, primopValue, primop);
303+
nix_init_primop(ctx, state, primopValue, primop);
304304
assert_ctx_ok();
305305

306306
nix_value * hello = nix_alloc_value(ctx, state);
307307
assert_ctx_ok();
308-
nix_init_string(ctx, hello, "hello");
308+
nix_init_string(ctx, state, hello, "hello");
309309
assert_ctx_ok();
310310

311311
nix_value * three = nix_alloc_value(ctx, state);
312312
assert_ctx_ok();
313-
nix_init_int(ctx, three, 3);
313+
nix_init_int(ctx, state, three, 3);
314314
assert_ctx_ok();
315315

316316
nix_value * partial = nix_alloc_value(ctx, state);
@@ -335,17 +335,17 @@ TEST_F(nix_api_expr_test, nix_expr_primop_arity_2_single_call)
335335
assert_ctx_ok();
336336
nix_value * primopValue = nix_alloc_value(ctx, state);
337337
assert_ctx_ok();
338-
nix_init_primop(ctx, primopValue, primop);
338+
nix_init_primop(ctx, state, primopValue, primop);
339339
assert_ctx_ok();
340340

341341
nix_value * hello = nix_alloc_value(ctx, state);
342342
assert_ctx_ok();
343-
nix_init_string(ctx, hello, "hello");
343+
nix_init_string(ctx, state, hello, "hello");
344344
assert_ctx_ok();
345345

346346
nix_value * three = nix_alloc_value(ctx, state);
347347
assert_ctx_ok();
348-
nix_init_int(ctx, three, 3);
348+
nix_init_int(ctx, state, three, 3);
349349
assert_ctx_ok();
350350

351351
nix_value * result = nix_alloc_value(ctx, state);
@@ -372,12 +372,12 @@ TEST_F(nix_api_expr_test, nix_expr_primop_bad_no_return)
372372
assert_ctx_ok();
373373
nix_value * primopValue = nix_alloc_value(ctx, state);
374374
assert_ctx_ok();
375-
nix_init_primop(ctx, primopValue, primop);
375+
nix_init_primop(ctx, state, primopValue, primop);
376376
assert_ctx_ok();
377377

378378
nix_value * three = nix_alloc_value(ctx, state);
379379
assert_ctx_ok();
380-
nix_init_int(ctx, three, 3);
380+
nix_init_int(ctx, state, three, 3);
381381
assert_ctx_ok();
382382

383383
nix_value * result = nix_alloc_value(ctx, state);
@@ -393,7 +393,7 @@ TEST_F(nix_api_expr_test, nix_expr_primop_bad_no_return)
393393
static void primop_bad_return_thunk(
394394
void * user_data, nix_c_context * context, EvalState * state, nix_value ** args, nix_value * ret)
395395
{
396-
nix_init_apply(context, ret, args[0], args[1]);
396+
nix_init_apply(context, state, ret, args[0], args[1]);
397397
}
398398

399399
TEST_F(nix_api_expr_test, nix_expr_primop_bad_return_thunk)
@@ -403,7 +403,7 @@ TEST_F(nix_api_expr_test, nix_expr_primop_bad_return_thunk)
403403
assert_ctx_ok();
404404
nix_value * primopValue = nix_alloc_value(ctx, state);
405405
assert_ctx_ok();
406-
nix_init_primop(ctx, primopValue, primop);
406+
nix_init_primop(ctx, state, primopValue, primop);
407407
assert_ctx_ok();
408408

409409
nix_value * toString = nix_alloc_value(ctx, state);
@@ -413,7 +413,7 @@ TEST_F(nix_api_expr_test, nix_expr_primop_bad_return_thunk)
413413

414414
nix_value * four = nix_alloc_value(ctx, state);
415415
assert_ctx_ok();
416-
nix_init_int(ctx, four, 4);
416+
nix_init_int(ctx, state, four, 4);
417417
assert_ctx_ok();
418418

419419
nix_value * result = nix_alloc_value(ctx, state);
@@ -452,12 +452,12 @@ TEST_F(nix_api_expr_test, nix_expr_primop_nix_err_key_conversion)
452452
assert_ctx_ok();
453453
nix_value * primopValue = nix_alloc_value(ctx, state);
454454
assert_ctx_ok();
455-
nix_init_primop(ctx, primopValue, primop);
455+
nix_init_primop(ctx, state, primopValue, primop);
456456
assert_ctx_ok();
457457

458458
nix_value * arg = nix_alloc_value(ctx, state);
459459
assert_ctx_ok();
460-
nix_init_int(ctx, arg, 42);
460+
nix_init_int(ctx, state, arg, 42);
461461
assert_ctx_ok();
462462

463463
nix_value * result = nix_alloc_value(ctx, state);
@@ -479,7 +479,7 @@ TEST_F(nix_api_expr_test, nix_expr_primop_nix_err_key_conversion)
479479
TEST_F(nix_api_expr_test, nix_value_call_multi_no_args)
480480
{
481481
nix_value * n = nix_alloc_value(ctx, state);
482-
nix_init_int(ctx, n, 3);
482+
nix_init_int(ctx, state, n, 3);
483483
assert_ctx_ok();
484484

485485
nix_value * r = nix_alloc_value(ctx, state);

src/libexpr-tests/nix_api_external.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ TEST_F(nix_api_expr_test, nix_expr_eval_external)
4444
{
4545
MyExternalValueDesc * external = new MyExternalValueDesc(42);
4646
ExternalValue * val = nix_create_external_value(ctx, external, external);
47-
nix_init_external(ctx, value, val);
47+
nix_init_external(ctx, state, value, val);
4848

4949
EvalState * stateResult = nix_state_create(nullptr, nullptr, store);
5050
nix_value * valueResult = nix_alloc_value(nullptr, stateResult);

0 commit comments

Comments
 (0)