From c7a87d05e245558177f4165a2fdb490abf221fd3 Mon Sep 17 00:00:00 2001 From: Minus Date: Mon, 16 Nov 2026 20:03:18 +0000 Subject: [PATCH] structural typing: type of an empty object is empty - empty.c type_name -> 'empty' - lf_type: with structural typing the type of an object IS its structure, so an object with no members has the empty type -- both the canonical 'empty' and a fresh '{}' - ffi test expectation updated; 84/84 tests --- src/cat/stdlib/stdlib.c | 18 +++++++++++++++++- src/cat/types/empty.c | 2 +- tests/run_tests.sh | 2 +- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/cat/stdlib/stdlib.c b/src/cat/stdlib/stdlib.c index 46ad6bc..fef3808 100644 --- a/src/cat/stdlib/stdlib.c +++ b/src/cat/stdlib/stdlib.c @@ -104,8 +104,24 @@ static int lf_tonumber(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs, static int lf_type(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs, CAT_VALUE_Value **out) { + CAT_VALUE_Value *v = nargs > 0 ? args[0] : NULL; + (void)vm; - *out = cat_types_ascii_text_new(nargs > 0 ? cat_value_type_name(args[0]) + /* Structural typing: the type of an object IS its structure, so an + * object with no members has the empty type. That covers both the + * canonical `empty` and a freshly built `{}`. */ + if (v != NULL && v->vtable != NULL && + v->vtable->kind == CAT_VALUE_KIND_TABLE && + (v->vtable == cat_types_table_vtable() || + v->vtable == cat_types_empty_vtable())) { + int members = (v->vtable == cat_types_table_vtable()) + ? cat_types_table_count(v) : 0; + if (members == 0) { + *out = cat_types_ascii_text_new("empty"); + return 0; + } + } + *out = cat_types_ascii_text_new(v != NULL ? cat_value_type_name(v) : "empty"); return 0; } diff --git a/src/cat/types/empty.c b/src/cat/types/empty.c index d253118..faabed8 100644 --- a/src/cat/types/empty.c +++ b/src/cat/types/empty.c @@ -30,7 +30,7 @@ static int empty_set_attribute(CAT_VALUE_Value *self, const char *name, } static CAT_VALUE_Vtable g_empty_vtable = { - .type_name = "object", + .type_name = "empty", .kind = CAT_VALUE_KIND_TABLE, .get_attribute = { empty_get_attribute }, .set_attribute = { empty_set_attribute }, diff --git a/tests/run_tests.sh b/tests/run_tests.sh index bb30809..ea98abe 100644 --- a/tests/run_tests.sh +++ b/tests/run_tests.sh @@ -120,7 +120,7 @@ check "ffi-abs" "abs(-42) = 42" "`echo "$out" | sed -n 5p`" out=`$BIN tests/ffi.cat` check "ffi-badload-empty" "bad load empty: true" "`echo "$out" | sed -n 1p`" -check "ffi-badload-type" "bad load type: object" "`echo "$out" | sed -n 2p`" +check "ffi-badload-type" "bad load type: empty" "`echo "$out" | sed -n 2p`" check "ffi-bound" "pow bound: true" "`echo "$out" | sed -n 3p`" check "ffi-missing-sym" "missing sym empty: true" "`echo "$out" | sed -n 4p`" check "ffi-badsig" "bad sig empty: true" "`echo "$out" | sed -n 5p`"