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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
////////////////////////////////////////////////////////////////////////////////
// This file is part of "Ad Astra", an embeddable scripting programming //
// language platform. //
// //
// This work is proprietary software with source-available code. //
// //
// To copy, use, distribute, or contribute to this work, you must agree to //
// the terms of the General License Agreement: //
// //
// https://github.com/Eliah-Lakhin/ad-astra/blob/master/EULA.md //
// //
// The agreement grants a Basic Commercial License, allowing you to use //
// this work in non-commercial and limited commercial products with a total //
// gross revenue cap. To remove this commercial limit for one of your //
// products, you must acquire a Full Commercial License. //
// //
// If you contribute to the source code, documentation, or related materials, //
// you must grant me an exclusive license to these contributions. //
// Contributions are governed by the "Contributions" section of the General //
// License Agreement. //
// //
// Copying the work in parts is strictly forbidden, except as permitted //
// under the General License Agreement. //
// //
// If you do not or cannot agree to the terms of this Agreement, //
// do not use this work. //
// //
// This work is provided "as is", without any warranties, express or implied, //
// except where such disclaimers are legally invalid. //
// //
// Copyright (c) 2024 Ilya Lakhin (Илья Александрович Лахин). //
// All rights reserved. //
////////////////////////////////////////////////////////////////////////////////
use take;
use export;
use Shared;
use crate::;
/// Assembly code for the Ad Astra Virtual Machine, ready for execution.
///
/// You can create this object using the
/// [compile](crate::analysis::ModuleRead::compile) function, and then run it
/// using the [ScriptFn::run] function.
///
/// The ScriptFn object is cheap to [Clone]. In the case of cloning, each clone
/// shares the same assembly code memory, but the execution
/// [context](ScriptFn::set_context) is unique to each clone.
///
/// ## Virtual Machine Design Overview
///
/// The assembly code design is currently an implementation detail and is
/// subject to continuous improvements, optimizations, and changes in future
/// minor versions of Ad Astra. For this reason, the crate API does not provide
/// direct access to manually alter the assembly code. However, for debugging
/// purposes, you can print the internals to the terminal using the [Debug]
/// implementation of the ScriptFn object.
///
/// The ScriptFn consists of Ad Astra assembly commands for the main script
/// module function (the top-level source code of a module itself serves as the
/// body of a function with zero parameters), as well as the assembly commands
/// for other script functions from this module.
///
/// The runtime executes each assembly command of the script function
/// sequentially. Some commands can conditionally or unconditionally jump to
/// other commands in the list.
///
/// The commands interact with the stack of the current thread by pulling some
/// [Cells](Cell) from the stack and pushing new Cells onto the stack.
/// Therefore, the Virtual Machine is a stack-based machine.
///
/// ## Isolation
///
/// Each assembly command is evaluated in a virtual environment. If for any
/// reason a command fails, the Virtual Machine immediately stops execution and
/// returns a [RuntimeError] from the [ScriptFn::run] function.
///
/// You can manually interrupt script function execution using the hook
/// mechanism. By setting a hook function with the
/// [set_runtime_hook](crate::interpret::set_runtime_hook) function, you enforce
/// the Virtual Machine to report every command execution to the hook. The hook,
/// in turn, can return `false` to signal the Virtual Machine to stop execution
/// and return from [ScriptFn::run] with a [RuntimeError::Interrupted] error.
///
/// ```rust
/// use ad_astra::interpret::set_runtime_hook;
///
/// set_runtime_hook(|_origin| true);
/// ```
///
/// The hook function is configured per OS process thread. By default, the
/// thread from which you call the [ScriptFn::run] function does not have a
/// configured hook, meaning that you trust the script to finish its job without
/// interruptions. In this trusting mode, script functions are executed slightly
/// faster than with a configured hook, but the downside is that you cannot
/// revoke control flow back to Rust until the Virtual Machine finishes its job.
/// This could be an issue, for example, if the script code contains
/// unconditional infinite loops.
///
/// Additionally, the hook function receives an [Origin] object as an argument
/// that roughly points to the original source code statements and expressions
/// of the script module that are about to be evaluated. You can use this
/// feature to organize interactive script evaluation.
///
/// ## Source Maps
///
/// In addition to the assembly commands, the ScriptFn object also holds a
/// mapping between the assembly commands and the source ranges from which these
/// commands were compiled.
///
/// The Virtual Machine uses this metadata to provide proper and descriptive
/// [runtime errors](RuntimeError) if a script execution flow ends with a script
/// evaluation error.
///
/// ## Concurrent Evaluation
///
/// Each script function is executed on the current OS thread from which
/// it was [run](ScriptFn::run).
///
/// The Ad Astra base language does not provide a built-in mechanism for
/// asynchronous script evaluation or thread management. However, you can
/// organize a multi-threaded execution environment depending on your design
/// goals using the export system.
///
/// For example, you can export a function from Rust to a script that takes
/// another function as a parameter (e.g., [Fn0](crate::runtime::ops::Fn0)).
/// In the script, the author can call this exported Rust function, passing
/// a script-defined function as an argument. The Rust function can then execute
/// the provided script function in another thread.
///
/// ```rust
/// # use std::thread::spawn;
/// #
/// # use ad_astra::{export, runtime::ops::Fn0};
/// #
/// # #[export(include)]
/// # #[export(package)]
/// # #[derive(Default)]
/// # struct Package;
/// #
/// #[export]
/// pub fn foo(f: Fn0<()>) {
/// spawn(move || {
/// let f = f;
///
/// let _ = f();
/// });
/// }
/// ```
/// A script function.
pub type ScriptFnType = ScriptFn;