mirascript-vm 0.1.105

Native Rust virtual machine for MiraScript
Documentation
use mirascript_vm::{
    MiraArray, MiraError, MiraRecord, MiraValue, Runtime, RuntimeErrorKind, compile,
};

#[test]
fn values_are_copy_and_eight_bytes() {
    fn assert_copy<T: Copy>() {}

    assert_copy::<MiraValue>();
    assert_eq!(std::mem::size_of::<MiraValue>(), 8);
}

#[test]
fn executes_core_language_features() {
    let mut runtime = Runtime::new();
    assert_eq!(runtime.eval_unchecked("1 + 2 * 3"), 7.into());
    assert_eq!(
        runtime
            .eval("let value = (a: 1, b: [2, 3]); value.b[-1] + value.a",)
            .unwrap(),
        4.into(),
    );
    assert_eq!(
        runtime
            .eval(
                "fn add(a, b) { a + b } fn twice(f, x) { f(f(x)) } twice(fn (x) { add(x, 2) }, 3)",
            )
            .unwrap(),
        7.into(),
    );
    assert_eq!(
        runtime
            .eval("let mut total = 0; for value in 1..5 { total += value; } total",)
            .unwrap(),
        15.into(),
    );
}

#[test]
fn numeric_operations_preserve_number_conversion() {
    let mut runtime = Runtime::new();
    for (source, expected) in [
        (r#""2" + true"#, 3),
        (r#""4" - true"#, 3),
        (r#""3" * true"#, 3),
        (r#""6" / true"#, 6),
        (r#""7" % "4""#, 3),
        (r#""2" ^ "3""#, 8),
    ] {
        assert_eq!(runtime.eval(source).unwrap(), expected.into(), "{source}");
    }
}

#[test]
fn script_and_runtime_are_independently_reusable() {
    let script = compile("value + PI").unwrap();

    let mut first = Runtime::new();
    first.insert_global("value", 1).unwrap();
    first.insert_global("PI", 10).unwrap();
    assert_eq!(first.run(&script).unwrap(), 11.into());
    first.insert_global("value", 2).unwrap();
    assert_eq!(first.run(&script).unwrap(), 12.into());

    let mut second = Runtime::new();
    second.insert_global("value", 5).unwrap();
    second.insert_global("PI", 20).unwrap();
    assert_eq!(second.run(&script).unwrap(), 25.into());

    let other = compile("value * 2").unwrap();
    assert_eq!(first.run(&other).unwrap(), 4.into());
}

#[test]
fn closures_capture_each_loop_iteration_but_cannot_escape() {
    let mut runtime = Runtime::new();
    let result = runtime.eval(
        "let mut first = nil; let mut second = nil; for value in 1..2 { if value == 1 { first = fn { value }; } else { second = fn { value }; } } first() * 10 + second()",
    )
    .unwrap();
    assert_eq!(result, 12.into());

    let error = runtime.eval("fn value { 1 } value").unwrap_err();
    assert!(matches!(
        error.as_ref(),
        MiraError::Runtime {
            kind: RuntimeErrorKind::EscapingClosure,
            ..
        }
    ));

    let error = runtime
        .eval("mod value { pub let x = 1; } value")
        .unwrap_err();
    assert!(matches!(
        error.as_ref(),
        MiraError::Runtime {
            kind: RuntimeErrorKind::EscapingClosure,
            ..
        }
    ));

    runtime
        .insert_fn(
            "wrap",
            mirascript_vm::MiraNativeFn::ok(|_, args| args.to_vec()),
        )
        .unwrap();
    let value = runtime.eval_unchecked("wrap(fn { 1 })");
    assert!(value.is_array());
    runtime.insert_global("value", value).unwrap();
    let inner = runtime.eval_unchecked("value[0]");
    assert!(inner.is_nil());
}

#[derive(MiraRecord)]
struct Position {
    x: f64,
    y: f64,
}

#[derive(MiraRecord)]
struct User {
    age: u8,
    #[mira(rename = "display_name")]
    name: String,
    position: Position,
    #[mira(skip)]
    _secret: String,
}

#[derive(MiraArray)]
#[allow(dead_code)]
struct Point(f64, f64, #[mira(skip)] String);

#[derive(MiraArray)]
struct Pair(f64, f64);

#[derive(MiraRecord)]
struct RecordWithNestedShapes {
    position: Position,
    pair: Pair,
}

#[derive(MiraArray)]
struct ArrayWithNestedShapes(Position, Pair);

#[test]
fn derived_values_are_live_runtime_views() {
    let mut runtime = Runtime::new();
    let user = runtime
        .insert_record(User {
            age: 42,
            name: "Ada".into(),
            position: Position { x: 3.0, y: 4.0 },
            _secret: "token".into(),
        })
        .unwrap();
    let point = runtime
        .insert_array(Point(3.0, 4.0, "metadata".into()))
        .unwrap();
    runtime
        .insert_global("user", MiraValue::record(user))
        .unwrap();
    runtime
        .insert_global("point", MiraValue::array(point))
        .unwrap();

    assert_eq!(
        runtime
            .eval("user.age + user.position.x + point[0] + point[-1]",)
            .unwrap(),
        52.into(),
    );
    let name = runtime.eval_unchecked("user.display_name");
    assert_eq!(name.as_str(&runtime).unwrap(), Some("Ada"));

    let user_value = runtime.get_record_mut(user).unwrap();
    user_value.age = 7;
    user_value.position.x = 10.0;
    assert_eq!(
        runtime.eval_unchecked("user.age + user.position.x"),
        17.into(),
    );

    let wrong = unsafe { user.erase_record().upcast::<Position>() };
    let error = runtime.get_record(wrong).err().unwrap();
    assert!(matches!(
        error.as_ref(),
        MiraError::Runtime {
            kind: RuntimeErrorKind::HandleTypeMismatch { category: "record" },
            ..
        }
    ));
}

#[test]
fn nested_derived_shapes_remain_live_for_record_and_array_parents() {
    let mut runtime = Runtime::new();
    let record = runtime
        .insert_record(RecordWithNestedShapes {
            position: Position { x: 1.0, y: 2.0 },
            pair: Pair(3.0, 4.0),
        })
        .unwrap();
    let array = runtime
        .insert_array(ArrayWithNestedShapes(
            Position { x: 5.0, y: 6.0 },
            Pair(7.0, 8.0),
        ))
        .unwrap();
    runtime
        .insert_global("record", MiraValue::record(record))
        .unwrap();
    runtime
        .insert_global("array", MiraValue::array(array))
        .unwrap();

    assert_eq!(
        runtime
            .eval("record.position.x + record.pair[1] + array[0].y + array[1][0]",)
            .unwrap(),
        18.into(),
    );
    assert_eq!(
        runtime
            .eval("record.position::keys()::len() + record.pair::len() + array[0]::keys()::len() + array[1]::len()",)
            .unwrap(),
        8.into(),
    );

    let projected_position = runtime
        .eval("record.position")
        .unwrap()
        .as_record()
        .unwrap();
    let projected_position = unsafe { projected_position.upcast::<Position>() };
    let projected_pair = runtime.eval_unchecked("array[1]").as_array().unwrap();
    let projected_pair = unsafe { projected_pair.upcast::<Pair>() };
    assert_eq!(runtime.get_record(projected_position).unwrap().x, 1.0);
    assert_eq!(runtime.get_array(projected_pair).unwrap().1, 8.0);

    let value = runtime.get_record_mut(record).unwrap();
    value.position.x = 10.0;
    value.pair.1 = 40.0;
    let value = runtime.get_array_mut(array).unwrap();
    value.0.y = 60.0;
    value.1.0 = 70.0;

    assert_eq!(runtime.get_record(projected_position).unwrap().x, 10.0);
    assert_eq!(runtime.get_array(projected_pair).unwrap().0, 70.0);
    assert_eq!(
        runtime
            .eval("record.position.x + record.pair[1] + array[0].y + array[1][0]",)
            .unwrap(),
        180.into(),
    );
}

#[test]
fn foreign_handles_are_rejected() {
    let mut first = Runtime::new();
    let value = first.insert("owned by first").unwrap();
    let mut second = Runtime::new();
    let error = second.insert_global("foreign", value).unwrap_err();
    assert!(matches!(
        error.as_ref(),
        MiraError::Runtime {
            kind: RuntimeErrorKind::ForeignHandle,
            ..
        }
    ));
}

#[test]
fn errors_preserve_diagnostics_and_runtime_context() {
    assert!(matches!(
        compile("let =").unwrap_err().as_ref(),
        MiraError::Compile { diagnostics } if !diagnostics.is_empty()
    ));

    let mut runtime = Runtime::new();
    match runtime
        .eval("fn outer { panic('boom') } outer()")
        .unwrap_err()
        .as_ref()
    {
        MiraError::Runtime { trace, .. } => {
            assert_eq!(trace.function.as_deref(), Some("<anonymous>"));
            assert!(trace.offset.is_some());
            assert_eq!(trace.stack, ["<anonymous>"]);
        }
        error => panic!("expected runtime error with context, got {error:?}"),
    }

    match runtime
        .eval("[1]::map(fn { panic('boom') })")
        .unwrap_err()
        .as_ref()
    {
        MiraError::Runtime { trace, .. } => {
            assert_eq!(trace.function.as_deref(), Some("<anonymous>"));
            assert!(trace.offset.is_some());
            assert_eq!(trace.stack, ["global.map", "<anonymous>"]);
        }
        error => panic!("expected nested runtime error with context, got {error:?}"),
    }
}