mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
use rtti to get type
This commit is contained in:
parent
5100f4e454
commit
ec044397e6
@ -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; \
|
||||
}
|
||||
|
@ -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; }
|
||||
|
@ -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;
|
||||
@ -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); }
|
||||
|
Loading…
x
Reference in New Issue
Block a user