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
This commit is contained in:
1 parent
0292d88a53
commit
c7a87d05e2
3 files changed
+19
-3
No files matched your search
+17
-1
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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 },
|
||||
|
||||
Reference in new issue
Block a user