This commit is contained in:
blueloveTH 2024-06-16 17:46:24 +08:00
parent 26815fae74
commit d21a9cffab
6 changed files with 23 additions and 38 deletions

View File

@ -34,7 +34,6 @@ typedef struct PyVar{
static_assert(sizeof(PyVar) == 16, "sizeof(PyVar) != 16");
PK_INLINE bool PyVar__is_null(const PyVar* self) { return self->type == 0; }
PK_INLINE PyObject* PyVar__get(const PyVar* self) { return self->_obj; }
PK_INLINE int64_t PyVar__hash(const PyVar* self) { return self->flags_ex + self->_i64; }
PK_INLINE bool PyVar__less(const PyVar* self, const PyVar* other){
@ -52,6 +51,8 @@ PK_INLINE void PyVar__ctor(PyVar* self, pkpy_Type type, PyObject* obj){
self->_obj = obj;
}
void PyVar__ctor2(PyVar* self, PyObject* existing);
#define pkpy_Var__is_null(self) ((self)->type == 0)
#define pkpy_Var__set_null(self) do { (self)->type = 0; } while(0)
bool pkpy_Var__eq__(void *vm, PyVar a, PyVar b);

View File

@ -25,7 +25,9 @@ struct PyVar final: ::PyVar {
PyVar() = default;
// implict conversion
PyVar(PyObject* p);
PyVar(PyObject* existing){
PyVar__ctor2(this, (::PyObject*)existing);
}
/* We must initialize all members to allow == operator to work correctly */
// zero initialized
@ -35,11 +37,7 @@ struct PyVar final: ::PyVar {
// PyObject* initialized (is_sso = false)
PyVar(Type type, PyObject* p){
this->type = type;
this->is_ptr = true;
this->flags = 0;
this->flags_ex = 0;
this->_obj = (::PyObject*)p;
PyVar__ctor(this, type, (::PyObject*)p);
}
// SSO initialized (is_sso = true)
@ -67,20 +65,12 @@ struct PyVar final: ::PyVar {
explicit operator bool () const { return (bool)type; }
void set_null() {
_qword(0) = 0;
_qword(1) = 0;
memset(this, 0, sizeof(PyVar));
}
i64 _qword(int i) const { return ((const i64*)this)[i]; }
i64& _qword(int i) { return ((i64*)this)[i]; }
bool operator== (const PyVar& other) const { return _qword(0) == other._qword(0) && _qword(1) == other._qword(1); }
bool operator!= (const PyVar& other) const { return _qword(0) != other._qword(0) || _qword(1) != other._qword(1); }
bool operator== (const PyVar& other) const { return PyVar__equal(this, &other); }
bool operator!= (const PyVar& other) const { return !PyVar__equal(this, &other); }
bool operator== (std::nullptr_t) const { return !(bool)type; }
bool operator!= (std::nullptr_t) const { return (bool)type; }
PyObject* get() const {
@ -99,7 +89,9 @@ struct PyVar final: ::PyVar {
obj_get_t<T> obj_get();
// std::less<> for map-like containers
bool operator< (const PyVar& other) const { return memcmp(this, &other, sizeof(PyVar)) < 0; }
bool operator< (const PyVar& other) const {
return PyVar__less(this, &other);
}
// implicit convert from ::PyVar
PyVar(const ::PyVar& var) {

View File

@ -26,16 +26,6 @@ PK_INLINE void PyObject__ctor(PyObject* self, pkpy_Type type, bool gc_is_large){
self->_attr = NULL;
}
PK_INLINE PyVar PyVar__fromobj(PyObject* self){
PyVar var;
var.type = self->type;
var.is_ptr = true;
var.flags = 0;
var.flags_ex = 0;
var._obj = self;
return var;
}
#ifdef __cplusplus
}
#endif

View File

@ -27,10 +27,7 @@ struct PyObject final: ::PyObject {
}
PyObject(Type type, bool gc_is_large){
this->type = type;
this->gc_is_large = gc_is_large;
this->gc_marked = false;
this->_attr = nullptr;
PyObject__ctor(this, type, gc_is_large);
}
};

10
src/objects/object.c Normal file
View File

@ -0,0 +1,10 @@
#include "pocketpy/objects/object.h"
void PyVar__ctor2(PyVar* self, PyObject* existing){
assert(existing);
self->type = existing->type;
self->is_ptr = true;
self->flags = 0;
self->flags_ex = 0;
self->_obj = existing;
}

View File

@ -1,5 +0,0 @@
#include "pocketpy/objects/object.hpp"
namespace pkpy {
PyVar::PyVar(PyObject* p) : PyVar(p->type, p) {}
} // namespace pkpy