luaur-rt 0.1.8

Safe, ergonomic, mlua-style API for luaur (pure-Rust Luau).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! Integration-style unit tests exercising the mlua-style API end to end.

use crate::prelude::*;
// `Arc<AtomicI64>` (rather than `Rc<Cell<i64>>`) so this capturing-closure test
// also compiles under the `send` feature, where `create_function`'s closure
// must be `Send`. Behaviorally identical in the single-threaded default build.
use std::sync::atomic::{AtomicI64, Ordering};
use std::sync::Arc;

#[test]
fn create_function_and_call_from_lua() {
    let lua = Lua::new();
    let add = lua
        .create_function(|_, (a, b): (i64, i64)| Ok(a + b))
        .unwrap();
    lua.globals().set("add", add).unwrap();
    let result: i64 = lua.load("return add(2, 3)").eval().unwrap();
    assert_eq!(result, 5);
}

#[test]
fn capturing_closure_counter() {
    let lua = Lua::new();
    let counter = Arc::new(AtomicI64::new(0));
    let c2 = counter.clone();
    let inc = lua
        .create_function(move |_, ()| Ok(c2.fetch_add(1, Ordering::SeqCst) + 1))
        .unwrap();
    lua.globals().set("inc", inc).unwrap();
    lua.load("inc(); inc(); inc()").exec().unwrap();
    assert_eq!(counter.load(Ordering::SeqCst), 3);
    let last: i64 = lua.load("return inc()").eval().unwrap();
    assert_eq!(last, 4);
    assert_eq!(counter.load(Ordering::SeqCst), 4);
}

#[test]
fn scalar_string_bool_option_round_trips() {
    let lua = Lua::new();

    let i: i64 = lua.load("return 42").eval().unwrap();
    assert_eq!(i, 42);

    let f: f64 = lua.load("return 3.5").eval().unwrap();
    assert_eq!(f, 3.5);

    let s: String = lua.load("return 'hello'").eval().unwrap();
    assert_eq!(s, "hello");

    let b: bool = lua.load("return true").eval().unwrap();
    assert!(b);

    let none: Option<i64> = lua.load("return nil").eval().unwrap();
    assert_eq!(none, None);

    let some: Option<i64> = lua.load("return 7").eval().unwrap();
    assert_eq!(some, Some(7));

    // Round-trip through a Rust function: identity on each type.
    let id_i = lua.create_function(|_, x: i64| Ok(x)).unwrap();
    lua.globals().set("id_i", id_i).unwrap();
    let back: i64 = lua.load("return id_i(123)").eval().unwrap();
    assert_eq!(back, 123);

    let id_s = lua.create_function(|_, x: String| Ok(x)).unwrap();
    lua.globals().set("id_s", id_s).unwrap();
    let back_s: String = lua.load("return id_s('world')").eval().unwrap();
    assert_eq!(back_s, "world");
}

#[test]
fn table_set_get_nested() {
    let lua = Lua::new();
    let t = lua.create_table();
    t.set("a", 1i64).unwrap();
    t.set("b", "two").unwrap();
    let inner = lua.create_table();
    inner.set("x", 10i64).unwrap();
    t.set("inner", inner).unwrap();

    let a: i64 = t.get("a").unwrap();
    assert_eq!(a, 1);
    let b: String = t.get("b").unwrap();
    assert_eq!(b, "two");
    assert!(t.contains_key("a").unwrap());
    assert!(!t.contains_key("missing").unwrap());

    let inner: Table = t.get("inner").unwrap();
    let x: i64 = inner.get("x").unwrap();
    assert_eq!(x, 10);
}

#[test]
fn vec_to_and_from_table() {
    let lua = Lua::new();
    // Vec -> table -> Lua, summed in Lua, back to Rust.
    let v = vec![1i64, 2, 3, 4];
    lua.globals().set("nums", v.clone()).unwrap();
    let len: i64 = lua.load("return #nums").eval().unwrap();
    assert_eq!(len, 4);
    let sum: i64 = lua
        .load("local s = 0; for _, n in ipairs(nums) do s = s + n end; return s")
        .eval()
        .unwrap();
    assert_eq!(sum, 10);

    // Lua sequence -> Rust Vec.
    let back: Vec<i64> = lua.load("return {10, 20, 30}").eval().unwrap();
    assert_eq!(back, vec![10, 20, 30]);
}

