blueloveTH 1 year ago
parent
commit
393952d800
2 changed files with 10 additions and 1 deletions
  1. 3 0
      include/pocketpy/common.h
  2. 7 1
      include/pocketpy/obj.h

+ 3 - 0
include/pocketpy/common.h

@@ -219,6 +219,9 @@ struct PyVar final{
     }
 
     i64 hash() const { return as<i64>(); }
+
+    template<typename T>
+    T& obj_get();
 };
 
 static_assert(sizeof(PyVar) == 16 && is_pod_v<PyVar>);

+ 7 - 1
include/pocketpy/obj.h

@@ -165,8 +165,14 @@ StrName _type_name(VM* vm, Type type);
 template<typename T> T to_void_p(VM*, PyVar);
 PyVar from_void_p(VM*, void*);
 
+template<typename T>
+T& PyVar::obj_get(){
+    static_assert(!std::is_reference_v<T>);
+    if(is_sso) return as<T>();
+    return ((Py_<T>*)(get()))->_value;
+}
 
-#define PK_OBJ_GET(T, obj) (is_sso_v<T> ? obj.as<T>() : (((Py_<T>*)(obj.get()))->_value))
+#define PK_OBJ_GET(T, obj) (obj).obj_get<T>()
 
 #define PK_OBJ_MARK(obj) \
     if(!is_tagged(obj) && !(obj)->gc_marked) {                          \