diff --git a/include/pocketpy/vm.h b/include/pocketpy/vm.h index 4a146d32..cfd0de2b 100644 --- a/include/pocketpy/vm.h +++ b/include/pocketpy/vm.h @@ -48,6 +48,8 @@ struct PyTypeInfo{ Vt(): _dtor(nullptr), _gc_mark(nullptr) {} + operator bool() const { return _dtor || _gc_mark; } + template inline static Vt get(){ static_assert(std::is_same_v>); diff --git a/src/vm.cpp b/src/vm.cpp index fd8d3513..5c9336f2 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -212,6 +212,15 @@ namespace pkpy{ Str error = _S("type ", base_info.name.escape(), " is not `subclass_enabled`"); throw std::runtime_error(error.c_str()); } + if(base_info.vt){ + if(vt){ + Str error = _S("type ", base_info.name.escape(), " has a custom vtable, cannot override"); + throw std::runtime_error(error.c_str()); + }else{ + // promote base vt to its subclass + vt = base_info.vt; + } + } _all_types.emplace_back(obj, base, mod, name, subclass_enabled, vt); return obj; }