From a950219f1aa2b65b63ee10a77d082f85f7b2b8d5 Mon Sep 17 00:00:00 2001 From: Minus Date: Tue, 17 Nov 2026 16:19:12 +0000 Subject: [PATCH] bytecode injection: bc_fn builds a first-class function Functions were already objects (they carry a member table + vtable, so `_doc_` and any attribute already worked). What was missing was a way to turn a chunk table into a *function value* rather than just running it: `bc_run` builds a prototype, runs it and frees it. - build.c: new `bc_fn(chunk)` returns a real closure built from the chunk. It can be stored, passed around, given attributes and called later, exactly like a compiler-produced closure. - function.{h,c}: `cat_types_function_new_owned()` so a closure built at runtime owns (and frees) its prototype; compiler-produced closures still do not (proto is owned by the module/bytecode tree). - tests/bc.cat: type/name/call/hof + injecting `_doc_` on the built function; wired into the suite (98 total, all green). --- include/cat/stdlib/build.h | 2 +- include/cat/types/function.h | 6 ++++++ src/cat/stdlib/build.c | 23 +++++++++++++++++++++++ src/cat/types/function.c | 15 ++++++++++++++- tests/bc.cat | 33 +++++++++++++++++++++++++++++++++ tests/run_tests.sh | 10 ++++++++++ 6 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 tests/bc.cat diff --git a/include/cat/stdlib/build.h b/include/cat/stdlib/build.h index 7685a5a..434691d 100644 --- a/include/cat/stdlib/build.h +++ b/include/cat/stdlib/build.h @@ -19,7 +19,7 @@ #include "cat/vm.h" -/* Install the builder natives (bc_run, bc_dump) into `vm`. */ +/* Install the builder natives (bc_run, bc_fn, bc_dump) into `vm`. */ int cat_stdlib_build_install(CAT_VM_State *vm); #endif /* CAT_STDLIB_BUILD_H */ diff --git a/include/cat/types/function.h b/include/cat/types/function.h index 2b45452..8b03d3a 100644 --- a/include/cat/types/function.h +++ b/include/cat/types/function.h @@ -18,11 +18,17 @@ typedef struct CAT_TYPES_FUNCTION_Closure { CAT_VALUE_Value **upvals; /* captured values */ int nupvals; CAT_VALUE_Value *attrs; /* optional member table (e.g. `_doc_`) */ + int owns_proto; /* 1 if this closure must free `proto` */ } CAT_TYPES_FUNCTION_Closure; /* Wrap a prototype as a callable closure. */ CAT_VALUE_Value *cat_types_function_new(struct CAT_BYTECODE_Function *proto); +/* Like cat_types_function_new but the closure takes ownership of + * `proto` and frees it when the closure is collected. Used when a + * function is built at runtime (e.g. injected bytecode). */ +CAT_VALUE_Value *cat_types_function_new_owned(struct CAT_BYTECODE_Function *proto); + /* The closure's own member table (created lazily; may be NULL). */ CAT_VALUE_Value *cat_types_function_attrs(CAT_VALUE_Value *fn); diff --git a/src/cat/stdlib/build.c b/src/cat/stdlib/build.c index de77a2c..24c5d5d 100644 --- a/src/cat/stdlib/build.c +++ b/src/cat/stdlib/build.c @@ -13,6 +13,7 @@ #include "cat/types/int.h" #include "cat/types/empty.h" #include "cat/types/ascii_text.h" +#include "cat/types/function.h" #include "cat/error.h" #include @@ -124,9 +125,31 @@ static int lf_bc_dump(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs, return 0; } +/* bc_fn(chunk) -- turn a chunk table into a real, first-class + * function value (a closure). Unlike bc_run, the function is not + * called here: it can be stored, passed around, given attributes, and + * invoked later, just like any other value. The closure owns the + * bytecode it was built from. */ +static int lf_bc_fn(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs, + CAT_VALUE_Value **out) +{ + CAT_BYTECODE_Function *proto; + + (void)vm; + if (nargs < 1) { + *out = cat_types_empty(); + return 0; + } + proto = build_function(args[0]); + *out = proto ? cat_types_function_new_owned(proto) + : cat_types_empty(); + return 0; +} + int cat_stdlib_build_install(CAT_VM_State *vm) { cat_vm_register_native(vm, "bc_run", lf_bc_run); + cat_vm_register_native(vm, "bc_fn", lf_bc_fn); cat_vm_register_native(vm, "bc_dump", lf_bc_dump); return 0; } diff --git a/src/cat/types/function.c b/src/cat/types/function.c index a0d940c..e841a8b 100644 --- a/src/cat/types/function.c +++ b/src/cat/types/function.c @@ -68,7 +68,10 @@ static void function_destroy(CAT_VALUE_Value *self) { CAT_TYPES_FUNCTION_Closure *cl = (CAT_TYPES_FUNCTION_Closure *)self; free(cl->upvals); - /* proto is owned by its enclosing module/bytecode tree */ + /* proto is owned by its enclosing module/bytecode tree, unless this + * closure built it at runtime (injected bytecode) */ + if (cl->owns_proto && cl->proto != NULL) + cat_bytecode_function_free(cl->proto); free(cl); } @@ -105,5 +108,15 @@ CAT_VALUE_Value *cat_types_function_new(CAT_BYTECODE_Function *proto) cl->proto = proto; cl->nupvals = 0; cl->upvals = NULL; + cl->owns_proto = 0; return &cl->base; } + +CAT_VALUE_Value *cat_types_function_new_owned(CAT_BYTECODE_Function *proto) +{ + CAT_VALUE_Value *fn = cat_types_function_new(proto); + + if (fn != NULL) + ((CAT_TYPES_FUNCTION_Closure *)fn)->owns_proto = 1; + return fn; +} diff --git a/tests/bc.cat b/tests/bc.cat new file mode 100644 index 0000000..de2f8b7 --- /dev/null +++ b/tests/bc.cat @@ -0,0 +1,33 @@ +-- tests/bc.cat -- injecting bytecode as a first-class function. +-- +-- A chunk table (see build.h) is turned into a real, callable function +-- value by `bc_fn`. Unlike `bc_run`, the function is not called here: +-- it can be stored, passed around, given attributes, and invoked later, +-- exactly like a closure the compiler produced. Opcodes come from +-- include/cat/bytecode.h. + +-- add(a, b): push local 0, push local 1, ADD, RETURN. +local add = { + name = "add", nparams = 2, nlocals = 2, + consts = { 0 }, + code = { + { op = 5, a = 0, b = 0, c = 0, line = 1 }, -- GETLOCAL 0 + { op = 5, a = 1, b = 0, c = 0, line = 1 }, -- GETLOCAL 1 + { op = 9, a = 0, b = 0, c = 0, line = 1 }, -- ADD + { op = 30, a = 0, b = 0, c = 0, line = 1 } -- RETURN + } +} +local f = bc_fn(add) + +print("type:", type(f)) +print("name:", f.name) +print("call:", f(20, 22)) +print("call2:", f(1, 2)) + +-- functions are objects: inject attributes and document them. +f._doc_ = "soma dois numeros" +print("doc:", f._doc_) + +-- higher-order: pass the injected function as a value. +function apply(fn, x, y) return fn(x, y) end +print("hof:", apply(f, 100, 1)) diff --git a/tests/run_tests.sh b/tests/run_tests.sh index 585dd39..a9c8a31 100644 --- a/tests/run_tests.sh +++ b/tests/run_tests.sh @@ -174,6 +174,16 @@ 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`" +# ---- bytecode injection (fc_fn) -------------------------------------- + +out=`$BIN tests/bc.cat` +check "bc-type" "type: function" "`echo "$out" | sed -n 1p`" +check "bc-name" "name: add" "`echo "$out" | sed -n 2p`" +check "bc-call" "call: 42" "`echo "$out" | sed -n 3p`" +check "bc-call2" "call2: 3" "`echo "$out" | sed -n 4p`" +check "bc-doc" "doc: soma dois numeros" "`echo "$out" | sed -n 5p`" +check "bc-hof" "hof: 101" "`echo "$out" | sed -n 6p`" + # ---- bytecode dump --------------------------------------------------- out=`$BIN --dump examples/hello.cat`