use rtti to get type

This commit is contained in:
blueloveTH 2024-02-28 17:36:39 +08:00
parent 5100f4e454
commit ec044397e6
3 changed files with 8 additions and 5 deletions

View File

@ -7,10 +7,7 @@
namespace pkpy {
#define PY_CLASS(T, mod, name) \
static Type _type(VM* vm) { \
PK_LOCAL_STATIC const std::pair<StrName, StrName> _path(#mod, #name); \
return PK_OBJ_GET(Type, vm->_modules[_path.first]->attr(_path.second)); \
} \
static Type _type(VM* vm) { return vm->_cxx_typeid_map[typeid(T)]; } \
static void _check_type(VM* vm, PyObject* val){ \
if(!vm->isinstance(val, T::_type(vm))){ \
vm->TypeError("expected '" #mod "." #name "', got " + _type_name(vm, vm->_tp(val)).escape()); \
@ -24,6 +21,7 @@ namespace pkpy {
} \
PyObject* type = vm->new_type_object(mod, #name, base); \
mod->attr().set(#name, type); \
vm->_cxx_typeid_map[typeid(T)] = PK_OBJ_GET(Type, type); \
T::_register(vm, mod, type); \
return type; \
}

View File

@ -18,6 +18,7 @@
#include <type_traits>
#include <random>
#include <deque>
#include <typeindex>
#include <initializer_list>
#define PK_VERSION "1.4.2"
@ -159,6 +160,7 @@ struct Discarded { };
struct Type {
int index;
constexpr Type(): index(-1) {}
constexpr Type(int index): index(index) {}
bool operator==(Type other) const { return this->index == other.index; }
bool operator!=(Type other) const { return this->index != other.index; }

View File

@ -141,6 +141,9 @@ public:
// cached code objects for FSTRING_EVAL
std::map<std::string_view, CodeObject_> _cached_codes;
// typeid -> Type
std::map<std::type_index, Type> _cxx_typeid_map;
void (*_ceval_on_step)(VM*, Frame*, Bytecode bc) = nullptr;
LineProfiler* _profiler = nullptr;