run-rs 0.2.30

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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
mod builtins;
mod bytecode;
mod compile;
mod console;
pub mod coverage;
mod crates_bridge;
mod eval;
mod format;
mod higher_order;
mod http;
mod int_methods;
mod iterator;
mod json_bridge;
mod jwt_bridge;
mod methods;
mod native;
mod numeric;
mod ops;
mod pbridge;
mod pchunk;
mod pdf_bridge;
mod phttp;
mod pjson;
mod pnative;
mod pops;
mod pprocess;
mod pratatui;
mod pregex;
mod process;
mod pstd;
mod pvalue;
mod pvm;
mod ratatui_bridge;
mod ratatui_render;
mod regex_bridge;
mod resolver;
mod runner;
mod service_bridge;
mod shared;
mod std_bridge;
mod typeir;
mod value;
mod vm;
mod vm_support;
mod winreg_bridge;
mod wmi_bridge;
mod xmltree_bridge;

use std::cell::RefCell;
use std::collections::HashMap;
use std::mem::replace;
use std::rc::Rc;
use std::sync::OnceLock;
use std::sync::atomic::{AtomicBool, Ordering};

use anyhow::{Result, anyhow, bail};
use syn::Item;

use crate::loader::ModuleSrc;
use bytecode::Chunk;
use compile::{Compiler, Ctx};
use resolver::{ModuleSyms, Res, Resolver, StructDef};
pub use value::Value;
pub use vm_support::{ErrReturn, ScriptPanic};

/// Set by the real Ctrl-C handler, which must stay `Send`, and drained by the
/// interpreter between loop iterations so it can run the script's own handler.
static CTRLC_HIT: AtomicBool = AtomicBool::new(false);
static CTRLC_INSTALLED: OnceLock<bool> = OnceLock::new();

thread_local! {
    static CTRLC_HANDLER: RefCell<Option<Value>> = const { RefCell::new(None) };
}

pub(crate) fn set_ctrlc_handler(closure: Value) -> Result<()> {
    CTRLC_HANDLER.with(|h| *h.borrow_mut() = Some(closure));
    if CTRLC_INSTALLED.set(true).is_ok() {
        ctrlc::set_handler(|| CTRLC_HIT.store(true, Ordering::SeqCst))
            .map_err(|e| anyhow!("could not install ctrl-c handler: {e}"))?;
    }
    Ok(())
}

/// The arguments a script sees through `std::env::args()`. Index 0 is the
/// script path, matching a real compiled binary.
static SCRIPT_ARGS: OnceLock<Vec<String>> = OnceLock::new();

pub fn set_script_args(args: Vec<String>) {
    SCRIPT_ARGS
        .set(args)
        .expect("script args are set exactly once");
}

pub(crate) fn script_args() -> Vec<String> {
    SCRIPT_ARGS.get().cloned().unwrap_or_default()
}

/// Entry point for `#[tokio::main]` scripts. These run on the parallel engine
/// with a real multi thread tokio runtime, values backed by `Arc` so tasks move
/// across threads.
pub fn run_parallel(modules: &[ModuleSrc]) -> Result<()> {
    let interp = Interp::load(modules, true)?;
    interp.run_parallel()
}

/// A module level const or static: compiled once, evaluated on first read.
enum GlobalSlot {
    Todo(Rc<Chunk>),
    Busy,
    Ready(Value),
}

/// The whole program, compiled to bytecode and ready to run.
pub struct Interp {
    /// Every function of every module, indexed by id. Direct calls use the id.
    functions: Vec<Rc<Chunk>>,
    /// Canonical name to function id, for calls resolved at runtime.
    fn_index: HashMap<String, u32>,
    /// Inherent and trait methods, keyed by (canonical type name, method name).
    methods: HashMap<(String, String), Rc<Chunk>>,
    /// Module tree and item tables, shared by compile and runtime lookups.
    resolver: Resolver,
    /// Consts and statics, evaluated lazily so declaration order is free.
    globals: RefCell<Vec<GlobalSlot>>,
    /// Root module imports, used by the bridge dispatch to expand aliases.
    uses: HashMap<String, Vec<String>>,
    main_index: Option<u32>,
    /// Lazily built field layouts for user structs, shared across coercions.
    shapes: RefCell<HashMap<Rc<str>, Rc<eval::Shape>>>,
}

