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:
Minus committed 2026-11-17 18:44:32 +00:00
1 parent 83629d0e1e
commit 210054f46c
8 files changed
+476 -9

No files matched your search

+40
View File
@@ -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
+16
View File
@@ -174,6 +174,22 @@ check "repr-list" "list: [a, b, _repr_]" "`echo "$out" | sed -
out=`printf 'x = 50\nx\nx + 5\n' | $BIN | tr '>' '\n' | grep -oE '[0-9]+'`
check "repl-echo" "50" "`echo "$out" | sed -n 1p`"
check "repl-state" "55" "`echo "$out" | sed -n 2p`"
# ---- memory buffer ----------------------------------------------------
out=`$BIN tests/memory.cat`
check "mem-u32" "u32: 1234" "`echo "$out" | sed -n 1p`"
check "mem-u64" "u64: 9999999999" "`echo "$out" | sed -n 2p`"
check "mem-text" "text: hi" "`echo "$out" | sed -n 3p`"
check "mem-size" "size: 16" "`echo "$out" | sed -n 4p`"
check "mem-f32" "f32: 3.5" "`echo "$out" | sed -n 5p`"
check "mem-zero" "zero: 0" "`echo "$out" | sed -n 7p`"
check "mem-oob" "caught oob" "`echo "$out" | sed -n 10p`"
out=`printf 'print(0xAB)\nprint(0b1010)\nprint(0o17)\n' | $BIN | grep -oE '[0-9]+'`
check "hex" "171" "`echo "$out" | sed -n 1p`"
check "bin" "10" "`echo "$out" | sed -n 2p`"
check "oct" "15" "`echo "$out" | sed -n 3p`"
# ---- imports: `use name` ---------------------------------------------
out=`$BIN tests/import.cat`