run-rs 0.6.4

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
//! Associated functions like `String::from`, `Vec::new`, `File::open`,
//! `Duration::from_secs`.

use num_traits::AsPrimitive;
use std::sync::Arc;

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

use super::cell;
use super::int_methods::{from_bytes, from_bytes_order};
use super::jwt_bridge::jwt_assoc;
use super::native::Native;
use super::numeric::IntWidth;
use super::std_bridge::{
    arg_int, arg_str, as_i64, bytes_to_string, make_duration, make_path, open_file, path_like,
};
use super::value::{CellKind, Value};

/// Associated functions like `String::new`, `Vec::new`, `HashMap::new`.
pub(super) fn assoc_fn(ty: &str, func: &str, args: &[Value]) -> Result<Option<Value>> {
    if matches!(ty, "Header" | "EncodingKey") {
        return jwt_assoc(ty, func, args);
    }
    if let Some(v) = super::ratatui::ratatui_assoc(ty, func, args) {
        return Ok(Some(v));
    }
    // The groups use disjoint type and name pairs, so the first helper that
    // recognizes the pair answers.
    if let Some(v) = conversion_assoc(ty, func, args)? {
        return Ok(Some(v));
    }
    if let Some(v) = container_assoc(ty, func, args)? {
        return Ok(Some(v));
    }
    if let Some(v) = fs_process_assoc(ty, func, args)? {
        return Ok(Some(v));
    }
    misc_assoc(ty, func, args)
}

