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
@@ -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`
|
||||
|
||||
Reference in new issue
Block a user