luars 0.26.2

A library for lua 5.5 runtime implementation in Rust
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/// Advanced function definition and call tests
use crate::lua_vm::{GlobalState, SafeOption};

#[test]
fn test_function_with_default_return() {
    let mut vm = GlobalState::new(SafeOption::default());
    vm.open_stdlib(crate::stdlib::Stdlib::All).unwrap();
    let result = vm.main_state().execute(
        r#"
        local function no_return() end
        local result = no_return()
        assert(result == nil)
    "#,
    );
    assert!(result.is_ok());
}

#[test]
fn test_function_multiple_returns() {
    let mut vm = GlobalState::new(SafeOption::default());
    vm.open_stdlib(crate::stdlib::Stdlib::All).unwrap();
    let result = vm.main_state().execute(
        r#"
        local function multi()
            return 1, 2, 3, 4, 5
        end
        local a, b, c, d, e = multi()
        assert(a == 1 and b == 2 and c == 3 and d == 4 and e == 5)
    "#,
    );
    assert!(result.is_ok());
}

#[test]
fn test_function_variable_returns() {
    let mut vm = GlobalState::new(SafeOption::default());
    vm.open_stdlib(crate::stdlib::Stdlib::All).unwrap();
    let result = vm.main_state().execute(
        r#"
        local function ret_n(n)
            if n == 1 then return "one"
            elseif n == 2 then return "two", 2
            else return "three", 3, 3.0
            end
        end
        local a = ret_n(1)
        assert(a == "one")
        local b, c = ret_n(2)
        assert(b == "two" and c == 2)
        local d, e, f = ret_n(3)
        assert(d == "three" and e == 3 and f == 3.0)
    "#,
    );
    assert!(result.is_ok());
}

#[test]
fn test_function_tail_call() {
    let mut vm = GlobalState::new(SafeOption::default());
    vm.open_stdlib(crate::stdlib::Stdlib::All).unwrap();
    let result = vm.main_state().execute(
        r#"
        local function countdown(n)
            if n == 0 then
                return "done"
            else
                return countdown(n - 1)
            end
        end
        assert(countdown(100) == "done")
    "#,
    );
    assert!(result.is_ok());
}

#[test]
fn test_function_vararg_basic() {
    let mut vm = GlobalState::new(SafeOption::default());
    vm.open_stdlib(crate::stdlib::Stdlib::All).unwrap();
    let result = vm.main_state().execute(
        r#"
        local function sum(...)
            local total = 0
            for i, v in ipairs({...}) do
                total = total + v
            end
            return total
        end
        assert(sum(1, 2, 3, 4, 5) == 15)
        assert(sum(10, 20) == 30)
    "#,
    );
    if let Err(e) = &result {
        eprintln!("Error: {:?}", e);
    }
    assert!(result.is_ok());
}

// Temporarily disabled due to VM issue with varargs
// #[test]
// fn test_function_vararg_with_named_params() {
//     let mut vm = LuaVM::new(SafeOption::default());
//     vm.open_libs();
//     let result = vm.execute_string(r#"
//         local function format(prefix, ...)
//             local args = {...}
//             local result = prefix
//             for i, v in ipairs(args) do
//                 result = result .. " " .. tostring(v)
//             end
//             return result
//         end
//         assert(format("Values:", 1, 2, 3) == "Values: 1 2 3")
//     "#);
//     assert!(result.is_ok());
// }

#[test]
fn test_function_vararg_count() {
    let mut vm = GlobalState::new(SafeOption::default());
    vm.open_stdlib(crate::stdlib::Stdlib::All).unwrap();
    let result = vm.main_state().execute(
        r#"
        local function count_args(...)
            return select('#', ...)
        end
        assert(count_args(1, 2, 3) == 3)
        assert(count_args() == 0)
        assert(count_args(nil, nil, 1) == 3)
    "#,
    );
    if let Err(e) = &result {
        eprintln!("Error: {:?}", e);
    }
    assert!(result.is_ok());
}

#[test]
fn test_function_vararg_select() {
    let mut vm = GlobalState::new(SafeOption::default());
    vm.open_stdlib(crate::stdlib::Stdlib::All).unwrap();
    let result = vm.main_state().execute(
        r#"
        local function get_nth(n, ...)
            return select(n, ...)
        end
        assert(get_nth(2, 10, 20, 30) == 20)
        assert(get_nth(3, "a", "b", "c", "d") == "c")
    "#,
    );
    if let Err(e) = &result {
        eprintln!("Error: {:?}", e);
    }
    assert!(result.is_ok());
}

