memory: a bounds-checked buffer + hex literals (FFI foundation)
To bind GLFW and Vulkan, the FFI needs to pass *structs*, not just
scalars. This adds the memory-safe foundation for that.
- cat/types/memory.{h,c}: the `memory` type -- an owned byte buffer
with named accessors (u8..u64, i8..i64, f32, f64, text, set_text,
ptr, size, fill, copy). Every access is bounds-checked and raises
an error instead of corrupting memory. `ptr` exposes the address for
an FFI call; values outlive the call, so the pointer stays valid.
- ffi.c: a `memory` value where a pointer is expected now passes its
buffer address -- the bridge that lets structs reach native code.
- lexer: 0x/0b/0o integer literals (0xAB), with `_` separators.
- stdlib: `memory(n)` constructor as a global.
- tests/memory.cat: reads/writes, zeroing, fill, out-of-bounds raising
(119 total, all green).
Proved live: glfwGetVersion(major,minor,rev) called through catscript
FFI + memory -> 3, 4, 0 (GLFW 3.4).
This commit is contained in:
1 parent
83629d0e1e
commit
210054f46c
8 files changed
+476
-9
No files matched your search
@@ -0,0 +1,40 @@
|
||||
-- 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
|
||||
Reference in new issue
Block a user