#[test]
fn table_pairs_iteration() {
    let lua = Lua::new();
    let t = lua.create_table();
    t.set("a", 1i64).unwrap();
    t.set("b", 2i64).unwrap();
    t.set("c", 3i64).unwrap();
    let mut sum = 0i64;
    let mut keys = Vec::new();
    for pair in t.pairs::<String, i64>() {
        let (k, v) = pair.unwrap();
        keys.push(k);
        sum += v;
    }
    keys.sort();
    assert_eq!(keys, vec!["a", "b", "c"]);
    assert_eq!(sum, 6);
}

#[test]
fn call_lua_function_from_rust() {
    let lua = Lua::new();
    let doubler: Function = lua
        .load("return function(x) return x * 2 end")
        .eval()
        .unwrap();
    let r: i64 = doubler.call(21i64).unwrap();
    assert_eq!(r, 42);
}

#[test]
fn rust_error_is_catchable_by_lua_pcall() {
    let lua = Lua::new();
    let boom = lua
        .create_function(|_, ()| -> Result<()> { Err(Error::runtime("kaboom")) })
        .unwrap();
    lua.globals().set("boom", boom).unwrap();
    // pcall should catch the error and report ok=false plus the message.
    let msg: String = lua
        .load("local ok, err = pcall(boom); assert(not ok); return tostring(err)")
        .eval()
        .unwrap();
    assert!(
        msg.contains("kaboom"),
        "expected message to contain kaboom: {msg}"
    );
}

#[test]
fn lua_error_call_surfaces_as_err() {
    let lua = Lua::new();
    let err = lua.load("error('boom')").exec().unwrap_err();
    let text = err.to_string();
    assert!(text.contains("boom"), "error should mention boom: {text}");
}

#[test]
fn rust_panic_in_closure_becomes_lua_error_not_abort() {
    let lua = Lua::new();
    let panicky = lua
        .create_function(|_, ()| -> Result<()> {
            panic!("intentional panic from rust callback");
        })
        .unwrap();
    lua.globals().set("panicky", panicky).unwrap();
    // The panic must be caught and converted to a catchable Lua error — the
    // process must NOT abort.
    let caught: String = lua
        .load("local ok, err = pcall(panicky); assert(not ok); return tostring(err)")
        .eval()
        .unwrap();
    assert!(
        caught.contains("intentional panic"),
        "panic message should surface: {caught}"
    );

    // And calling it directly (no pcall) surfaces as Err on the Rust side.
    let direct = lua.load("panicky()").exec();
    assert!(direct.is_err());
}

// --- UserData ------------------------------------------------------------

struct Vec2 {
    x: f64,
    y: f64,
}

impl UserData for Vec2 {
    fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {
        methods.add_method("magnitude", |_, this, ()| {
            Ok((this.x * this.x + this.y * this.y).sqrt())
        });
        methods.add_method("get_x", |_, this, ()| Ok(this.x));
        methods.add_method_mut("scale", |_, this, factor: f64| {
            this.x *= factor;
            this.y *= factor;
            Ok(())
        });
        // __add meta-method: returns the component-wise sum's magnitude (we
        // can't easily return a fresh Vec2 userdata from here without a
        // constructor, so we return a number to keep the test self-contained).
        methods.add_meta_method("__add", |_, this, other: f64| Ok(this.x + this.y + other));
    }
}

#[test]
fn userdata_method_from_lua() {
    let lua = Lua::new();
    let v = lua.create_userdata(Vec2 { x: 3.0, y: 4.0 }).unwrap();
    lua.globals().set("v", v).unwrap();

    let mag: f64 = lua.load("return v:magnitude()").eval().unwrap();
    assert!((mag - 5.0).abs() < 1e-9, "magnitude should be 5, got {mag}");

    let x: f64 = lua.load("return v:get_x()").eval().unwrap();
    assert_eq!(x, 3.0);
}