impl Interp {
    pub fn load(modules: &[ModuleSrc], async_mode: bool) -> Result<Self> {
        let mut resolver = build_module_tree(modules);
        let mut pending_fns: Vec<(usize, Rc<syn::ItemFn>)> = Vec::new();
        let mut pending_impls: Vec<(usize, Rc<syn::ItemImpl>)> = Vec::new();
        let mut pending_consts: Vec<(usize, Rc<syn::Expr>)> = Vec::new();

        for (m, src) in modules.iter().enumerate() {
            for item in &src.items {
                register_item(
                    &mut resolver,
                    m,
                    item,
                    &mut pending_fns,
                    &mut pending_impls,
                    &mut pending_consts,
                )?;
            }
        }
        resolver.reject_module_globs()?;

        // Impl targets resolve only after every module registered its types.
        let mut pending_methods: Vec<(String, String, usize, Rc<syn::ImplItemFn>)> = Vec::new();
        for (m, imp) in &pending_impls {
            let type_name = impl_target(&resolver, *m, &imp.self_ty)
                .ok_or_else(|| anyhow!("unsupported impl target"))?;
            for it in &imp.items {
                if let syn::ImplItem::Fn(f) = it {
                    pending_methods.push((
                        type_name.clone(),
                        f.sig.ident.to_string(),
                        *m,
                        Rc::new(f.clone()),
                    ));
                }
            }
        }

        // Stated return scalars of the script's own functions, one more place
        // a `Default` payload is written down. A name defined in more than one
        // module with differing returns answers for neither.
        let mut seen_returns: HashMap<String, Option<bytecode::ScalarTy>> = HashMap::default();
        for (_, f) in &pending_fns {
            let lowered = match &f.sig.output {
                syn::ReturnType::Type(_, ty) => bytecode::ScalarTy::lower(ty),
                syn::ReturnType::Default => None,
            };
            seen_returns
                .entry(f.sig.ident.to_string())
                .and_modify(|known| {
                    if *known != lowered {
                        *known = None;
                    }
                })
                .or_insert(lowered);
        }
        let fn_returns: HashMap<String, bytecode::ScalarTy> = seen_returns
            .into_iter()
            .filter_map(|(name, scalar)| scalar.map(|s| (name, s)))
            .collect();

        let mut functions = Vec::with_capacity(pending_fns.len());
        for (m, f) in &pending_fns {
            let ctx = Ctx {
                resolver: &resolver,
                module: *m,
                file: modules[*m].file.clone(),
                async_mode,
                impl_type: None,
                fn_returns: &fn_returns,
            };
            let mut c = Compiler::new(&ctx);
            functions.push(Rc::new(c.compile_fn(&f.sig, &f.block)?));
        }
        let mut methods = HashMap::default();
        for (ty, name, m, f) in &pending_methods {
            let ctx = Ctx {
                resolver: &resolver,
                module: *m,
                file: modules[*m].file.clone(),
                async_mode,
                impl_type: Some(ty),
                fn_returns: &fn_returns,
            };
            let mut c = Compiler::new(&ctx);
            methods.insert(
                (ty.clone(), name.clone()),
                Rc::new(c.compile_fn(&f.sig, &f.block)?),
            );
        }
        let mut globals = Vec::with_capacity(pending_consts.len());
        for (m, expr) in &pending_consts {
            let ctx = Ctx {
                resolver: &resolver,
                module: *m,
                file: modules[*m].file.clone(),
                async_mode,
                impl_type: None,
                fn_returns: &fn_returns,
            };
            let mut c = Compiler::new(&ctx);
            globals.push(GlobalSlot::Todo(Rc::new(c.compile_const(expr)?)));
        }

        let mut fn_index = HashMap::default();
        for syms in &resolver.modules {
            let prefix = syms.path.join("::");
            for (name, &idx) in &syms.fns {
                let key = if prefix.is_empty() {
                    name.clone()
                } else {
                    format!("{prefix}::{name}")
                };
                fn_index.insert(key, idx);
            }
        }
        let main_index = resolver.modules[0].fns.get("main").copied();
        let uses = resolver.modules[0].uses.clone();
        Ok(Interp {
            functions,
            fn_index,
            methods,
            resolver,
            globals: RefCell::new(globals),
            uses,
            main_index,
            shapes: RefCell::new(HashMap::default()),
        })
    }

