change PY_NULL into nullptr

This commit is contained in:
blueloveTH 2024-05-24 23:16:29 +08:00
parent 617ff4fc77
commit 6099e62314
3 changed files with 8 additions and 9 deletions

View File

@ -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<i64>(p)) {}
PyVar(Type type, PyObject* p): type(type), is_ptr(true), flags(0), _0(0), _1(reinterpret_cast<i64>(p)) {}
// SSO initialized (is_sso = true)
template<typename T>
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<T>() = value;
}

View File

@ -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<T> PyVar::obj_get(){
}
#define PK_OBJ_GET(T, obj) (obj).obj_get<T>()
#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<T>(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;

View File

@ -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