Files
moonwell-client-core/crates/benilla-lua/tests/unwind.rs
T
Sam Sung a04df2d17d benilla-lua's VM and compiler behave as the client's Lua 5.0 (#1768)
benilla-lua now matches the client's Lua 5.0 wherever an addon can see the difference:
- Number keys hash as `hashnum` does (`0x6fa260`), so `pairs` walks them in the client's order.
- Calls nest at most 4096 deep (`0x6f61c0`).
- `^` calls the global `__pow` (`0x6f9b5e`).
- The parser's limits and messages are the client's (`0x6ff440`); the client has no assignment limit.
- A numeric for's variable is its counter (`0x6fe380`, `0x6f9262`).
- A generic for uses `forlist`'s layout (`0x6fe540`): the first name is the control, and closures share the variables.
- Only calls name a callee (`0x6fc499`), and a stray token reports `<eof> expected` (`0x871cf4`).

Each fix has a test that fails without it. Two audits re-derived every address from the bytes. The bench is faster or level.

Fixes #1369
Fixes #1370
Fixes #1371
Fixes #1372
Fixes #1374
Fixes #1377
Fixes #1752
Fixes #1753
Fixes #1754

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

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-17 11:07:05 +07:00

73 lines
2.9 KiB
Rust

//! A Rust panic that unwinds out of the VM leaves it as it was when the host called in.
fn boom(_: &mut benilla_lua::Vm, _: usize) -> Result<usize, benilla_lua::LuaError> {
panic!("a native broke")
}
#[test]
fn a_panic_through_the_vm_leaves_it_usable() {
let mut vm = benilla_lua::new_state();
vm.register("boom", boom);
vm.exec(
b"function deep(n) if n == 0 then return boom() end return deep(n - 1) + 1 end",
b"=t",
)
.unwrap();
for _ in 0..300 {
let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
let _ = vm.exec(b"local t = setmetatable({}, {__index = function() return deep(20) end}) return t.x", b"=t");
}));
assert!(r.is_err());
}
// No frame, depth or handler leaked: a chunk still compiles and deep calls still run.
let r = vm
.exec(b"local function f(n) if n == 0 then return 0 end return 1 + f(n - 1) end return f(4000)", b"=t")
.unwrap();
assert_eq!(vm.display(r[0]), "4000");
let caught = vm.exec(b"return pcall(error, 'x')", b"=t").unwrap();
assert_eq!(vm.display(caught[0]), "false");
}
/// A panic that passes a protected call leaves its handler behind no more: a native that
/// catches the panic and raises runs no handler of a call that already ended.
#[test]
fn a_panic_through_xpcall_leaves_no_stale_handler() {
fn boom(_: &mut benilla_lua::Vm, _: usize) -> Result<usize, benilla_lua::LuaError> {
panic!("boom");
}
fn catcher(vm: &mut benilla_lua::Vm, nargs: usize) -> Result<usize, benilla_lua::LuaError> {
let f = vm.arg(nargs, 0);
let r =
std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| vm.pcall_value(f, &[], 0)));
assert!(r.is_err(), "the panic reached the catcher");
Err(vm.rt_error("after"))
}
let mut vm = benilla_lua::new_state();
vm.register("boom", boom);
vm.register("catcher", catcher);
let r = vm
.exec(
b"local ok, e = pcall(catcher, function() xpcall(boom, function(m) Hit = true return m end) end) \
return ok, e, Hit == nil",
b"=t",
)
.unwrap();
let got: Vec<String> = r.into_iter().map(|v| vm.display(v)).collect();
assert_eq!(got, ["false", "after", "true"]);
}
/// A type error the host's own call or index raises, with no Lua frame running, names no
/// variable, a register given or not: there is no function whose register it could be.
#[test]
fn a_type_error_with_no_frame_running_names_no_variable() {
use benilla_lua::value::Value;
let mut vm = benilla_lua::new_state();
let e = vm.pcall_value(Value::Bool(true), &[], 0).unwrap_err();
assert_eq!(vm.display(e), "attempt to call a boolean value");
let key = Value::Str(vm.heap.intern(b"x"));
for reg in [None, Some(0)] {
let e = vm.index(Value::Bool(true), key, reg).unwrap_err().value;
assert_eq!(vm.display(e), "attempt to index a boolean value");
}
}