From 6099e623141f3436c900430787b9f06a35ce699b Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Fri, 24 May 2024 23:16:29 +0800 Subject: [PATCH] change `PY_NULL` into `nullptr` --- include/pocketpy/common.h | 10 +++++----- include/pocketpy/obj.h | 6 +++--- src/codeobject.cpp | 1 - 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/include/pocketpy/common.h b/include/pocketpy/common.h index 6bf29c51..c2f39b43 100644 --- a/include/pocketpy/common.h +++ b/include/pocketpy/common.h @@ -182,7 +182,7 @@ struct const_sso_var {}; struct PyVar final{ Type type; - bool is_sso; + bool is_ptr; uint8_t flags; // 12 bytes SSO int _0; i64 _1; @@ -192,14 +192,14 @@ struct PyVar final{ /* We must initialize all members to allow == operator to work correctly */ // constexpr initialized - constexpr PyVar(const const_sso_var&, Type type, int value): type(type), is_sso(true), flags(0), _0(value), _1(0) {} + constexpr PyVar(const const_sso_var&, Type type, int value): type(type), is_ptr(false), flags(0), _0(value), _1(0) {} // zero initialized - constexpr PyVar(std::nullptr_t): type(0), is_sso(false), flags(0), _0(0), _1(0) {} + constexpr PyVar(std::nullptr_t): type(0), is_ptr(false), flags(0), _0(0), _1(0) {} // PyObject* initialized (is_sso = false) - PyVar(Type type, PyObject* p): type(type), is_sso(false), flags(0), _0(0), _1(reinterpret_cast(p)) {} + PyVar(Type type, PyObject* p): type(type), is_ptr(true), flags(0), _0(0), _1(reinterpret_cast(p)) {} // SSO initialized (is_sso = true) template - PyVar(Type type, T value): type(type), is_sso(true), flags(0), _0(0), _1(0) { + PyVar(Type type, T value): type(type), is_ptr(false), flags(0), _0(0), _1(0) { static_assert(sizeof(T) <= 12, "SSO size exceeded"); as() = value; } diff --git a/include/pocketpy/obj.h b/include/pocketpy/obj.h index c3488ae4..597f23df 100644 --- a/include/pocketpy/obj.h +++ b/include/pocketpy/obj.h @@ -153,7 +153,7 @@ inline constexpr int py_sizeof = PyObject::FIXED_SIZE + sizeof(T); const int kTpIntIndex = 3; const int kTpFloatIndex = 4; -inline bool is_tagged(PyVar p) noexcept { return p.is_sso; } +inline bool is_tagged(PyVar p) noexcept { return !p.is_ptr; } inline bool is_float(PyVar p) noexcept { return p.type.index == kTpFloatIndex; } inline bool is_int(PyVar p) noexcept { return p.type.index == kTpIntIndex; } @@ -189,7 +189,7 @@ obj_get_t PyVar::obj_get(){ } #define PK_OBJ_GET(T, obj) (obj).obj_get() -#define PK_OBJ_MARK(obj) if(!is_tagged(obj)) vm->__obj_gc_mark(obj.get()); +#define PK_OBJ_MARK(obj) if((obj).is_ptr) vm->__obj_gc_mark(obj.get()); #define VAR(x) py_var(vm, x) #define CAST(T, x) py_cast(vm, x) @@ -207,7 +207,7 @@ inline bool try_cast_int(PyVar obj, i64* val) noexcept { return false; } -extern PyVar const PY_NULL; +#define PY_NULL nullptr extern PyVar const PY_OP_CALL; extern PyVar const PY_OP_YIELD; diff --git a/src/codeobject.cpp b/src/codeobject.cpp index 7749086b..b45cc3bd 100644 --- a/src/codeobject.cpp +++ b/src/codeobject.cpp @@ -7,7 +7,6 @@ namespace pkpy{ blocks.push_back(CodeBlock(CodeBlockType::NO_BLOCK, -1, 0, 0)); } - PyVar const PY_NULL(Type(), new PyObject(Type())); PyVar const PY_OP_CALL(Type(), new PyObject(Type())); PyVar const PY_OP_YIELD(Type(), new PyObject(Type())); } // namespace pkpy \ No newline at end of file