blueloveTH 1 سال پیش
والد
کامیت
73bca886b5
6فایلهای تغییر یافته به همراه31 افزوده شده و 7 حذف شده
  1. 2 1
      build_g.sh
  2. 0 2
      include/pocketpy/interpreter/vm.h
  3. 9 2
      include/pocketpy/pocketpy.h
  4. 5 0
      src/public/cast.c
  5. 13 0
      src/public/stackops.c
  6. 2 2
      src/public/values.c

+ 2 - 1
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

+ 0 - 2
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;

+ 9 - 2
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);

+ 5 - 0
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;
 }

+ 13 - 0
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;

+ 2 - 2
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); }