1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//! Host-facing configuration knobs: environment overrides, staged
//! application settings, the linked resource file, runtime state backends
//! (settings/file/clock/interaction), and the mock-clock controls (initial
//! date/time and `allow_system_time`).
//!
//! Everything here is staged on the interpreter and applied to the shared
//! `vb6runtime` state snapshot at the start of every run, so hosts configure
//! once and re-run freely.
use vb6runtime::state::environment as env_state;
use vb6runtime::state::resources as resources_state;
use vb6runtime::state::settings as settings_state;
use crate::interpreter::Interpreter;
impl Interpreter {
/// Assign an environment variable before the next run.
///
/// `Environ$`/`Environ` read these values during execution, on top of the
/// process environment. The assignment survives [`Interpreter::clear`] and
/// is re-applied at the start of every run, so it can be configured once
/// before calling [`Interpreter::run_source`] or [`Interpreter::run_module`].
pub fn set_environment(&mut self, name: &str, value: &str) {
self.environment.insert(name.to_string(), value.to_string());
}
/// Clear all environment variables installed with [`Interpreter::set_environment`].
pub fn clear_environment(&mut self) {
for name in self.environment.keys() {
env_state::remove_env(name);
}
self.environment.clear();
}
/// Assign an application setting before the next run.
///
/// `GetSetting` reads these values during execution, on top of any values
/// already present in the settings store (or on disk). The assignment
/// survives [`Interpreter::clear`] and is re-applied at the start of every
/// run, so it can be configured once before calling
/// [`Interpreter::run_source`] or [`Interpreter::run_module`]. A setting
/// staged later overrides an earlier one with the same
/// `(appname, section, key)`.
pub fn set_setting(&mut self, appname: &str, section: &str, key: &str, value: &str) {
self.settings.push((
appname.to_string(),
section.to_string(),
key.to_string(),
value.to_string(),
));
}
/// The value for `(appname, section, key)`, or `None` when unset.
///
/// Staged settings win over values already in the store; among staged
/// settings the most recently staged value wins.
pub fn get_setting(&self, appname: &str, section: &str, key: &str) -> Option<String> {
for (a, s, k, v) in self.settings.iter().rev() {
if a.eq_ignore_ascii_case(appname)
&& s.eq_ignore_ascii_case(section)
&& k.eq_ignore_ascii_case(key)
{
return Some(v.clone());
}
}
settings_state::get(appname, section, key)
}
/// Remove a single setting, both staged and from the store.
pub fn remove_setting(&mut self, appname: &str, section: &str, key: &str) {
self.settings.retain(|(a, s, k, _)| {
!(a.eq_ignore_ascii_case(appname)
&& s.eq_ignore_ascii_case(section)
&& k.eq_ignore_ascii_case(key))
});
let _ = settings_state::remove_key(appname, section, key);
}
/// Remove every setting staged with [`Interpreter::set_setting`], both
/// staged and from the store.
pub fn clear_settings(&mut self) {
for (appname, section, key, _) in &self.settings {
let _ = settings_state::remove_key(appname, section, key);
}
self.settings.clear();
}
/// Redirect the settings store to `root` for this interpreter.
///
/// Equivalent to [`vb6runtime::state::settings::set_store_root`], scoped
/// to the interpreter for convenience.
pub fn set_settings_store_root(&self, root: impl Into<std::path::PathBuf>) {
settings_state::set_store_root(root);
}
/// Set the active settings backend for this interpreter.
///
/// Equivalent to [`vb6runtime::state::settings::set_backend`], scoped
/// to the interpreter for convenience. After switching, all settings
/// are reloaded from the new backend.
///
/// # Examples
///
/// ```ignore
/// use vb6interpret::Interpreter;
/// use vb6runtime::state::settings::memory::MemoryBackend;
///
/// let mut interp = Interpreter::new();
/// interp.set_settings_backend(Box::new(MemoryBackend::new()));
/// ```
pub fn set_settings_backend(
&self,
backend: Box<dyn vb6runtime::state::settings::backend::SettingsBackend>,
) {
settings_state::set_backend(backend);
}
/// Reset the settings backend to the platform default.
///
/// Equivalent to [`vb6runtime::state::settings::reset_backend`], scoped
/// to the interpreter for convenience.
pub fn reset_settings_backend(&self) {
settings_state::reset_backend();
}
/// Link the `.res` file the program's `LoadRes*` functions read from.
///
/// A VB6 project links exactly one resource file at compile time (the
/// `ResFile32=` entry in the `.vbp`), and `LoadResData`, `LoadResPicture`,
/// and `LoadResString` all read from it without naming it. This stages the
/// equivalent binding: it survives [`Interpreter::clear`] and is applied at
/// the start of every run, so it can be configured once before calling
/// [`Interpreter::run_source`] or [`Interpreter::run_module`].
///
/// `path` is resolved through the active file backend at first use, so a
/// relative path is taken against the runtime file root and a missing file
/// is not reported until a `LoadRes*` call needs it — matching VB6, where a
/// broken resource link surfaces as a run-time error.
///
/// # Examples
///
/// ```ignore
/// use vb6interpret::Interpreter;
///
/// let mut interp = Interpreter::new();
/// interp.set_resource_file("MyApp.res");
/// interp.run_source("Debug.Print LoadResString(1001)")?;
/// ```
pub fn set_resource_file(&mut self, path: impl Into<String>) {
self.resource_file = Some(path.into());
}
/// The staged `.res` file path, or `None` when no resource file is linked.
pub fn resource_file(&self) -> Option<&str> {
self.resource_file.as_deref()
}
/// Unlink the resource file, as a project with no `ResFile32=` entry.
///
/// Subsequent `LoadRes*` calls raise error 326.
pub fn clear_resource_file(&mut self) {
self.resource_file = None;
resources_state::clear();
}
/// Set the active file backend for this interpreter.
///
/// Equivalent to [`vb6runtime::state::file::set_backend`], scoped
/// to the interpreter for convenience.
///
/// # Examples
///
/// ```ignore
/// use vb6interpret::Interpreter;
/// use vb6runtime::state::file::memory::MemoryBackend;
///
/// let mut interp = Interpreter::new();
/// interp.set_file_backend(Box::new(MemoryBackend::new()));
/// ```
pub fn set_file_backend(&self, backend: Box<dyn vb6runtime::state::file::FileBackend>) {
vb6runtime::state::file::set_backend(backend);
}
/// Reset the file backend to the platform default.
///
/// Equivalent to [`vb6runtime::state::file::reset_backend`], scoped
/// to the interpreter for convenience.
pub fn reset_file_backend(&self) {
vb6runtime::state::file::reset_backend();
}
/// Set the active clock backend for this interpreter.
///
/// Equivalent to [`vb6runtime::state::clock::set_backend`], scoped
/// to the interpreter for convenience.
///
/// # Examples
///
/// ```ignore
/// use vb6interpret::Interpreter;
/// use vb6runtime::state::clock::memory::MemoryBackend;
///
/// let mut interp = Interpreter::new();
/// interp.set_clock_backend(Box::new(MemoryBackend::new()));
/// ```
pub fn set_clock_backend(&self, backend: Box<dyn vb6runtime::state::clock::ClockBackend>) {
vb6runtime::state::clock::set_backend(backend);
}
/// Reset the clock backend to the platform default.
///
/// Equivalent to [`vb6runtime::state::clock::reset_backend`], scoped
/// to the interpreter for convenience.
pub fn reset_clock_backend(&self) {
vb6runtime::state::clock::reset_backend();
}
/// Set the active interaction backend for this interpreter.
///
/// Equivalent to [`vb6runtime::state::interaction::set_backend`],
/// scoped to the interpreter for convenience. Hosts use this to install
/// a memory backend whose `MsgBox` dialogs are answered from a scripted
/// response list instead of showing real windows.
///
/// # Examples
///
/// ```ignore
/// use vb6interpret::Interpreter;
/// use vb6runtime::state::interaction::{memory::MemoryBackend, MsgBoxButton};
///
/// let mut interp = Interpreter::new();
/// let backend = MemoryBackend::with_msgbox_responses([MsgBoxButton::Yes]);
/// interp.set_interaction_backend(Box::new(backend));
/// ```
pub fn set_interaction_backend(
&self,
backend: Box<dyn vb6runtime::state::interaction::InteractionBackend>,
) {
vb6runtime::state::interaction::set_backend(backend);
}
/// Reset the interaction backend to the platform default.
///
/// Equivalent to [`vb6runtime::state::interaction::reset_backend`],
/// scoped to the interpreter for convenience.
pub fn reset_interaction_backend(&self) {
vb6runtime::state::interaction::reset_backend();
}
/// Control whether `Date` and `Time` statements may modify the real
/// system clock.
///
/// - `true` (default): statements write to the real system clock.
/// - `false`: statements write to an internal mock clock that advances
/// in real time from the set point. The real clock is never touched.
///
/// When set to `false`, the current real date/time is captured as the
/// mock clock's starting point (unless overridden with
/// [`set_initial_date`] or [`set_initial_time`]).
pub fn set_allow_system_time(&mut self, allowed: bool) {
self.allow_system_time = allowed;
}
/// Whether `Date` and `Time` statements may modify the real system clock.
pub fn allow_system_time(&self) -> bool {
self.allow_system_time
}
/// Set an initial date for the mock clock at the start of a run.
///
/// Automatically disables real-clock writes (equivalent to calling
/// [`set_allow_system_time(false)`](Self::set_allow_system_time)).
/// When set, the mock clock starts at this date (preserving the current
/// time-of-day) instead of the real system date.
pub fn set_initial_date(&mut self, date: vb6runtime::civil::Date) {
self.allow_system_time = false;
self.initial_date = Some(date);
}
/// Clear any initial date override.
pub fn clear_initial_date(&mut self) {
self.initial_date = None;
}
/// Set an initial time for the mock clock at the start of a run.
///
/// Automatically disables real-clock writes (equivalent to calling
/// [`set_allow_system_time(false)`](Self::set_allow_system_time)).
/// When set, the mock clock starts at this time (preserving the current
/// date) instead of the real system time.
pub fn set_initial_time(&mut self, time: vb6runtime::civil::Time) {
self.allow_system_time = false;
self.initial_time = Some(time);
}
/// Set both the initial date and time for the mock clock at the start
/// of a run.
///
/// Automatically disables real-clock writes. This is a convenience
/// shorthand for calling [`set_initial_date`] and [`set_initial_time`]
/// together.
pub fn set_initial_date_time(
&mut self,
date: vb6runtime::civil::Date,
time: vb6runtime::civil::Time,
) {
self.allow_system_time = false;
self.initial_date = Some(date);
self.initial_time = Some(time);
}
/// Clear any initial time override.
pub fn clear_initial_time(&mut self) {
self.initial_time = None;
}
}