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
@@ -24,6 +24,7 @@
|
||||
#include "cat/types/function.h"
|
||||
#include "cat/types/file.h"
|
||||
#include "cat/types/ffi.h"
|
||||
#include "cat/types/memory.h"
|
||||
#include "cat/types/empty.h"
|
||||
|
||||
#include <stdio.h>
|
||||
@@ -544,6 +545,19 @@ static int lf_dofile(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* `memory(n)` -- allocate an owned, bounds-checked byte buffer. */
|
||||
static int lf_memory(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
long long n = (nargs >= 1) ? cat_types_number_as_int(args[0]) : 0;
|
||||
|
||||
(void)vm;
|
||||
if (n < 0)
|
||||
n = 0;
|
||||
*out = cat_types_memory_new((size_t)n);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* `use expr` -- import another catscript source file (alias of dofile),
|
||||
* so `use "std/io/file.cat"` pulls its definitions into this unit. */
|
||||
static int lf_use(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
@@ -667,6 +681,10 @@ int cat_stdlib_stdlib_install(CAT_VM_State *vm)
|
||||
cat_vm_set_global(vm, "ffi", cat_types_ffi_module());
|
||||
/* the empty object: the value for "no value" (catscript has no nil) */
|
||||
cat_vm_set_global(vm, "empty", cat_types_empty());
|
||||
/* `memory(n)` -- an owned, bounds-checked byte buffer */
|
||||
cat_vm_set_global(vm, "memory",
|
||||
cat_types_native_new_doc("memory", lf_memory,
|
||||
"memory(n) -- buffer de n bytes com limites verificados."));
|
||||
/* `module` reflects the unit currently executing (name/path/dir) */
|
||||
cat_vm_set_global(vm, "module", cat_module_table("main", NULL));
|
||||
return 0;
|
||||
|
||||
Reference in new issue
Block a user