summaryrefslogtreecommitdiffstats
path: root/struct.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2021-07-09 08:07:02 -0700
committerKaz Kylheku <kaz@kylheku.com>2021-07-09 08:07:02 -0700
commitc6cc120ad042fa234da17cf929c6fdfae1000a5b (patch)
tree1b4537d65bfece34b3176242a364f7c6df77e0da /struct.c
parent618a854df42cb43e410ba488a6634dae16a3e36f (diff)
downloadtxr-c6cc120ad042fa234da17cf929c6fdfae1000a5b.tar.gz
txr-c6cc120ad042fa234da17cf929c6fdfae1000a5b.tar.bz2
txr-c6cc120ad042fa234da17cf929c6fdfae1000a5b.zip
struct: rework stype_handle change.
The change which allows stype_handle to recognize struct instances, and obtain their type, has some possibly unwanted ramifications, since the function is widely used. Let's refactor things so that, for now, only the struct_type_name function takes advantage of this flexibility. * struct.c (stype_handle_impl): New static function, copy of stype_handle, but taking an obj_ok Boolean argument to indicate whether an object instance is an acceptable argument, whose type should be fetched. (stype_handle): Now a wrapper around stype_handle_impl passing nil for the obj_ok argument. (stype_handle_obj): New static function. Passes t for the ok_obj argument of stype_handle_impl. (struct_type_name): Call stype_handle_obj instead of stype_handle. (super): Take advantage of stype_handle_obj to reduce code. * txr.1: Update documentation of struct-type-name, and improve that of super.
Diffstat (limited to 'struct.c')
-rw-r--r--struct.c26
1 files changed, 14 insertions, 12 deletions
diff --git a/struct.c b/struct.c
index 61916ddb..aa6b86d1 100644
--- a/struct.c
+++ b/struct.c
@@ -274,7 +274,7 @@ static struct struct_inst *struct_handle(val obj, val ctx)
ctx, obj, nao);
}
-static struct struct_type *stype_handle(val *pobj, val ctx)
+static struct struct_type *stype_handle_impl(val *pobj, val obj_ok, val ctx)
{
val obj = *pobj;
@@ -291,7 +291,7 @@ static struct struct_type *stype_handle(val *pobj, val ctx)
case COBJ:
if (obj->co.cls == struct_type_cls)
return coerce(struct struct_type *, obj->co.handle);
- if (obj->co.cls == struct_cls)
+ if (obj_ok && obj->co.cls == struct_cls)
return struct_handle(obj, ctx)->type;
/* fallthrough */
default:
@@ -300,6 +300,16 @@ static struct struct_type *stype_handle(val *pobj, val ctx)
}
}
+static struct struct_type *stype_handle(val *pobj, val ctx)
+{
+ return stype_handle_impl(pobj, nil, ctx);
+}
+
+static struct struct_type *stype_handle_obj(val *pobj, val ctx)
+{
+ return stype_handle_impl(pobj, t, ctx);
+}
+
static void static_slot_home_fixup(struct struct_type *st)
{
cnum i;
@@ -635,15 +645,7 @@ val super(val type, val idx)
self, idx, nao);
{
- struct struct_type *st;
-
- if (structp(type)) {
- struct struct_inst *si = coerce(struct struct_inst *, type->co.handle);
- st = si->type;
- } else {
- st = stype_handle(&type, self);
- }
-
+ struct struct_type *st = stype_handle_obj(&type, self);
return if2(ix < st->nsupers, st->sus[ix]->self);
}
}
@@ -1613,7 +1615,7 @@ val struct_type(val strct)
val struct_type_name(val stype)
{
- struct struct_type *st = stype_handle(&stype, lit("struct-type-name"));
+ struct struct_type *st = stype_handle_obj(&stype, lit("struct-type-name"));
return st->name;
}