yomi_core/vm/config.rs
1//! VM settings.
2
3use super::InterruptHandle;
4
5/// The default of `VmConfig::seed` (fixed; the same seed gives the same random sequence).
6pub const DEFAULT_SEED: u64 = 0x5EED_0000_0000_0001;
7/// The default of `VmConfig::max_total_bytes` (64 MiB).
8pub const DEFAULT_MAX_TOTAL_BYTES: usize = 64 * 1024 * 1024;
9/// The default of `VmConfig::history_limit`.
10pub const DEFAULT_HISTORY_LIMIT: usize = 200;
11
12/// How a load treats variables whose saved type no longer matches the scripts (`spec/host/04-save.md`, 4.6).
13#[non_exhaustive]
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
15pub enum LoadPolicy {
16 /// Fails with a `LoadError` (the default).
17 #[default]
18 Strict,
19 /// Resets such variables to their declared initial values and goes back to the start of the label.
20 Lenient,
21}
22
23/// VM settings.
24#[non_exhaustive]
25#[derive(Debug, Clone)]
26pub struct VmConfig {
27 /// The number of instructions one `run` can execute. `None` means no limit.
28 pub max_steps_per_run: Option<u64>,
29 /// The maximum depth of calls in scripts. Label `call`s (call stack frames) and `fn` calls (including recursion) are
30 /// counted together. Trying to go beyond this is a runtime error (`CallDepthExceeded`).
31 pub max_call_depth: u32,
32 /// The label to start from. `module.label` (for a program with one module, just the label name also works).
33 pub start: String,
34 /// The maximum size of one value (bytes). For strings, the length; for `list`, `struct`, `enum` and `Option`, an estimate
35 /// of the total of the contents ([`crate::value::Value::approx_bytes`]). Exceeding it is the runtime error
36 /// `ValueTooLarge`.
37 pub max_value_bytes: usize,
38 /// The maximum total size (bytes; the estimate of [`crate::value::Value::approx_bytes`]) of values held in variables.
39 /// Counts module variables, host-declared variables and local variables (including call stack frames and `fn` call
40 /// frames). Exceeding it is the runtime error `MemoryLimitExceeded`. `None` means no limit. The default is 64 MiB.
41 pub max_total_bytes: Option<usize>,
42 /// Passed stop points (`module.label#id`). Read by `seen`. Also used when rebuilding the arguments of a stop point on load,
43 /// so it is part of the settings passed to `Vm::load`. To replace it later, use [`crate::vm::Vm::set_seen_points`].
44 pub seen_points: Vec<String>,
45 /// The maximum number of entries kept in the history of stop points ([`crate::vm::Vm::history`]). The oldest are dropped.
46 /// 0 means nothing is recorded. The default is 200.
47 pub history_limit: usize,
48 /// The seed of the random generator (`random`). The same seed, script and input give the same sequence. On load, the state
49 /// of the random generator in the save is used (the seed is not).
50 pub seed: u64,
51 /// Persistent data (the bytes returned by [`crate::vm::Vm::save_persistent`]). If present, the values of
52 /// `persistent var`s and the passed stop points are put in when creating (loading) the VM. Takes priority over
53 /// `seen_points` (passed stop points are replaced by this). When rebuilding the arguments of a stop point on load,
54 /// scripts that read `persistent var`s and `seen` get the same arguments as when saved. To pass it later, use
55 /// [`crate::vm::Vm::load_persistent`].
56 pub persistent: Option<Vec<u8>>,
57 /// How loads treat variables whose type no longer matches the scripts. Applies to `Vm::load`, `Vm::new` with
58 /// `persistent` and `Vm::load_persistent`. The default is [`LoadPolicy::Strict`].
59 pub load_policy: LoadPolicy,
60 /// The token that interrupts a `run` from another thread (`spec/host/01-execution.md`, 1.9a). If `None`, the VM makes its
61 /// own (get it with [`crate::vm::Vm::interrupt_handle`]). Pass one to share it between VMs.
62 pub interrupt: Option<InterruptHandle>,
63}
64
65impl Default for VmConfig {
66 fn default() -> Self {
67 Self {
68 max_steps_per_run: Some(50_000_000),
69 max_call_depth: 256,
70 start: "start".to_string(),
71 max_value_bytes: 16 * 1024 * 1024,
72 max_total_bytes: Some(DEFAULT_MAX_TOTAL_BYTES),
73 seen_points: Vec::new(),
74 history_limit: DEFAULT_HISTORY_LIMIT,
75 seed: DEFAULT_SEED,
76 persistent: None,
77 load_policy: LoadPolicy::Strict,
78 interrupt: None,
79 }
80 }
81}
82
83impl VmConfig {
84 /// Sets how loads treat variables whose type no longer matches the scripts.
85 pub fn load_policy(mut self, policy: LoadPolicy) -> Self {
86 self.load_policy = policy;
87 self
88 }
89
90 /// Sets the maximum size of one value (bytes).
91 pub fn max_value_bytes(mut self, bytes: usize) -> Self {
92 self.max_value_bytes = bytes;
93 self
94 }
95
96 /// Sets the maximum total size of values held in variables (bytes; `None` means no limit).
97 pub fn max_total_bytes(mut self, bytes: Option<usize>) -> Self {
98 self.max_total_bytes = bytes;
99 self
100 }
101
102 /// Sets the label to start from (`module.label`; with one module, just the label name also works).
103 pub fn start(mut self, label: &str) -> Self {
104 self.start = label.to_string();
105 self
106 }
107
108 /// Sets the passed stop points (`module.label#id`; as returned by [`crate::vm::Vm::seen_points`]).
109 pub fn seen_points<I, S>(mut self, points: I) -> Self
110 where
111 I: IntoIterator<Item = S>,
112 S: Into<String>,
113 {
114 self.seen_points = points.into_iter().map(Into::into).collect();
115 self
116 }
117
118 /// Sets the maximum number of entries in the history of stop points (0 means nothing is recorded).
119 pub fn history_limit(mut self, limit: usize) -> Self {
120 self.history_limit = limit;
121 self
122 }
123
124 /// Sets the seed of the random generator.
125 pub fn seed(mut self, seed: u64) -> Self {
126 self.seed = seed;
127 self
128 }
129
130 /// Sets the persistent data (the bytes of [`crate::vm::Vm::save_persistent`]).
131 pub fn persistent_data(mut self, bytes: Vec<u8>) -> Self {
132 self.persistent = Some(bytes);
133 self
134 }
135
136 /// Sets the maximum depth of calls.
137 pub fn max_call_depth(mut self, depth: u32) -> Self {
138 self.max_call_depth = depth;
139 self
140 }
141
142 /// Sets the token that interrupts a `run`.
143 pub fn interrupt(mut self, handle: InterruptHandle) -> Self {
144 self.interrupt = Some(handle);
145 self
146 }
147
148 /// Sets the maximum number of instructions in one `run`.
149 pub fn max_steps_per_run(mut self, steps: Option<u64>) -> Self {
150 self.max_steps_per_run = steps;
151 self
152 }
153}