Something went wrong. Try again.
Personal fork of prime agent. Mirror of: https://mnemosyne.sigilzero.dev/sigilzero/prime-agent
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140use std::sync::Arc;
use mlua::{Error, GCMode, Lua, Result, UserData};
#[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");
if cfg!(feature = "luajit") && lua.set_memory_limit(0).is_err() { // seems this luajit version does not support memory limit return Ok(()); }
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");
// Test memory limit during chunk loading lua.set_memory_limit(1024)?; match lua .load("local t = {}; for i = 1,10000 do t[i] = i end") .into_function() { Err(Error::MemoryError(_)) => {} _ => panic!("did not trigger memory error"), };
Ok(())}
#[test]fn test_memory_limit_thread() -> Result<()> { let lua = Lua::new();
let f = lua .load("local t = {}; for i = 1,10000 do t[i] = i end") .into_function()?;
if cfg!(feature = "luajit") && lua.set_memory_limit(0).is_err() { // seems this luajit version does not support memory limit return Ok(()); }
let thread = lua.create_thread(f)?; lua.set_memory_limit(lua.used_memory() + 10000)?; match thread.resume::<()>(()) { Err(Error::MemoryError(_)) => {} something_else => panic!("did not trigger memory error: {:?}", something_else), };
Ok(())}
#[test]fn test_gc_control() -> Result<()> { let lua = Lua::new(); let globals = lua.globals();
#[cfg(any(feature = "lua55", feature = "lua54"))] { assert_eq!(lua.gc_gen(0, 0), GCMode::Incremental); assert_eq!(lua.gc_inc(0, 0, 0), GCMode::Generational); }
#[cfg(any( feature = "lua55", feature = "lua54", feature = "lua53", feature = "lua52", feature = "luau" ))] { assert!(lua.gc_is_running()); lua.gc_stop(); assert!(!lua.gc_is_running()); lua.gc_restart(); assert!(lua.gc_is_running()); }
assert_eq!(lua.gc_inc(200, 100, 13), GCMode::Incremental);
struct MyUserdata(#[allow(unused)] Arc<()>); impl UserData for MyUserdata {}
let rc = Arc::new(()); globals.set("userdata", lua.create_userdata(MyUserdata(rc.clone()))?)?; globals.raw_remove("userdata")?;
assert_eq!(Arc::strong_count(&rc), 2); lua.gc_collect()?; lua.gc_collect()?; assert_eq!(Arc::strong_count(&rc), 1);
Ok(())}
#[cfg(any(feature = "lua53", feature = "lua52"))]#[test]fn test_gc_error() { use mlua::Error;
let lua = Lua::new(); match lua .load( r#" val = nil table = {} setmetatable(table, { __gc = function() error("gcwascalled") end }) table = nil collectgarbage("collect") "#, ) .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"), }}