Compare commits

...

9 Commits

Author SHA1 Message Date
blueloveTH
31fa85f22e some fix 2024-06-26 16:39:17 +08:00
blueloveTH
57563d4e40 some rename 2024-06-26 13:40:53 +08:00
blueloveTH
6956631c61 some fix 2024-06-26 13:35:28 +08:00
blueloveTH
16f1d3fcfb some fix 2024-06-26 13:24:02 +08:00
blueloveTH
b20246b172 some fix 2024-06-26 13:20:57 +08:00
blueloveTH
cff541b94b some fix 2024-06-26 13:17:29 +08:00
blueloveTH
d44781fa1e some fix 2024-06-26 13:12:30 +08:00
blueloveTH
ae002a6c17 Update pocketpy.h 2024-06-26 13:08:14 +08:00
blueloveTH
11ea812897 some fix 2024-06-26 12:52:55 +08:00
6 changed files with 99 additions and 76 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

@ -12,8 +12,6 @@ typedef int16_t py_Type;
typedef PyVar* py_Ref;
typedef int (*py_CFunction)(int argc, py_Ref argv);
typedef struct py_Str py_Str;
typedef struct py_Error{
py_Type type;
} py_Error;
@ -50,30 +48,38 @@ 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);
/// Create a new object.
/// @param out output reference.
/// @param type type of the object.
/// @param slots number of slots. Use -1 to create a `__dict__`.
/// @param udsize size of your userdata. You can use `py_touserdata()` to get the pointer to it.
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);
void py_pushbool(bool);
void py_pushstr(const py_Str*);
void py_pushcstr(const char*);
void py_pushcstrn(const char*, int);
void py_pushstr(const char*);
void py_pushstrn(const char*, int);
void py_pushnone();
void py_pushnull();
void py_push_notimplemented();
/************* Type Cast *************/
int64_t py_toint(py_Ref);
double py_tofloat(py_Ref);
bool py_castfloat(py_Ref, double* out);
bool py_tobool(py_Ref);
const py_Str* py_tostr(py_Ref);
const char* py_tocstr(py_Ref);
int64_t py_toint(const py_Ref);
double py_tofloat(const py_Ref);
bool py_castfloat(const py_Ref, double* out);
bool py_tobool(const py_Ref);
const char* py_tostr(const py_Ref);
const char* py_tostrn(const py_Ref, int* out);
void* py_touserdata(py_Ref);
void* py_touserdata(const py_Ref);
#define py_isint(self) py_istype(self, tp_int)
#define py_isfloat(self) py_istype(self, tp_float)
@ -94,11 +100,13 @@ void py_setslot(py_Ref self, int i, const py_Ref val);
py_Ref py_getupvalue(py_Ref self);
void py_setupvalue(py_Ref self, const py_Ref val);
// int py_getattr(const py_Ref self, py_Name name, py_Ref out);
// int py_setattr(py_Ref self, py_Name name, const py_Ref val);
/// Gets the attribute of the object.
int py_getattr(const py_Ref self, py_Name name, py_Ref out);
/// Sets the attribute of the object.
int py_setattr(py_Ref self, py_Name name, const py_Ref val);
/// Copies src to dst.
void py_copyref(const py_Ref src, py_Ref dst);
/// Equivalent to `*dst = *src`.
void py_assign(py_Ref dst, const py_Ref src);
/************* Stack Operations *************/
py_Ref py_gettop();
@ -115,7 +123,7 @@ void py_pop();
void py_shrink(int n);
/// Pushes the object to the stack.
void py_pushref(const py_Ref src);
void py_push(const py_Ref src);
/// Get a temporary variable from the stack and returns the reference to it.
py_Ref py_pushtmp();
@ -125,7 +133,9 @@ void py_poptmp(int n);
/************* Modules *************/
py_Ref py_newmodule(const char* name, const char* package);
py_Ref py_getmodule(const char* name);
void py_import(const char* name, py_Ref out);
/// Import a module.
int py_import(const char* name, py_Ref out);
/************* Errors *************/
py_Error* py_getlasterror();
@ -165,8 +175,8 @@ void py_dict__setitem(py_Ref self, const py_Ref key, const py_Ref val);
void py_dict__delitem(py_Ref self, const py_Ref key);
void py_dict__clear(py_Ref self);
int py_str(const py_Ref, py_Str* out);
int py_repr(const py_Ref, py_Str* out);
int py_str(const py_Ref, char* out);
int py_repr(const py_Ref, char* out);
#ifdef __cplusplus
}

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,15 +4,15 @@
#include "pocketpy/objects/object.h"
#include "pocketpy/interpreter/vm.h"
int64_t py_toint(py_Ref self){
int64_t py_toint(const py_Ref self){
return self->_i64;
}
double py_tofloat(py_Ref self){
double py_tofloat(const py_Ref self){
return self->_f64;
}
bool py_castfloat(py_Ref self, double* out){
bool py_castfloat(const py_Ref self, double* out){
switch(self->type){
case tp_int:
*out = (double)self->_i64;
@ -28,20 +28,22 @@ bool py_castfloat(py_Ref self, double* out){
}
}
bool py_tobool(py_Ref self){
bool py_tobool(const py_Ref self){
return self->extra;
}
const py_Str* py_tostr(py_Ref self){
return PyObject__value(self->_obj);
const char* py_tostr(const py_Ref self){
py_Str* ud = PyObject__value(self->_obj);
return py_Str__data(ud);
}
const char* py_tocstr(py_Ref self){
const py_Str* s = PyObject__value(self->_obj);
return py_Str__data(s);
const char* py_tostrn(const py_Ref self, int* out){
py_Str* ud = PyObject__value(self->_obj);
*out = ud->size;
return py_Str__data(ud);
}
void* py_touserdata(py_Ref self){
void* py_touserdata(const py_Ref self){
assert(self && self->is_ptr);
return PyObject__value(self->_obj);
}

View File

@ -27,7 +27,7 @@ void py_setslot(py_Ref self, int i, const py_Ref val){
PyObject__slots(self->_obj)[i] = *val;
}
void py_copyref(const py_Ref src, py_Ref dst){
void py_assign(py_Ref dst, const py_Ref src){
*dst = *src;
}
@ -75,7 +75,7 @@ void py_shrink(int n){
vm->stack.sp -= n;
}
void py_pushref(const py_Ref src){
void py_push(const py_Ref src){
pk_VM* vm = pk_current_vm;
*vm->stack.sp++ = *src;
}

View File

@ -4,89 +4,100 @@
#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); }
void py_pushbool(bool val) { py_newbool(pk_current_vm->stack.sp++, val); }
void py_pushstr(const py_Str* val) { py_newstr(pk_current_vm->stack.sp++, py_Str__data(val)); }
void py_pushstr(const char* val) { py_newstr(pk_current_vm->stack.sp++, val); }
void py_pushcstr(const char* val) { py_newstr(pk_current_vm->stack.sp++, val); }
void py_pushstrn(const char* val, int size) { py_newstrn(pk_current_vm->stack.sp++, val, size); }
void py_pushcstrn(const char* val, int size) { py_newstrn(pk_current_vm->stack.sp++, val, size); }
void py_push_none() { py_newnone(pk_current_vm->stack.sp++); }
void py_push_null() { py_newnull(pk_current_vm->stack.sp++); }
void py_push_notimplemented() {
pk_VM* vm = pk_current_vm;