/// String, char, and numeric constructors and conversions.
fn conversion_assoc(ty: &str, func: &str, args: &[Value]) -> Result<Option<Value>> {
    Ok(Some(match (ty, func) {
        ("String", "new" | "with_capacity") => Value::str(""),
        ("String", "from") => Value::str(args.first().map(Value::display).unwrap_or_default()),
        ("String", "from_utf8_lossy") => Value::str(bytes_to_string(args.first())),
        // `char::from` only converts a u8 in real Rust, so the byte range is
        // enforced even though every integer is an i64 here.
        ("char", "from") => match args.first() {
            Some(Value::Char(c)) => Value::Char(*c),
            Some(Value::Int(n)) => match u8::try_from(*n) {
                Ok(b) => Value::Char(char::from(b)),
                Err(_) => bail!("`char::from` needs a u8"),
            },
            _ => bail!("`char::from` needs a u8"),
        },
        ("char", "from_u32") => match args.first().and_then(as_i64) {
            Some(n) => match u32::try_from(n).ok().and_then(char::from_u32) {
                Some(c) => Value::some(Value::Char(c)),
                None => Value::none(),
            },
            _ => Value::none(),
        },
        ("char", "from_digit") => {
            let n = args.first().and_then(as_i64).unwrap_or(-1);
            let radix = args.get(1).and_then(as_i64).unwrap_or(10);
            match (u32::try_from(n), u32::try_from(radix)) {
                (Ok(n), Ok(radix)) => match char::from_digit(n, radix) {
                    Some(c) => Value::some(Value::Char(c)),
                    None => Value::none(),
                },
                _ => Value::none(),
            }
        }
        // Every 64-bit-and-under type parses the same way here, values are
        // untyped ints. The 128-bit types parse in their real width below.
        (
            "i8" | "i16" | "i32" | "i64" | "isize" | "u8" | "u16" | "u32" | "u64" | "usize",
            "from_str_radix",
        ) => {
            let text = args.first().map(Value::display).unwrap_or_default();
            let radix = radix_arg(args);
            match i64::from_str_radix(text.trim(), radix) {
                Ok(n) => Value::ok(Value::Int(n)),
                Err(e) => Value::err(Value::str(e.to_string())),
            }
        }
        ("i128", "from_str_radix") => {
            let text = args.first().map(Value::display).unwrap_or_default();
            match i128::from_str_radix(text.trim(), radix_arg(args)) {
                Ok(n) => Value::ok(Value::Big(n, IntWidth::I128)),
                Err(e) => Value::err(Value::str(e.to_string())),
            }
        }
        ("u128", "from_str_radix") => {
            let text = args.first().map(Value::display).unwrap_or_default();
            match u128::from_str_radix(text.trim(), radix_arg(args)) {
                Ok(n) => Value::ok(Value::Big(n.cast_signed(), IntWidth::U128)),
                Err(e) => Value::err(Value::str(e.to_string())),
            }
        }
        // Numeric `T::from(x)`. Every integer is an i64 here, so a widening
        // conversion just carries the value. `from` on a bool gives 0 or 1,
        // the same as `usize::from(cond)` and the like.
        ("u128" | "i128", "from") => {
            let width = IntWidth::parse(ty).expect("128-bit width parses");
            Value::int_of_width(i128::from(int_from_arg(ty, args.first())?), width)
        }
        (
            "i8" | "i16" | "i32" | "i64" | "isize" | "u8" | "u16" | "u32" | "u64" | "usize",
            "from",
        ) => Value::Int(int_from_arg(ty, args.first())?),
        ("f32" | "f64", "from") => match args.first() {
            Some(Value::Float(f)) => Value::Float(*f),
            Some(Value::Int(n)) => Value::Float(AsPrimitive::<f64>::as_(*n)),
            Some(Value::Bool(b)) => Value::Float(if *b { 1.0 } else { 0.0 }),
            _ => bail!("`{ty}::from` needs a number"),
        },
        // Fallible `T::try_from(x)`. The value fits when it lands inside the
        // target range, so a narrowing conversion reports overflow with the
        // same message as the real `TryFromIntError`.
        (
            "i8" | "i16" | "i32" | "i64" | "i128" | "isize" | "u8" | "u16" | "u32" | "u64" | "u128"
            | "usize",
            "try_from",
        ) => {
            let n = int_from_arg(ty, args.first())?;
            if int_fits(ty, n) {
                Value::ok(Value::Int(n))
            } else {
                Value::err(Value::str(
                    "out of range integral type conversion attempted",
                ))
            }
        }
        // `T::from_le_bytes` and its be and ne siblings. The result carries
        // the named width, so a `u32` read stays a u32 and an `i32` read of
        // the same four bytes is negative where the top bit is set.
        (
            "i8" | "i16" | "i32" | "i64" | "i128" | "isize" | "u8" | "u16" | "u32" | "u64" | "u128"
            | "usize",
            "from_le_bytes" | "from_be_bytes" | "from_ne_bytes",
        ) => int_from_bytes(ty, func, args)?,
        ("String", "from_utf8") => Value::ok(Value::str(bytes_to_string(args.first()))),
        _ => return Ok(None),
    }))
}

