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
|
extern crate rlua;
use rlua::{Function, Lua, String};
#[test]
fn test_function() {
let lua = Lua::new();
let globals = lua.globals();
lua.exec::<()>(
r#"
function concat(arg1, arg2)
return arg1 .. arg2
end
"#,
None,
).unwrap();
let concat = globals.get::<_, Function>("concat").unwrap();
assert_eq!(concat.call::<_, String>(("foo", "bar")).unwrap(), "foobar");
}
#[test]
fn test_bind() {
let lua = Lua::new();
let globals = lua.globals();
lua.exec::<()>(
r#"
function concat(...)
local res = ""
for _, s in pairs({...}) do
res = res..s
end
return res
end
"#,
None,
).unwrap();
let mut concat = globals.get::<_, Function>("concat").unwrap();
concat = concat.bind("foo").unwrap();
concat = concat.bind("bar").unwrap();
concat = concat.bind(("baz", "baf")).unwrap();
assert_eq!(
concat.call::<_, String>(("hi", "wut")).unwrap(),
"foobarbazbafhiwut"
);
}
#[test]
fn test_rust_function() {
let lua = Lua::new();
let globals = lua.globals();
lua.exec::<()>(
r#"
function lua_function()
return rust_function()
end
-- Test to make sure chunk return is ignored
return 1
"#,
None,
).unwrap();
let lua_function = globals.get::<_, Function>("lua_function").unwrap();
let rust_function = lua.create_function(|_, ()| Ok("hello")).unwrap();
globals.set("rust_function", rust_function).unwrap();
assert_eq!(lua_function.call::<_, String>(()).unwrap(), "hello");
}
|