run-rs 0.6.6

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
//! Bridges for `std` paths a script calls: fs, io, env, paths, metadata, and
//! streams.

use std::sync::Arc;

use anyhow::{Result, bail};

use super::crates_bridge::crate_bridge;
use super::json_bridge::bridge_serde_json;
use super::native::Native;
use super::native_methods;
use super::value::{StructData, Value};

/// The `std::fs` free functions.
fn fs_native_call(func: &str, args: &[Value]) -> Result<Option<Value>> {
    let s = |i: usize| -> Result<String> {
        match args.get(i) {
            Some(v) => Ok(path_like(v)),
            None => bail!("missing argument {i} for fs::{func}"),
        }
    };
    Ok(Some(match func {
        "read_to_string" => wrap_io(std::fs::read_to_string(s(0)?)),
        "read" => wrap_bytes(std::fs::read(s(0)?)),
        "write" => wrap_unit(std::fs::write(s(0)?, s(1)?)),
        "create_dir_all" => wrap_unit(std::fs::create_dir_all(s(0)?)),
        "create_dir" => wrap_unit(std::fs::create_dir(s(0)?)),
        "remove_file" => wrap_unit(std::fs::remove_file(s(0)?)),
        "remove_dir_all" => wrap_unit(std::fs::remove_dir_all(s(0)?)),
        "remove_dir" => wrap_unit(std::fs::remove_dir(s(0)?)),
        "copy" => match std::fs::copy(s(0)?, s(1)?) {
            Ok(n) => Value::ok(Value::Int(i64::try_from(n).unwrap_or(i64::MAX))),
            Err(e) => Value::err(super::native::io_error_value(&e)),
        },
        "rename" => wrap_unit(std::fs::rename(s(0)?, s(1)?)),
        "read_dir" => match std::fs::read_dir(s(0)?) {
            Ok(rd) => {
                let mut items = Vec::new();
                for e in rd {
                    match e {
                        Ok(entry) => items.push(Value::ok(make_dir_entry(&entry))),
                        Err(err) => items.push(Value::err(super::native::io_error_value(&err))),
                    }
                }
                Value::ok(Value::vec(items))
            }
            Err(e) => Value::err(super::native::io_error_value(&e)),
        },
        "canonicalize" => match std::fs::canonicalize(s(0)?) {
            Ok(p) => Value::ok(make_path(p.display().to_string())),
            Err(e) => Value::err(super::native::io_error_value(&e)),
        },
        "metadata" => match std::fs::metadata(s(0)?) {
            Ok(m) => Value::ok(make_metadata(&m)),
            Err(e) => Value::err(super::native::io_error_value(&e)),
        },
        "symlink_metadata" => match std::fs::symlink_metadata(s(0)?) {
            Ok(m) => Value::ok(make_metadata(&m)),
            Err(e) => Value::err(super::native::io_error_value(&e)),
        },
        "read_link" => match std::fs::read_link(s(0)?) {
            Ok(p) => Value::ok(make_path(p.display().to_string())),
            Err(e) => Value::err(super::native::io_error_value(&e)),
        },
        "hard_link" => wrap_unit(std::fs::hard_link(s(0)?, s(1)?)),
        // The platform specific names are aliased to one cross-platform
        // helper, so the cfg gated `use` a script needs to type-check on
        // each os all dispatch here at runtime.
        "symlink" | "symlink_file" | "symlink_dir" => wrap_unit(make_symlink(&s(0)?, &s(1)?)),
        "set_permissions" => wrap_unit(set_permissions_impl(
            &s(0)?,
            args.get(1).and_then(perm_mode),
        )),
        _ => return crate_bridge("fs", func, args),
    }))
}

