blueloveTH 1 yıl önce
ebeveyn
işleme
7e35fa2d56

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

+ 1 - 1
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);

+ 1 - 0
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);

+ 14 - 10
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;
 }

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

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

+ 1 - 1
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);

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