    /// If a Ctrl-C arrived, run the script's registered handler closure.
    pub(super) fn run_pending_ctrlc(&self) -> Result<()> {
        // Cheap relaxed load first, this runs on every loop iteration.
        if !CTRLC_HIT.load(Ordering::Relaxed) {
            return Ok(());
        }
        if !CTRLC_HIT.swap(false, Ordering::SeqCst) {
            return Ok(());
        }
        let handler = CTRLC_HANDLER.with(|h| h.borrow().clone());
        if let Some(Value::Closure(clo)) = handler {
            self.call_closure(&clo, &[])?;
        }
        Ok(())
    }

    /// Run `fn main`. Its returned `Result::Err` is reported like anyhow does.
    /// Report methods the interpreter does not implement, without running
    /// anything. Used by `rust check`.
    pub fn coverage(&self, engine: coverage::Engine) -> Vec<coverage::Finding> {
        let user = self.methods.keys().map(|(_, m)| m.clone());
        coverage::report(&self.functions, user, engine)
    }

    pub fn run_main(&self) -> Result<()> {
        let idx = self
            .main_index
            .ok_or_else(|| anyhow!("no `main` function found"))?;
        let chunk = self.functions[idx as usize].clone();
        let ret = self.run_chunk(&chunk, &[], &[])?;
        if let Value::Enum {
            enum_name,
            variant,
            data,
        } = &ret
            && &**enum_name == "Result"
            && &**variant == "Err"
        {
            let msg = data.first().map(|v| v.display()).unwrap_or_default();
            return Err(anyhow::Error::new(vm_support::ErrReturn(msg)));
        }
        Ok(())
    }

    /// Run a `#[tokio::main]` program on the parallel engine. Compiles once to
    /// the fast bytecode, converts it to `Arc` based `PChunk`, then runs `main`
    /// as a blocking task on a multi thread tokio runtime so awaited futures can
    /// be driven with `block_on` from a worker.
    fn run_parallel(&self) -> Result<()> {
        use std::sync::Arc;
        let rt = tokio::runtime::Builder::new_multi_thread()
            .enable_all()
            .build()
            .map_err(|e| anyhow!("cannot start tokio runtime: {e}"))?;
        let functions: Vec<Arc<pchunk::PChunk>> =
            self.functions.iter().map(|c| pchunk::convert(c)).collect();
        let methods = self
            .methods
            .iter()
            .map(|(k, c)| (k.clone(), pchunk::convert(c)))
            .collect();
        let globals: Vec<parking_lot::Mutex<pvm::PGlobalSlot>> = self
            .globals
            .borrow()
            .iter()
            .map(|slot| {
                let g = match slot {
                    GlobalSlot::Todo(c) => pvm::PGlobalSlot::Todo(pchunk::convert(c)),
                    _ => pvm::PGlobalSlot::Busy,
                };
                parking_lot::Mutex::new(g)
            })
            .collect();
        let pinterp = Arc::new(pvm::PInterp {
            functions,
            fn_index: self.fn_index.clone(),
            methods,
            globals,
            structs: self.build_pstructs(),
            rt: rt.handle().clone(),
        });
        let idx = self
            .main_index
            .ok_or_else(|| anyhow!("no `main` function found"))? as usize;
        let main_chunk = pinterp.functions[idx].clone();
        let runner = pinterp.clone();
        let joined = rt.block_on(async move {
            tokio::task::spawn_blocking(move || runner.run_chunk(&main_chunk, &[], &[])).await
        });
        let ret = joined.map_err(|e| anyhow!("main task panicked: {e}"))??;
        if let pvalue::PValue::Enum {
            enum_name,
            variant,
            data,
        } = &ret
            && &**enum_name == "Result"
            && &**variant == "Err"
        {
            let msg = data.first().map(|v| v.display()).unwrap_or_default();
            return Err(anyhow::Error::new(vm_support::ErrReturn(msg)));
        }
        Ok(())
    }

