basic tests for nonstatic userdata
This commit is contained in:
parent
add547b356
commit
6153ec4b95
|
@ -1,7 +1,7 @@
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use {Error, Function, Lua, String, UserData, UserDataMethods};
|
use {Error, Function, Lua, MetaMethod, String, UserData, UserDataMethods};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn scope_func() {
|
fn scope_func() {
|
||||||
|
@ -100,3 +100,136 @@ fn outer_lua_access() {
|
||||||
});
|
});
|
||||||
assert_eq!(table.get::<_, String>("a").unwrap(), "b");
|
assert_eq!(table.get::<_, String>("a").unwrap(), "b");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn scope_userdata_methods() {
|
||||||
|
struct MyUserData<'a>(&'a Cell<i64>);
|
||||||
|
|
||||||
|
impl<'a> UserData for MyUserData<'a> {
|
||||||
|
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
|
||||||
|
methods.add_method("inc", |_, data, ()| {
|
||||||
|
data.0.set(data.0.get() + 1);
|
||||||
|
Ok(())
|
||||||
|
});
|
||||||
|
|
||||||
|
methods.add_method("dec", |_, data, ()| {
|
||||||
|
data.0.set(data.0.get() - 1);
|
||||||
|
Ok(())
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let lua = Lua::new();
|
||||||
|
|
||||||
|
let i = Cell::new(42);
|
||||||
|
lua.scope(|scope| {
|
||||||
|
let f: Function =
|
||||||
|
lua.eval(
|
||||||
|
r#"
|
||||||
|
function(u)
|
||||||
|
u:inc()
|
||||||
|
u:inc()
|
||||||
|
u:inc()
|
||||||
|
u:dec()
|
||||||
|
end
|
||||||
|
"#,
|
||||||
|
None,
|
||||||
|
).unwrap();
|
||||||
|
|
||||||
|
f.call::<_, ()>(scope.create_userdata(MyUserData(&i)).unwrap())
|
||||||
|
.unwrap();
|
||||||
|
});
|
||||||
|
|
||||||
|
assert_eq!(i.get(), 44);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn scope_userdata_functions() {
|
||||||
|
struct MyUserData<'a>(&'a i64);
|
||||||
|
|
||||||
|
impl<'a> UserData for MyUserData<'a> {
|
||||||
|
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
|
||||||
|
methods.add_meta_function(MetaMethod::Add, |lua, ()| {
|
||||||
|
let globals = lua.globals();
|
||||||
|
globals.set("i", globals.get::<_, i64>("i")? + 1)?;
|
||||||
|
Ok(())
|
||||||
|
});
|
||||||
|
methods.add_meta_function(MetaMethod::Sub, |lua, ()| {
|
||||||
|
let globals = lua.globals();
|
||||||
|
globals.set("i", globals.get::<_, i64>("i")? + 1)?;
|
||||||
|
Ok(())
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let lua = Lua::new();
|
||||||
|
let f =
|
||||||
|
lua.exec::<Function>(
|
||||||
|
r#"
|
||||||
|
i = 0
|
||||||
|
return function(u)
|
||||||
|
_ = u + u
|
||||||
|
_ = u - 1
|
||||||
|
_ = 1 + u
|
||||||
|
end
|
||||||
|
"#,
|
||||||
|
None,
|
||||||
|
).unwrap();
|
||||||
|
|
||||||
|
let dummy = 0;
|
||||||
|
lua.scope(|scope| {
|
||||||
|
f.call::<_, ()>(scope.create_userdata(MyUserData(&dummy)).unwrap())
|
||||||
|
.unwrap();
|
||||||
|
});
|
||||||
|
|
||||||
|
assert_eq!(lua.globals().get::<_, i64>("i").unwrap(), 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn scope_userdata_mismatch() {
|
||||||
|
struct MyUserData<'a>(&'a Cell<i64>);
|
||||||
|
|
||||||
|
impl<'a> UserData for MyUserData<'a> {
|
||||||
|
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
|
||||||
|
methods.add_method("inc", |_, data, ()| {
|
||||||
|
data.0.set(data.0.get() + 1);
|
||||||
|
Ok(())
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let lua = Lua::new();
|
||||||
|
lua.exec::<()>(
|
||||||
|
r#"
|
||||||
|
function okay(a, b)
|
||||||
|
a.inc(a)
|
||||||
|
b.inc(b)
|
||||||
|
end
|
||||||
|
|
||||||
|
function bad(a, b)
|
||||||
|
a.inc(b)
|
||||||
|
end
|
||||||
|
"#,
|
||||||
|
None,
|
||||||
|
).unwrap();
|
||||||
|
|
||||||
|
let a = Cell::new(1);
|
||||||
|
let b = Cell::new(1);
|
||||||
|
|
||||||
|
let okay: Function = lua.globals().get("okay").unwrap();
|
||||||
|
let bad: Function = lua.globals().get("bad").unwrap();
|
||||||
|
|
||||||
|
lua.scope(|scope| {
|
||||||
|
let au = scope.create_userdata(MyUserData(&a)).unwrap();
|
||||||
|
let bu = scope.create_userdata(MyUserData(&b)).unwrap();
|
||||||
|
assert!(okay.call::<_, ()>((au.clone(), bu.clone())).is_ok());
|
||||||
|
match bad.call::<_, ()>((au, bu)) {
|
||||||
|
Err(Error::CallbackError { ref cause, .. }) => match *cause.as_ref() {
|
||||||
|
Error::UserDataTypeMismatch => {}
|
||||||
|
ref other => panic!("wrong error type {:?}", other),
|
||||||
|
},
|
||||||
|
Err(other) => panic!("wrong error type {:?}", other),
|
||||||
|
Ok(_) => panic!("incorrectly returned Ok"),
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue