use rustia::{
CompiledModule, Effects, Error, Exports, HostContext, HostModule, HostMut, HostSchema, Input,
InvocationLimits, ModuleSource, OpCapability, Profile, Result, Rustia, ServiceModuleResolver,
SlotWrites, Slots, VmError, VmErrorKind,
};
use std::cell::Cell;
use std::panic::{catch_unwind, AssertUnwindSafe};
use std::rc::Rc;
#[derive(Clone, Debug, Eq, PartialEq)]
enum RecordKind {
Effect(i64),
SlotWrite { slot: usize, value: i64 },
}
#[derive(Clone, Debug, Eq, PartialEq)]
struct Record {
sequence: u32,
kind: RecordKind,
}
struct Host {
input: i64,
slots: Vec<i64>,
}
impl HostSchema for Host {
type Input = i64;
type Slots = [i64];
type External = Host;
type Effect = i64;
type SlotWrite = (usize, i64);
type Record = Record;
}
impl HostContext<Host> for Host {
fn input(&self) -> &i64 {
&self.input
}
fn slots(&self) -> &[i64] {
&self.slots
}
fn external_mut(&mut self) -> &mut Host {
self
}
fn record_effect(&self, sequence: u32, effect: i64) -> Record {
Record {
sequence,
kind: RecordKind::Effect(effect),
}
}
fn record_slot_write(&self, sequence: u32, write: (usize, i64)) -> Record {
Record {
sequence,
kind: RecordKind::SlotWrite {
slot: write.0,
value: write.1,
},
}
}
fn record_bytes(&self, _record: &Record) -> u64 {
16
}
}
fn configured_vm() -> Rustia<Host> {
let mut rustia = rustia::RustiaBuilder::standard()
.profile(Profile::DeterministicSim)
.build_for_host::<Host>()
.unwrap();
rustia
.add_input("read_input", 2, |input: Input<'_, Host>, offset: i64| {
*input.get() + offset
})
.unwrap();
rustia
.add_slot_read("read_slot", 2, |slots: Slots<'_, Host>, slot: usize| {
slots.get()[slot]
})
.unwrap();
rustia
.add_effect(
"emit",
2,
|mut effects: Effects<'_, Host>, value: i64| -> Result<()> { effects.emit(value) },
)
.unwrap();
rustia
.add_slot_write(
"write_slot",
2,
|mut writes: SlotWrites<'_, Host>, slot: usize, value: i64| -> Result<()> {
writes.write((slot, value))
},
)
.unwrap();
rustia
}
#[test]
fn created_context_callbacks_rebind_inputs_exchange_live_values_and_release_on_reset() {
use rustia::{HostFunction, Table};
use std::sync::Arc;
let mut rustia = rustia::RustiaBuilder::standard()
.profile(Profile::DeterministicSim)
.build_for_host::<Host>()
.unwrap();
let capture = Arc::new(());
let weak = Arc::downgrade(&capture);
let read = rustia
.create_function(
HostFunction::input(move |input: Input<'_, Host>| {
let _keep = &capture;
*input.get()
})
.fuel_cost(2),
)
.unwrap();
let emit = rustia
.create_function(
HostFunction::effect(
|mut effects: Effects<'_, Host>, value: i64, table: Table| -> Result<Table> {
effects.emit(value)?;
Ok(table)
},
)
.fuel_cost(3),
)
.unwrap();
let table = rustia.create_table().unwrap();
for input in [3, 7] {
let mut host = Host {
input,
slots: Vec::new(),
};
let outcome = rustia
.invoke(
&mut host,
InvocationLimits::new(1000)
.with_max_records(1)
.with_max_record_bytes(16),
|invocation| {
let value = read.call_in::<_, _, i64>(invocation, ())?;
assert_eq!(value, input);
let source = format!("return {value}");
let number = invocation.load(&source).eval_live()?;
emit.call_values_in(invocation, &[number, table.as_value()])
},
)
.unwrap();
assert_eq!(outcome.records().len(), 1);
assert_eq!(outcome.records()[0].kind, RecordKind::Effect(input));
let returned = outcome.value()[0].clone();
assert!(returned.raw_equal(&mut rustia, &table.as_value()).unwrap());
}
rustia.reset_decision().unwrap();
assert!(weak.upgrade().is_none());
}
#[test]
fn replacing_typed_registrations_releases_obsolete_handlers() {
use rustia::HostFunction;
use std::sync::Arc;
let mut rustia = rustia::RustiaBuilder::standard()
.profile(Profile::DeterministicSim)
.build_for_host::<Host>()
.unwrap();
let capture = Arc::new(());
let weak = Arc::downgrade(&capture);
rustia
.register_function(
"read",
HostFunction::input(move |input: Input<'_, Host>| {
let _keep = &capture;
*input.get()
}),
)
.unwrap();
rustia
.register_function(
"read",
HostFunction::input(|input: Input<'_, Host>| *input.get() + 1),
)
.unwrap();
rustia.reset_decision().unwrap();
assert!(weak.upgrade().is_none());
let mut host = Host {
input: 5,
slots: Vec::new(),
};
let outcome = rustia
.invoke(&mut host, InvocationLimits::new(1000), |invocation| {
invocation.load("return read()").eval::<i64>()
})
.unwrap();
assert_eq!(*outcome.value(), 6);
}
#[test]
fn shared_definitions_keep_empty_name_validation_and_live_signature_metadata() {
use rustia::{HostFunction, Table};
let mut rustia = rustia::RustiaBuilder::standard()
.profile(Profile::DeterministicSim)
.build_for_host::<Host>()
.unwrap();
assert!(rustia
.add_input("", 1, |input: Input<'_, Host>| *input.get())
.is_err());
assert!(rustia.register_function("", || 1_i64).is_err());
let module = HostModule::<Host>::new("api").function(
"id",
HostFunction::input(|_: Input<'_, Host>, table: Table| -> Result<(Table, i64)> {
Ok((table, 1))
})
.fuel_cost(3),
);
let metadata = module.functions().next().unwrap();
assert_eq!(metadata.name, "id");
assert_eq!(metadata.capability, OpCapability::ReadsDeterministicInput);
assert_eq!(metadata.parameter_count, 1);
assert_eq!(metadata.parameter_types, &[std::any::type_name::<Table>()]);
assert_eq!(
metadata.return_types,
&[std::any::type_name::<Table>(), "i64"]
);
assert_eq!(metadata.fuel_cost, 3);
assert!(metadata.fallible);
rustia.install_host_module(module).unwrap();
}
trait TypedTestSetup {
fn install_source(&mut self, source: &str) -> Result<Exports>;
fn install_typed(&mut self, module: &CompiledModule) -> Result<Exports>;
}
impl TypedTestSetup for Rustia<Host> {
fn install_source(&mut self, source: &str) -> Result<Exports> {
let module = Rustia::compile_module("typed-test.luau", source)?;
self.install_typed(&module)
}
fn install_typed(&mut self, module: &CompiledModule) -> Result<Exports> {
let mut context = Host {
input: 0,
slots: Vec::new(),
};
let outcome = self.invoke(&mut context, InvocationLimits::new(100_000), |invocation| {
invocation.install(module)
})?;
let (exports, _, _) = outcome.into_parts();
Ok(exports)
}
}
#[test]
fn context_is_rebound_and_success_publishes_dense_ordered_records() {
let mut rustia = configured_vm();
let module = Rustia::compile_module(
"decision.luau",
r#"
return {
decide = function(offset)
local value = read_input(offset)
emit(value)
write_slot(1, value + read_slot(1))
return value
end,
}
"#,
)
.unwrap();
let exports = rustia.install_typed(&module).unwrap();
let limits = InvocationLimits::new(10_000)
.with_max_records(8)
.with_max_record_bytes(128);
let mut first_host = Host {
input: 10,
slots: vec![3, 4],
};
let first = rustia
.invoke(&mut first_host, limits, |invocation| {
let decide = exports.function_in(invocation, "decide")?;
decide.call_in::<_, _, i64>(invocation, (5_i64,))
})
.unwrap();
assert_eq!(*first.value(), 15);
assert_eq!(
first.records(),
[
Record {
sequence: 0,
kind: RecordKind::Effect(15),
},
Record {
sequence: 1,
kind: RecordKind::SlotWrite { slot: 1, value: 19 },
},
]
);
let mut second_host = Host {
input: -2,
slots: vec![100, 7],
};
let second = rustia
.invoke(&mut second_host, limits, |invocation| {
let decide = exports.function_in(invocation, "decide")?;
decide.call_in::<_, _, i64>(invocation, (1_i64,))
})
.unwrap();
assert_eq!(*second.value(), -1);
assert_eq!(second.records()[0].sequence, 0);
assert_eq!(second.records()[0].kind, RecordKind::Effect(-1));
}
#[test]
fn typed_invocations_exchange_rooted_live_values_without_snapshotting() {
let mut rustia = configured_vm();
let mut host = Host {
input: 0,
slots: vec![0],
};
let limits = InvocationLimits::new(100_000);
let function = rustia
.invoke(&mut host, limits, |invocation| {
invocation
.load(
"return function(value) local result = { value = value }; result.self = result; return value, result end",
)
.source_name("live-function.luau")
.eval_live()
})
.unwrap()
.into_parts()
.0
.as_function(&rustia)
.unwrap()
.unwrap();
let argument = rustia
.invoke(&mut host, limits, |invocation| {
invocation
.load("return { answer = 42 }")
.source_name("live-argument.luau")
.eval_live()
})
.unwrap()
.into_parts()
.0;
let results = rustia
.invoke(&mut host, limits, |invocation| {
function.call_values_in(invocation, std::slice::from_ref(&argument))
})
.unwrap()
.into_parts()
.0;
assert!(argument.raw_equal(&mut rustia, &results[0]).unwrap());
let result_table = results[1].as_table(&rustia).unwrap().unwrap();
let result_value = result_table.get(&mut rustia, "value").unwrap();
assert!(argument.raw_equal(&mut rustia, &result_value).unwrap());
}
#[test]
fn typed_install_remains_provisional_until_the_outer_invocation_commits() {
let mut rustia = configured_vm();
let shared = rustia.create_table().unwrap();
shared.set(&mut rustia, "value", 1_i64).unwrap();
rustia
.globals()
.unwrap()
.set_live(&mut rustia, "shared_state", &shared.as_value())
.unwrap();
let module = Rustia::compile_module(
"provisional-install.luau",
"shared_state.value = 99; emit(7); return { answer = shared_state.value }",
)
.unwrap();
let mut host = Host {
input: 0,
slots: Vec::new(),
};
let limits = InvocationLimits::new(10_000)
.with_max_records(2)
.with_max_record_bytes(32);
let mut lane = Vec::new();
let error = rustia
.invoke_into(&mut host, limits, &mut lane, |invocation| {
let _ = invocation.install(&module)?;
Err::<(), _>(Error::embed("reject outer invocation".to_owned()))
})
.unwrap_err();
assert!(error.to_string().contains("reject outer invocation"));
assert!(lane.is_empty());
assert_eq!(
shared
.get(&mut rustia, "value")
.unwrap()
.as_number(&rustia)
.unwrap(),
Some(1.0)
);
let outcome = rustia
.invoke(&mut host, limits, |invocation| invocation.install(&module))
.unwrap();
assert_eq!(outcome.records()[0].kind, RecordKind::Effect(7));
let exports = outcome.into_parts().0;
assert_eq!(
shared
.get(&mut rustia, "value")
.unwrap()
.as_number(&rustia)
.unwrap(),
Some(99.0)
);
let answer = rustia
.invoke(&mut host, limits, |invocation| {
exports.get_in::<_, i64>(invocation, "answer")
})
.unwrap();
assert_eq!(*answer.value(), 99);
}
#[test]
fn caught_failed_require_keeps_completed_guest_writes_and_typed_records() {
let resolver = ServiceModuleResolver::from_fn(|requested, _| match requested {
"bad" => Ok(ModuleSource::new(
"bad",
"emit(7); shared_state.value = 99; error('bad module')",
)),
"good" => Ok(ModuleSource::new("good", "emit(6); return 42")),
_ => Err(Error::embed(format!("unknown module {requested}"))),
});
let mut rustia = Rustia::builder()
.profile(Profile::DeterministicSim)
.module_resolver(resolver)
.build_for_host::<Host>()
.unwrap();
rustia
.add_effect(
"emit",
2,
|mut effects: Effects<'_, Host>, value: i64| -> Result<()> { effects.emit(value) },
)
.unwrap();
let shared = rustia.create_table().unwrap();
shared.set(&mut rustia, "value", 1_i64).unwrap();
rustia
.globals()
.unwrap()
.set_live(&mut rustia, "shared_state", &shared.as_value())
.unwrap();
let module = Rustia::compile_module(
"require-recovery.luau",
r#"
local ok = pcall(require, "bad")
assert(not ok)
local answer = require("good")
emit(8)
return { answer = answer }
"#,
)
.unwrap();
let mut host = Host {
input: 0,
slots: Vec::new(),
};
let outcome = rustia
.invoke(
&mut host,
InvocationLimits::new(100_000)
.with_max_records(4)
.with_max_record_bytes(64),
|invocation| invocation.install(&module),
)
.unwrap();
assert_eq!(
outcome
.records()
.iter()
.map(|record| &record.kind)
.collect::<Vec<_>>(),
[
&RecordKind::Effect(7),
&RecordKind::Effect(6),
&RecordKind::Effect(8)
]
);
assert_eq!(
shared
.get(&mut rustia, "value")
.unwrap()
.as_number(&rustia)
.unwrap(),
Some(99.0)
);
}
#[test]
fn typed_exports_expose_rooted_live_values() {
let mut rustia = configured_vm();
let mut host = Host {
input: 0,
slots: vec![0],
};
let limits = InvocationLimits::new(100_000);
let module = Rustia::compile_module(
"live-exports.luau",
"local shared = { answer = 42 }; return { left = shared, right = shared }",
)
.unwrap();
let exports = rustia
.invoke(&mut host, limits, |invocation| invocation.install(&module))
.unwrap()
.into_parts()
.0;
let (left, right) = rustia
.invoke(&mut host, limits, |invocation| {
Ok((
exports.get_live_in(invocation, "left")?,
exports.get_live_in(invocation, "right")?,
))
})
.unwrap()
.into_parts()
.0;
assert!(left.raw_equal(&mut rustia, &right).unwrap());
}
#[test]
fn failed_host_call_rolls_back_its_suffix_but_pcall_keeps_completed_calls() {
let mut rustia = configured_vm();
rustia
.add_effect(
"emit_then_fail",
2,
|mut effects: Effects<'_, Host>, value: i64| -> Result<()> {
effects.emit(value)?;
Err(Error::embed("expected host failure".to_owned()))
},
)
.unwrap();
let exports = rustia
.install_source(
r#"
return {
decide = function()
emit(1)
local ok = pcall(function() emit_then_fail(2) end)
emit(3)
return ok
end,
}
"#,
)
.unwrap();
let mut host = Host {
input: 0,
slots: vec![],
};
let outcome = rustia
.invoke(
&mut host,
InvocationLimits::new(10_000)
.with_max_records(8)
.with_max_record_bytes(128),
|invocation| {
let decide = exports.function_in(invocation, "decide")?;
decide.call_in::<_, _, bool>(invocation, ())
},
)
.unwrap();
assert!(!*outcome.value());
assert_eq!(
outcome.records(),
[
Record {
sequence: 0,
kind: RecordKind::Effect(1),
},
Record {
sequence: 1,
kind: RecordKind::Effect(3),
},
]
);
}
#[test]
fn record_limit_is_hard_even_if_callback_and_guest_try_to_swallow_it() {
let mut rustia = configured_vm();
rustia
.add_effect(
"ignore_emit_error",
2,
|mut effects: Effects<'_, Host>, value: i64| {
let _ = effects.emit(value);
},
)
.unwrap();
let exports = rustia
.install_source(
r#"
return {
decide = function()
ignore_emit_error(1)
local ok = pcall(function() ignore_emit_error(2) end)
return ok
end,
}
"#,
)
.unwrap();
let mut host = Host {
input: 0,
slots: vec![],
};
let mut lane = vec![Record {
sequence: 99,
kind: RecordKind::Effect(99),
}];
let error = rustia
.invoke_into(
&mut host,
InvocationLimits::new(10_000)
.with_max_records(1)
.with_max_record_bytes(128),
&mut lane,
|invocation| {
let decide = exports.function_in(invocation, "decide")?;
decide.call_in::<_, _, bool>(invocation, ())
},
)
.unwrap_err();
assert!(matches!(
error.kind(),
rustia::ErrorKind::Vm(error)
if matches!(
error.kind(),
VmErrorKind::HostResourceExhausted {
resource: "effect-record count",
limit: 1
}
)
));
assert_eq!(lane.len(), 1);
assert_eq!(lane[0].sequence, 99);
}
#[test]
fn a_completed_effect_inside_guest_pcall_survives_the_caught_guest_error() {
let mut rustia = configured_vm();
let exports = rustia
.install_source(
r#"
return {
decide = function()
local ok = pcall(function()
emit(10)
error("caught")
end)
emit(20)
return ok
end,
}
"#,
)
.unwrap();
let mut host = Host {
input: 0,
slots: vec![],
};
let outcome = rustia
.invoke(
&mut host,
InvocationLimits::new(10_000)
.with_max_records(8)
.with_max_record_bytes(128),
|invocation| {
let decide = exports.function_in(invocation, "decide")?;
decide.call_in::<_, _, bool>(invocation, ())
},
)
.unwrap();
assert!(!*outcome.value());
assert_eq!(
outcome
.records()
.iter()
.map(|record| &record.kind)
.collect::<Vec<_>>(),
[&RecordKind::Effect(10), &RecordKind::Effect(20)]
);
}
#[test]
fn outer_guest_error_and_typed_return_error_publish_nothing() {
let mut rustia = configured_vm();
let exports = rustia
.install_source(
r#"
return {
guest_error = function()
emit(1)
error("uncaught")
end,
wrong_return = function()
emit(2)
return {}
end,
}
"#,
)
.unwrap();
let mut host = Host {
input: 0,
slots: vec![],
};
let prefix = Record {
sequence: 77,
kind: RecordKind::Effect(77),
};
let mut lane = vec![prefix.clone()];
let error = rustia
.invoke_into(
&mut host,
InvocationLimits::new(10_000)
.with_max_records(8)
.with_max_record_bytes(128),
&mut lane,
|invocation| {
let function = exports.function_in(invocation, "guest_error")?;
function.call_in::<_, _, ()>(invocation, ())
},
)
.unwrap_err();
assert!(error.to_string().contains("uncaught"));
assert_eq!(lane.as_slice(), std::slice::from_ref(&prefix));
let outcome = rustia
.invoke(
&mut host,
InvocationLimits::new(10_000)
.with_max_records(8)
.with_max_record_bytes(128),
|invocation| {
let function = exports.function_in(invocation, "wrong_return")?;
let conversion = function.call_in::<_, _, i64>(invocation, ());
assert!(conversion.is_err());
Ok(())
},
)
.unwrap();
assert!(outcome.records().is_empty());
}
#[test]
fn outer_operation_panic_drops_records_and_leaves_the_vm_reusable() {
let mut rustia = configured_vm();
let exports = rustia
.install_source("return { decide = function() emit(9) end }")
.unwrap();
let mut host = Host {
input: 0,
slots: vec![],
};
let limits = InvocationLimits::new(10_000)
.with_max_records(2)
.with_max_record_bytes(32);
let mut lane = Vec::new();
let unwind = catch_unwind(AssertUnwindSafe(|| {
let _ = rustia.invoke_into::<(), _>(&mut host, limits, &mut lane, |invocation| {
exports
.function_in(invocation, "decide")?
.call_in::<_, _, ()>(invocation, ())?;
panic!("outer operation panic");
});
}));
assert!(unwind.is_err());
assert!(lane.is_empty());
let outcome = rustia
.invoke(&mut host, limits, |invocation| {
exports
.function_in(invocation, "decide")?
.call_in::<_, _, ()>(invocation, ())
})
.unwrap();
assert_eq!(
outcome.records(),
[Record {
sequence: 0,
kind: RecordKind::Effect(9),
}]
);
}
#[test]
fn byte_budget_is_checked_before_publication() {
let mut rustia = configured_vm();
let exports = rustia
.install_source("return { decide = function() emit(1); emit(2); return true end }")
.unwrap();
let mut host = Host {
input: 0,
slots: vec![],
};
let error = rustia
.invoke(
&mut host,
InvocationLimits::new(10_000)
.with_max_records(8)
.with_max_record_bytes(31),
|invocation| {
let decide = exports.function_in(invocation, "decide")?;
decide.call_in::<_, _, bool>(invocation, ())
},
)
.unwrap_err();
assert!(matches!(
error.kind(),
rustia::ErrorKind::Vm(error)
if matches!(
error.kind(),
VmErrorKind::HostResourceExhausted {
resource: "effect-record bytes",
limit: 31
}
)
));
}
#[test]
fn fuel_is_shared_and_uncatchable_across_calls_in_one_invocation() {
let mut rustia = configured_vm();
rustia
.add_effect(
"expensive_emit",
40,
|mut effects: Effects<'_, Host>, value: i64| -> Result<()> { effects.emit(value) },
)
.unwrap();
let exports = rustia
.install_source(
r#"
return {
once = function(value)
pcall(expensive_emit, value)
return value
end,
}
"#,
)
.unwrap();
let mut host = Host {
input: 0,
slots: vec![],
};
let error = rustia
.invoke(
&mut host,
InvocationLimits::new(70)
.with_max_records(8)
.with_max_record_bytes(128),
|invocation| {
let once = exports.function_in(invocation, "once")?;
let _: i64 = once.call_in(invocation, (1_i64,))?;
let _: i64 = once.call_in(invocation, (2_i64,))?;
Ok(())
},
)
.unwrap_err();
assert!(matches!(
error.kind(),
rustia::ErrorKind::Vm(error) if matches!(error.kind(), VmErrorKind::FuelExhausted { limit: 70 })
));
}
#[test]
fn recursion_exhaustion_aborts_the_invocation_and_discards_staged_records() {
let mut rustia = rustia::RustiaBuilder::standard()
.profile(Profile::DeterministicSim)
.recursion_limit(8)
.build_for_host::<Host>()
.unwrap();
rustia
.add_effect(
"emit",
1,
|mut effects: Effects<'_, Host>, value: i64| -> Result<()> { effects.emit(value) },
)
.unwrap();
let exports = rustia
.install_source(
"counter=0; local function recur() return string.len('abc',recur()) end; \
return {run=function() emit(7); counter=1; pcall(recur); counter=2; return 2 end, \
counter=function() return counter end}",
)
.unwrap();
let mut host = Host {
input: 0,
slots: vec![],
};
let limits = InvocationLimits::new(100_000)
.with_max_records(8)
.with_max_record_bytes(128);
let prefix = Record {
sequence: 77,
kind: RecordKind::Effect(77),
};
let mut lane = vec![prefix.clone()];
let error = rustia
.invoke_into(&mut host, limits, &mut lane, |invocation| {
let run = exports.function_in(invocation, "run")?;
assert!(run.call_in::<_, _, i64>(invocation, ()).is_err());
Ok(99)
})
.unwrap_err();
assert!(matches!(error.kind(), rustia::ErrorKind::Vm(error)
if matches!(error.kind(), VmErrorKind::RecursionLimit { limit: 8 })));
assert_eq!(lane.as_slice(), std::slice::from_ref(&prefix));
let recovered = rustia
.invoke(&mut host, limits, |invocation| {
let counter = exports.function_in(invocation, "counter")?;
counter.call_in::<_, _, i64>(invocation, ())
})
.unwrap();
assert_eq!(*recovered.value(), 1);
assert!(recovered.records().is_empty());
}
#[test]
fn host_panic_rolls_back_poisons_and_reset_recovers_bindings() {
let mut rustia = configured_vm();
rustia
.add_effect(
"panic_after_emit",
2,
|mut effects: Effects<'_, Host>, value: i64| -> Result<()> {
effects.emit(value)?;
panic!("deliberate host panic")
},
)
.unwrap();
let module = Rustia::compile_module(
"panic.luau",
r#"
return {
panic_now = function() panic_after_emit(5) end,
healthy = function() emit(6); return 42 end,
}
"#,
)
.unwrap();
let exports = rustia.install_typed(&module).unwrap();
let mut host = Host {
input: 0,
slots: vec![],
};
let mut lane = Vec::new();
let error = rustia
.invoke_into(
&mut host,
InvocationLimits::new(10_000)
.with_max_records(8)
.with_max_record_bytes(128),
&mut lane,
|invocation| {
let function = exports.function_in(invocation, "panic_now")?;
function.call_in::<_, _, ()>(invocation, ())
},
)
.unwrap_err();
assert!(matches!(
error.kind(),
rustia::ErrorKind::Vm(error) if matches!(error.kind(), VmErrorKind::HostPanicked { .. })
));
assert!(lane.is_empty());
let poisoned = rustia
.invoke(
&mut host,
InvocationLimits::new(10_000)
.with_max_records(8)
.with_max_record_bytes(128),
|_invocation| Ok(()),
)
.unwrap_err();
assert!(matches!(
poisoned.kind(),
rustia::ErrorKind::Vm(error) if matches!(error.kind(), VmErrorKind::VmPoisoned)
));
rustia.reset_decision().unwrap();
let exports = rustia.install_typed(&module).unwrap();
let outcome = rustia
.invoke(
&mut host,
InvocationLimits::new(10_000)
.with_max_records(8)
.with_max_record_bytes(128),
|invocation| {
let healthy = exports.function_in(invocation, "healthy")?;
healthy.call_in::<_, _, i64>(invocation, ())
},
)
.unwrap();
assert_eq!(*outcome.value(), 42);
assert_eq!(outcome.records()[0].kind, RecordKind::Effect(6));
}
#[test]
fn suspended_coroutine_uses_the_context_of_the_resume_invocation() {
let mut rustia = configured_vm();
let exports = rustia
.install_source(
r#"
local thread = coroutine.create(function()
coroutine.yield("paused")
return read_input(0)
end)
return {
start = function()
local ok, value = coroutine.resume(thread)
return ok and value
end,
finish = function()
local ok, value = coroutine.resume(thread)
return ok and value
end,
}
"#,
)
.unwrap();
let limits = InvocationLimits::new(10_000)
.with_max_records(8)
.with_max_record_bytes(128);
let mut first_host = Host {
input: 11,
slots: vec![],
};
let start = rustia
.invoke(&mut first_host, limits, |invocation| {
let function = exports.function_in(invocation, "start")?;
function.call_in::<_, _, String>(invocation, ())
})
.unwrap();
assert_eq!(start.value(), "paused");
let mut second_host = Host {
input: 22,
slots: vec![],
};
let finish = rustia
.invoke(&mut second_host, limits, |invocation| {
let function = exports.function_in(invocation, "finish")?;
function.call_in::<_, _, i64>(invocation, ())
})
.unwrap();
assert_eq!(*finish.value(), 22);
}
#[test]
fn context_reaches_metamethods_and_accepts_unrelated_block_local_borrows() {
struct BorrowedSchema;
struct BorrowedHost<'a> {
input: &'a i64,
marker: Rc<Cell<usize>>,
external: (),
}
impl HostSchema for BorrowedSchema {
type Input = i64;
type Slots = ();
type External = ();
type Effect = std::convert::Infallible;
type SlotWrite = std::convert::Infallible;
type Record = std::convert::Infallible;
}
impl HostContext<BorrowedSchema> for BorrowedHost<'_> {
fn input(&self) -> &i64 {
self.input
}
fn slots(&self) -> &() {
&()
}
fn external_mut(&mut self) -> &mut () {
&mut self.external
}
fn record_effect(
&self,
_sequence: u32,
effect: std::convert::Infallible,
) -> std::convert::Infallible {
match effect {}
}
fn record_slot_write(
&self,
_sequence: u32,
write: std::convert::Infallible,
) -> std::convert::Infallible {
match write {}
}
fn record_bytes(&self, record: &std::convert::Infallible) -> u64 {
match *record {}
}
}
let marker = Rc::new(Cell::new(0));
let mut rustia = rustia::RustiaBuilder::standard()
.profile(Profile::DeterministicSim)
.build_for_host::<BorrowedSchema>()
.unwrap();
rustia
.add_input("nested_input", 2, |input: Input<'_, BorrowedSchema>| {
*input.get()
})
.unwrap();
let module = Rustia::compile_module(
"borrowed-context.luau",
r#"
local value = setmetatable({}, {
__add = function() return nested_input() end,
})
return { decide = function() return value + value end }
"#,
)
.unwrap();
let exports = {
let source = 37;
let mut host = BorrowedHost {
input: &source,
marker: Rc::clone(&marker),
external: (),
};
let exports = rustia
.invoke(&mut host, InvocationLimits::new(10_000), |invocation| {
invocation.install(&module)
})
.unwrap()
.into_parts()
.0;
let outcome = rustia
.invoke(&mut host, InvocationLimits::new(10_000), |invocation| {
let decide = exports.function_in(invocation, "decide")?;
decide.call_in::<_, _, i64>(invocation, ())
})
.unwrap();
assert_eq!(*outcome.value(), 37);
assert_eq!(host.marker.get(), 0);
exports
};
{
let unrelated_source = -11;
let mut unrelated_host = BorrowedHost {
input: &unrelated_source,
marker: Rc::clone(&marker),
external: (),
};
let outcome = rustia
.invoke(
&mut unrelated_host,
InvocationLimits::new(10_000),
|invocation| {
let decide = exports.function_in(invocation, "decide")?;
decide.call_in::<_, _, i64>(invocation, ())
},
)
.unwrap();
assert_eq!(*outcome.value(), -11);
}
}
#[test]
fn pure_host_panic_is_caught_rolls_back_and_poisons() {
let mut rustia = configured_vm();
rustia
.add_pure("pure_panic", 2, || -> i64 { panic!("pure callback panic") })
.unwrap();
let exports = rustia
.install_source(
r#"
return {
decide = function()
emit(1)
pcall(pure_panic)
emit(2)
end,
}
"#,
)
.unwrap();
let mut host = Host {
input: 0,
slots: Vec::new(),
};
let mut lane = Vec::new();
let error = rustia
.invoke_into(
&mut host,
InvocationLimits::new(10_000)
.with_max_records(8)
.with_max_record_bytes(128),
&mut lane,
|invocation| {
let decide = exports.function_in(invocation, "decide")?;
decide.call_in::<_, _, ()>(invocation, ())
},
)
.unwrap_err();
assert!(matches!(
error.kind(),
rustia::ErrorKind::Vm(error)
if matches!(
error.kind(),
VmErrorKind::HostPanicked { function }
if function == "pure_panic"
)
));
assert!(lane.is_empty());
assert!(matches!(
(rustia
.invoke(
&mut host,
InvocationLimits::new(10_000),
|_invocation| Ok(()),
)
.unwrap_err()).kind(),
rustia::ErrorKind::Vm(error) if matches!(error.kind(), VmErrorKind::VmPoisoned)
));
}
#[test]
fn direct_host_panic_poisoning_is_enforced_by_the_vm_core() {
let mut rustia = rustia::RustiaBuilder::standard()
.profile(Profile::DeterministicSim)
.build()
.unwrap();
rustia
.add_fn("panic_now", || -> i64 { panic!("direct callback panic") })
.unwrap();
assert!(matches!(
(rustia.eval::<()>("panic_now()").unwrap_err()).kind(),
rustia::ErrorKind::Vm(error)
if matches!(error.kind(), VmErrorKind::HostPanicked { .. })
));
assert!(matches!(
(rustia.eval::<i64>("return 1").unwrap_err()).kind(),
rustia::ErrorKind::Vm(error) if matches!(error.kind(), VmErrorKind::VmPoisoned)
));
assert!(matches!(
(rustia
.invoke(&mut (), InvocationLimits::new(0), |_invocation| Ok(()))
.unwrap_err()).kind(),
rustia::ErrorKind::Vm(error) if matches!(error.kind(), VmErrorKind::VmPoisoned)
));
rustia.reset_decision().unwrap();
assert_eq!(rustia.eval::<i64>("return 1").unwrap(), 1);
rustia
.invoke(&mut (), InvocationLimits::new(0), |_invocation| Ok(()))
.unwrap();
}
#[test]
fn host_service_panic_rolls_back_records_poisons_and_leaves_reset_usable() {
let mut rustia = Rustia::builder()
.profile(Profile::DeterministicSim)
.stdout(|_| panic!("stdout service panic"))
.build_for_host::<Host>()
.unwrap();
rustia
.add_effect(
"emit",
2,
|mut effects: Effects<'_, Host>, value: i64| -> Result<()> { effects.emit(value) },
)
.unwrap();
let module = Rustia::compile_module(
"service-panic.luau",
r#"
return {
decide = function()
emit(1)
print("panic")
emit(2)
end,
}
"#,
)
.unwrap();
let mut host = Host {
input: 0,
slots: Vec::new(),
};
let installed = rustia
.invoke(&mut host, InvocationLimits::new(10_000), |invocation| {
invocation.install(&module)
})
.unwrap();
let (exports, _, _) = installed.into_parts();
let mut lane = Vec::new();
let error = rustia
.invoke_into(
&mut host,
InvocationLimits::new(10_000)
.with_max_records(2)
.with_max_record_bytes(32),
&mut lane,
|invocation| {
exports
.function_in(invocation, "decide")?
.call_in::<_, _, ()>(invocation, ())
},
)
.unwrap_err();
assert!(matches!(
error.kind(),
rustia::ErrorKind::Vm(error)
if matches!(
error.kind(),
VmErrorKind::HostPanicked { function }
if function == "service::stdout"
)
));
assert!(lane.is_empty());
assert!(matches!(
(rustia
.invoke(&mut host, InvocationLimits::new(0), |_invocation| Ok(()))
.unwrap_err()).kind(),
rustia::ErrorKind::Vm(error) if matches!(error.kind(), VmErrorKind::VmPoisoned)
));
let mut poison_probe = Vec::new();
assert!(matches!(
(rustia
.invoke_into(
&mut host,
InvocationLimits::new(0).with_max_records(4),
&mut poison_probe,
|_invocation| Ok(()),
)
.unwrap_err()).kind(),
rustia::ErrorKind::Vm(error) if matches!(error.kind(), VmErrorKind::VmPoisoned)
));
assert_eq!(poison_probe.capacity(), 0);
rustia.reset_decision().unwrap();
rustia
.invoke(&mut host, InvocationLimits::new(0), |_invocation| Ok(()))
.unwrap();
}
#[test]
fn typed_builder_preserves_profile_validation() {
let mut rustia = Rustia::builder()
.profile(Profile::DeterministicSim)
.fuel(7)
.build_for_host::<Host>()
.unwrap();
assert!(matches!(
(rustia.add_external(
"external",
1,
|mut host: HostMut<'_, Host>| host.get_mut().input
)).as_ref().err().map(|error| error.kind()),
Some(rustia::ErrorKind::Vm(error))
if matches!(
error.kind(),
VmErrorKind::CapabilityRemoved {
feature: "host function reading external mutable state",
operation: Some(ref operation),
profile: Profile::DeterministicSim,
} if operation == "external"
)
));
let mut host = Host {
input: 0,
slots: Vec::new(),
};
let outcome = rustia
.invoke(&mut host, InvocationLimits::new(100), |invocation| {
Ok(invocation.fuel_remaining())
})
.unwrap();
assert_eq!(*outcome.value(), 7);
}
#[test]
fn invocation_run_compiles_source_with_the_vm_profile() {
let mut rustia = rustia::RustiaBuilder::standard()
.profile(Profile::Puc51Compat)
.build_for_host::<Host>()
.unwrap();
let mut host = Host {
input: 0,
slots: Vec::new(),
};
let outcome = rustia
.invoke(&mut host, InvocationLimits::new(10_000), |invocation| {
invocation
.load("local t = {1, [1] = 9} return t[1]")
.source_name("puc-invocation.luau")
.eval::<f64>()
})
.unwrap();
assert_eq!(*outcome.value(), 1.0);
}
#[test]
fn typed_host_modules_are_inspectable_atomic_and_persist_across_reset() {
let mut rustia = rustia::RustiaBuilder::standard()
.profile(Profile::DeterministicSim)
.build_for_host::<Host>()
.unwrap();
let rejected = HostModule::<Host>::new("rejected")
.input("input", 2, |input: Input<'_, Host>| *input.get())
.external("mutate", 2, |mut host: HostMut<'_, Host>| {
host.get_mut().input += 1;
});
let error = rustia.install_host_module(rejected).unwrap_err();
assert!(matches!(
error.kind(),
rustia::ErrorKind::Vm(error)
if matches!(
error.kind(),
VmErrorKind::CapabilityRemoved {
feature: "host function reading external mutable state",
operation: Some(operation),
profile: Profile::DeterministicSim,
} if operation == "rejected.mutate"
)
));
let module = HostModule::<Host>::new("world")
.pure("double", 1, |value: i64| value * 2)
.input("input", 2, |input: Input<'_, Host>| *input.get())
.effect(
"emit",
3,
|mut effects: Effects<'_, Host>, value: i64| -> Result<()> { effects.emit(value) },
);
assert_eq!(module.namespace(), "world");
let metadata = module
.functions()
.map(|function| {
(
function.name.to_owned(),
function.fuel_cost,
function.capability,
function.parameter_count,
)
})
.collect::<Vec<_>>();
assert_eq!(
metadata,
[
("double".to_owned(), 1, OpCapability::Pure, 1),
(
"input".to_owned(),
2,
OpCapability::ReadsDeterministicInput,
0,
),
(
"emit".to_owned(),
3,
OpCapability::EmitsDeterministicEffect,
1,
),
]
);
rustia.install_host_module(module).unwrap();
let source = r#"
return {
decide = function()
local value = world.double(world.input())
world.emit(value)
return value
end,
}
"#;
let exports = rustia.install_source(source).unwrap();
let mut host = Host {
input: 21,
slots: Vec::new(),
};
let outcome = rustia
.invoke(
&mut host,
InvocationLimits::new(10_000)
.with_max_records(2)
.with_max_record_bytes(32),
|invocation| {
let decide = exports.function_in(invocation, "decide")?;
decide.call_in::<_, _, i64>(invocation, ())
},
)
.unwrap();
assert_eq!(*outcome.value(), 42);
assert_eq!(outcome.records()[0].kind, RecordKind::Effect(42));
rustia.reset_decision().unwrap();
let exports = rustia.install_source(source).unwrap();
host.input = 7;
let outcome = rustia
.invoke(
&mut host,
InvocationLimits::new(10_000)
.with_max_records(2)
.with_max_record_bytes(32),
|invocation| {
let decide = exports.function_in(invocation, "decide")?;
decide.call_in::<_, _, i64>(invocation, ())
},
)
.unwrap();
assert_eq!(*outcome.value(), 14);
}
#[test]
fn mutable_external_context_is_profile_gated() {
let mut deterministic = rustia::RustiaBuilder::standard()
.profile(Profile::DeterministicSim)
.build_for_host::<Host>()
.unwrap();
let rejected = deterministic.add_external("increment", 2, |mut host: HostMut<'_, Host>| {
host.get_mut().input += 1;
host.get_mut().input
});
assert!(matches!(
(rejected.unwrap_err()).kind(),
rustia::ErrorKind::Vm(error)
if matches!(error.kind(), VmErrorKind::CapabilityRemoved { .. })
));
let mut standard = rustia::RustiaBuilder::standard()
.profile(Profile::ReproducibleStandard)
.build_for_host::<Host>()
.unwrap();
standard
.add_external("increment", 2, |mut host: HostMut<'_, Host>| {
host.get_mut().input += 1;
host.get_mut().input
})
.unwrap();
let exports = standard
.install_source("return { decide = function() return increment() end }")
.unwrap();
let mut host = Host {
input: 5,
slots: Vec::new(),
};
let outcome = standard
.invoke(&mut host, InvocationLimits::new(10_000), |invocation| {
exports
.function_in(invocation, "decide")?
.call_in::<_, _, i64>(invocation, ())
})
.unwrap();
assert_eq!(*outcome.value(), 6);
assert_eq!(host.input, 6);
}
#[test]
fn exact_limits_publish_without_growing_a_preallocated_lane_and_report_usage() {
let mut rustia = configured_vm();
let exports = rustia
.install_source("return { decide = function() emit(1); emit(2); return 7 end }")
.unwrap();
let mut host = Host {
input: 0,
slots: Vec::new(),
};
let mut lane = Vec::with_capacity(3);
lane.push(Record {
sequence: 99,
kind: RecordKind::Effect(99),
});
let allocation = lane.as_ptr();
let receipt = rustia
.invoke_into(
&mut host,
InvocationLimits::new(10_000)
.with_max_records(2)
.with_max_record_bytes(32),
&mut lane,
|invocation| {
let decide = exports.function_in(invocation, "decide")?;
decide.call_in::<_, _, i64>(invocation, ())
},
)
.unwrap();
assert_eq!(*receipt.value(), 7);
assert_eq!(receipt.record_count(), 2);
assert_eq!(receipt.record_bytes(), 32);
assert!(receipt.fuel_used() > 0);
assert_eq!(lane.as_ptr(), allocation);
assert_eq!(lane.len(), 3);
assert_eq!(lane[1].sequence, 0);
assert_eq!(lane[2].sequence, 1);
}
#[test]
fn a_soft_failure_in_a_later_rust_call_rolls_back_only_that_call() {
let mut rustia = configured_vm();
let exports = rustia
.install_source(
r#"
return {
first = function() emit(1) end,
second = function() emit(2); error("second failed") end,
}
"#,
)
.unwrap();
let mut host = Host {
input: 0,
slots: Vec::new(),
};
let outcome = rustia
.invoke(
&mut host,
InvocationLimits::new(10_000)
.with_max_records(4)
.with_max_record_bytes(64),
|invocation| {
exports
.function_in(invocation, "first")?
.call_in::<_, _, ()>(invocation, ())?;
let error = exports
.function_in(invocation, "second")?
.call_in::<_, _, ()>(invocation, ())
.unwrap_err();
assert!(error.to_string().contains("second failed"));
Ok(())
},
)
.unwrap();
assert_eq!(
outcome.records(),
[Record {
sequence: 0,
kind: RecordKind::Effect(1),
}]
);
}
#[test]
fn invocation_run_conversion_failure_rolls_back_module_main_records() {
let mut rustia = configured_vm();
let mut host = Host {
input: 0,
slots: Vec::new(),
};
let outcome = rustia
.invoke(
&mut host,
InvocationLimits::new(10_000)
.with_max_records(4)
.with_max_record_bytes(64),
|invocation| {
let conversion = invocation
.load("emit(9); return {}")
.source_name("bad-main.luau")
.eval::<i64>();
assert!(conversion.is_err());
Ok(())
},
)
.unwrap();
assert!(outcome.records().is_empty());
}
#[test]
fn transactional_resource_errors_are_hard_without_changing_legacy_pcall() {
let mut legacy = rustia::RustiaBuilder::standard()
.profile(Profile::DeterministicSim)
.build()
.unwrap();
legacy
.add_fn("oom", || -> Result<()> {
Err(Error::from(VmError::new(VmErrorKind::OutOfMemory)))
})
.unwrap();
let caught: bool = legacy.eval("local ok = pcall(oom); return ok").unwrap();
assert!(!caught);
let mut rustia = configured_vm();
rustia
.add_pure("oom", 2, || -> Result<()> {
Err(Error::from(VmError::new(VmErrorKind::OutOfMemory)))
})
.unwrap();
let exports = rustia
.install_source("return { decide = function() pcall(oom); emit(1) end }")
.unwrap();
let mut host = Host {
input: 0,
slots: Vec::new(),
};
let error = rustia
.invoke(
&mut host,
InvocationLimits::new(10_000)
.with_max_records(2)
.with_max_record_bytes(32),
|invocation| {
exports
.function_in(invocation, "decide")?
.call_in::<_, _, ()>(invocation, ())
},
)
.unwrap_err();
assert!(matches!(
error.kind(),
rustia::ErrorKind::Vm(error) if matches!(error.kind(), VmErrorKind::OutOfMemory)
));
}
#[test]
fn resumed_protected_coroutine_cannot_swallow_a_hard_yield_boundary() {
let mut rustia = configured_vm();
let exports = rustia
.install_source(
r#"
local object = setmetatable({}, {
__index = function()
coroutine.yield("illegal")
end,
})
local thread = coroutine.create(function()
local ok, value = pcall(function()
coroutine.yield("paused")
return object.missing
end)
return ok, value
end)
return {
resume = function()
return coroutine.resume(thread)
end,
}
"#,
)
.unwrap();
let mut host = Host {
input: 0,
slots: Vec::new(),
};
let first = rustia
.invoke(&mut host, InvocationLimits::new(10_000), |invocation| {
exports
.function_in(invocation, "resume")?
.call_in::<_, _, (bool, String)>(invocation, ())
})
.unwrap();
assert_eq!(first.value(), &(true, "paused".to_owned()));
let error = rustia
.invoke(&mut host, InvocationLimits::new(10_000), |invocation| {
exports
.function_in(invocation, "resume")?
.call_in::<_, _, ()>(invocation, ())
})
.unwrap_err();
assert!(matches!(
error.kind(),
rustia::ErrorKind::Vm(error)
if matches!(error.kind(), VmErrorKind::YieldAcrossBoundary)
));
}
#[test]
fn resumed_xpcall_coroutine_cannot_swallow_a_hard_yield_boundary() {
let mut rustia = configured_vm();
let exports = rustia
.install_source(
r#"
local object = setmetatable({}, {
__index = function()
coroutine.yield("illegal")
end,
})
local thread = coroutine.create(function()
local ok, value = xpcall(function()
coroutine.yield("paused")
return object.missing
end, function(error)
return error
end)
return ok, value
end)
return {
resume = function()
return coroutine.resume(thread)
end,
}
"#,
)
.unwrap();
let mut host = Host {
input: 0,
slots: Vec::new(),
};
let first = rustia
.invoke(&mut host, InvocationLimits::new(10_000), |invocation| {
exports
.function_in(invocation, "resume")?
.call_in::<_, _, (bool, String)>(invocation, ())
})
.unwrap();
assert_eq!(first.value(), &(true, "paused".to_owned()));
let error = rustia
.invoke(&mut host, InvocationLimits::new(10_000), |invocation| {
exports
.function_in(invocation, "resume")?
.call_in::<_, _, ()>(invocation, ())
})
.unwrap_err();
assert!(matches!(
error.kind(),
rustia::ErrorKind::Vm(error)
if matches!(error.kind(), VmErrorKind::YieldAcrossBoundary)
));
}
#[test]
fn deterministic_oom_escapes_pcall_and_discards_the_outer_journal() {
let mut rustia = rustia::RustiaBuilder::deterministic()
.memory_limit_bytes(1_000_000)
.build_for_host::<Host>()
.unwrap();
rustia
.add_effect(
"emit",
2,
|mut effects: Effects<'_, Host>, value: i64| -> Result<()> { effects.emit(value) },
)
.unwrap();
let exports = rustia
.install_source(
r#"
return {
decide = function()
emit(1)
pcall(function()
local values = {}
for i = 1, 20000 do
values[i] = string.rep("x", 100) .. i
end
end)
emit(2)
return true
end,
}
"#,
)
.unwrap();
let mut host = Host {
input: 0,
slots: vec![],
};
let mut lane = Vec::new();
let error = rustia
.invoke_into(
&mut host,
InvocationLimits::new(10_000_000)
.with_max_records(8)
.with_max_record_bytes(128),
&mut lane,
|invocation| {
let decide = exports.function_in(invocation, "decide")?;
decide.call_in::<_, _, bool>(invocation, ())
},
)
.unwrap_err();
assert!(
matches!(
error.kind(),
rustia::ErrorKind::Vm(error) if matches!(error.kind(), VmErrorKind::OutOfMemory)
),
"unexpected deterministic OOM error: {error:?}"
);
assert!(
lane.is_empty(),
"OOM must not publish earlier staged effects"
);
}