diff --git a/include/pocketpy/common.h b/include/pocketpy/common.h index e065cf63..1262251e 100644 --- a/include/pocketpy/common.h +++ b/include/pocketpy/common.h @@ -219,6 +219,9 @@ struct PyVar final{ } i64 hash() const { return as(); } + + template + T& obj_get(); }; static_assert(sizeof(PyVar) == 16 && is_pod_v); diff --git a/include/pocketpy/obj.h b/include/pocketpy/obj.h index 7a67a712..65cf31ba 100644 --- a/include/pocketpy/obj.h +++ b/include/pocketpy/obj.h @@ -165,8 +165,14 @@ StrName _type_name(VM* vm, Type type); template T to_void_p(VM*, PyVar); PyVar from_void_p(VM*, void*); +template +T& PyVar::obj_get(){ + static_assert(!std::is_reference_v); + if(is_sso) return as(); + return ((Py_*)(get()))->_value; +} -#define PK_OBJ_GET(T, obj) (is_sso_v ? obj.as() : (((Py_*)(obj.get()))->_value)) +#define PK_OBJ_GET(T, obj) (obj).obj_get() #define PK_OBJ_MARK(obj) \ if(!is_tagged(obj) && !(obj)->gc_marked) { \