This commit is contained in:
blueloveTH 2024-08-19 14:18:36 +08:00
parent aaf7a84a3a
commit a11d3276f9
7 changed files with 17 additions and 21 deletions

View File

@ -6,6 +6,7 @@
typedef struct py_TypeInfo { typedef struct py_TypeInfo {
py_Name name; py_Name name;
py_Type base; py_Type base;
struct py_TypeInfo* base_ti;
py_TValue self; py_TValue self;
py_TValue module; // the module where the type is defined py_TValue module; // the module where the type is defined

View File

@ -894,7 +894,7 @@ FrameResult VM__run_top_frame(VM* self) {
// call on_end_subclass // call on_end_subclass
py_TypeInfo* ti = TypeList__get(&self->types, py_totype(TOP())); py_TypeInfo* ti = TypeList__get(&self->types, py_totype(TOP()));
if(ti->base != tp_object) { if(ti->base != tp_object) {
py_TypeInfo* base_ti = TypeList__get(&self->types, ti->base); py_TypeInfo* base_ti = ti->base_ti;
if(base_ti->on_end_subclass) base_ti->on_end_subclass(ti); if(base_ti->on_end_subclass) base_ti->on_end_subclass(ti);
} }
} }

View File

@ -33,11 +33,13 @@ static void py_TypeInfo__ctor(py_TypeInfo* self,
py_Name name, py_Name name,
py_Type index, py_Type index,
py_Type base, py_Type base,
py_TypeInfo* base_ti,
py_TValue module) { py_TValue module) {
memset(self, 0, sizeof(py_TypeInfo)); memset(self, 0, sizeof(py_TypeInfo));
self->name = name; self->name = name;
self->base = base; self->base = base;
self->base_ti = base_ti;
// create type object with __dict__ // create type object with __dict__
ManagedHeap* heap = &pk_current_vm->heap; ManagedHeap* heap = &pk_current_vm->heap;
@ -323,7 +325,7 @@ py_Type pk_newtype(const char* name,
if(base_ti && base_ti->is_sealed) { if(base_ti && base_ti->is_sealed) {
c11__abort("type '%s' is not an acceptable base type", py_name2str(base_ti->name)); c11__abort("type '%s' is not an acceptable base type", py_name2str(base_ti->name));
} }
py_TypeInfo__ctor(ti, py_name(name), index, base, module ? *module : *py_NIL); py_TypeInfo__ctor(ti, py_name(name), index, base, base_ti, module ? *module : *py_NIL);
if(!dtor && base) dtor = base_ti->dtor; if(!dtor && base) dtor = base_ti->dtor;
ti->dtor = dtor; ti->dtor = dtor;
ti->is_python = is_python; ti->is_python = is_python;
@ -697,6 +699,4 @@ bool pk_wrapper__NotImplementedError(int argc, py_Ref argv) {
return py_exception(tp_NotImplementedError, ""); return py_exception(tp_NotImplementedError, "");
} }
py_TypeInfo* pk__type_info(py_Type type) { py_TypeInfo* pk__type_info(py_Type type) { return TypeList__get(&pk_current_vm->types, type); }
return TypeList__get(&pk_current_vm->types, type);
}

View File

@ -86,6 +86,5 @@ void pk__add_module_enum() {
py_bindproperty(type, "name", Enum__name, NULL); py_bindproperty(type, "name", Enum__name, NULL);
py_bindproperty(type, "value", Enum__value, NULL); py_bindproperty(type, "value", Enum__value, NULL);
py_TypeInfo* ti = pk__type_info(type); pk__type_info(type)->on_end_subclass = Enum__on_end_subclass;
ti->on_end_subclass = Enum__on_end_subclass;
} }

View File

@ -190,24 +190,22 @@ bool pk_loadmethod(py_StackRef self, py_Name name) {
py_Ref py_tpfindmagic(py_Type t, py_Name name) { py_Ref py_tpfindmagic(py_Type t, py_Name name) {
assert(py_ismagicname(name)); assert(py_ismagicname(name));
TypeList* types = &pk_current_vm->types; py_TypeInfo* ti = pk__type_info(t);
do { do {
py_TypeInfo* ti = TypeList__get(types, t);
py_Ref f = &ti->magic[name]; py_Ref f = &ti->magic[name];
if(!py_isnil(f)) return f; if(!py_isnil(f)) return f;
t = ti->base; ti = ti->base_ti;
} while(t); } while(ti);
return NULL; return NULL;
} }
py_Ref py_tpfindname(py_Type t, py_Name name) { py_Ref py_tpfindname(py_Type t, py_Name name) {
TypeList* types = &pk_current_vm->types; py_TypeInfo* ti = pk__type_info(t);
do { do {
py_TypeInfo* ti = TypeList__get(types, t);
py_Ref res = py_getdict(&ti->self, name); py_Ref res = py_getdict(&ti->self, name);
if(res) return res; if(res) return res;
t = ti->base; ti = ti->base_ti;
} while(t); } while(ti);
return NULL; return NULL;
} }

View File

@ -74,7 +74,7 @@ static bool type__base__(int argc, py_Ref argv) {
PY_CHECK_ARGC(1); PY_CHECK_ARGC(1);
py_TypeInfo* ti = pk__type_info(py_totype(argv)); py_TypeInfo* ti = pk__type_info(py_totype(argv));
if(ti->base) { if(ti->base) {
py_assign(py_retval(), py_tpobject(ti->base)); py_assign(py_retval(), &ti->base_ti->self);
} else { } else {
py_newnone(py_retval()); py_newnone(py_retval());
} }

View File

@ -45,10 +45,8 @@ int py_bool(py_Ref val) {
} }
bool py_hash(py_Ref val, int64_t* out) { bool py_hash(py_Ref val, int64_t* out) {
py_Type t = val->type; py_TypeInfo* ti = pk__type_info(val->type);
TypeList* types = &pk_current_vm->types;
do { do {
py_TypeInfo* ti = TypeList__get(types, t);
py_Ref _hash = &ti->magic[__hash__]; py_Ref _hash = &ti->magic[__hash__];
if(py_isnone(_hash)) break; if(py_isnone(_hash)) break;
py_Ref _eq = &ti->magic[__eq__]; py_Ref _eq = &ti->magic[__eq__];
@ -59,8 +57,8 @@ bool py_hash(py_Ref val, int64_t* out) {
*out = py_toint(py_retval()); *out = py_toint(py_retval());
return true; return true;
} }
t = ti->base; ti = ti->base_ti;
} while(t); } while(ti);
return TypeError("unhashable type: '%t'", val->type); return TypeError("unhashable type: '%t'", val->type);
} }