From 11ea812897c678dac5e1a3e639348d12403f1864 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Wed, 26 Jun 2024 12:52:55 +0800 Subject: [PATCH] some fix --- include/pocketpy/interpreter/gc.h | 4 +- include/pocketpy/pocketpy.h | 9 ++-- src/interpreter/gc.c | 8 ++-- src/public/values.c | 71 +++++++++++++++++-------------- 4 files changed, 51 insertions(+), 41 deletions(-) diff --git a/include/pocketpy/interpreter/gc.h b/include/pocketpy/interpreter/gc.h index d7844121..eeb005be 100644 --- a/include/pocketpy/interpreter/gc.h +++ b/include/pocketpy/interpreter/gc.h @@ -28,8 +28,8 @@ void pk_ManagedHeap__collect_if_needed(pk_ManagedHeap* self); int pk_ManagedHeap__collect(pk_ManagedHeap* self); int pk_ManagedHeap__sweep(pk_ManagedHeap* self); -PyObject* pk_ManagedHeap__new(pk_ManagedHeap* self, py_Type type, int slots, int size); -PyObject* pk_ManagedHeap__gcnew(pk_ManagedHeap* self, py_Type type, int slots, int size); +PyObject* pk_ManagedHeap__new(pk_ManagedHeap* self, py_Type type, int slots, int udsize); +PyObject* pk_ManagedHeap__gcnew(pk_ManagedHeap* self, py_Type type, int slots, int udsize); // external implementation void pk_ManagedHeap__mark(pk_ManagedHeap* self); diff --git a/include/pocketpy/pocketpy.h b/include/pocketpy/pocketpy.h index bd728691..88f78502 100644 --- a/include/pocketpy/pocketpy.h +++ b/include/pocketpy/pocketpy.h @@ -50,12 +50,13 @@ void py_newtuple(py_Ref, int); // void py_newlist(py_Ref); // new style decl-based function -void py_newfunction(py_Ref self, py_CFunction, const char* sig); -void py_newfunction2(py_Ref self, py_CFunction, const char* sig, BindType bt, const char* docstring, const py_Ref upvalue); +void py_newfunction(py_Ref out, py_CFunction, const char* sig); +void py_newfunction2(py_Ref out, py_CFunction, const char* sig, BindType bt, const char* docstring, const py_Ref upvalue); // old style argc-based function -void py_newnativefunc(py_Ref self, py_CFunction, int argc); -void py_newnativefunc2(py_Ref self, py_CFunction, int argc, BindType bt, const char* docstring, const py_Ref upvalue); +void py_newnativefunc(py_Ref out, py_CFunction, int argc); +void py_newnativefunc2(py_Ref out, py_CFunction, int argc, BindType bt, const char* docstring, const py_Ref upvalue); +void py_newobject(py_Ref out, py_Type type, int slots, int udsize); /************* Stack Values Creation *************/ void py_pushint(int64_t); void py_pushfloat(double); diff --git a/src/interpreter/gc.c b/src/interpreter/gc.c index 891fe53e..181dcb71 100644 --- a/src/interpreter/gc.c +++ b/src/interpreter/gc.c @@ -88,14 +88,14 @@ int pk_ManagedHeap__sweep(pk_ManagedHeap *self){ return freed; } -PyObject* pk_ManagedHeap__new(pk_ManagedHeap *self, py_Type type, int slots, int size){ - PyObject* obj = PyObject__new(type, slots, size); +PyObject* pk_ManagedHeap__new(pk_ManagedHeap *self, py_Type type, int slots, int udsize){ + PyObject* obj = PyObject__new(type, slots, udsize); c11_vector__push(PyObject*, &self->no_gc, obj); return obj; } -PyObject* pk_ManagedHeap__gcnew(pk_ManagedHeap *self, py_Type type, int slots, int size){ - PyObject* obj = PyObject__new(type, slots, size); +PyObject* pk_ManagedHeap__gcnew(pk_ManagedHeap *self, py_Type type, int slots, int udsize){ + PyObject* obj = PyObject__new(type, slots, udsize); c11_vector__push(PyObject*, &self->gen, obj); self->gc_counter++; return obj; diff --git a/src/public/values.c b/src/public/values.c index 2f5e0923..bacdb9ef 100644 --- a/src/public/values.c +++ b/src/public/values.c @@ -4,78 +4,87 @@ #include "pocketpy/objects/object.h" #include "pocketpy/interpreter/vm.h" -void py_newint(py_Ref self, int64_t val) { - self->type = tp_int; - self->is_ptr = false; - self->_i64 = val; +void py_newint(py_Ref out, int64_t val) { + out->type = tp_int; + out->is_ptr = false; + out->_i64 = val; } -void py_newfloat(py_Ref self, double val) { - self->type = tp_float; - self->is_ptr = false; - self->_f64 = val; +void py_newfloat(py_Ref out, double val) { + out->type = tp_float; + out->is_ptr = false; + out->_f64 = val; } -void py_newbool(py_Ref self, bool val) { +void py_newbool(py_Ref out, bool val) { pk_VM* vm = pk_current_vm; - *self = val ? vm->True : vm->False; + *out = val ? vm->True : vm->False; } -void py_newstr(py_Ref self, const char* data) { +void py_newstr(py_Ref out, const char* data) { pk_ManagedHeap* heap = &pk_current_vm->heap; PyObject* obj = pk_ManagedHeap__gcnew(heap, tp_str, 0, sizeof(py_Str)); py_Str__ctor(PyObject__value(obj), data); - self->type = tp_str; - self->is_ptr = true; - self->_obj = obj; + out->type = tp_str; + out->is_ptr = true; + out->_obj = obj; } -void py_newstrn(py_Ref self, const char* data, int size) { +void py_newstrn(py_Ref out, const char* data, int size) { pk_ManagedHeap* heap = &pk_current_vm->heap; PyObject* obj = pk_ManagedHeap__gcnew(heap, tp_str, 0, sizeof(py_Str)); py_Str__ctor2((py_Str*)PyObject__value(obj), data, size); - self->type = tp_str; - self->is_ptr = true; - self->_obj = obj; + out->type = tp_str; + out->is_ptr = true; + out->_obj = obj; } -void py_newnone(py_Ref self) { +void py_newnone(py_Ref out) { pk_VM* vm = pk_current_vm; - *self = vm->None; + *out = vm->None; } -void py_newnull(py_Ref self) { self->type = 0; } +void py_newnull(py_Ref out) { out->type = 0; } -void py_newtuple(py_Ref self, int n) { +void py_newtuple(py_Ref out, int n) { pk_VM* vm = pk_current_vm; PyObject* obj = pk_ManagedHeap__gcnew(&vm->heap, tp_tuple, n, 0); - self->type = tp_tuple; - self->is_ptr = true; - self->_obj = obj; + out->type = tp_tuple; + out->is_ptr = true; + out->_obj = obj; } -void py_newfunction(py_Ref self, py_CFunction f, const char* sig) { - py_newfunction2(self, f, sig, BindType_FUNCTION, NULL, NULL); +void py_newfunction(py_Ref out, py_CFunction f, const char* sig) { + py_newfunction2(out, f, sig, BindType_FUNCTION, NULL, NULL); } -void py_newfunction2(py_Ref self, +void py_newfunction2(py_Ref out, py_CFunction f, const char* sig, BindType bt, const char* docstring, const py_Ref upvalue) {} -void py_newnativefunc(py_Ref self, py_CFunction f, int argc) { - py_newnativefunc2(self, f, argc, BindType_FUNCTION, NULL, NULL); +void py_newnativefunc(py_Ref out, py_CFunction f, int argc) { + py_newnativefunc2(out, f, argc, BindType_FUNCTION, NULL, NULL); } -void py_newnativefunc2(py_Ref self, +void py_newnativefunc2(py_Ref out, py_CFunction f, int argc, BindType bt, const char* docstring, const py_Ref upvalue) {} +void py_newobject(py_Ref out, py_Type type, int slots, int udsize){ + pk_ManagedHeap* heap = &pk_current_vm->heap; + PyObject* obj = pk_ManagedHeap__gcnew(heap, type, slots, udsize); + out->type = type; + out->is_ptr = true; + out->_obj = obj; +} + + void py_pushint(int64_t val) { py_newint(pk_current_vm->stack.sp++, val); } void py_pushfloat(double val) { py_newfloat(pk_current_vm->stack.sp++, val); }