d6655d8c10
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>
97 lines
3.5 KiB
Rust
97 lines
3.5 KiB
Rust
//! Long and deep source on a 2 MB thread: a left-nested chain of any length compiles and runs,
|
|
//! as 5.1's parser reads it in a loop, and deep nesting stops at 5.1's level limit with its
|
|
//! message (or, for loops, at its limit on locals). Neither may overflow the Rust stack.
|
|
|
|
fn on_small_stack(f: impl FnOnce() + Send + 'static) {
|
|
std::thread::Builder::new()
|
|
.stack_size(2 << 20)
|
|
.spawn(f)
|
|
.unwrap()
|
|
.join()
|
|
.unwrap();
|
|
}
|
|
|
|
/// The chunk's first result, or its error.
|
|
fn run(src: String) -> String {
|
|
let mut vm = benilla_lua::new_state();
|
|
vm.exec(
|
|
b"function f() return f end m = { m = function(self) return self end }",
|
|
b"=setup",
|
|
)
|
|
.unwrap();
|
|
match vm.exec(src.as_bytes(), b"=t") {
|
|
Ok(v) => v.first().map_or("none".into(), |&v| vm.display(v)),
|
|
Err(e) => vm.display(e),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn left_nested_chains_compile_at_any_length() {
|
|
on_small_stack(|| {
|
|
let cases = [
|
|
(
|
|
format!("local x = f{} return type(x)", "()".repeat(30000)),
|
|
"function",
|
|
),
|
|
(
|
|
format!("local x = 1{} return x", " and 1".repeat(40000)),
|
|
"1",
|
|
),
|
|
(format!("return m{}", ":m()".repeat(20000)), "table"),
|
|
(format!("return 1{}", "+1".repeat(200000)), "200001"),
|
|
(
|
|
format!("return {}false", "false or ".repeat(49999)),
|
|
"false",
|
|
),
|
|
(
|
|
format!("local t = {{}} t.a = t return t{} == t", ".a".repeat(50000)),
|
|
"true",
|
|
),
|
|
(format!("local x = 2 return x{}", "*x".repeat(30000)), "inf"),
|
|
(format!("return 1 == 1{}", " == true".repeat(30000)), "true"),
|
|
];
|
|
for (src, want) in cases {
|
|
assert_eq!(run(src.clone()), want, "{}", &src[..40]);
|
|
}
|
|
});
|
|
}
|
|
|
|
#[test]
|
|
fn deep_nesting_stops_at_the_level_limit() {
|
|
on_small_stack(|| {
|
|
let n = 100000;
|
|
let cases = [
|
|
format!("return {}1{}", "(".repeat(n), ")".repeat(n)),
|
|
format!("return {}1", "not ".repeat(n)),
|
|
format!("return {}1", "- ".repeat(n)),
|
|
format!("return 1{}", " .. 1".repeat(n)),
|
|
format!("return 2{}", " ^ 2".repeat(n)),
|
|
format!("return {}1{}", "{".repeat(n), "}".repeat(n)),
|
|
format!("return {}1{}", "f(".repeat(n), ")".repeat(n)),
|
|
format!("return {}1{}", "m[".repeat(n), "]".repeat(n)),
|
|
format!("{}{}", "do ".repeat(n), "end ".repeat(n)),
|
|
format!("{}{}", "if x then ".repeat(n), "end ".repeat(n)),
|
|
format!("{}{}", "while x do ".repeat(n), "end ".repeat(n)),
|
|
format!("{}{}", "repeat ".repeat(n), "until x ".repeat(n)),
|
|
format!("{}{}", "for i = 1, 2 do ".repeat(n), "end ".repeat(n)),
|
|
format!("{}{}", "for k in x do ".repeat(n), "end ".repeat(n)),
|
|
format!(
|
|
"return {}1{}",
|
|
"function() return ".repeat(n),
|
|
" end".repeat(n)
|
|
),
|
|
format!("{}{}", "local function g() ".repeat(n), "end ".repeat(n)),
|
|
];
|
|
for src in cases {
|
|
let got = run(src.clone());
|
|
// Four locals a loop reach 5.1's 200 before the levels do.
|
|
let locals = src.starts_with("for") && got.ends_with("more than 200 local variables");
|
|
assert!(
|
|
got.ends_with("chunk has too many syntax levels") || locals,
|
|
"{}: {got}",
|
|
&src[..40]
|
|
);
|
|
}
|
|
});
|
|
}
|