/// Containers, wrappers, paths, regex, and the option and result names.
fn container_assoc(ty: &str, func: &str, args: &[Value]) -> Result<Option<Value>> {
    Ok(Some(match (ty, func) {
        // The shape carries every field a later builder call can set, since a
        // shape cannot grow after the instance exists.
        ("Command", "new") => command_new(args.first().cloned().unwrap_or_else(|| Value::str(""))),
        ("Vec", "new" | "with_capacity") => Value::vec(vec![]),
        ("Vec", "from") => match args.first() {
            Some(Value::Vec(v)) => Value::vec(v.lock().clone()),
            Some(other) => Value::vec(vec![other.clone()]),
            None => Value::vec(vec![]),
        },
        ("HashMap" | "BTreeMap", "new") | ("HashMap", "with_capacity") => Value::map(),
        ("HashSet" | "BTreeSet", "new") | ("HashSet", "with_capacity") => Value::set(),
        // `Rc::clone(&x)` is `x.clone()` spelled as the docs recommend,
        // and a cell's clone shares its slot, so handing the value through
        // is exactly right for both.
        ("Box", "new") | ("Rc" | "Arc", "clone") => args.first().cloned().unwrap_or(Value::Unit),
        // Real shared cells: cloning shares the slot and writes through one
        // handle show through every handle. `Box` above stays transparent,
        // ownership is what the value model already gives every value.
        ("Rc", "new") => {
            cell::make_cell(CellKind::Rc, args.first().cloned().unwrap_or(Value::Unit))
        }
        ("Arc", "new") => {
            cell::make_cell(CellKind::Arc, args.first().cloned().unwrap_or(Value::Unit))
        }
        ("RefCell", "new") => cell::make_cell(
            CellKind::RefCell,
            args.first().cloned().unwrap_or(Value::Unit),
        ),
        ("Cell", "new") => {
            cell::make_cell(CellKind::Cell, args.first().cloned().unwrap_or(Value::Unit))
        }
        ("Mutex", "new") => cell::make_cell(
            CellKind::Mutex,
            args.first().cloned().unwrap_or(Value::Unit),
        ),
        ("Rc" | "Arc", "strong_count") => {
            let Some(Value::Cell(_, slot)) = args.first() else {
                bail!("strong_count needs an Rc or Arc argument");
            };
            // Two in-flight copies exist during this call, the taken arg
            // window value and the bridge's imaging clone of it, which real
            // Rust's borrowed `&Rc` does not have. A borrow passed through
            // further function hops is modeled as a clone per hop, so a
            // count read deep in a call chain can still run high.
            super::shared::usize_value(Arc::strong_count(slot) - 2)
        }
        // Our file and pipe readers are already buffered, so wrapping is a
        // pass-through; a raw socket is turned into a buffered reader.
        ("BufReader" | "BufWriter", "new" | "with_capacity") => match args.last() {
            Some(Value::Native(h)) if matches!(&*h.lock(), Native::Stream(_)) => {
                let cloned = {
                    let locked = h.lock();
                    let Native::Stream(s) = &*locked else {
                        unreachable!()
                    };
                    s.try_clone()
                };
                match cloned {
                    Ok(clone) => Native::Reader(std::io::BufReader::new(
                        Box::new(clone) as Box<dyn std::io::Read + Send>
                    ))
                    .wrap(),
                    Err(e) => return Err(anyhow!("cannot buffer socket: {e}")),
                }
            }
            other => other.cloned().unwrap_or(Value::Unit),
        },
        ("PathBuf", "new") => make_path(""),
        ("PathBuf" | "Path", "from") | ("Path", "new") => {
            make_path(args.first().map(path_like).unwrap_or_default())
        }
        ("Regex", "new") => {
            let pat = args.first().map(Value::display).unwrap_or_default();
            match regex::Regex::new(&pat) {
                Ok(compiled) => Value::ok(super::regex_bridge::make_regex(compiled, &pat)),
                Err(e) => Value::err(Value::str(e.to_string())),
            }
        }
        ("Some", _) | ("Option", "Some") => {
            Value::some(args.first().cloned().unwrap_or(Value::Unit))
        }
        ("Result", "Ok") => Value::ok(args.first().cloned().unwrap_or(Value::Unit)),
        ("Result", "Err") => Value::err(args.first().cloned().unwrap_or(Value::Unit)),
        _ => return Ok(None),
    }))
}

/// The `Command` builder shape shared by `Command::new` wherever it is spelled.
pub(super) fn command_new(program: Value) -> Value {
    Value::struct_of(
        "Command",
        [
            ("program".into(), program),
            ("args".into(), Value::vec(vec![])),
            ("cwd".into(), Value::Unit),
            ("envs".into(), Value::Unit),
            ("stdin".into(), Value::Unit),
            ("stdout".into(), Value::Unit),
            ("stderr".into(), Value::Unit),
        ],
    )
}