#[test]
fn test_function_nested_calls() {
    let mut vm = GlobalState::new(SafeOption::default());
    vm.open_stdlib(crate::stdlib::Stdlib::All).unwrap();
    let result = vm.main_state().execute(
        r#"
        local function add(a, b) return a + b end
        local function mul(a, b) return a * b end
        local function calc(a, b, c)
            return add(mul(a, b), c)
        end
        assert(calc(2, 3, 4) == 10)
        assert(calc(5, 4, 1) == 21)
    "#,
    );
    assert!(result.is_ok());
}

#[test]
fn test_function_as_parameter() {
    let mut vm = GlobalState::new(SafeOption::default());
    vm.open_stdlib(crate::stdlib::Stdlib::All).unwrap();
    let result = vm.main_state().execute(
        r#"
        local function apply(f, x, y)
            return f(x, y)
        end
        local function add(a, b) return a + b end
        local function mul(a, b) return a * b end
        assert(apply(add, 3, 4) == 7)
        assert(apply(mul, 3, 4) == 12)
    "#,
    );
    assert!(result.is_ok());
}

#[test]
fn test_function_returning_function() {
    let mut vm = GlobalState::new(SafeOption::default());
    vm.open_stdlib(crate::stdlib::Stdlib::All).unwrap();
    let result = vm.main_state().execute(
        r#"
        local function get_operation(op)
            if op == "add" then
                return function(a, b) return a + b end
            elseif op == "mul" then
                return function(a, b) return a * b end
            end
        end
        local add = get_operation("add")
        local mul = get_operation("mul")
        assert(add(3, 4) == 7)
        assert(mul(3, 4) == 12)
    "#,
    );
    assert!(result.is_ok());
}

#[test]
fn test_function_table_of_functions() {
    let mut vm = GlobalState::new(SafeOption::default());
    vm.open_stdlib(crate::stdlib::Stdlib::All).unwrap();
    let result = vm.main_state().execute(
        r#"
        local ops = {
            add = function(a, b) return a + b end,
            sub = function(a, b) return a - b end,
            mul = function(a, b) return a * b end,
            div = function(a, b) return a / b end
        }
        assert(ops.add(10, 5) == 15)
        assert(ops.sub(10, 5) == 5)
        assert(ops.mul(10, 5) == 50)
        assert(ops.div(10, 5) == 2)
    "#,
    );
    assert!(result.is_ok());
}

#[test]
fn test_function_anonymous_immediate_call() {
    let mut vm = GlobalState::new(SafeOption::default());
    vm.open_stdlib(crate::stdlib::Stdlib::All).unwrap();
    let result = vm.main_state().execute(
        r#"
        local result = (function(x, y)
            return x * x + y * y
        end)(3, 4)
        assert(result == 25)
    "#,
    );
    assert!(result.is_ok());
}

#[test]
fn test_function_method_call_chain() {
    let mut vm = GlobalState::new(SafeOption::default());
    vm.open_stdlib(crate::stdlib::Stdlib::All).unwrap();
    let result = vm.main_state().execute(
        r#"
        local obj = {value = 10}
        function obj:add(n)
            self.value = self.value + n
            return self
        end
        function obj:mul(n)
            self.value = self.value * n
            return self
        end
        function obj:get()
            return self.value
        end
        obj:add(5):mul(2):add(10)
        assert(obj:get() == 40)
    "#,
    );
    assert!(result.is_ok());
}

#[test]
fn test_function_local_function_scope() {
    let mut vm = GlobalState::new(SafeOption::default());
    vm.open_stdlib(crate::stdlib::Stdlib::All).unwrap();
    let result = vm.main_state().execute(
        r#"
        local function outer()
            local value = 10
            local function inner()
                return value * 2
            end
            return inner()
        end
        assert(outer() == 20)
    "#,
    );
    assert!(result.is_ok());
}

