blueloveTH 9 miesięcy temu
rodzic
commit
29cd6fe59b

+ 6 - 0
include/pocketpy/config.h

@@ -55,3 +55,9 @@
 #else
     #define PK_PLATFORM_SEP '/'
 #endif
+
+#ifdef __cplusplus
+    #ifndef restrict
+        #define restrict
+    #endif
+#endif

+ 1 - 1
include/pocketpy/pocketpy.h

@@ -47,7 +47,7 @@ typedef py_TValue* py_GlobalRef;
 typedef py_TValue* py_StackRef;
 /// An item reference to a container object. It invalidates when the container is modified.
 typedef py_TValue* py_ItemRef;
-/// An output reference for returning a value.
+/// An output reference for returning a value. Only use this for function arguments.
 typedef py_TValue* py_OutRef;
 
 typedef struct py_Frame py_Frame;

+ 2 - 2
src/interpreter/frame.c

@@ -192,7 +192,7 @@ const char* py_Frame_sourceloc(py_Frame* self, int* lineno) {
     return loc.src->filename->data;
 }
 
-void py_Frame_newglobals(py_Frame* frame, py_Ref out) {
+void py_Frame_newglobals(py_Frame* frame, py_OutRef out) {
     if(!frame) {
         pk_mappingproxy__namedict(out, &pk_current_vm->main);
         return;
@@ -204,7 +204,7 @@ void py_Frame_newglobals(py_Frame* frame, py_Ref out) {
     }
 }
 
-void py_Frame_newlocals(py_Frame* frame, py_Ref out) {
+void py_Frame_newlocals(py_Frame* frame, py_OutRef out) {
     if(!frame) {
         py_newdict(out);
         return;

+ 3 - 3
src/modules/pickle.c

@@ -580,7 +580,7 @@ bool py_pickle_loads_body(const unsigned char* p, int memo_length, c11_smallmap_
             }
             case PKL_BUILD_LIST: {
                 int length = pkl__read_int(&p);
-                py_OutRef val = py_retval();
+                py_Ref val = py_retval();
                 py_newlistn(val, length);
                 for(int i = length - 1; i >= 0; i--) {
                     py_StackRef item = py_peek(-1);
@@ -592,7 +592,7 @@ bool py_pickle_loads_body(const unsigned char* p, int memo_length, c11_smallmap_
             }
             case PKL_BUILD_TUPLE: {
                 int length = pkl__read_int(&p);
-                py_OutRef val = py_retval();
+                py_Ref val = py_retval();
                 py_Ref p = py_newtuple(val, length);
                 for(int i = length - 1; i >= 0; i--) {
                     p[i] = *py_peek(-1);
@@ -603,7 +603,7 @@ bool py_pickle_loads_body(const unsigned char* p, int memo_length, c11_smallmap_
             }
             case PKL_BUILD_DICT: {
                 int length = pkl__read_int(&p);
-                py_OutRef val = py_pushtmp();
+                py_Ref val = py_pushtmp();
                 py_newdict(val);
                 py_StackRef begin = py_peek(-1) - 2 * length;
                 py_StackRef end = py_peek(-1);

+ 3 - 4
src/modules/vmath.c

@@ -4,7 +4,6 @@
 #include "pocketpy/common/sstream.h"
 #include "pocketpy/common/utils.h"
 #include "pocketpy/interpreter/vm.h"
-#include "pocketpy/objects/object.h"
 #include <math.h>
 
 static bool isclose(float a, float b) { return fabs(a - b) < 1e-4; }
@@ -544,7 +543,7 @@ static bool mat3x3__eq__(int argc, py_Ref argv) {
 
 DEFINE_BOOL_NE(mat3x3, mat3x3__eq__)
 
-static void matmul(const c11_mat3x3* lhs, const c11_mat3x3* rhs, c11_mat3x3* out) {
+static void matmul(const c11_mat3x3* lhs, const c11_mat3x3* rhs, c11_mat3x3* restrict out) {
     out->_11 = lhs->_11 * rhs->_11 + lhs->_12 * rhs->_21 + lhs->_13 * rhs->_31;
     out->_12 = lhs->_11 * rhs->_12 + lhs->_12 * rhs->_22 + lhs->_13 * rhs->_32;
     out->_13 = lhs->_11 * rhs->_13 + lhs->_12 * rhs->_23 + lhs->_13 * rhs->_33;
@@ -562,7 +561,7 @@ static float determinant(const c11_mat3x3* m) {
            m->_13 * (m->_21 * m->_32 - m->_22 * m->_31);
 }
 
-static bool inverse(const c11_mat3x3* m, c11_mat3x3* out) {
+static bool inverse(const c11_mat3x3* m, c11_mat3x3* restrict out) {
     float det = determinant(m);
     if(isclose(det, 0)) return false;
     float invdet = 1.0f / det;
@@ -578,7 +577,7 @@ static bool inverse(const c11_mat3x3* m, c11_mat3x3* out) {
     return true;
 }
 
-static void trs(c11_vec2 t, float r, c11_vec2 s, c11_mat3x3* out) {
+static void trs(c11_vec2 t, float r, c11_vec2 s, c11_mat3x3* restrict out) {
     float cr = cosf(r);
     float sr = sinf(r);
     // clang-format off

+ 2 - 2
src/public/modules.c

@@ -500,12 +500,12 @@ static bool builtins_locals(int argc, py_Ref argv) {
     return true;
 }
 
-void py_newglobals(py_Ref out) {
+void py_newglobals(py_OutRef out) {
     py_Frame* frame = pk_current_vm->top_frame;
     py_Frame_newglobals(frame, out);
 }
 
-void py_newlocals(py_Ref out) {
+void py_newlocals(py_OutRef out) {
     py_Frame* frame = pk_current_vm->top_frame;
     py_Frame_newlocals(frame, out);
 }

+ 1 - 1
src/public/py_dict.c

@@ -266,7 +266,7 @@ static bool dict__new__(int argc, py_Ref argv) {
     return true;
 }
 
-void py_newdict(py_Ref out) {
+void py_newdict(py_OutRef out) {
     Dict* ud = py_newobject(out, tp_dict, 0, sizeof(Dict));
     Dict__ctor(ud, 7, 8);
 }

+ 2 - 3
src/public/py_list.c

@@ -1,16 +1,15 @@
 #include "pocketpy/pocketpy.h"
 
 #include "pocketpy/common/utils.h"
-#include "pocketpy/objects/object.h"
 #include "pocketpy/interpreter/vm.h"
 #include "pocketpy/common/sstream.h"
 
-void py_newlist(py_Ref out) {
+void py_newlist(py_OutRef out) {
     List* ud = py_newobject(out, tp_list, 0, sizeof(List));
     c11_vector__ctor(ud, sizeof(py_TValue));
 }
 
-void py_newlistn(py_Ref out, int n) {
+void py_newlistn(py_OutRef out, int n) {
     py_newlist(out);
     List* ud = py_touserdata(out);
     c11_vector__reserve(ud, n);

+ 1 - 2
src/public/py_slice.c

@@ -1,11 +1,10 @@
 #include "pocketpy/pocketpy.h"
 
-#include "pocketpy/common/utils.h"
 #include "pocketpy/common/sstream.h"
 #include "pocketpy/objects/object.h"
 #include "pocketpy/interpreter/vm.h"
 
-void py_newslice(py_Ref out) {
+void py_newslice(py_OutRef out) {
     VM* vm = pk_current_vm;
     PyObject* obj = ManagedHeap__gcnew(&vm->heap, tp_slice, 3, 0);
     out->type = tp_slice;

+ 3 - 3
src/public/py_str.c

@@ -6,9 +6,9 @@
 #include "pocketpy/interpreter/vm.h"
 #include "pocketpy/common/sstream.h"
 
-void py_newstr(py_Ref out, const char* data) { py_newstrv(out, (c11_sv){data, strlen(data)}); }
+void py_newstr(py_OutRef out, const char* data) { py_newstrv(out, (c11_sv){data, strlen(data)}); }
 
-char* py_newstrn(py_Ref out, int size) {
+char* py_newstrn(py_OutRef out, int size) {
     if(size < 8) {
         out->type = tp_str;
         out->is_ptr = false;
@@ -42,7 +42,7 @@ void py_newfstr(py_OutRef out, const char* fmt, ...) {
     c11_sbuf__py_submit(&buf, out);
 }
 
-unsigned char* py_newbytes(py_Ref out, int size) {
+unsigned char* py_newbytes(py_OutRef out, int size) {
     ManagedHeap* heap = &pk_current_vm->heap;
     // 4 bytes size + data
     PyObject* obj = ManagedHeap__gcnew(heap, tp_bytes, 0, sizeof(c11_bytes) + size);

+ 1 - 1
src/public/py_tuple.c

@@ -5,7 +5,7 @@
 #include "pocketpy/objects/object.h"
 #include "pocketpy/interpreter/vm.h"
 
-py_ObjectRef py_newtuple(py_Ref out, int n) {
+py_ObjectRef py_newtuple(py_OutRef out, int n) {
     VM* vm = pk_current_vm;
     PyObject* obj = ManagedHeap__gcnew(&vm->heap, tp_tuple, n, 0);
     out->type = tp_tuple;

+ 11 - 11
src/public/values.c

@@ -7,42 +7,42 @@
 #include "pocketpy/interpreter/vm.h"
 #include "pocketpy/compiler/compiler.h"
 
-void py_newint(py_Ref out, int64_t val) {
+void py_newint(py_OutRef out, py_i64 val) {
     out->type = tp_int;
     out->is_ptr = false;
     out->_i64 = val;
 }
 
-void py_newfloat(py_Ref out, double val) {
+void py_newfloat(py_OutRef out, py_f64 val) {
     out->type = tp_float;
     out->is_ptr = false;
     out->_f64 = val;
 }
 
-void py_newbool(py_Ref out, bool val) {
+void py_newbool(py_OutRef out, bool val) {
     out->type = tp_bool;
     out->is_ptr = false;
     out->_bool = val;
 }
 
-void py_newnone(py_Ref out) {
+void py_newnone(py_OutRef out) {
     out->type = tp_NoneType;
     out->is_ptr = false;
 }
 
-void py_newnotimplemented(py_Ref out) {
+void py_newnotimplemented(py_OutRef out) {
     out->type = tp_NotImplementedType;
     out->is_ptr = false;
 }
 
-void py_newellipsis(py_Ref out) {
+void py_newellipsis(py_OutRef out) {
     out->type = tp_ellipsis;
     out->is_ptr = false;
 }
 
-void py_newnil(py_Ref out) { out->type = 0; }
+void py_newnil(py_OutRef out) { out->type = 0; }
 
-void py_newnativefunc(py_Ref out, py_CFunction f) {
+void py_newnativefunc(py_OutRef out, py_CFunction f) {
     out->type = tp_nativefunc;
     out->is_ptr = false;
     out->_cfunc = f;
@@ -90,7 +90,7 @@ void py_bind(py_Ref obj, const char* sig, py_CFunction f) {
 }
 
 py_Name
-    py_newfunction(py_Ref out, const char* sig, py_CFunction f, const char* docstring, int slots) {
+    py_newfunction(py_OutRef out, const char* sig, py_CFunction f, const char* docstring, int slots) {
     char buffer[256];
     snprintf(buffer, sizeof(buffer), "def %s: pass", sig);
     // fn(a, b, *c, d=1) -> None
@@ -118,13 +118,13 @@ py_Name
     return decl_name;
 }
 
-void py_newboundmethod(py_Ref out, py_Ref self, py_Ref func) {
+void py_newboundmethod(py_OutRef out, py_Ref self, py_Ref func) {
     py_newobject(out, tp_boundmethod, 2, 0);
     py_setslot(out, 0, self);
     py_setslot(out, 1, func);
 }
 
-void* py_newobject(py_Ref out, py_Type type, int slots, int udsize) {
+void* py_newobject(py_OutRef out, py_Type type, int slots, int udsize) {
     ManagedHeap* heap = &pk_current_vm->heap;
     PyObject* obj = ManagedHeap__gcnew(heap, type, slots, udsize);
     out->type = type;