pub(super) fn native_call(module: &str, func: &str, args: &[Value]) -> Result<Option<Value>> {
    if module == "serde_json" {
        return bridge_serde_json(func, args).map(Some);
    }
    if module == "fs" {
        return fs_native_call(func, args);
    }
    let s = |i: usize| -> Result<String> {
        match args.get(i) {
            Some(v) => Ok(path_like(v)),
            None => bail!("missing argument {i} for {module}::{func}"),
        }
    };
    Ok(Some(match (module, func) {
        ("env", "args") => Value::vec(super::script_args().into_iter().map(Value::str).collect()),
        ("env", "var") => match std::env::var(s(0)?) {
            Ok(v) => Value::ok(Value::str(v)),
            // The structured `VarError`, so `Err(VarError::NotPresent)`
            // matches and `{e:?}` prints `NotPresent` like real Rust.
            Err(std::env::VarError::NotPresent) => {
                Value::err(Value::enum_of("VarError", "NotPresent", Vec::new()))
            }
            Err(std::env::VarError::NotUnicode(os)) => Value::err(Value::enum_of(
                "VarError",
                "NotUnicode",
                vec![Value::str(os.to_string_lossy().into_owned())],
            )),
        },
        ("env", "current_dir") => match std::env::current_dir() {
            Ok(p) => Value::ok(make_path(p.display().to_string())),
            Err(e) => Value::err(super::native::io_error_value(&e)),
        },
        ("env", "set_var") => {
            // Safety: scripts treat the environment as script-wide state, the
            // same trade a single threaded interpreter always made.
            unsafe { std::env::set_var(s(0)?, s(1)?) };
            Value::Unit
        }
        ("env", "remove_var") => {
            unsafe { std::env::remove_var(s(0)?) };
            Value::Unit
        }
        ("env", "var_os") => match std::env::var_os(s(0)?) {
            Some(v) => Value::some(make_os_string(v.to_string_lossy().into_owned())),
            None => Value::none(),
        },
        ("env", "vars" | "vars_os") => Value::vec(
            std::env::vars()
                .map(|(k, v)| Value::tuple(vec![Value::str(k), Value::str(v)]))
                .collect(),
        ),
        ("env", "set_current_dir") => wrap_unit(std::env::set_current_dir(s(0)?)),
        ("env", "temp_dir") => make_path(std::env::temp_dir().display().to_string()),
        ("process", "exit") => {
            let code = args
                .first()
                .and_then(as_i64)
                .and_then(|c| i32::try_from(c).ok())
                .unwrap_or(0);
            std::process::exit(code);
        }
        ("process", "abort") => std::process::abort(),
        ("process", "id") => Value::Int(i64::from(std::process::id())),
        // -- io -------------------------------------------------------
        ("io", "stdin") => make_std_stream(
            "stdin",
            Native::Reader(std::io::BufReader::new(Box::new(std::io::stdin()))),
        ),
        ("io", "stdout") => make_std_stream("stdout", Native::Writer(Box::new(std::io::stdout()))),
        ("io", "stderr") => make_std_stream("stderr", Native::Writer(Box::new(std::io::stderr()))),
        _ => return crate_bridge(module, func, args),
    }))
}

/// A symlink helper that picks the right platform call. On Windows a file vs
/// dir symlink needs distinct functions; the target kind comes from whether
/// the source exists as a directory.
fn make_symlink(src: &str, dst: &str) -> std::io::Result<()> {
    #[cfg(unix)]
    {
        std::os::unix::fs::symlink(src, dst)
    }
    #[cfg(windows)]
    {
        if std::path::Path::new(src).is_dir() {
            std::os::windows::fs::symlink_dir(src, dst)
        } else {
            std::os::windows::fs::symlink_file(src, dst)
        }
    }
}

fn perm_mode(v: &Value) -> Option<u32> {
    if let Value::Struct(st) = v
        && &**st.name() == "Permissions"
    {
        return st
            .get("mode")
            .and_then(|m| as_i64(&m))
            .and_then(|m| u32::try_from(m).ok());
    }
    None
}

fn set_permissions_impl(path: &str, mode: Option<u32>) -> std::io::Result<()> {
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(path, std::fs::Permissions::from_mode(mode.unwrap_or(0o644)))
    }
    #[cfg(windows)]
    {
        let mut permissions = std::fs::metadata(path)?.permissions();
        permissions.set_readonly(mode.is_some_and(|mode| mode & 0o222 == 0));
        std::fs::set_permissions(path, permissions)
    }
}

pub(super) fn as_i64(v: &Value) -> Option<i64> {
    match v {
        Value::Int(i) => Some(*i),
        _ => None,
    }
}

/// Turn a value into a path string. A `Path`/`PathBuf` value carries the path
/// in its `s` field; an `OsString` carries its text there too. Anything else
/// uses its display form.
pub(super) fn path_like(v: &Value) -> String {
    match v {
        Value::Str(s) => s.to_string(),
        Value::Struct(st) if matches!(&**st.name(), "Path" | "PathBuf" | "OsString") => {
            st.get("s").map(|s| s.display()).unwrap_or_default()
        }
        other => other.display(),
    }
}

