diff --git a/include/pocketpy/common/vector.h b/include/pocketpy/common/vector.h index 891e5944..3d35e422 100644 --- a/include/pocketpy/common/vector.h +++ b/include/pocketpy/common/vector.h @@ -11,7 +11,7 @@ extern "C" { #endif -typedef struct c11_array{ +typedef struct c11_array { void* data; int count; int elem_size; @@ -21,7 +21,7 @@ void c11_array__ctor(c11_array* self, int elem_size, int count); void c11_array__dtor(c11_array* self); c11_array c11_array__copy(const c11_array* self); -typedef struct c11_vector{ +typedef struct c11_vector { void* data; int count; int capacity; @@ -40,54 +40,57 @@ c11_array c11_vector__submit(c11_vector* self); #define c11__setitem(T, self, index, value) ((T*)(self)->data)[index] = value; #define c11__at(T, self, index) ((T*)(self)->data + index) -#define c11_vector__push(T, self, elem) \ - do{ \ - if((self)->count == (self)->capacity) c11_vector__reserve((self), (self)->capacity*2); \ - ((T*)(self)->data)[(self)->count] = (elem); \ - (self)->count++; \ - }while(0) +#define c11_vector__push(T, self, elem) \ + do { \ + if((self)->count == (self)->capacity) c11_vector__reserve((self), (self)->capacity * 2); \ + ((T*)(self)->data)[(self)->count] = (elem); \ + (self)->count++; \ + } while(0) #define c11_vector__pop(self) (--(self)->count) -#define c11_vector__back(T, self) \ - (((T*)(self)->data)[(self)->count - 1]) +#define c11_vector__back(T, self) (((T*)(self)->data)[(self)->count - 1]) -#define c11_vector__extend(T, self, p, size) \ - do{ \ - c11_vector__reserve((self), (self)->count + (size)); \ - memcpy((T*)(self)->data + (self)->count, (p), (size) * sizeof(T)); \ - (self)->count += (size); \ - }while(0) +#define c11_vector__extend(T, self, p, size) \ + do { \ + c11_vector__reserve((self), (self)->count + (size)); \ + memcpy((T*)(self)->data + (self)->count, (p), (size) * sizeof(T)); \ + (self)->count += (size); \ + } while(0) + +#define c11_vector__insert(T, self, index, elem) \ + do { \ + if((self)->count == (self)->capacity) c11_vector__reserve((self), (self)->capacity * 2); \ + T* p = (T*)(self)->data + (index); \ + memmove(p + 1, p, ((self)->count - (index)) * sizeof(T)); \ + *p = (elem); \ + (self)->count++; \ + } while(0) + +#define c11_vector__erase(T, self, index) \ + do { \ + T* p = (T*)(self)->data + (index); \ + memmove(p, p + 1, ((self)->count - (index)-1) * sizeof(T)); \ + (self)->count--; \ + } while(0) + +#define c11_vector__reverse(T, self, start, end) \ + do { \ + T* p = (T*)(self)->data + (start); \ + T* q = (T*)(self)->data + (end); \ + while(p < q) { \ + T tmp = *p; \ + *p = *q; \ + *q = tmp; \ + p++; \ + q--; \ + } \ + } while(0) -#define c11_vector__insert(T, self, index, elem) \ - do{ \ - if((self)->count == (self)->capacity) c11_vector__reserve((self), (self)->capacity*2); \ - T* p = (T*)(self)->data + (index); \ - memmove(p + 1, p, ((self)->count - (index)) * sizeof(T)); \ - *p = (elem); \ - (self)->count++; \ - }while(0) - -#define c11_vector__erase(T, self, index) \ - do{ \ - T* p = (T*)(self)->data + (index); \ - memmove(p, p + 1, ((self)->count - (index) - 1) * sizeof(T)); \ - (self)->count--; \ - }while(0) - -#define c11_vector__reverse(T, self, start, end) \ - do{ \ - T* p = (T*)(self)->data + (start); \ - T* q = (T*)(self)->data + (end); \ - while(p < q){ \ - T tmp = *p; *p = *q; *q = tmp; \ - p++; q--; \ - } \ - }while(0) - -#define c11__foreach(T, self, it) \ - for(T* it = (T*)(self)->data; it != (T*)(self)->data + (self)->count; it++) +// NOTE: here we do an extra NULL check for it to avoid UB +#define c11__foreach(T, self, it) \ + for(T* it = (self)->data; it && it != (T*)(self)->data + (self)->count; it++) #ifdef __cplusplus } diff --git a/src/common/vector.c b/src/common/vector.c index b46676df..241f7bd9 100644 --- a/src/common/vector.c +++ b/src/common/vector.c @@ -48,8 +48,8 @@ c11_vector c11_vector__copy(const c11_vector* self){ void c11_vector__reserve(c11_vector* self, int capacity){ if(capacity < 4) capacity = 4; if(capacity <= self->capacity) return; + self->data = realloc(self->data, self->elem_size * capacity); self->capacity = capacity; - self->data = realloc(self->data, self->elem_size * self->capacity); } void c11_vector__clear(c11_vector* self){ diff --git a/src/interpreter/ceval.c b/src/interpreter/ceval.c index 1cf0b7d0..02bdf4c8 100644 --- a/src/interpreter/ceval.c +++ b/src/interpreter/ceval.c @@ -3,6 +3,7 @@ #include "pocketpy/common/sstream.h" #include "pocketpy/objects/codeobject.h" #include "pocketpy/pocketpy.h" +#include int UnboundLocalError(py_Name name) { return -1; } @@ -604,6 +605,18 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) { // DISPATCH_JUMP_ABSOLUTE(target) // } /*****************************************/ + case OP_FSTRING_EVAL: { + assert(false); + } + case OP_REPR: { + assert(false); + } + case OP_CALL: { + assert(false); + } + case OP_CALL_VARGS: { + assert(false); + } case OP_RETURN_VALUE: { if(byte.arg == BC_NOARG) { self->last_retval = POPX(); diff --git a/src/interpreter/py_number.c b/src/interpreter/py_number.c index f0cc3b90..1a92df13 100644 --- a/src/interpreter/py_number.c +++ b/src/interpreter/py_number.c @@ -98,11 +98,13 @@ static bool _py_number__pow__(int argc, py_Ref argv) { py_newfloat(py_retval(), pow(lhs, rhs)); } } else { + // rhs >= 0 int64_t ret = 1; - while(rhs) { + while(true){ if(rhs & 1) ret *= lhs; - lhs *= lhs; rhs >>= 1; + if(!rhs) break; + lhs *= lhs; // place this here to avoid overflow } py_newint(py_retval(), ret); } diff --git a/src/public/vm.c b/src/public/vm.c index a4ed705c..059b9856 100644 --- a/src/public/vm.c +++ b/src/public/vm.c @@ -67,12 +67,13 @@ static void disassemble(CodeObject* co) { } char buf[32]; - snprintf(buf, sizeof(buf), "%-8s%-3s%-3d", line, pointer, i); + snprintf(buf, sizeof(buf), "%-8s%-3s%-3d ", line, pointer, i); c11_sbuf__write_cstr(&ss, buf); - snprintf(buf, sizeof(buf), " %-24s", OP_NAMES[byte.op]); - c11_sbuf__write_cstr(&ss, buf); + c11_sbuf__write_cstr(&ss, OP_NAMES[byte.op]); c11_sbuf__write_char(&ss, ex.is_virtual ? '*' : ' '); + int padding = 24 - strlen(OP_NAMES[byte.op]); + for(int j = 0; j < padding; j++) c11_sbuf__write_char(&ss, ' '); // _opcode_argstr(this, i, byte, co); do {