From cd99034f797a9b66e646012ce394771271b0b502 Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Thu, 17 Jun 2021 08:28:55 +0200 Subject: [PATCH] Berry improve inheritance #107 --- lib/libesp32/Berry/src/be_class.c | 2 ++ lib/libesp32/Berry/src/be_class.h | 3 +++ lib/libesp32/Berry/src/be_vm.c | 5 +++++ 3 files changed, 10 insertions(+) diff --git a/lib/libesp32/Berry/src/be_class.c b/lib/libesp32/Berry/src/be_class.c index 280e3e6d0..ffb196372 100644 --- a/lib/libesp32/Berry/src/be_class.c +++ b/lib/libesp32/Berry/src/be_class.c @@ -165,6 +165,7 @@ static binstance* newobjself(bvm *vm, bclass *c) while (v < end) { var_setnil(v); ++v; } obj->_class = c; obj->super = NULL; + obj->sub = NULL; } return obj; } @@ -178,6 +179,7 @@ static binstance* newobject(bvm *vm, bclass *c) be_incrtop(vm); /* protect new objects from GC */ for (c = c->super; c; c = c->super) { prev->super = newobjself(vm, c); + prev->super->sub = prev; prev = prev->super; } be_stackpop(vm, 1); diff --git a/lib/libesp32/Berry/src/be_class.h b/lib/libesp32/Berry/src/be_class.h index 148f1b57a..3b9458013 100644 --- a/lib/libesp32/Berry/src/be_class.h +++ b/lib/libesp32/Berry/src/be_class.h @@ -18,11 +18,13 @@ #define be_class_members(cl) ((cl)->members) #define be_class_super(cl) ((cl)->super) #define be_class_setsuper(self, sup) ((self)->super = (sup)) +#define be_class_setsub(self, sub) ((self)->sub = (sub)) #define be_instance_name(obj) ((obj)->_class->name) #define be_instance_class(obj) ((obj)->_class) #define be_instance_members(obj) ((obj)->members) #define be_instance_member_count(obj) ((obj)->_class->nvar) #define be_instance_super(obj) ((obj)->super) +#define be_instance_sub(obj) ((obj)->sub) struct bclass { bcommon_header; @@ -41,6 +43,7 @@ struct bclass { struct binstance { bcommon_header; struct binstance *super; + struct binstance *sub; bclass *_class; bgcobject *gray; /* for gc gray list */ bvalue members[1]; /* members variable data field */ diff --git a/lib/libesp32/Berry/src/be_vm.c b/lib/libesp32/Berry/src/be_vm.c index b58da91d9..97fee645c 100644 --- a/lib/libesp32/Berry/src/be_vm.c +++ b/lib/libesp32/Berry/src/be_vm.c @@ -779,6 +779,11 @@ newframe: /* a new call frame */ int type = obj_attribute(vm, b, c, a); reg = vm->reg; if (basetype(type) == BE_FUNCTION) { + /* check if the object is a superinstance, if so get the lowest possible subclass */ + while (obj->sub) { + obj = obj->sub; + } + var_setobj(&self, var_type(&self), obj); /* replace superinstance by lowest subinstance */ a[1] = self; } else { vm_error(vm, "attribute_error",