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,
|
static int lf_type(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||||
CAT_VALUE_Value **out)
|
CAT_VALUE_Value **out)
|
||||||
{
|
{
|
||||||
|
CAT_VALUE_Value *v = nargs > 0 ? args[0] : NULL;
|
||||||
|
|
||||||
(void)vm;
|
(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");
|
: "empty");
|
||||||
return 0;
|
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 = {
|
static CAT_VALUE_Vtable g_empty_vtable = {
|
||||||
.type_name = "object",
|
.type_name = "empty",
|
||||||
.kind = CAT_VALUE_KIND_TABLE,
|
.kind = CAT_VALUE_KIND_TABLE,
|
||||||
.get_attribute = { empty_get_attribute },
|
.get_attribute = { empty_get_attribute },
|
||||||
.set_attribute = { empty_set_attribute },
|
.set_attribute = { empty_set_attribute },
|
||||||
|
|||||||
+1
-1
@@ -120,7 +120,7 @@ check "ffi-abs" "abs(-42) = 42" "`echo "$out" | sed -n 5p`"
|
|||||||
|
|
||||||
out=`$BIN tests/ffi.cat`
|
out=`$BIN tests/ffi.cat`
|
||||||
check "ffi-badload-empty" "bad load empty: true" "`echo "$out" | sed -n 1p`"
|
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-bound" "pow bound: true" "`echo "$out" | sed -n 3p`"
|
||||||
check "ffi-missing-sym" "missing sym empty: true" "`echo "$out" | sed -n 4p`"
|
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`"
|
check "ffi-badsig" "bad sig empty: true" "`echo "$out" | sed -n 5p`"
|
||||||
|
|||||||
Reference in new issue
Block a user