diff --git a/build_g.sh b/build_g.sh index d9cac018..b04f75df 100644 --- a/build_g.sh +++ b/build_g.sh @@ -4,8 +4,9 @@ python prebuild.py SRC=$(find src/ -name "*.c") -FLAGS="-std=c11 -lm -Iinclude -O0 -Wfatal-errors -g -DDEBUG -DPK_ENABLE_OS=1" # -fsanitize=address,leak,undefined" +FLAGS="-std=c11 -lm -Iinclude -O0 -Wfatal-errors -g -DDEBUG -DPK_ENABLE_OS=1" +SANITIZE_FLAGS="-fsanitize=address,leak,undefined" echo "Compiling C files..." -clang $FLAGS $SRC src2/main.c -o main +clang $FLAGS $SANITIZE_FLAGS $SRC src2/main.c -o main diff --git a/include/pocketpy/common/strname.h b/include/pocketpy/common/strname.h index eece32d5..244d7464 100644 --- a/include/pocketpy/common/strname.h +++ b/include/pocketpy/common/strname.h @@ -9,7 +9,7 @@ extern "C" { typedef uint16_t StrName; -#define py_name(name) pk_StrName__map(#name) +#define py_name(name) pk_StrName__map(name) uint16_t pk_StrName__map(const char*); uint16_t pk_StrName__map2(c11_string); diff --git a/include/pocketpy/pocketpy.h b/include/pocketpy/pocketpy.h index b75f7a90..45510388 100644 --- a/include/pocketpy/pocketpy.h +++ b/include/pocketpy/pocketpy.h @@ -124,6 +124,7 @@ void py_import(const char* name, py_Ref out); py_Error* py_getlasterror(); void py_Error__print(py_Error*); +/************* Operators *************/ int py_eq(const py_Ref, const py_Ref); int py_le(const py_Ref, const py_Ref); int py_hash(const py_Ref, int64_t* out); diff --git a/include/pocketpy/xmacros/smallmap.h b/include/pocketpy/xmacros/smallmap.h index 7dd26bf9..cf5a811b 100644 --- a/include/pocketpy/xmacros/smallmap.h +++ b/include/pocketpy/xmacros/smallmap.h @@ -74,13 +74,15 @@ void METHOD(delete)(NAME* self) { void METHOD(set)(NAME* self, K key, V value) { int index; c11__lower_bound(KV, self->data, self->count, key, partial_less, &index); - KV* it = c11__at(KV, self, index); - if(index != self->count && equal(it->key, key)) { - it->value = value; - } else { - KV kv = {key, value}; - c11_vector__insert(KV, self, index, kv); + if(index != self->count){ + KV* it = c11__at(KV, self, index); + if(equal(it->key, key)){ + it->value = value; + return; + } } + KV kv = {key, value}; + c11_vector__insert(KV, self, index, kv); } V* METHOD(try_get)(const NAME* self, K key) { @@ -113,10 +115,12 @@ bool METHOD(contains)(const NAME* self, K key) { bool METHOD(del)(NAME* self, K key) { int index; c11__lower_bound(KV, self->data, self->count, key, partial_less, &index); - KV* it = c11__at(KV, self, index); - if(index != self->count && equal(it->key, key)) { - c11_vector__erase(KV, self, index); - return true; + if(index != self->count){ + KV* it = c11__at(KV, self, index); + if(equal(it->key, key)){ + c11_vector__erase(KV, self, index); + return true; + } } return false; } diff --git a/src/interpreter/gc.c b/src/interpreter/gc.c index 1ea28ead..891fe53e 100644 --- a/src/interpreter/gc.c +++ b/src/interpreter/gc.c @@ -115,5 +115,13 @@ PyObject* PyObject__new(py_Type type, int slots, int size){ self->type = type; self->gc_marked = false; self->slots = slots; + + // initialize slots or dict + void* p = (char*)self + 8; + if(slots >= 0){ + memset(p, 0, slots*sizeof(PyVar)); + }else{ + pk_NameDict__ctor(p); + } return self; } diff --git a/src/interpreter/py_ops.c b/src/interpreter/py_ops.c new file mode 100644 index 00000000..abf41701 --- /dev/null +++ b/src/interpreter/py_ops.c @@ -0,0 +1,14 @@ +#include "pocketpy/interpreter/vm.h" +#include "pocketpy/pocketpy.h" + +int py_eq(const py_Ref lhs, const py_Ref rhs){ + return 0; +} + +int py_le(const py_Ref lhs, const py_Ref rhs){ + return 0; +} + +int py_hash(const py_Ref self, int64_t* out){ + return 0; +} \ No newline at end of file diff --git a/src/interpreter/vm.c b/src/interpreter/vm.c index 61127b9c..f5049dca 100644 --- a/src/interpreter/vm.c +++ b/src/interpreter/vm.c @@ -176,7 +176,7 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self){ py_Type pk_VM__new_type(pk_VM* self, const char* name, py_Type base, const PyVar* module, bool subclass_enabled){ py_Type type = self->types.count; pk_TypeInfo* ti = c11_vector__emplace(&self->types); - PyObject* typeobj = pk_ManagedHeap__gcnew(&self->heap, tp_type, 0, sizeof(py_Type)); + PyObject* typeobj = pk_ManagedHeap__gcnew(&self->heap, tp_type, -1, sizeof(py_Type)); py_Type* value = PyObject__value(typeobj); *value = type; pk_TypeInfo__ctor(ti, py_name(name), base, typeobj, module, subclass_enabled); diff --git a/src/public/values.c b/src/public/values.c index 243a5c24..8025c226 100644 --- a/src/public/values.c +++ b/src/public/values.c @@ -24,7 +24,7 @@ void py_newbool(py_Ref self, bool val) { void py_newstr(py_Ref self, const char* data) { pk_ManagedHeap* heap = &pk_current_vm->heap; PyObject* obj = pk_ManagedHeap__gcnew(heap, tp_str, 0, sizeof(py_Str)); - py_Str__ctor((py_Str*)PyObject__value(obj), data); + py_Str__ctor(PyObject__value(obj), data); self->type = tp_str; self->is_ptr = true; self->_obj = obj;