From 73bca886b53213941b64208603e9fb67706286fe Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Wed, 26 Jun 2024 11:28:38 +0800 Subject: [PATCH] some fix --- build_g.sh | 3 ++- include/pocketpy/interpreter/vm.h | 2 -- include/pocketpy/pocketpy.h | 11 +++++++++-- src/public/cast.c | 5 +++++ src/public/stackops.c | 13 +++++++++++++ src/public/values.c | 4 ++-- 6 files changed, 31 insertions(+), 7 deletions(-) diff --git a/build_g.sh b/build_g.sh index b04f75df..64e2035d 100644 --- a/build_g.sh +++ b/build_g.sh @@ -5,7 +5,8 @@ python prebuild.py SRC=$(find src/ -name "*.c") FLAGS="-std=c11 -lm -Iinclude -O0 -Wfatal-errors -g -DDEBUG -DPK_ENABLE_OS=1" -SANITIZE_FLAGS="-fsanitize=address,leak,undefined" +# SANITIZE_FLAGS="-fsanitize=address,leak,undefined" +SANITIZE_FLAGS="" echo "Compiling C files..." clang $FLAGS $SANITIZE_FLAGS $SRC src2/main.c -o main diff --git a/include/pocketpy/interpreter/vm.h b/include/pocketpy/interpreter/vm.h index 5ff76570..ecdfeb22 100644 --- a/include/pocketpy/interpreter/vm.h +++ b/include/pocketpy/interpreter/vm.h @@ -66,8 +66,6 @@ typedef struct pk_VM { FuncDecl_ __dynamic_func_decl; PyVar __vectorcall_buffer[PK_MAX_CO_VARNAMES]; - void* userdata; // for user-defined data (unused by pkpy itself) - pk_ManagedHeap heap; ValueStack stack; // put `stack` at the end for better cache locality } pk_VM; diff --git a/include/pocketpy/pocketpy.h b/include/pocketpy/pocketpy.h index 45510388..bd728691 100644 --- a/include/pocketpy/pocketpy.h +++ b/include/pocketpy/pocketpy.h @@ -51,10 +51,10 @@ void py_newtuple(py_Ref, int); // 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 userdata); +void py_newfunction2(py_Ref self, 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 userdata); +void py_newnativefunc2(py_Ref self, py_CFunction, int argc, BindType bt, const char* docstring, const py_Ref upvalue); /************* Stack Values Creation *************/ void py_pushint(int64_t); @@ -73,6 +73,8 @@ bool py_tobool(py_Ref); const py_Str* py_tostr(py_Ref); const char* py_tocstr(py_Ref); +void* py_touserdata(py_Ref); + #define py_isint(self) py_istype(self, tp_int) #define py_isfloat(self) py_istype(self, tp_float) #define py_isbool(self) py_istype(self, tp_bool) @@ -89,6 +91,9 @@ void py_setdict(py_Ref self, py_Name name, const py_Ref val); py_Ref py_getslot(const py_Ref self, int i); 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); @@ -100,6 +105,8 @@ py_Ref py_gettop(); void py_settop(const py_Ref); py_Ref py_getsecond(); void py_setsecond(const py_Ref); +void py_duptop(); +void py_dupsecond(); /// Returns a reference to the i-th object from the top of the stack. /// i should be negative, e.g. (-1) means TOS. py_Ref py_peek(int i); diff --git a/src/public/cast.c b/src/public/cast.c index 69f2915d..d32979fd 100644 --- a/src/public/cast.c +++ b/src/public/cast.c @@ -41,6 +41,11 @@ const char* py_tocstr(py_Ref self){ return py_Str__data(s); } +void* py_touserdata(py_Ref self){ + assert(self && self->is_ptr); + return PyObject__value(self->_obj); +} + bool py_istype(const py_Ref self, py_Type type){ return self->type == type; } diff --git a/src/public/stackops.c b/src/public/stackops.c index dab7e619..8926c622 100644 --- a/src/public/stackops.c +++ b/src/public/stackops.c @@ -4,6 +4,7 @@ #include "pocketpy/objects/object.h" #include "pocketpy/interpreter/vm.h" + py_Ref py_getdict(const py_Ref self, py_Name name){ assert(self && self->is_ptr); return pk_NameDict__try_get(PyObject__dict(self->_obj), name); @@ -47,6 +48,18 @@ void py_setsecond(const py_Ref val){ pk_current_vm->stack.sp[-2] = *val; } +void py_duptop(){ + pk_VM* vm = pk_current_vm; + *vm->stack.sp = vm->stack.sp[-1]; + vm->stack.sp++; +} + +void py_dupsecond(){ + pk_VM* vm = pk_current_vm; + *vm->stack.sp = vm->stack.sp[-2]; + vm->stack.sp++; +} + py_Ref py_peek(int i){ assert(i < 0); return pk_current_vm->stack.sp + i; diff --git a/src/public/values.c b/src/public/values.c index 8025c226..2f5e0923 100644 --- a/src/public/values.c +++ b/src/public/values.c @@ -63,7 +63,7 @@ void py_newfunction2(py_Ref self, const char* sig, BindType bt, const char* docstring, - const py_Ref userdata) {} + const py_Ref upvalue) {} void py_newnativefunc(py_Ref self, py_CFunction f, int argc) { py_newnativefunc2(self, f, argc, BindType_FUNCTION, NULL, NULL); @@ -74,7 +74,7 @@ void py_newnativefunc2(py_Ref self, int argc, BindType bt, const char* docstring, - const py_Ref userdata) {} + const py_Ref upvalue) {} void py_pushint(int64_t val) { py_newint(pk_current_vm->stack.sp++, val); }