Files
moonwell-client-core/crates/benilla-lua/tests/handles.rs
T
Mathih13 d6655d8c10 benilla-lua: benilla's own 1.12 Lua VM, opt-in behind benilla-vm (#1254)
benilla-lua is a Lua 5.1.5 interpreter in Rust that speaks the 1.12.1 client's grammar. It is opt-in behind the `benilla-vm` feature, with `benilla-mlua`, the slice of mlua that benilla-ui uses, over it. The C build stays the default. Its front end ports 5.1.5's `lparser.c` and `lcode.c`, so registers, limits and messages match the C build's.

Our finish on top:
- the collector counts growth as C does, off the write path;
- frames under a leaky generic `for` start nil, as `luaD_precall`;
- the budget hook counts what C dispatches, and costs nothing when unset;
- `__index` chains stop at 100;
- mlua 0.11's conversions;
- `vm_parity` answers recorded per platform.

Tests: `vm_parity` on both VMs; the front-end corpus (8,543 chunks) against `string.dump`; 7,613 app and UI tests on benilla-lua. Built on macOS; Linux by CI.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Sam <samuv.eth@gmail.com>
2026-10-16 16:16:09 +07:00

57 lines
1.7 KiB
Rust

//! A heap handle is a plain index anyone can build. One to no live object, stale or made up,
//! reads some object of the right type in a release build and panics in a debug one; it never
//! reads freed or out-of-bounds memory.
use benilla_lua::heap::Heap;
use benilla_lua::value::{FuncRef, StrRef, TableRef, UserRef};
/// `f`'s answer, or `None` where a debug build's check panicked.
fn read<T>(f: impl FnOnce() -> T) -> Option<T> {
let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(f));
assert!(
r.is_ok() || cfg!(debug_assertions),
"only a debug build's handle check panics"
);
r.ok()
}
#[test]
fn a_made_up_handle_reads_no_memory_outside_the_heap() {
for i in [0, 1, 63, 64, 1 << 20, u32::MAX] {
let heap = Heap::default();
read(|| heap.table(TableRef(i)).meta.is_some());
read(|| {
matches!(
heap.func(FuncRef(i)),
benilla_lua::heap::FuncObj::Native { .. }
)
});
read(|| heap.user(UserRef(i)).tag);
read(|| heap.upval(i));
read(|| {
heap.str(StrRef {
id: i & 0xff_ffff,
hash: 0,
})
.len()
});
}
}
#[test]
fn a_stale_handle_reads_no_freed_memory() {
let mut vm = benilla_lua::new_state();
let t = vm.heap.new_table(0, 4);
let k = vm.str(b"key");
vm.heap.table_mut(t).set(k, k).unwrap();
vm.full_gc();
// Nothing held the table: its slot is free, then taken by others.
let heap = vm.heap.clone();
read(move || heap.table(t).meta.is_some());
for _ in 0..100 {
vm.heap.new_table(0, 0);
}
let heap = vm.heap.clone();
read(move || heap.table(t).meta.is_some());
}