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,48 @@
|
||||
/*
|
||||
* cat/types/memory.h -- the concrete `memory` type.
|
||||
*
|
||||
* Naming rule: symbols here are cat_types_memory_* / CAT_TYPES_MEMORY_*.
|
||||
*
|
||||
* `memory` is a bounds-checked, owned byte buffer. It exists so
|
||||
* catscript can build C structs and buffers for FFI calls (GLFW,
|
||||
* Vulkan, ...) without giving up memory safety:
|
||||
*
|
||||
* - every read/write is bounds-checked; an out-of-range offset
|
||||
* raises an error instead of corrupting memory or crashing;
|
||||
* - the buffer owns its storage and frees it when collected;
|
||||
* - `ptr` exposes the address for an FFI call (valid for the call,
|
||||
* and for the whole VM run since values are not freed early).
|
||||
*
|
||||
* local m = memory(16)
|
||||
* m:set_u32(0, 1234)
|
||||
* print(m:u32(0)) -- 1234
|
||||
* m:set_text(4, "hi") -- 7 bytes: "hi" + NUL is at 4..6
|
||||
* print(m:text(4)) -- hi
|
||||
*/
|
||||
#ifndef CAT_TYPES_MEMORY_H
|
||||
#define CAT_TYPES_MEMORY_H
|
||||
|
||||
#include "cat/value.h"
|
||||
|
||||
struct CAT_VM_State;
|
||||
|
||||
/* CAT_TYPES_MEMORY_Memory -- an owned, bounds-checked byte buffer. */
|
||||
typedef struct CAT_TYPES_MEMORY_Memory {
|
||||
CAT_VALUE_Value base;
|
||||
unsigned char *data;
|
||||
size_t size;
|
||||
} CAT_TYPES_MEMORY_Memory;
|
||||
|
||||
/* Allocate a zeroed buffer of `size` bytes. */
|
||||
CAT_VALUE_Value *cat_types_memory_new(size_t size);
|
||||
|
||||
/* Wrap existing bytes (copies `size` bytes; caller keeps its own). */
|
||||
CAT_VALUE_Value *cat_types_memory_from(const void *src, size_t size);
|
||||
|
||||
/* The raw bytes and size (for native code that needs the storage). */
|
||||
unsigned char *cat_types_memory_data(CAT_VALUE_Value *v);
|
||||
size_t cat_types_memory_size(CAT_VALUE_Value *v);
|
||||
|
||||
CAT_VALUE_Vtable *cat_types_memory_vtable(void);
|
||||
|
||||
#endif /* CAT_TYPES_MEMORY_H */
|
||||
Reference in new issue
Block a user