diff --git a/include/pocketpy/config.h b/include/pocketpy/config.h index 12fcbb10..746fe53e 100644 --- a/include/pocketpy/config.h +++ b/include/pocketpy/config.h @@ -55,3 +55,9 @@ #else #define PK_PLATFORM_SEP '/' #endif + +#ifdef __cplusplus + #ifndef restrict + #define restrict + #endif +#endif diff --git a/include/pocketpy/pocketpy.h b/include/pocketpy/pocketpy.h index a61f023b..afe38808 100644 --- a/include/pocketpy/pocketpy.h +++ b/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; diff --git a/src/interpreter/frame.c b/src/interpreter/frame.c index 396f239d..40ecfb2a 100644 --- a/src/interpreter/frame.c +++ b/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; diff --git a/src/modules/pickle.c b/src/modules/pickle.c index b7159a9c..cc0112c9 100644 --- a/src/modules/pickle.c +++ b/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); diff --git a/src/modules/vmath.c b/src/modules/vmath.c index 92e7cdf9..e0badc05 100644 --- a/src/modules/vmath.c +++ b/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 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 diff --git a/src/public/modules.c b/src/public/modules.c index a379d41b..bd47e3b3 100644 --- a/src/public/modules.c +++ b/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); } diff --git a/src/public/py_dict.c b/src/public/py_dict.c index 50906a48..6ed723bf 100644 --- a/src/public/py_dict.c +++ b/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); } diff --git a/src/public/py_list.c b/src/public/py_list.c index d33585cf..cb0ac7f8 100644 --- a/src/public/py_list.c +++ b/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); diff --git a/src/public/py_slice.c b/src/public/py_slice.c index de96f24e..5d0fe627 100644 --- a/src/public/py_slice.c +++ b/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; diff --git a/src/public/py_str.c b/src/public/py_str.c index 03d217da..06175ec2 100644 --- a/src/public/py_str.c +++ b/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); diff --git a/src/public/py_tuple.c b/src/public/py_tuple.c index 1c0f97b2..0471bd84 100644 --- a/src/public/py_tuple.c +++ b/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; diff --git a/src/public/values.c b/src/public/values.c index 5db42560..edb86f4e 100644 --- a/src/public/values.c +++ b/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;