run-rs 0.3.5

Run a subset of Rust as an interpreted script
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! The register machine frame loop. It runs `Chunk` over `Value`, so a task
//! can run on any worker thread. `exec` owns the call frames and applies the
//! `Flow` each dispatched op answers with; the op bodies live in `vm_step`.

use std::collections::HashMap;
use std::mem::{replace, take};
use std::sync::Arc;

use anyhow::{Result, anyhow, bail};
use parking_lot::Mutex;
use tokio::runtime::Handle;

use super::bytecode::{Chunk, Member, path_call_chunk};
use super::native::Native;
use super::typeir::TypeIr;
use super::value::{ClosureData, StructShape, Upvalue, Value};
use super::vm_step::{Flow, StepCtx, step};
use super::vm_support::trace_error;

const MAX_CALL_DEPTH: usize = 100_000;

fn swap_option<T>(current: &mut Option<T>, next: Option<T>) -> Option<T> {
    match next {
        Some(value) => current.replace(value),
        None => current.take(),
    }
}

/// One backtrace entry: the function, its file, and the line of the op at
/// `ip`.
fn frame_line(chunk: &Chunk, ip: usize) -> (String, String, u32) {
    let line = chunk.lines.get(ip).copied().unwrap_or(0);
    (chunk.name.clone(), chunk.file.to_string(), line)
}

/// A module level const or static: converted once, evaluated on first read.
/// Each slot has its own lock so tasks on different threads can read globals.
pub enum GlobalSlot {
    Todo(Arc<Chunk>),
    Busy,
    Ready(Value),
}

/// A user enum's variants, precomputed at load so runtime dispatch never
/// touches the syn AST, which is not `Send`.
pub struct EnumDef {
    pub name: Arc<str>,
    /// Variant name and whether it is a unit variant.
    pub variants: Vec<(Arc<str>, bool)>,
}

/// The compiled program plus the runtime handle, shared across worker threads.
pub struct Vm {
    pub functions: Vec<Arc<Chunk>>,
    pub fn_index: HashMap<String, u32>,
    pub methods: HashMap<(String, String), Arc<Chunk>>,
    pub globals: Vec<Mutex<GlobalSlot>>,
    /// User struct layouts precomputed at load, for coercion and typed json.
    pub structs: super::json_bridge::Structs,
    /// Root module imports, used by the bridge dispatch to expand aliases.
    pub uses: HashMap<String, Vec<String>>,
    /// User enums with their variants, for dynamic variant construction.
    pub enums: Vec<EnumDef>,
    /// Canonical names of unit structs, for `struct Marker;` used as a value.
    pub unit_structs: Vec<Arc<str>>,
    /// Canonical names of every user struct, for tuple struct calls.
    pub struct_names: std::collections::HashSet<String>,
    pub rt: Handle,
}

impl Vm {
    /// Expand the first path segment through the `use` table.
    pub(super) fn canonical(&self, segs: &[String]) -> Vec<String> {
        if let Some(full) = self.uses.get(&segs[0]) {
            let mut out = full.clone();
            out.extend_from_slice(&segs[1..]);
            out
        } else {
            segs.to_vec()
        }
    }

    /// A unit variant of a user enum, matched by canonical or bare enum name.
    pub(super) fn unit_variant(&self, enum_name: Option<&str>, variant: &str) -> Option<Value> {
        for def in &self.enums {
            if let Some(want) = enum_name
                && want != &*def.name
                && want != super::resolver::bare(&def.name)
            {
                continue;
            }
            if def
                .variants
                .iter()
                .any(|(name, unit)| &**name == variant && *unit)
            {
                return Some(Value::Enum {
                    enum_name: def.name.clone(),
                    variant: Arc::from(variant),
                    data: Arc::from(Vec::new()),
                });
            }
        }
        None
    }

    /// A tuple variant of a user enum built from call arguments.
    pub(super) fn make_tuple_variant(
        &self,
        enum_name: Option<&str>,
        variant: &str,
        args: &[Value],
    ) -> Option<Value> {
        for def in &self.enums {
            if let Some(want) = enum_name
                && want != &*def.name
                && want != super::resolver::bare(&def.name)
            {
                continue;
            }
            if def.variants.iter().any(|(name, _)| &**name == variant) {
                return Some(Value::Enum {
                    enum_name: def.name.clone(),
                    variant: Arc::from(variant),
                    data: args.iter().cloned().collect(),
                });
            }
        }
        None
    }

    pub(super) fn make_tuple_struct(name: &str, args: Vec<Value>) -> Value {
        let fields = (0..args.len()).map(|i| i.to_string().into()).collect();
        Value::structure(StructShape::new(name, fields), args)
    }

