2020-05-16 10:13:38 -04:00
|
|
|
#![cfg_attr(
|
|
|
|
all(feature = "luajit", target_os = "macos", target_arch = "x86_64"),
|
|
|
|
feature(link_args)
|
|
|
|
)]
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
all(feature = "luajit", target_os = "macos", target_arch = "x86_64"),
|
|
|
|
link_args = "-pagezero_size 10000 -image_base 100000000",
|
|
|
|
allow(unused_attributes)
|
|
|
|
)]
|
|
|
|
extern "system" {}
|
|
|
|
|
2020-05-05 21:32:05 -04:00
|
|
|
use std::sync::Arc;
|
2019-09-28 10:23:17 -04:00
|
|
|
|
2019-10-14 17:21:30 -04:00
|
|
|
use mlua::{Lua, Result, UserData};
|
2019-09-28 10:23:17 -04:00
|
|
|
|
2020-05-08 19:18:48 -04:00
|
|
|
#[cfg(any(feature = "lua54", feature = "lua53", feature = "lua52"))]
|
|
|
|
use mlua::Error;
|
|
|
|
|
|
|
|
#[cfg(any(feature = "lua54", feature = "lua53", feature = "lua52"))]
|
|
|
|
#[test]
|
|
|
|
fn test_memory_limit() -> Result<()> {
|
|
|
|
let lua = Lua::new();
|
|
|
|
|
|
|
|
let initial_memory = lua.used_memory();
|
|
|
|
assert!(
|
|
|
|
initial_memory > 0,
|
|
|
|
"used_memory reporting is wrong, lua uses memory for stdlib"
|
|
|
|
);
|
|
|
|
|
|
|
|
let f = lua
|
|
|
|
.load("local t = {}; for i = 1,10000 do t[i] = i end")
|
|
|
|
.into_function()?;
|
|
|
|
f.call::<_, ()>(()).expect("should trigger no memory limit");
|
|
|
|
|
|
|
|
lua.set_memory_limit(initial_memory + 10000)?;
|
|
|
|
match f.call::<_, ()>(()) {
|
|
|
|
Err(Error::MemoryError(_)) => {}
|
|
|
|
something_else => panic!("did not trigger memory error: {:?}", something_else),
|
|
|
|
};
|
|
|
|
|
|
|
|
lua.set_memory_limit(0)?;
|
|
|
|
f.call::<_, ()>(()).expect("should trigger no memory limit");
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-09-28 10:23:17 -04:00
|
|
|
#[test]
|
|
|
|
fn test_gc_control() -> Result<()> {
|
2019-10-14 17:21:30 -04:00
|
|
|
let lua = Lua::new();
|
2020-01-07 15:53:47 -05:00
|
|
|
let globals = lua.globals();
|
2019-09-28 10:23:17 -04:00
|
|
|
|
2020-05-08 07:42:40 -04:00
|
|
|
#[cfg(any(feature = "lua54", feature = "lua53", feature = "lua52"))]
|
2019-10-14 17:21:30 -04:00
|
|
|
{
|
|
|
|
assert!(lua.gc_is_running());
|
|
|
|
lua.gc_stop();
|
|
|
|
assert!(!lua.gc_is_running());
|
|
|
|
lua.gc_restart();
|
|
|
|
assert!(lua.gc_is_running());
|
|
|
|
}
|
2019-09-28 10:23:17 -04:00
|
|
|
|
2020-05-05 21:32:05 -04:00
|
|
|
struct MyUserdata(Arc<()>);
|
2019-09-28 10:23:17 -04:00
|
|
|
impl UserData for MyUserdata {}
|
|
|
|
|
2020-05-05 21:32:05 -04:00
|
|
|
let rc = Arc::new(());
|
2020-01-07 15:53:47 -05:00
|
|
|
globals.set("userdata", lua.create_userdata(MyUserdata(rc.clone()))?)?;
|
|
|
|
globals.raw_remove("userdata")?;
|
2019-09-28 10:23:17 -04:00
|
|
|
|
2020-05-05 21:32:05 -04:00
|
|
|
assert_eq!(Arc::strong_count(&rc), 2);
|
2019-09-28 10:23:17 -04:00
|
|
|
lua.gc_collect()?;
|
|
|
|
lua.gc_collect()?;
|
2020-05-05 21:32:05 -04:00
|
|
|
assert_eq!(Arc::strong_count(&rc), 1);
|
2019-09-28 10:23:17 -04:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-11-29 08:26:30 -05:00
|
|
|
#[cfg(any(feature = "lua53", feature = "lua52"))]
|
2019-09-28 10:23:17 -04:00
|
|
|
#[test]
|
|
|
|
fn test_gc_error() {
|
2019-10-14 17:21:30 -04:00
|
|
|
use mlua::Error;
|
|
|
|
|
|
|
|
let lua = Lua::new();
|
2019-09-28 10:23:17 -04:00
|
|
|
match lua
|
|
|
|
.load(
|
|
|
|
r#"
|
2020-01-06 18:59:50 -05:00
|
|
|
val = nil
|
|
|
|
table = {}
|
|
|
|
setmetatable(table, {
|
|
|
|
__gc = function()
|
|
|
|
error("gcwascalled")
|
|
|
|
end
|
|
|
|
})
|
|
|
|
table = nil
|
|
|
|
collectgarbage("collect")
|
2019-09-28 10:23:17 -04:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.exec()
|
|
|
|
{
|
|
|
|
Err(Error::GarbageCollectorError(_)) => {}
|
|
|
|
Err(e) => panic!("__gc error did not result in correct error, instead: {}", e),
|
|
|
|
Ok(()) => panic!("__gc error did not result in error"),
|
|
|
|
}
|
|
|
|
}
|