This commit is contained in:
blueloveTH 2024-06-26 12:52:55 +08:00
parent 73bca886b5
commit 11ea812897
4 changed files with 51 additions and 41 deletions

View File

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

View File

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

View File

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

View File

@ -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); }