#[test]
fn userdata_method_mut_from_lua() {
    let lua = Lua::new();
    let v = lua.create_userdata(Vec2 { x: 3.0, y: 4.0 }).unwrap();
    lua.globals().set("v", v).unwrap();
    let mag: f64 = lua.load("v:scale(2); return v:magnitude()").eval().unwrap();
    assert!(
        (mag - 10.0).abs() < 1e-9,
        "scaled magnitude should be 10, got {mag}"
    );
}

#[test]
fn userdata_meta_method_add() {
    let lua = Lua::new();
    let v = lua.create_userdata(Vec2 { x: 3.0, y: 4.0 }).unwrap();
    lua.globals().set("v", v).unwrap();
    // v + 10 -> __add(v, 10) -> 3 + 4 + 10 = 17
    let r: f64 = lua.load("return v + 10").eval().unwrap();
    assert_eq!(r, 17.0);
}

#[test]
fn variadic_args() {
    let lua = Lua::new();
    let sum = lua
        .create_function(|_, nums: Variadic<i64>| Ok(nums.iter().sum::<i64>()))
        .unwrap();
    lua.globals().set("vsum", sum).unwrap();
    let r: i64 = lua.load("return vsum(1, 2, 3, 4, 5)").eval().unwrap();
    assert_eq!(r, 15);
}

#[test]
fn multiple_return_values_via_tuple() {
    let lua = Lua::new();
    let (a, b, c): (i64, String, bool) = lua.load("return 1, 'two', true").eval().unwrap();
    assert_eq!(a, 1);
    assert_eq!(b, "two");
    assert!(c);
}

#[test]
fn integer_range_check_rejects_overflow() {
    let lua = Lua::new();
    // u8 from a number larger than 255 should fail conversion.
    let id_u8 = lua.create_function(|_, x: u8| Ok(x)).unwrap();
    lua.globals().set("id_u8", id_u8).unwrap();
    let err = lua.load("return id_u8(300)").exec();
    assert!(err.is_err(), "u8 conversion from 300 must error");
}

// ---------------------------------------------------------------------------
// Per-VM thread-local cleanup on drop. Each per-VM map (interrupt closure,
// memory control, compiler, serde sentinels) is keyed by the state/global
// pointer; before this was wired into `LuaInner::drop`, an entry leaked one
// slot per state created (and the serde sentinel — holding a `Table` handle —
// additionally pinned the whole VM alive and aborted at thread-local teardown).
// These tests assert the maps return to their prior size after the states drop.
// Delta-based so they're robust to any states other tests leave live.
// ---------------------------------------------------------------------------

#[test]
fn interrupt_map_does_not_leak_across_dropped_states() {
    let before = crate::interrupt::interrupts_len();
    for _ in 0..20 {
        let lua = Lua::new();
        lua.set_interrupt(|_| Ok(crate::interrupt::VmState::Continue));
        // drop without remove_interrupt -> before the fix this leaked an entry
    }
    assert_eq!(
        crate::interrupt::interrupts_len(),
        before,
        "interrupt map leaked entries after dropping states"
    );
}

#[test]
fn memory_control_map_does_not_leak_across_dropped_states() {
    let before = crate::memory::memory_controls_len();
    for _ in 0..20 {
        let lua = Lua::new();
        lua.set_memory_limit(1 << 20).unwrap();
    }
    assert_eq!(
        crate::memory::memory_controls_len(),
        before,
        "memory-control map leaked entries after dropping states"
    );
}

#[test]
fn compiler_map_does_not_leak_across_dropped_states() {
    let before = crate::luau_ext::vm_compilers_len();
    for _ in 0..20 {
        let lua = Lua::new();
        lua.set_compiler(crate::compiler::Compiler::new());
    }
    assert_eq!(
        crate::luau_ext::vm_compilers_len(),
        before,
        "compiler map leaked entries after dropping states"
    );
}

#[test]
fn sandbox_saved_globals_do_not_pin_dropped_states() {
    // Sandbox WITHOUT unsandboxing, then drop. Before the fix the saved-globals
    // Table pinned the VM (state never closed); now it lives in the registry and
    // is freed with the state, so the drop actually runs. We can't observe the
    // (now absent) leak directly here, but this must not abort or hang.
    for _ in 0..20 {
        let lua = Lua::new();
        lua.sandbox(true).unwrap();
    }
    // A sandbox round-trip on a fresh state still works after the refactor.
    let lua = Lua::new();
    lua.sandbox(true).unwrap();
    lua.sandbox(false).unwrap();
    lua.globals().set("x", 1).unwrap();
    assert_eq!(lua.globals().get::<i64>("x").unwrap(), 1);
}

