Remove Class_Clone Class_Clone had a fragile implementation and was only used in Class_singleton. Replace it with an internal function to create a simple subclass. Make sure that this function accounts for Init_Obj zeroing the Class object.
Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/e0fd278f Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/e0fd278f Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/e0fd278f Branch: refs/heads/master Commit: e0fd278ff959458175a415a407dd5579f9b721bc Parents: 12c4653 Author: Nick Wellnhofer <[email protected]> Authored: Tue May 12 00:24:46 2015 +0200 Committer: Nick Wellnhofer <[email protected]> Committed: Tue May 12 18:34:59 2015 +0200 ---------------------------------------------------------------------- runtime/core/Clownfish/Class.c | 45 +++++++++++++++++++---------------- runtime/core/Clownfish/Class.cfh | 3 --- 2 files changed, 25 insertions(+), 23 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/e0fd278f/runtime/core/Clownfish/Class.c ---------------------------------------------------------------------- diff --git a/runtime/core/Clownfish/Class.c b/runtime/core/Clownfish/Class.c index e0a3ab5..d35fb94 100644 --- a/runtime/core/Clownfish/Class.c +++ b/runtime/core/Clownfish/Class.c @@ -215,18 +215,6 @@ Class_Destroy_IMP(Class *self) { THROW(ERR, "Insane attempt to destroy Class for class '%o'", self->name); } -Class* -Class_Clone_IMP(Class *self) { - Class *twin - = (Class*)Memory_wrapped_calloc(self->class_alloc_size, 1); - - memcpy(twin, self, self->class_alloc_size); - Class_Init_Obj(self->klass, twin); // Set refcount. - twin->name = Str_Clone(self->name); - - return twin; -} - void Class_Override_IMP(Class *self, cfish_method_t method, size_t offset) { union { char *char_ptr; cfish_method_t *func_ptr; } pointer; @@ -271,6 +259,30 @@ Class_init_registry() { } } +static Class* +S_simple_subclass(Class *parent, String *name) { + Class *subclass + = (Class*)Memory_wrapped_calloc(parent->class_alloc_size, 1); + Class_Init_Obj(parent->klass, subclass); + + subclass->parent = parent; + subclass->flags = parent->flags; + subclass->parcel_id = parent->parcel_id; + subclass->obj_alloc_size = parent->obj_alloc_size; + subclass->class_alloc_size = parent->class_alloc_size; + subclass->methods = (Method**)CALLOCATE(1, sizeof(Method*)); + + subclass->name_internal = Str_Clone(name); + subclass->name + = Str_new_wrap_trusted_utf8(Str_Get_Ptr8(subclass->name_internal), + Str_Get_Size(subclass->name_internal)); + + memcpy(subclass->vtable, parent->vtable, + parent->class_alloc_size - offsetof(Class, vtable)); + + return subclass; +} + Class* Class_singleton(String *class_name, Class *parent) { if (Class_registry == NULL) { @@ -294,14 +306,7 @@ Class_singleton(String *class_name, Class *parent) { } } - // Copy source class. - singleton = Class_Clone(parent); - - // Turn clone into child. - singleton->parent = parent; - DECREF(singleton->name); - singleton->name = Str_Clone(class_name); - singleton->methods = (Method**)CALLOCATE(1, sizeof(Method*)); + singleton = S_simple_subclass(parent, class_name); // Allow host methods to override. fresh_host_methods = Class_fresh_host_methods(class_name); http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/e0fd278f/runtime/core/Clownfish/Class.cfh ---------------------------------------------------------------------- diff --git a/runtime/core/Clownfish/Class.cfh b/runtime/core/Clownfish/Class.cfh index 94c89a5..be8520e 100644 --- a/runtime/core/Clownfish/Class.cfh +++ b/runtime/core/Clownfish/Class.cfh @@ -135,9 +135,6 @@ final class Clownfish::Class inherits Clownfish::Obj { Vector* Get_Methods(Class *self); - public incremented Class* - Clone(Class *self); - public void Destroy(Class *self); }