/// Files, permissions, and process stream markers.
fn fs_process_assoc(ty: &str, func: &str, args: &[Value]) -> Result<Option<Value>> {
    Ok(Some(match (ty, func) {
        ("Permissions", "from_mode") => {
            let mode = args.first().and_then(as_i64).unwrap_or(0o644);
            Value::struct_of("Permissions", vec![("mode".into(), Value::Int(mode))])
        }
        // -- files -----------------------------------------------------
        ("File", "open") => open_file(&arg_str(args, 0), std::fs::OpenOptions::new().read(true)),
        ("File", "create") => open_file(
            &arg_str(args, 0),
            std::fs::OpenOptions::new()
                .write(true)
                .create(true)
                .truncate(true),
        ),
        ("File", "create_new") => open_file(
            &arg_str(args, 0),
            std::fs::OpenOptions::new().write(true).create_new(true),
        ),
        ("OpenOptions", "new") => Value::struct_of(
            "OpenOptions",
            [
                "read",
                "write",
                "append",
                "create",
                "create_new",
                "truncate",
            ]
            .into_iter()
            .map(|k| (Arc::from(k), Value::Bool(false))),
        ),
        ("Stdio", "piped" | "inherit" | "null") => {
            Value::struct_of("Stdio", [("kind".into(), Value::str(func))])
        }
        // `Stdio::from(file)` sends a child's stream straight to an open file.
        // The marker carries the file, and the handle is cloned when the
        // command is built so the script keeps its own copy.
        ("Stdio", "from") => {
            let Some(file @ Value::Native(_)) = args.first() else {
                bail!(
                    "Stdio::from takes an open File, got {}",
                    args.first().map_or("nothing", Value::type_name)
                );
            };
            Value::struct_of(
                "Stdio",
                [
                    ("kind".into(), Value::str("file")),
                    ("file".into(), file.clone()),
                ],
            )
        }
        _ => return Ok(None),
    }))
}

/// Time, net, pdf, xml, and the seek positions.
fn misc_assoc(ty: &str, func: &str, args: &[Value]) -> Result<Option<Value>> {
    Ok(Some(match (ty, func) {
        // -- time ------------------------------------------------------
        ("Instant", "now") => Native::Instant(std::time::Instant::now()).wrap(),
        ("SystemTime", "now") => Native::SystemTime(std::time::SystemTime::now()).wrap(),
        ("Duration", "from_secs") => make_duration(std::time::Duration::from_secs(
            u64::try_from(arg_int(args, 0)).unwrap_or_default(),
        )),
        ("Duration", "from_millis") => make_duration(std::time::Duration::from_millis(
            u64::try_from(arg_int(args, 0)).unwrap_or_default(),
        )),
        ("Duration", "from_micros") => make_duration(std::time::Duration::from_micros(
            u64::try_from(arg_int(args, 0)).unwrap_or_default(),
        )),
        ("Duration", "from_nanos") => make_duration(std::time::Duration::from_nanos(
            u64::try_from(arg_int(args, 0)).unwrap_or_default(),
        )),
        ("Duration", "new") => make_duration(std::time::Duration::new(
            u64::try_from(arg_int(args, 0)).unwrap_or_default(),
            u32::try_from(arg_int(args, 1)).unwrap_or_default(),
        )),
        // -- net -------------------------------------------------------
        ("TcpListener", "bind") => match std::net::TcpListener::bind(arg_str(args, 0)) {
            Ok(l) => Value::ok(Native::Listener(l).wrap()),
            Err(e) => Value::err(Value::str(e.to_string())),
        },
        ("TcpStream", "connect") => match std::net::TcpStream::connect(arg_str(args, 0)) {
            Ok(s) => Value::ok(Native::Stream(s).wrap()),
            Err(e) => Value::err(Value::str(e.to_string())),
        },
        ("UdpSocket", "bind") => match std::net::UdpSocket::bind(arg_str(args, 0)) {
            Ok(s) => Value::ok(Native::Udp(s).wrap()),
            Err(e) => Value::err(Value::str(e.to_string())),
        },
        ("Document", "load") => super::pdf_bridge::load(&arg_str(args, 0)),
        ("Element", "parse") => super::xmltree_bridge::parse(args),
        ("Element", "new") => super::xmltree_bridge::new_element(&arg_str(args, 0)),
        // The real xmltree node enum, constructed like `SeekFrom` below since
        // no user declaration exists for it.
        ("XMLNode", "Element" | "Text" | "Comment" | "CData" | "ProcessingInstruction") => {
            Value::enum_of("XMLNode", func, args.to_vec())
        }
        ("SeekFrom", "Start" | "End" | "Current") => Value::enum_of(
            "SeekFrom",
            func,
            vec![args.first().cloned().unwrap_or(Value::Int(0))],
        ),
        _ => return Ok(None),
    }))
}

