-- tests/memory.cat -- the bounds-checked `memory` buffer. -- -- `memory(n)` owns n bytes. Fields are read/written through named -- methods; every access is bounds-checked and raises instead of -- corrupting memory. local m = memory(16) m:set_u32(0, 1234) print("u32:", m:u32(0)) m:set_u64(8, 9999999999) print("u64:", m:u64(8)) m:set_text(4, "hi") print("text:", m:text(4)) print("size:", m:size()) m:set_f32(0, 3.5) print("f32:", m:f32(0)) m:set_f64(8, 2.5) print("f64:", m:f64(8)) -- zeroed on allocation local z = memory(4) print("zero:", z:u32(0)) -- a freed-pointer-style address is available for FFI calls local p = m:ptr() print("ptr:", p > 0) -- fill and re-read local f = memory(8) f:fill(0xAB) print("fill:", f:u8(0)) -- out-of-bounds raises (caught here) try local small = memory(2) small:u32(0) print("no raise") except e print("caught oob") end