From ec044397e62eb00fdd55aeaecffc48d49b1dc42c Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Wed, 28 Feb 2024 17:36:39 +0800 Subject: [PATCH] use rtti to get type --- include/pocketpy/cffi.h | 6 ++---- include/pocketpy/common.h | 2 ++ include/pocketpy/vm.h | 5 ++++- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/include/pocketpy/cffi.h b/include/pocketpy/cffi.h index c2c72933..98c7878c 100644 --- a/include/pocketpy/cffi.h +++ b/include/pocketpy/cffi.h @@ -7,10 +7,7 @@ namespace pkpy { #define PY_CLASS(T, mod, name) \ - static Type _type(VM* vm) { \ - PK_LOCAL_STATIC const std::pair _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; \ } diff --git a/include/pocketpy/common.h b/include/pocketpy/common.h index bfe8cdc2..d1b774a7 100644 --- a/include/pocketpy/common.h +++ b/include/pocketpy/common.h @@ -18,6 +18,7 @@ #include #include #include +#include #include #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; } diff --git a/include/pocketpy/vm.h b/include/pocketpy/vm.h index 1c1f0593..0363a652 100644 --- a/include/pocketpy/vm.h +++ b/include/pocketpy/vm.h @@ -141,6 +141,9 @@ public: // cached code objects for FSTRING_EVAL std::map _cached_codes; + // typeid -> Type + std::map _cxx_typeid_map; + void (*_ceval_on_step)(VM*, Frame*, Bytecode bc) = nullptr; LineProfiler* _profiler = nullptr; @@ -191,7 +194,7 @@ public: return _run_top_frame(); } - void _push_varargs(){ } + void _push_varargs(){} void _push_varargs(PyObject* _0){ PUSH(_0); } void _push_varargs(PyObject* _0, PyObject* _1){ PUSH(_0); PUSH(_1); } void _push_varargs(PyObject* _0, PyObject* _1, PyObject* _2){ PUSH(_0); PUSH(_1); PUSH(_2); }