Skip to content

Commit ce603c1

Browse files
committed
Issue #253: Add a test case.
1 parent 173856a commit ce603c1

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

tests/userdata.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,43 @@ fn test_functions() {
274274
assert_eq!(get_constant.call::<_, i64>(()).unwrap(), 7);
275275
});
276276
}
277+
278+
#[test]
279+
fn test_align() {
280+
#[derive(Clone)]
281+
#[repr(align(32))]
282+
struct MyUserData(u8);
283+
284+
assert_eq!(std::mem::align_of_val(&MyUserData(0)), 32);
285+
286+
impl UserData for MyUserData {
287+
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
288+
methods.add_method_mut("check", |_, this: &mut MyUserData, ()| {
289+
let ptr = this as *const MyUserData;
290+
let ptrval = ptr as usize;
291+
assert_eq!(ptrval & 31, 0);
292+
assert_eq!(this.0, 42);
293+
this.0 = 99;
294+
Ok(77)
295+
});
296+
}
297+
}
298+
299+
Lua::new().context(|lua| {
300+
let globals = lua.globals();
301+
let userdata = lua.create_userdata(MyUserData(42)).unwrap();
302+
globals.set("userdata", userdata.clone()).unwrap();
303+
lua.load(
304+
r#"
305+
function f()
306+
return userdata:check()
307+
end
308+
"#,
309+
)
310+
.exec()
311+
.unwrap();
312+
let f = globals.get::<_, Function>("f").unwrap();
313+
assert_eq!(f.call::<_, i64>(()).unwrap(), 77);
314+
assert_eq!(globals.get::<_, MyUserData>("userdata").unwrap().0, 99);
315+
});
316+
}

0 commit comments

Comments
 (0)