    /// If a Ctrl-C arrived, run the script's registered handler closure.
    pub(super) fn run_pending_ctrlc(self: &Arc<Self>) -> Result<()> {
        if let Some(handler) = super::pending_ctrlc_handler()
            && let Value::Closure(clo) = handler
        {
            self.call_closure_data(&clo, &[])?;
        }
        Ok(())
    }
}

/// A binding of a generic parameter name to the lowered concrete type a
/// caller passed by turbofish, the parallel twin of `TypeEnv` in vm.rs.
pub(super) type TypeEnv = Arc<[(Arc<str>, TypeIr)]>;

pub(super) fn empty_type_env() -> TypeEnv {
    Arc::from(Vec::new())
}

struct Frame {
    chunk: Arc<Chunk>,
    closure: Option<Arc<ClosureData>>,
    ip: usize,
    base: usize,
    dst: u16,
    /// The caller's arg window, so the callee's final parameter values can be
    /// handed back on return for `&mut` argument writebacks.
    abase: u16,
    argc: u16,
    type_env: TypeEnv,
}

impl Vm {
    pub fn run_chunk(
        self: &Arc<Self>,
        chunk: &Arc<Chunk>,
        args: &[Value],
        upvalues: &[Upvalue],
    ) -> Result<Value> {
        // A path forwarder's arity is only a guess, so rebuild it for the
        // count actually passed. `u8::saturating_add` handed to `fold` takes
        // two arguments where the guess was one.
        let rebuilt;
        let chunk = if chunk.path_forwarder && args.len() != chunk.num_params {
            rebuilt = path_call_chunk(chunk.paths[0].0.clone(), args.len());
            &rebuilt
        } else {
            chunk
        };
        if args.len() != chunk.num_params {
            bail!(
                "`{}` expects {} args but got {}",
                chunk.name,
                chunk.num_params,
                args.len()
            );
        }
        let mut stack = vec![Value::Unit; chunk.num_regs.max(chunk.num_params)];
        for (i, a) in args.iter().enumerate() {
            stack[i] = a.clone();
        }
        self.exec(chunk, &mut stack, upvalues)
    }

    fn exec(
        self: &Arc<Self>,
        entry: &Arc<Chunk>,
        stack: &mut Vec<Value>,
        entry_upvalues: &[Upvalue],
    ) -> Result<Value> {
        let mut frames: Vec<Frame> = Vec::new();
        let mut local_cells: HashMap<usize, Arc<Mutex<Value>>> = HashMap::new();
        let mut cur = entry.clone();
        let mut cur_clo: Option<Arc<ClosureData>> = None;
        let mut cur_tenv: TypeEnv = empty_type_env();
        let mut base = 0usize;
        let mut ip = 0usize;

        // The dispatch runs inside one immediately called closure so an error
        // can be annotated with the script call chain still held in `frames`
        // and the failing op still addressed by `cur` and `ip`. The closure
        // runs exactly once, so the hot loop itself is unchanged.
        let result = (|| -> Result<Value> {
            loop {
                let flow = match cur.code.get(ip) {
                    None => Flow::Ret(Value::Unit),
                    Some(op) => step(
                        &mut StepCtx {
                            vm: self,
                            cur: &cur,
                            cur_clo: &cur_clo,
                            cur_tenv: &cur_tenv,
                            entry_upvalues,
                            local_cells: &mut local_cells,
                            stack: &mut *stack,
                            base,
                            ip,
                        },
                        op,
                    )?,
                };
                match flow {
                    Flow::Next => ip += 1,
                    Flow::Jump(to) => ip = to,
                    Flow::Ret(v) => {
                        let Some(f) = frames.pop() else { return Ok(v) };
                        let callee_base = base;
                        let callee_end = callee_base + cur.num_regs;
                        local_cells.retain(|slot, _| *slot < callee_base || *slot >= callee_end);
                        cur = f.chunk;
                        cur_clo = f.closure;
                        cur_tenv = f.type_env;
                        ip = f.ip;
                        // The callee's final parameter values go back into the
                        // caller's arg window, where a `&mut` argument
                        // writeback emitted by the compiler picks them up.
                        base = f.base;
                        for i in 0..f.argc as usize {
                            stack[base + f.abase as usize + i] = take(&mut stack[callee_base + i]);
                        }
                        stack[base + f.dst as usize] = v;
                    }
                    Flow::Call(req) => {
                        if frames.len() >= MAX_CALL_DEPTH {
                            bail!("stack overflow: call depth exceeded {MAX_CALL_DEPTH}");
                        }
                        let nbase = base + cur.num_regs;
                        let need = nbase + req.chunk.num_regs.max(req.chunk.num_params);
                        if stack.len() < need {
                            stack.resize(need, Value::Unit);
                        }
                        for i in 0..req.argc {
                            stack[nbase + i] = take(&mut stack[base + req.abase + i]);
                        }
                        for slot in &mut stack[nbase + req.argc..need] {
                            *slot = Value::Unit;
                        }
                        frames.push(Frame {
                            chunk: replace(&mut cur, req.chunk),
                            closure: swap_option(&mut cur_clo, req.closure),
                            ip: ip + 1,
                            base,
                            dst: req.dst,
                            abase: u16::try_from(req.abase).expect("register index fits u16"),
                            argc: u16::try_from(req.argc).expect("argument count fits u16"),
                            type_env: replace(&mut cur_tenv, req.type_env),
                        });
                        base = nbase;
                        ip = 0;
                    }
                }
            }
        })();
        result.map_err(|e| {
            let trace = std::iter::once(frame_line(&cur, ip)).chain(
                frames
                    .iter()
                    .rev()
                    .map(|f| frame_line(&f.chunk, f.ip.saturating_sub(1))),
            );
            trace_error(e, trace)
        })
    }

    /// Drive an awaited value to its result. A `JoinHandle` joins, a future is
    /// run to completion, anything else is already a value.
    pub(super) fn await_value(&self, v: Value) -> Result<Value> {
        let Value::Native(n) = v else { return Ok(v) };
        let taken = replace(&mut *n.lock(), Native::Taken);
        match taken {
            // Awaiting a JoinHandle yields `Result<T, JoinError>` in real Rust,
            // so it wraps. A script that passes `rust check` writes `.await?` or
            // `.await.unwrap()`, and both need the Ok layer to be here.
            Native::Task(h) => Ok(match self.rt.block_on(h) {
                Ok(v) => Value::ok(v),
                Err(e) => Value::err(Value::str(e.to_string())),
            }),
            // Awaiting a plain future yields its output directly, no wrapper.
            Native::Future(f) => Ok(self.rt.block_on(f)),
            Native::Taken => bail!("this value was already awaited"),
            // Everything else is a live resource, not an awaitable. Put it back
            // so the handle stays usable after the bad await is reported.
            other => {
                let name = other.type_name();
                *n.lock() = other;
                bail!("cannot await a {name}")
            }
        }
    }

    pub(super) fn user_function(&self, name: &str) -> Option<Arc<Chunk>> {
        self.fn_index
            .get(name)
            .map(|&i| self.functions[i as usize].clone())
    }

    pub(super) fn user_method(&self, ty: &str, name: &str) -> Option<Arc<Chunk>> {
        self.methods
            .get(&(ty.to_string(), name.to_string()))
            .cloned()
    }

    /// Value of a module const or static, evaluated on first read and cached.
    pub(super) fn global(self: &Arc<Self>, idx: usize) -> Result<Value> {
        {
            match &*self.globals[idx].lock() {
                GlobalSlot::Ready(v) => return Ok(v.clone()),
                GlobalSlot::Busy => {
                    bail!("constant initializers depend on each other in a cycle")
                }
                GlobalSlot::Todo(_) => {}
            }
        }
        let chunk = {
            let mut slot = self.globals[idx].lock();
            match replace(&mut *slot, GlobalSlot::Busy) {
                GlobalSlot::Todo(c) => c,
                other => {
                    *slot = other;
                    bail!("constant initializers depend on each other in a cycle");
                }
            }
        };
        let v = self.run_chunk(&chunk, &[], &[])?;
        *self.globals[idx].lock() = GlobalSlot::Ready(v.clone());
        Ok(v)
    }
}

/// A field access on a struct or tuple, the parallel twin of `get_field`.
impl Vm {
    pub(super) fn get_field(recv: &Value, member: &Member) -> Result<Value> {
        match (recv, member) {
            (Value::Struct(s), Member::Named(n)) => {
                s.get(n).ok_or_else(|| anyhow!("no field `{n}`"))
            }
            (Value::Tuple(t), Member::Indexed(i)) => t
                .lock()
                .get(*i)
                .cloned()
                .ok_or_else(|| anyhow!("no tuple index {i}")),
            (Value::Struct(s), Member::Indexed(i)) => s
                .values
                .lock()
                .get(*i)
                .cloned()
                .ok_or_else(|| anyhow!("no field {i}")),
            _ => bail!("cannot read a field of {}", recv.type_name()),
        }
    }

    pub(super) fn set_field(recv: &Value, member: &Member, v: Value) -> Result<()> {
        match (recv, member) {
            (Value::Struct(s), Member::Named(n)) => {
                if !s.set(n, v) {
                    bail!("no field `{n}`");
                }
            }
            (Value::Tuple(t), Member::Indexed(i)) => {
                let mut t = t.lock();
                if *i >= t.len() {
                    bail!("no tuple index {i}");
                }
                t[*i] = v;
            }
            _ => bail!("cannot set a field of {}", recv.type_name()),
        }
        Ok(())
    }
}