/// Wrap a std stream handle so `is_terminal` can name its stream while reads
/// and writes delegate to the inner native handle.
pub(super) fn make_std_stream(kind: &str, inner: Native) -> Value {
    Value::struct_of(
        "StdStream",
        [
            ("kind".into(), Value::str(kind)),
            ("inner".into(), inner.wrap()),
        ],
    )
}

pub(super) fn std_stream_method(
    s: &Arc<StructData>,
    name: &str,
    args: &mut [Value],
) -> Result<Value> {
    use std::io::IsTerminal;
    if name == "is_terminal" {
        let kind = s.get("kind").map(|v| v.display()).unwrap_or_default();
        let tty = match kind.as_str() {
            "stdin" => std::io::stdin().is_terminal(),
            "stderr" => std::io::stderr().is_terminal(),
            _ => std::io::stdout().is_terminal(),
        };
        return Ok(Value::Bool(tty));
    }
    if matches!(name, "lock" | "by_ref") {
        return Ok(Value::Struct(s.clone()));
    }
    let inner = match s.get("inner") {
        Some(Value::Native(h)) => h.clone(),
        _ => bail!("std stream lost its handle"),
    };
    match native_methods::native_method(&inner, name, args)? {
        Some(v) => Ok(v),
        None => bail!("unknown method `{name}` on a std stream"),
    }
}

/// Turn a script `Duration` value into a real `std::time::Duration`.
pub(super) fn duration_from_value(v: &Value) -> Option<std::time::Duration> {
    if let Value::Struct(s) = v
        && &**s.name() == "Duration"
    {
        let secs = u64::try_from(field_int(s, "secs")).unwrap_or_default();
        let nanos = u32::try_from(field_int(s, "nanos")).unwrap_or_default();
        return Some(std::time::Duration::new(secs, nanos));
    }
    None
}

/// Build a `Duration` value carrying whole and sub-second parts.
pub(super) fn make_duration(d: std::time::Duration) -> Value {
    Value::struct_of(
        "Duration",
        [
            (
                "secs".into(),
                Value::Int(i64::try_from(d.as_secs()).unwrap_or(i64::MAX)),
            ),
            ("nanos".into(), Value::Int(i64::from(d.subsec_nanos()))),
        ],
    )
}

/// Build a `Metadata` value with the common accessors materialized as fields.
/// The Unix `MetadataExt` fields are gated so the interpreter still builds on
/// Windows, where a script would use different accessors.
pub(super) fn make_metadata(m: &std::fs::Metadata) -> Value {
    let mut f: Vec<(Arc<str>, Value)> = vec![
        (
            "len".into(),
            Value::Int(i64::try_from(m.len()).unwrap_or(i64::MAX)),
        ),
        ("is_dir".into(), Value::Bool(m.is_dir())),
        ("is_file".into(), Value::Bool(m.is_file())),
        ("is_symlink".into(), Value::Bool(m.is_symlink())),
        ("readonly".into(), Value::Bool(m.permissions().readonly())),
    ];
    #[cfg(unix)]
    {
        use std::os::unix::fs::MetadataExt;
        use std::os::unix::fs::PermissionsExt;
        f.push(("mode".into(), Value::Int(i64::from(m.permissions().mode()))));
        f.push(("dev".into(), Value::Int(m.dev().cast_signed())));
        f.push(("ino".into(), Value::Int(m.ino().cast_signed())));
        f.push(("uid".into(), Value::Int(i64::from(m.uid()))));
        f.push(("gid".into(), Value::Int(i64::from(m.gid()))));
        f.push(("mtime".into(), Value::Int(m.mtime())));
    }
    if let Ok(t) = m.modified() {
        f.push(("modified".into(), Native::SystemTime(t).wrap()));
    }
    Value::struct_of("Metadata", f)
}

// -- path, directory entry, and file type ----------------------------------

pub(super) fn make_path(s: impl Into<String>) -> Value {
    Value::struct_of("Path", [("s".into(), Value::str(s.into()))])
}

pub(super) fn make_os_string(s: impl Into<String>) -> Value {
    Value::struct_of("OsString", [("s".into(), Value::str(s.into()))])
}

pub(super) fn os_string_method(s: &Arc<StructData>, method: &str) -> Result<Value> {
    let value = s.get("s").map(|value| value.display()).unwrap_or_default();
    Ok(match method {
        "into" => make_path(value),
        "to_string_lossy" | "to_str" => Value::str(value),
        "is_empty" => Value::Bool(value.is_empty()),
        _ => bail!("unknown method `{method}` on OsString"),
    })
}