#[cfg(feature = "serde")]
#[test]
fn serde_sentinels_do_not_leak_or_pin_dropped_states() {
    use crate::serde::LuaSerdeExt;
    let before = crate::serde::sentinels_len();
    for _ in 0..20 {
        let lua = Lua::new();
        // Touch the null + array-metatable sentinels, then drop the state.
        let _ = lua.null();
        let v: Value = lua.to_value(&vec![1, 2, 3]).unwrap();
        let _back: Vec<i64> = lua.from_value(v).unwrap();
    }
    assert_eq!(
        crate::serde::sentinels_len(),
        before,
        "serde sentinel map leaked entries after dropping states (the state was \
         pinned by the cached Table handle, so LuaInner::drop never ran)"
    );
}

// ---------------------------------------------------------------------------
// Multi-value collection must reserve stack headroom. `value_from_stack`
// duplicates each reference-typed value onto the stack (lua_pushvalue) before
// popping it into a registry ref; after a LUA_MULTRET call, ~LUA_MINSTACK
// results fill the C frame exactly to `ci->top`, so the duplicating push
// overruns the frame (api_incr_top assert in lua_pushvalue) unless the
// collection loop reserves headroom first. Found by the `run` fuzzer:
// `local t={a=1}; return <~20 values including t>`. C++ Luau is unaffected (it
// has no such Rust result-collection wrapper).
// ---------------------------------------------------------------------------

#[test]
fn function_call_collects_many_reference_returns() {
    let lua = Lua::new();
    // 25 reference-typed return values (the same table), above the ~20 threshold.
    let src = format!("local a = {{}}\nreturn {}", vec!["a"; 25].join(","));
    let f = lua.load(&src).into_function().unwrap();
    let r: Variadic<Value> = f.call(()).unwrap();
    assert_eq!(r.len(), 25, "all 25 reference results must be collected");
}

#[test]
fn chunk_eval_collects_many_reference_returns() {
    let lua = Lua::new();
    let src = format!("local a = {{}}\nreturn {}", vec!["a"; 25].join(","));
    let r: Variadic<Value> = lua.load(&src).eval().unwrap();
    assert_eq!(r.len(), 25);
}

#[test]
fn callback_receives_many_reference_args() {
    // A C-function frame is guaranteed LUA_MINSTACK headroom above its args, so
    // collect_args' duplicating reads are safe even for many reference args —
    // this asserts that (a regression guard for the convention).
    let lua = Lua::new();
    let f = lua
        .create_function(|_, args: Variadic<Value>| Ok(args.len() as i64))
        .unwrap();
    lua.globals().set("count", f).unwrap();
    let src = format!("local a = {{}}\nreturn count({})", vec!["a"; 30].join(","));
    let n: i64 = lua.load(&src).eval().unwrap();
    assert_eq!(n, 30);
}

// Compiling unary minus on an integer literal must not overflow-panic. The
// compiler negates an integer constant via two's complement (`~v + 1`), which
// MUST wrap: `-0i` gives `~0 + 1 == 0`, and `-<i64::MIN>i` wraps back to i64::MIN
// — a checked `+ 1` panicked on both under overflow-checks. Found by the compile
// fuzzer; C++ Luau compiles these cleanly.
#[test]
fn negate_integer_literal_zero_does_not_overflow() {
    let lua = Lua::new();
    // `-0i`: the compiler negates the integer constant via `~0 + 1`, which must
    // WRAP to 0; a checked `+ 1` panicked here under overflow-checks (found by the
    // compile fuzzer). C++ Luau compiles it cleanly.
    assert!(lua.load("return -0i").into_function().is_ok());
    // Sanity: other integer-literal negations still compile.
    assert!(lua.load("return -1i").into_function().is_ok());
    assert!(lua.load("return 0i").into_function().is_ok());
}