Skip to content

Commit 8ce6de1

Browse files
committed
feat: enable direct trait addition from TypeInit
1 parent 3455c95 commit 8ce6de1

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

argon/vm/datatype/arobject.cpp

+13-13
Original file line numberDiff line numberDiff line change
@@ -655,16 +655,7 @@ ArObject *argon::vm::datatype::TypeNew(const TypeInfo *type, const char *name, c
655655
((char *) ret->doc)[doc_len] = '\0';
656656
}
657657

658-
if ((ret->mro = ComputeMRO(ret, bases, length)) == nullptr) {
659-
memory::Free((void *) ret->name);
660-
memory::Free((void *) ret->qname);
661-
memory::Free((void *) ret->doc);
662-
663-
memory::Free(ret);
664-
return nullptr;
665-
}
666-
667-
if (!TypeInit(ret, ns))
658+
if (!TypeInit(ret, ns, bases, length))
668659
Release((ArObject **) &ret);
669660

670661
return (ArObject *) ret;
@@ -1080,7 +1071,7 @@ bool MethodCheckOverride(TypeInfo *type) {
10801071
return true;
10811072
}
10821073

1083-
bool argon::vm::datatype::TypeInit(TypeInfo *type, ArObject *auxiliary) {
1074+
bool argon::vm::datatype::TypeInit(TypeInfo *type, ArObject *auxiliary, TypeInfo **bases, unsigned int length) {
10841075
bool qname_free = false;
10851076

10861077
if (ENUMBITMASK_ISTRUE(type->flags, TypeInfoFlags::INITIALIZED))
@@ -1089,9 +1080,14 @@ bool argon::vm::datatype::TypeInit(TypeInfo *type, ArObject *auxiliary) {
10891080
assert(type->tp_map == nullptr);
10901081

10911082
// Calculate MRO
1083+
if (bases != nullptr && length > 0) {
1084+
if ((type->mro = ComputeMRO(type, bases, length)) == nullptr)
1085+
return false;
1086+
}
1087+
10921088
if (type->object != nullptr && type->object->traits != nullptr) {
10931089
// Count base traits
1094-
unsigned int length = 0;
1090+
length = 0;
10951091
for (auto **base = type->object->traits; *base != nullptr; length++, base++);
10961092

10971093
if ((type->mro = ComputeMRO(type, type->object->traits, length)) == nullptr)
@@ -1141,9 +1137,13 @@ bool argon::vm::datatype::TypeInit(TypeInfo *type, ArObject *auxiliary) {
11411137
return true;
11421138

11431139
ERROR:
1140+
Release(&type->mro);
11441141
Release(&type->tp_map);
1145-
if (qname_free)
1142+
1143+
if (qname_free) {
11461144
vm::memory::Free((char *) type->qname);
1145+
type->qname = nullptr;
1146+
}
11471147

11481148
return false;
11491149
}

argon/vm/datatype/arobject.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ namespace argon::vm::datatype {
7070

7171
bool IsTrue(const ArObject *object);
7272

73-
bool TypeInit(TypeInfo *type, ArObject *auxiliary);
73+
bool TypeInit(TypeInfo *type, ArObject *auxiliary, TypeInfo **bases, unsigned int length);
74+
75+
inline bool TypeInit(TypeInfo *type, ArObject *auxiliary) {
76+
return TypeInit(type, auxiliary, nullptr, 0);
77+
}
7478

7579
bool TraitIsImplemented(const TypeInfo *obj_type, const TypeInfo *type);
7680

0 commit comments

Comments
 (0)