pub(super) fn make_dir_entry(entry: &std::fs::DirEntry) -> Value {
    Value::struct_of(
        "DirEntry",
        [
            (
                "path".into(),
                Value::str(entry.path().display().to_string()),
            ),
            (
                "name".into(),
                Value::str(entry.file_name().to_string_lossy().into_owned()),
            ),
        ],
    )
}

pub(super) fn make_file_type(path: &std::path::Path) -> Value {
    // DirEntry::file_type does not follow symlinks, so a symlink to a dir
    // reports is_symlink, not is_dir, same as the real std.
    let ft = path.symlink_metadata().map(|m| m.file_type());
    let is = |f: &dyn Fn(&std::fs::FileType) -> bool| Value::Bool(ft.as_ref().is_ok_and(f));
    Value::struct_of(
        "FileType",
        [
            ("is_dir".into(), is(&|t| t.is_dir())),
            ("is_file".into(), is(&|t| t.is_file())),
            ("is_symlink".into(), is(&|t| t.is_symlink())),
        ],
    )
}

pub(super) fn path_string(s: &StructData, key: &str) -> String {
    s.get(key).map(|v| v.display()).unwrap_or_default()
}

pub(super) fn path_method(st: &Arc<StructData>, method: &str, args: &[Value]) -> Result<Value> {
    let s = path_string(st, "s");
    let p = std::path::Path::new(&s);
    let opt_str = |o: Option<&std::ffi::OsStr>| match o {
        Some(v) => Value::some(Value::str(v.to_string_lossy().into_owned())),
        None => Value::none(),
    };
    Ok(match method {
        "display" | "to_string_lossy" => Value::str(s.clone()),
        "to_str" => Value::some(Value::str(s.clone())),
        "into_string" | "into_os_string" => Value::ok(Value::str(s.clone())),
        "to_owned" | "to_path_buf" | "clone" | "as_path" | "as_os_str" => make_path(s.clone()),
        "is_dir" => Value::Bool(p.is_dir()),
        "is_file" => Value::Bool(p.is_file()),
        "is_absolute" => Value::Bool(p.is_absolute()),
        "exists" => Value::Bool(p.exists()),
        "file_name" => match p.file_name() {
            Some(n) => Value::some(make_path(n.to_string_lossy().into_owned())),
            None => Value::none(),
        },
        "file_stem" => opt_str(p.file_stem()),
        "extension" => opt_str(p.extension()),
        "with_extension" => make_path(p.with_extension(arg_str(args, 0)).display().to_string()),
        "parent" => match p.parent() {
            Some(par) => Value::some(make_path(par.display().to_string())),
            None => Value::none(),
        },
        "ancestors" => Value::vec(
            p.ancestors()
                .map(|ancestor| make_path(ancestor.display().to_string()))
                .collect(),
        ),
        "join" | "push" => {
            let joined = p.join(args.first().map(Value::display).unwrap_or_default());
            make_path(joined.display().to_string())
        }
        // Path compares whole components, so "/a/bc" does not start with "/a/b"
        // the way the str method would say it does.
        "starts_with" => {
            Value::Bool(p.starts_with(args.first().map(Value::display).unwrap_or_default()))
        }
        "ends_with" => {
            Value::Bool(p.ends_with(args.first().map(Value::display).unwrap_or_default()))
        }
        _ => bail!("unknown method `{method}` on Path"),
    })
}

pub(super) fn dir_entry_method(s: &Arc<StructData>, method: &str) -> Result<Value> {
    let path = path_string(s, "path");
    Ok(match method {
        "path" => make_path(path),
        "file_name" => make_path(path_string(s, "name")),
        "file_type" => Value::ok(make_file_type(std::path::Path::new(&path))),
        _ => bail!("unknown method `{method}` on DirEntry"),
    })
}

pub(super) fn file_type_method(s: &Arc<StructData>, method: &str) -> Result<Value> {
    let get = |k: &str| s.get(k).unwrap_or(Value::Bool(false));
    Ok(match method {
        "is_dir" => get("is_dir"),
        "is_file" => get("is_file"),
        "is_symlink" => get("is_symlink"),
        _ => bail!("unknown method `{method}` on FileType"),
    })
}