/// Pull an integer out of a `from`/`try_from` argument. Ints carry through,
/// a bool becomes 0 or 1, and a char becomes its scalar value.
/// The `u32` radix argument of `from_str_radix`, 10 when unreadable.
fn radix_arg(args: &[Value]) -> u32 {
    args.get(1)
        .and_then(as_i64)
        .and_then(|r| u32::try_from(r).ok())
        .unwrap_or(10)
}

fn int_from_arg(ty: &str, v: Option<&Value>) -> Result<i64> {
    match v {
        Some(Value::Int(n)) => Ok(*n),
        Some(Value::Bool(b)) => Ok(i64::from(*b)),
        Some(Value::Char(c)) => Ok(*c as i64),
        // A width-tagged or 128-bit value converts when it fits an i64. The
        // callers check the target's own range on top of this.
        Some(tagged @ (Value::IntW(..) | Value::Big(..))) => match tagged.int_parts() {
            Some((n, _)) => i64::try_from(n).map_err(|_| anyhow!("`{ty}` conversion out of range")),
            None => bail!("`{ty}` conversion out of range"),
        },
        _ => bail!("`{ty}` conversion needs an integer"),
    }
}

/// `T::from_le_bytes([..])` and its be and ne siblings, over the same shared
/// core the `to_*_bytes` methods use.
fn int_from_bytes(ty: &str, func: &str, args: &[Value]) -> Result<Value> {
    let (Some(width), Some(order)) = (IntWidth::parse(ty), from_bytes_order(func)) else {
        bail!("`{ty}::{func}` is not a byte conversion");
    };
    let bytes = byte_array(ty, func, args.first())?;
    Ok(Value::int_of_width(
        from_bytes(width, order, &bytes)?,
        width,
    ))
}

/// The `[u8; N]` argument of a byte conversion. An array literal is a vec at
/// runtime, so the shape real Rust guarantees in its type is read back here.
fn byte_array(ty: &str, func: &str, arg: Option<&Value>) -> Result<Vec<i128>> {
    let Some(Value::Vec(items)) = arg else {
        bail!("`{ty}::{func}` needs a byte array");
    };
    let items = items.lock();
    let mut out = Vec::with_capacity(items.len());
    for item in items.iter() {
        let Some((value, _)) = item.int_parts() else {
            bail!("`{ty}::{func}` needs a byte array");
        };
        out.push(value);
    }
    Ok(out)
}

/// Whether `n` lands inside the target integer type range.
fn int_fits(ty: &str, n: i64) -> bool {
    match ty {
        "i8" => i8::try_from(n).is_ok(),
        "i16" => i16::try_from(n).is_ok(),
        "i32" => i32::try_from(n).is_ok(),
        "u8" => u8::try_from(n).is_ok(),
        "u16" => u16::try_from(n).is_ok(),
        "u32" => u32::try_from(n).is_ok(),
        "u64" | "u128" | "usize" => n >= 0,
        _ => true,
    }
}