2018-09-16 20:15:51 -04:00
|
|
|
extern crate rlua;
|
|
|
|
|
|
|
|
use rlua::{Function, Lua, String};
|
2018-02-09 23:52:05 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_function() {
|
|
|
|
let lua = Lua::new();
|
|
|
|
let globals = lua.globals();
|
2018-10-01 06:00:21 -04:00
|
|
|
lua.exec::<_, ()>(
|
2018-02-09 23:52:05 -05:00
|
|
|
r#"
|
|
|
|
function concat(arg1, arg2)
|
|
|
|
return arg1 .. arg2
|
|
|
|
end
|
|
|
|
"#,
|
|
|
|
None,
|
2019-09-27 12:38:24 -04:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-02-09 23:52:05 -05:00
|
|
|
|
|
|
|
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();
|
2018-10-01 06:00:21 -04:00
|
|
|
lua.exec::<_, ()>(
|
2018-02-09 23:52:05 -05:00
|
|
|
r#"
|
|
|
|
function concat(...)
|
|
|
|
local res = ""
|
|
|
|
for _, s in pairs({...}) do
|
|
|
|
res = res..s
|
|
|
|
end
|
|
|
|
return res
|
|
|
|
end
|
|
|
|
"#,
|
|
|
|
None,
|
2019-09-27 12:38:24 -04:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-02-09 23:52:05 -05:00
|
|
|
|
|
|
|
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();
|
2018-10-01 06:00:21 -04:00
|
|
|
lua.exec::<_, ()>(
|
2018-02-09 23:52:05 -05:00
|
|
|
r#"
|
|
|
|
function lua_function()
|
|
|
|
return rust_function()
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Test to make sure chunk return is ignored
|
|
|
|
return 1
|
|
|
|
"#,
|
|
|
|
None,
|
2019-09-27 12:38:24 -04:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-02-09 23:52:05 -05:00
|
|
|
|
|
|
|
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");
|
|
|
|
}
|