    // -- lookups used by the bridge dispatch and the VM ---------------------

    pub(super) fn user_function(&self, name: &str) -> Option<Rc<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<Rc<Chunk>> {
        self.methods
            .get(&(ty.to_string(), name.to_string()))
            .cloned()
    }

    fn structs(&self) -> &HashMap<Rc<str>, StructDef> {
        &self.resolver.structs
    }

    fn enums(&self) -> &HashMap<Rc<str>, Rc<syn::ItemEnum>> {
        &self.resolver.enums
    }

    fn resolver(&self) -> &Resolver {
        &self.resolver
    }

    /// Value of a const or static, evaluated on first use so cross module
    /// declaration order never matters.
    fn global(&self, idx: usize) -> Result<Value> {
        {
            let globals = self.globals.borrow();
            match &globals[idx] {
                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 globals = self.globals.borrow_mut();
            match replace(&mut globals[idx], GlobalSlot::Busy) {
                GlobalSlot::Todo(c) => c,
                other => {
                    globals[idx] = other;
                    bail!("constant initializers depend on each other in a cycle");
                }
            }
        };
        let v = self.run_chunk(&chunk, &[], &[])?;
        self.globals.borrow_mut()[idx] = GlobalSlot::Ready(v.clone());
        Ok(v)
    }
}

/// Build the empty module table with parent and child links.
fn build_module_tree(modules: &[ModuleSrc]) -> Resolver {
    let index: HashMap<String, usize> = modules
        .iter()
        .enumerate()
        .map(|(i, m)| (m.path.join("::"), i))
        .collect();
    let mut syms: Vec<ModuleSyms> = modules
        .iter()
        .map(|m| ModuleSyms {
            path: m.path.clone(),
            ..ModuleSyms::default()
        })
        .collect();
    for (i, m) in modules.iter().enumerate() {
        if let Some((name, parent_path)) = m.path.split_last() {
            let parent = index[&parent_path.join("::")];
            syms[i].parent = Some(parent);
            syms[parent].children.insert(name.clone(), i);
        }
    }
    Resolver {
        modules: syms,
        structs: HashMap::default(),
        enums: HashMap::default(),
    }
}

fn register_item(
    resolver: &mut Resolver,
    m: usize,
    item: &Item,
    pending_fns: &mut Vec<(usize, Rc<syn::ItemFn>)>,
    pending_impls: &mut Vec<(usize, Rc<syn::ItemImpl>)>,
    pending_consts: &mut Vec<(usize, Rc<syn::Expr>)>,
) -> Result<()> {
    match item {
        Item::Fn(f) => {
            let name = f.sig.ident.to_string();
            resolver.modules[m]
                .fns
                .insert(name, pending_fns.len() as u32);
            pending_fns.push((m, Rc::new(f.clone())));
        }
        Item::Struct(s) => {
            let name = s.ident.to_string();
            let canon: Rc<str> = resolver.canon(m, &name).into();
            resolver.modules[m].structs.insert(name, canon.clone());
            resolver.structs.insert(
                canon,
                StructDef {
                    ast: Rc::new(s.clone()),
                    module: m,
                },
            );
        }
        Item::Enum(e) => {
            let name = e.ident.to_string();
            let canon: Rc<str> = resolver.canon(m, &name).into();
            resolver.modules[m].enums.insert(name, canon.clone());
            resolver.enums.insert(canon, Rc::new(e.clone()));
        }
        Item::Impl(imp) => pending_impls.push((m, Rc::new(imp.clone()))),
        Item::Use(u) => {
            let syms = &mut resolver.modules[m];
            let mut prefix = Vec::new();
            collect_use_tree(&u.tree, &mut prefix, &mut syms.uses, &mut syms.globs);
        }
        Item::Const(c) => {
            resolver.modules[m]
                .consts
                .insert(c.ident.to_string(), pending_consts.len() as u32);
            pending_consts.push((m, Rc::new((*c.expr).clone())));
        }
        Item::Static(s) => {
            if matches!(s.mutability, syn::StaticMutability::Mut(_)) {
                bail!("unsupported feature: `static mut`");
            }
            resolver.modules[m]
                .consts
                .insert(s.ident.to_string(), pending_consts.len() as u32);
            pending_consts.push((m, Rc::new((*s.expr).clone())));
        }
        Item::Type(t) => {
            resolver.modules[m]
                .aliases
                .insert(t.ident.to_string(), Rc::new((*t.ty).clone()));
        }
        Item::Trait(_) => {}
        Item::Mod(_) => bail!("module declarations must be expanded by the loader"),
        other => bail!("unsupported item: {}", quote_kind(other)),
    }
    Ok(())
}

/// Canonical name of the type an `impl` block targets.
fn impl_target(resolver: &Resolver, m: usize, ty: &syn::Type) -> Option<String> {
    let syn::Type::Path(p) = ty else { return None };
    let segs: Vec<String> = p
        .path
        .segments
        .iter()
        .map(|s| s.ident.to_string())
        .collect();
    match resolver.resolve(m, &segs) {
        Ok(Res::Struct(c) | Res::Enum(c)) => Some(c.to_string()),
        // An impl on something else, a bridge type name for example, keeps
        // the old bare name behavior.
        _ => segs.last().cloned(),
    }
}

fn collect_use_tree(
    tree: &syn::UseTree,
    prefix: &mut Vec<String>,
    out: &mut HashMap<String, Vec<String>>,
    globs: &mut Vec<Vec<String>>,
) {
    match tree {
        syn::UseTree::Path(p) => {
            prefix.push(p.ident.to_string());
            collect_use_tree(&p.tree, prefix, out, globs);
            prefix.pop();
        }
        syn::UseTree::Name(n) => {
            let name = n.ident.to_string();
            if name == "self" {
                // `use a::b::{self}` imports the module under its own name.
                if let Some(last) = prefix.last() {
                    out.insert(last.clone(), prefix.clone());
                }
                return;
            }
            let mut full = prefix.clone();
            full.push(name.clone());
            out.insert(name, full);
        }
        syn::UseTree::Rename(r) => {
            let mut full = prefix.clone();
            full.push(r.ident.to_string());
            out.insert(r.rename.to_string(), full);
        }
        syn::UseTree::Group(g) => {
            for item in &g.items {
                collect_use_tree(item, prefix, out, globs);
            }
        }
        syn::UseTree::Glob(_) => globs.push(prefix.clone()),
    }
}

fn quote_kind(item: &Item) -> &'static str {
    match item {
        Item::Fn(_) => "fn",
        Item::Struct(_) => "struct",
        Item::Enum(_) => "enum",
        Item::Impl(_) => "impl",
        Item::Trait(_) => "trait",
        Item::Macro(_) => "macro",
        Item::Union(_) => "union",
        _ => "item",
    }
}