-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathmemory.rs
68 lines (58 loc) · 2.02 KB
/
memory.rs
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
use std::sync::Arc;
use rlua::{Error, Lua, Nil, RluaCompat, UserData};
#[cfg(not(rlua_luajit))] // Custom allocators for LuaJIT not available
#[test]
fn test_memory_limit() {
let lua = Lua::new();
let initial_memory = lua.used_memory();
assert!(
initial_memory > 0,
"used_memory reporting is wrong, lua uses memory for stdlib"
);
lua.context(|ctx| {
let f = ctx
.load("local t = {}; for i = 1,10000 do t[i] = i end")
.into_function()
.unwrap();
f.call::<_, ()>(()).expect("should trigger no memory limit");
// It's not clear this is needed. On Lua 5.1, we fail to allocate
// memory in the following `f.call` before actually running the
// function otherwise.
lua.gc_collect().expect("should collect garbage");
lua.set_memory_limit(initial_memory + 10000).unwrap();
match f.call::<_, ()>(()) {
Err(Error::MemoryError(_)) => {}
something_else => panic!("did not trigger memory error: {:?}", something_else),
}
lua.set_memory_limit(usize::MAX).unwrap();
f.call::<_, ()>(()).expect("should trigger no memory limit");
});
}
#[test]
fn test_gc_control() {
let lua = Lua::new();
#[cfg(any(rlua_lua53, rlua_lua54))]
assert!(lua.gc_is_running());
lua.gc_stop();
#[cfg(any(rlua_lua53, rlua_lua54))]
assert!(!lua.gc_is_running());
lua.gc_restart();
#[cfg(any(rlua_lua53, rlua_lua54))]
assert!(lua.gc_is_running());
struct MyUserdata(Arc<()>);
impl UserData for MyUserdata {}
lua.context(|ctx| {
let rc = Arc::new(());
ctx.globals()
.set(
"userdata",
ctx.create_userdata(MyUserdata(rc.clone())).unwrap(),
)
.unwrap();
ctx.globals().set("userdata", Nil).unwrap();
assert_eq!(Arc::strong_count(&rc), 2);
lua.gc_collect().unwrap();
lua.gc_collect().unwrap();
assert_eq!(Arc::strong_count(&rc), 1);
});
}