From 9eebc5bbed4f53fde9b339ca5091b199ebfeca56 Mon Sep 17 00:00:00 2001 From: khchen Date: Thu, 21 Jul 2022 11:02:36 +0800 Subject: [PATCH 1/2] native class can be inherited in script --- src/core/value.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/core/value.c b/src/core/value.c index 8c945517..60819f2b 100644 --- a/src/core/value.c +++ b/src/core/value.c @@ -553,10 +553,13 @@ Instance* newInstance(PKVM* vm, Class* cls) { vmPushTempRef(vm, &inst->_super); // inst. inst->cls = cls; - if (cls->new_fn != NULL) { - inst->native = cls->new_fn(vm); - } else { - inst->native = NULL; + inst->native = NULL; + while (cls != NULL) { + if (cls->new_fn != NULL) { + inst->native = cls->new_fn(vm); + break; + } + cls = cls->super_class; } inst->attribs = newMap(vm); From b16080d3b2f740b9c185b4a7d98ddf2c8eb79a4b Mon Sep 17 00:00:00 2001 From: khchen Date: Thu, 11 Aug 2022 11:23:16 +0800 Subject: [PATCH 2/2] look up destructor in inheritance chain, too. --- src/core/value.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/core/value.c b/src/core/value.c index 60819f2b..4a525b69 100644 --- a/src/core/value.c +++ b/src/core/value.c @@ -1223,9 +1223,15 @@ void freeObject(PKVM* vm, Object* self) { case OBJ_INST: { Instance* inst = (Instance*)self; - if (inst->cls->delete_fn != NULL) { - inst->cls->delete_fn(vm, inst->native); + Class* cls = inst->cls; + while (cls != NULL) { + if (cls->delete_fn != NULL) { + cls->delete_fn(vm, inst->native); + break; + } + cls = cls->super_class; } + DEALLOCATE(vm, inst, Instance); return; }