pub(super) fn metadata_method(s: &Arc<StructData>, name: &str) -> Result<Value> {
    let get = |k: &str| s.get(k).unwrap_or(Value::Unit);
    Ok(match name {
        "len" => get("len"),
        "is_dir" => get("is_dir"),
        "is_file" => get("is_file"),
        "is_symlink" => get("is_symlink"),
        "modified" | "created" | "accessed" => match s.get("modified") {
            Some(v) => Value::ok(v),
            None => Value::err(Value::str("timestamp not available".to_string())),
        },
        "mode" | "dev" | "ino" | "uid" | "gid" | "mtime" => get(name),
        "permissions" => Value::struct_of(
            "Permissions",
            [
                ("mode".into(), get("mode")),
                ("readonly".into(), get("readonly")),
            ],
        ),
        _ => bail!("unknown method `{name}` on Metadata"),
    })
}

pub(super) fn wrap_io(r: std::io::Result<String>) -> Value {
    match r {
        Ok(s) => Value::ok(Value::str(s)),
        Err(e) => Value::err(super::native::io_error_value(&e)),
    }
}

pub(super) fn wrap_bytes(r: std::io::Result<Vec<u8>>) -> Value {
    match r {
        Ok(bytes) => Value::ok(Value::vec(
            bytes
                .into_iter()
                .map(|b| Value::Int(i64::from(b)))
                .collect(),
        )),
        Err(e) => Value::err(super::native::io_error_value(&e)),
    }
}

pub(super) fn wrap_unit(r: std::io::Result<()>) -> Value {
    match r {
        Ok(()) => Value::ok(Value::Unit),
        Err(e) => Value::err(super::native::io_error_value(&e)),
    }
}

pub(super) fn field_int(s: &StructData, k: &str) -> i64 {
    match s.get(k) {
        Some(Value::Int(i)) => i,
        _ => 0,
    }
}

pub(super) fn arg_str(args: &[Value], i: usize) -> String {
    args.get(i).map(path_like).unwrap_or_default()
}

pub(super) fn arg_int(args: &[Value], i: usize) -> i64 {
    match args.get(i) {
        Some(Value::Int(n)) => *n,
        _ => 0,
    }
}

pub(super) fn open_file(path: &str, opts: &std::fs::OpenOptions) -> Value {
    match opts.open(path) {
        Ok(f) => Value::ok(Native::File(std::io::BufReader::new(f)).wrap()),
        Err(e) => Value::err(super::native::io_error_value(&e)),
    }
}

// Methods on the OpenOptions struct built by `OpenOptions::new`. The builder
// setters return a fresh struct with one flag flipped, matching the real
// `&mut self -> &mut Self` chain, and `open` assembles a real std OpenOptions
// from the flags and opens the file.
pub(super) fn openoptions_method(s: &StructData, name: &str, args: &[Value]) -> Result<Value> {
    const FLAGS: [&str; 6] = [
        "read",
        "write",
        "append",
        "create",
        "create_new",
        "truncate",
    ];
    let field_bool = |k: &str| matches!(s.get(k), Some(Value::Bool(true)));
    if FLAGS.contains(&name) {
        let on = matches!(args.first(), Some(Value::Bool(true)));
        let pairs = FLAGS.iter().map(|&k| {
            (
                Arc::from(k),
                Value::Bool(if k == name { on } else { field_bool(k) }),
            )
        });
        return Ok(Value::struct_of("OpenOptions", pairs));
    }
    if name == "open" {
        let path = args.first().map(path_like).unwrap_or_default();
        let mut opts = std::fs::OpenOptions::new();
        opts.read(field_bool("read"))
            .write(field_bool("write"))
            .append(field_bool("append"))
            .create(field_bool("create"))
            .create_new(field_bool("create_new"))
            .truncate(field_bool("truncate"));
        return Ok(open_file(&path, &opts));
    }
    bail!("unknown method `{name}` on OpenOptions")
}

pub(super) fn bytes_to_string(arg: Option<&Value>) -> String {
    match arg {
        Some(Value::Str(s)) => s.to_string(),
        Some(Value::Vec(v)) => {
            let bytes: Vec<u8> = v
                .lock()
                .iter()
                .filter_map(|x| match x {
                    Value::Int(i) => u8::try_from(*i).ok(),
                    _ => None,
                })
                .collect();
            String::from_utf8_lossy(&bytes).into_owned()
        }
        _ => String::new(),
    }
}