37
37
#include " machine-runtime-config.h"
38
38
#include " machine.h"
39
39
#include " semantic-version.h"
40
- #include " uarch-config.h"
41
40
#include " uarch-interpret.h"
42
41
43
42
namespace cartesi {
@@ -68,18 +67,24 @@ void ju_get_field(const nlohmann::json &j, const K &key, T &value, const std::st
68
67
69
68
// Allows use contains when the index is an integer and j contains an array
70
69
template <typename T>
71
- inline bool contains (const nlohmann::json &j, T i)
70
+ inline bool contains (const nlohmann::json &j, T i, const std::string &path )
72
71
requires(std::is_integral_v<T>)
73
72
{
73
+ if (!j.empty () && !j.is_array ()) {
74
+ throw std::invalid_argument (" \" " s + path + " \" not an array" );
75
+ }
74
76
if constexpr (std::is_signed_v<T>) {
75
- return j. is_array () && i >= 0 && i < static_cast <T>(j.size ());
77
+ return i >= 0 && i < static_cast <T>(j.size ());
76
78
} else {
77
- return j. is_array () && i < j.size ();
79
+ return i < j.size ();
78
80
}
79
81
}
80
82
81
83
// Overload for case where index is a string and j contains an object
82
- inline bool contains (const nlohmann::json &j, const std::string &s) {
84
+ inline bool contains (const nlohmann::json &j, const std::string &s, const std::string &path) {
85
+ if (!j.empty () && !j.is_object ()) {
86
+ throw std::invalid_argument (" \" " s + path + " \" not an object" );
87
+ }
83
88
return j.contains (s);
84
89
}
85
90
@@ -340,54 +345,55 @@ template <typename K>
340
345
void ju_get_opt_field (const nlohmann::json &j, const K &key, memory_range_config &value,
341
346
const std::string &path = " params/" );
342
347
343
- // / \brief Attempts to load a cmio_buffer_config object from a field in a JSON object
348
+ // / \brief Attempts to load a backing_store_config object from a field in a JSON object
344
349
// / \tparam K Key type (explicit extern declarations for uint64_t and std::string are provided)
345
350
// / \param j JSON object to load from
346
351
// / \param key Key to load value from
347
352
// / \param value Object to store value
348
353
// / \param path Path to j
349
354
template <typename K>
350
- void ju_get_opt_field (const nlohmann::json &j, const K &key, cmio_buffer_config &value,
355
+ void ju_get_opt_field (const nlohmann::json &j, const K &key, backing_store_config &value,
351
356
const std::string &path = " params/" );
352
357
353
- // / \brief Attempts to load a flash_drive_configs object from a field in a JSON object
358
+ // / \brief Attempts to load a backing_store_config_only object from a field in a JSON object
354
359
// / \tparam K Key type (explicit extern declarations for uint64_t and std::string are provided)
355
360
// / \param j JSON object to load from
356
361
// / \param key Key to load value from
357
362
// / \param value Object to store value
358
363
// / \param path Path to j
359
364
template <typename K>
360
- void ju_get_opt_field (const nlohmann::json &j, const K &key, flash_drive_configs &value,
365
+ void ju_get_opt_field (const nlohmann::json &j, const K &key, backing_store_config_only &value,
361
366
const std::string &path = " params/" );
362
367
363
- // / \brief Attempts to load a virtio_device_config object from a field in a JSON object
368
+ // / \brief Attempts to load a flash_drive_configs object from a field in a JSON object
364
369
// / \tparam K Key type (explicit extern declarations for uint64_t and std::string are provided)
365
370
// / \param j JSON object to load from
366
371
// / \param key Key to load value from
367
372
// / \param value Object to store value
368
373
// / \param path Path to j
369
374
template <typename K>
370
- void ju_get_opt_field (const nlohmann::json &j, const K &key, virtio_device_config &value,
375
+ void ju_get_opt_field (const nlohmann::json &j, const K &key, flash_drive_configs &value,
371
376
const std::string &path = " params/" );
372
377
373
- // / \brief Attempts to load an virtio_configs object from a field in a JSON object
378
+ // / \brief Attempts to load a virtio_device_config object from a field in a JSON object
374
379
// / \tparam K Key type (explicit extern declarations for uint64_t and std::string are provided)
375
380
// / \param j JSON object to load from
376
381
// / \param key Key to load value from
377
382
// / \param value Object to store value
378
383
// / \param path Path to j
379
384
template <typename K>
380
- void ju_get_opt_field (const nlohmann::json &j, const K &key, virtio_configs &value,
385
+ void ju_get_opt_field (const nlohmann::json &j, const K &key, virtio_device_config &value,
381
386
const std::string &path = " params/" );
382
387
383
- // / \brief Attempts to load a tlb_config object from a field in a JSON object
388
+ // / \brief Attempts to load an virtio_configs object from a field in a JSON object
384
389
// / \tparam K Key type (explicit extern declarations for uint64_t and std::string are provided)
385
390
// / \param j JSON object to load from
386
391
// / \param key Key to load value from
387
392
// / \param value Object to store value
388
393
// / \param path Path to j
389
394
template <typename K>
390
- void ju_get_opt_field (const nlohmann::json &j, const K &key, tlb_config &value, const std::string &path = " params/" );
395
+ void ju_get_opt_field (const nlohmann::json &j, const K &key, virtio_configs &value,
396
+ const std::string &path = " params/" );
391
397
392
398
// / \brief Attempts to load a clint_config object from a field in a JSON object
393
399
// / \tparam K Key type (explicit extern declarations for uint64_t and std::string are provided)
@@ -445,24 +451,24 @@ template <typename K>
445
451
void ju_get_opt_field (const nlohmann::json &j, const K &key, uarch_processor_config &value,
446
452
const std::string &path = " params/" );
447
453
448
- // / \brief Attempts to load an uarch_ram_config object from a field in a JSON object
454
+ // / \brief Attempts to load an uarch_config object from a field in a JSON object
449
455
// / \tparam K Key type (explicit extern declarations for uint64_t and std::string are provided)
450
456
// / \param j JSON object to load from
451
457
// / \param key Key to load value from
452
458
// / \param value Object to store value
453
459
// / \param path Path to j
454
460
template <typename K>
455
- void ju_get_opt_field (const nlohmann::json &j, const K &key, uarch_ram_config &value,
456
- const std::string &path = " params/" );
461
+ void ju_get_opt_field (const nlohmann::json &j, const K &key, uarch_config &value, const std::string &path = " params/" );
457
462
458
- // / \brief Attempts to load an uarch_config object from a field in a JSON object
463
+ // / \brief Attempts to load an hash_tree_config object from a field in a JSON object
459
464
// / \tparam K Key type (explicit extern declarations for uint64_t and std::string are provided)
460
465
// / \param j JSON object to load from
461
466
// / \param key Key to load value from
462
467
// / \param value Object to store value
463
468
// / \param path Path to j
464
469
template <typename K>
465
- void ju_get_opt_field (const nlohmann::json &j, const K &key, uarch_config &value, const std::string &path = " params/" );
470
+ void ju_get_opt_field (const nlohmann::json &j, const K &key, hash_tree_config &value,
471
+ const std::string &path = " params/" );
466
472
467
473
// / \brief Attempts to load a machine_config object from a field in a JSON object
468
474
// / \tparam K Key type (explicit extern declarations for uint64_t and std::string are provided)
@@ -512,7 +518,7 @@ void ju_get_opt_field(const nlohmann::json &j, const K &key, fork_result &value,
512
518
template <typename K, typename A>
513
519
void ju_get_opt_vector_like_field (const nlohmann::json &j, const K &key, A &value, const std::string &path) {
514
520
value.clear ();
515
- if (!contains (j, key)) {
521
+ if (!contains (j, key, path )) {
516
522
return ;
517
523
}
518
524
const auto &jk = j[key];
@@ -542,7 +548,7 @@ void ju_get_opt_vector_like_field(const nlohmann::json &j, const K &key, A &valu
542
548
// / \detail Throws error if field is missing
543
549
template <typename K, typename A>
544
550
void ju_get_vector_like_field (const nlohmann::json &j, const K &key, A &value, const std::string &path = " params/" ) {
545
- if (!contains (j, key)) {
551
+ if (!contains (j, key, path )) {
546
552
throw std::invalid_argument (" missing field \" " s + path + to_string (key) + " \" " s);
547
553
}
548
554
return ju_get_opt_vector_like_field (j, key, value, path);
@@ -557,7 +563,7 @@ void ju_get_vector_like_field(const nlohmann::json &j, const K &key, A &value, c
557
563
template <typename T, typename K>
558
564
void ju_get_opt_field (const nlohmann::json &j, const K &key, optional_param<T> &value,
559
565
const std::string &path = " params/" ) {
560
- if (contains (j, key)) {
566
+ if (contains (j, key, path )) {
561
567
value.emplace ();
562
568
ju_get_opt_field (j, key, value.value (), path);
563
569
}
@@ -572,7 +578,7 @@ void ju_get_opt_field(const nlohmann::json &j, const K &key, optional_param<T> &
572
578
// / \detail Throws error if field is missing
573
579
template <typename T, typename K>
574
580
void ju_get_field (const nlohmann::json &j, const K &key, T &value, const std::string &path) {
575
- if (!contains (j, key)) {
581
+ if (!contains (j, key, path )) {
576
582
throw std::invalid_argument (" missing field \" " s + path + to_string (key) + " \" " s);
577
583
}
578
584
ju_get_opt_field (j, key, value, path);
@@ -604,22 +610,22 @@ void to_json(nlohmann::json &j, const bracket_note &b);
604
610
void to_json (nlohmann::json &j, const std::vector<bracket_note> &bs);
605
611
void to_json (nlohmann::json &j, const std::vector<access> &as);
606
612
void to_json (nlohmann::json &j, const access_log &log);
613
+ void to_json (nlohmann::json &j, const backing_store_config &config);
614
+ void to_json (nlohmann::json &j, const backing_store_config_only &config);
607
615
void to_json (nlohmann::json &j, const memory_range_config &config);
608
- void to_json (nlohmann::json &j, const cmio_buffer_config &config);
609
616
void to_json (nlohmann::json &j, const processor_config &config);
610
617
void to_json (nlohmann::json &j, const flash_drive_configs &fs);
611
618
void to_json (nlohmann::json &j, const virtio_device_config &config);
612
619
void to_json (nlohmann::json &j, const virtio_configs &vs);
613
620
void to_json (nlohmann::json &j, const ram_config &config);
614
621
void to_json (nlohmann::json &j, const dtb_config &config);
615
- void to_json (nlohmann::json &j, const tlb_config &config);
616
622
void to_json (nlohmann::json &j, const clint_config &config);
617
623
void to_json (nlohmann::json &j, const plic_config &config);
618
624
void to_json (nlohmann::json &j, const htif_config &config);
619
625
void to_json (nlohmann::json &j, const cmio_config &config);
620
626
void to_json (nlohmann::json &j, const uarch_processor_config &config);
621
- void to_json (nlohmann::json &j, const uarch_ram_config &config);
622
627
void to_json (nlohmann::json &j, const uarch_config &config);
628
+ void to_json (nlohmann::json &j, const hash_tree_config &config);
623
629
void to_json (nlohmann::json &j, const machine_config &config);
624
630
void to_json (nlohmann::json &j, const concurrency_runtime_config &config);
625
631
void to_json (nlohmann::json &j, const htif_runtime_config &config);
@@ -731,9 +737,13 @@ extern template void ju_get_opt_field(const nlohmann::json &j, const uint64_t &k
731
737
const std::string &base = " params/" );
732
738
extern template void ju_get_opt_field (const nlohmann::json &j, const std::string &key, memory_range_config &value,
733
739
const std::string &base = " params/" );
734
- extern template void ju_get_opt_field (const nlohmann::json &j, const uint64_t &key, cmio_buffer_config &value,
740
+ extern template void ju_get_opt_field (const nlohmann::json &j, const uint64_t &key, backing_store_config &value,
741
+ const std::string &base = " params/" );
742
+ extern template void ju_get_opt_field (const nlohmann::json &j, const std::string &key, backing_store_config &value,
735
743
const std::string &base = " params/" );
736
- extern template void ju_get_opt_field (const nlohmann::json &j, const std::string &key, cmio_buffer_config &value,
744
+ extern template void ju_get_opt_field (const nlohmann::json &j, const uint64_t &key, backing_store_config_only &value,
745
+ const std::string &base = " params/" );
746
+ extern template void ju_get_opt_field (const nlohmann::json &j, const std::string &key, backing_store_config_only &value,
737
747
const std::string &base = " params/" );
738
748
extern template void ju_get_opt_field (const nlohmann::json &j, const uint64_t &key, flash_drive_configs &value,
739
749
const std::string &base = " params/" );
@@ -747,10 +757,6 @@ extern template void ju_get_opt_field(const nlohmann::json &j, const uint64_t &k
747
757
const std::string &base = " params/" );
748
758
extern template void ju_get_opt_field (const nlohmann::json &j, const std::string &key, virtio_configs &value,
749
759
const std::string &base = " params/" );
750
- extern template void ju_get_opt_field (const nlohmann::json &j, const uint64_t &key, tlb_config &value,
751
- const std::string &base = " params/" );
752
- extern template void ju_get_opt_field (const nlohmann::json &j, const std::string &key, tlb_config &value,
753
- const std::string &base = " params/" );
754
760
extern template void ju_get_opt_field (const nlohmann::json &j, const uint64_t &key, clint_config &value,
755
761
const std::string &base = " params/" );
756
762
extern template void ju_get_opt_field (const nlohmann::json &j, const std::string &key, clint_config &value,
@@ -775,14 +781,14 @@ extern template void ju_get_opt_field(const nlohmann::json &j, const uint64_t &k
775
781
const std::string &base = " params/" );
776
782
extern template void ju_get_opt_field (const nlohmann::json &j, const std::string &key, uarch_processor_config &value,
777
783
const std::string &base = " params/" );
778
- extern template void ju_get_opt_field (const nlohmann::json &j, const uint64_t &key, uarch_ram_config &value,
779
- const std::string &base = " params/" );
780
- extern template void ju_get_opt_field (const nlohmann::json &j, const std::string &key, uarch_ram_config &value,
781
- const std::string &base = " params/" );
782
784
extern template void ju_get_opt_field (const nlohmann::json &j, const uint64_t &key, uarch_config &value,
783
785
const std::string &base = " params/" );
784
786
extern template void ju_get_opt_field (const nlohmann::json &j, const std::string &key, uarch_config &value,
785
787
const std::string &base = " params/" );
788
+ extern template void ju_get_opt_field (const nlohmann::json &j, const uint64_t &key, hash_tree_config &value,
789
+ const std::string &base = " params/" );
790
+ extern template void ju_get_opt_field (const nlohmann::json &j, const std::string &key, hash_tree_config &value,
791
+ const std::string &base = " params/" );
786
792
extern template void ju_get_opt_field (const nlohmann::json &j, const uint64_t &key, machine_config &value,
787
793
const std::string &base = " params/" );
788
794
extern template void ju_get_opt_field (const nlohmann::json &j, const std::string &key, machine_config &value,
@@ -808,11 +814,11 @@ nlohmann::json to_json(const T &v) {
808
814
}
809
815
810
816
template <typename T>
811
- T from_json (const char *s) {
817
+ T from_json (const char *s, const char *path ) {
812
818
T value{};
813
819
if (s) {
814
- const nlohmann::json j = nlohmann::json{{" value " , nlohmann::json::parse (s)}};
815
- ju_get_field (j, " value " s , value, " " s);
820
+ const nlohmann::json j = nlohmann::json{{path , nlohmann::json::parse (s)}};
821
+ ju_get_field (j, std::string{path} , value, " " s);
816
822
}
817
823
return value;
818
824
}
0 commit comments