#[test]
fn test_function_early_return() {
    let mut vm = GlobalState::new(SafeOption::default());
    vm.open_stdlib(crate::stdlib::Stdlib::All).unwrap();
    let result = vm.main_state().execute(
        r#"
        local function check(x)
            if x < 0 then return "negative" end
            if x == 0 then return "zero" end
            return "positive"
        end
        assert(check(-5) == "negative")
        assert(check(0) == "zero")
        assert(check(5) == "positive")
    "#,
    );
    assert!(result.is_ok());
}

#[test]
fn test_function_multiple_definitions() {
    let mut vm = GlobalState::new(SafeOption::default());
    vm.open_stdlib(crate::stdlib::Stdlib::All).unwrap();
    let result = vm.main_state().execute(
        r#"
        local function foo() return "first" end
        local x = foo()
        local function foo() return "second" end
        local y = foo()
        assert(x == "first" and y == "second")
    "#,
    );
    assert!(result.is_ok());
}

#[test]
fn test_function_pcall_wrapper() {
    let mut vm = GlobalState::new(SafeOption::default());
    vm.open_stdlib(crate::stdlib::Stdlib::All).unwrap();
    let result = vm.main_state().execute(
        r#"
        local function safe_divide(a, b)
            if b == 0 then
                error("division by zero")
            end
            return a / b
        end
        local ok, result = pcall(safe_divide, 10, 2)
        assert(ok and result == 5)
        local ok2, err = pcall(safe_divide, 10, 0)
        assert(not ok2)
    "#,
    );
    assert!(result.is_ok());
}

#[test]
fn test_function_ipairs_wrapper() {
    let mut vm = GlobalState::new(SafeOption::default());
    vm.open_stdlib(crate::stdlib::Stdlib::All).unwrap();
    let result = vm.main_state().execute(
        r#"
        local function map(t, f)
            local result = {}
            for i, v in ipairs(t) do
                result[i] = f(v)
            end
            return result
        end
        local squares = map({1, 2, 3, 4}, function(x) return x * x end)
        assert(squares[1] == 1 and squares[2] == 4 and squares[3] == 9 and squares[4] == 16)
    "#,
    );
    assert!(result.is_ok());
}

#[test]
fn test_function_returns_in_table_constructor() {
    let mut vm = GlobalState::new(SafeOption::default());
    vm.open_stdlib(crate::stdlib::Stdlib::All).unwrap();
    let result = vm.main_state().execute(
        r#"
        local function triple()
            return 1, 2, 3
        end

        local t = {triple()}
        assert(#t == 3)
        assert(t[1] == 1 and t[2] == 2 and t[3] == 3)
        "#,
    );
    assert!(result.is_ok(), "Error: {:?}", result);
}

#[test]
fn test_function_returns_as_function_arguments() {
    let mut vm = GlobalState::new(SafeOption::default());
    vm.open_stdlib(crate::stdlib::Stdlib::All).unwrap();
    let result = vm.main_state().execute(
        r#"
        local function triple()
            return 1, 2, 3
        end

        local function sum3(a, b, c)
            return a + b + c
        end

        assert(sum3(triple()) == 6)
        "#,
    );
    assert!(result.is_ok(), "Error: {:?}", result);
}

#[test]
fn test_function_reduce() {
    let mut vm = GlobalState::new(SafeOption::default());
    vm.open_stdlib(crate::stdlib::Stdlib::All).unwrap();
    let result = vm.main_state().execute(
        r#"
        local function reduce(t, f, init)
            local acc = init
            for i, v in ipairs(t) do
                acc = f(acc, v)
            end
            return acc
        end
        local sum = reduce({1, 2, 3, 4, 5}, function(a, b) return a + b end, 0)
        assert(sum == 15)
    "#,
    );
    assert!(result.is_ok());
}

#[test]
fn test_local_after_nested_function_stays_local() {
    let mut vm = GlobalState::new(SafeOption::default());
    vm.open_stdlib(crate::stdlib::Stdlib::All).unwrap();
    let result = vm.main_state().execute(
        r#"
        local MAX_NESTING <const> = 32

        local function render()
            local result = {}

            local function dump()
                if MAX_NESTING > 0 then
                    result[#result + 1] = "x,\n"
                end
            end

            dump()

            local last = result[#result]
            result[#result] = string.sub(last, 1, #last - 2) .. "\n"

            return table.concat(result)
        end

        assert(render() == "x\n")
        "#,
    );
    assert!(result.is_ok(), "Error: {:?}", result.err());
}