working on json module; bugfix with struct constructing
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 15 Jun 2021 18:14:03 +0000 (13:14 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 15 Jun 2021 18:14:03 +0000 (13:14 -0500)
bin/onyx
modules/json/decoder.onyx
modules/json/example.onyx
modules/json/types.onyx
src/onyxtypes.c

index 9c856b9f9d32fa1743467ca1b1db90735047a061..383ec51dbeb786693c081051b09a9d8a76a701a6 100755 (executable)
Binary files a/bin/onyx and b/bin/onyx differ
index 003f04c631b54fdfa20ce7963c789aaf93b3ebc3..37364374dde4dbaf577f8492044f5257d5825055 100644 (file)
@@ -7,5 +7,5 @@ decode_string :: (data: str, allocator := context.allocator) -> ^Value {
 }
 
 decode :: (stream: ^io.Stream, allocator := context.allocator) -> ^Value {
-    
+
 }
\ No newline at end of file
index 04468ece4a953b9fea29eea07562f75062601853..57d10d1fd64dde89f4da5f20052e76bef5c0b6e7 100644 (file)
@@ -8,7 +8,10 @@ json :: package json
 json_string := "{ \"test\": \"Hello, World!\", \"array\": [1,2,3,4,5], }";
 
 main :: (args: [] cstr) {
-    decoded_json := json.decode_string(json_string);
+    arena := alloc.arena.make(context.allocator, 4096);
+    defer alloc.arena.free(^arena);
+
+    decoded_json := json.decode_string(json_string, alloc.arena.make_allocator(^arena));
     defer json.free(decoded_json);
 
     test_str := decoded_json |> json.get("test")
index b4c4c72ea32535db486d4deda82f769af4edce1d..1e9d2b7a8062785c3eb34d3cb07d0d66ac40b37b 100644 (file)
@@ -14,24 +14,25 @@ Value :: struct {
         Object;
     }
 
-    KeyValue :: struct {
-        key : str;
-        value : ^Value;
-    }
-
     type : Type;
     allocator : Allocator;
     use value : struct #union {
         bool_   : bool;
         int_    : i64;
         float_  : f64;
-        str_    : str;
+        str_    : str;   // This is allocated out of the allocator above.
 
         @CompilerBug @Cleanup // I don't want these to be pointers in the long run. In theory, they should be
         // able to be normal values, but there is a compiler bug that prevents the size and alignment of Value
         // being known.
+        //
+        // Thinking about it a little more, I think having these be pointers will be better from a performance
+        // point of view, because the overhead of copying the return/parameter values would be significant.
         array_  : [..] ^Value;
-        object_ : [..] KeyValue;
+        object_ : [..] struct {
+            key : str;        // This is allocated out of the allocator on the Value.
+            value : ^Value;
+        };
     };
 }
 
@@ -52,6 +53,10 @@ get :: (v: ^Value, key: str) -> ^Value {
 }
 
 free :: (v: ^Value) do switch v.type {
+    case .String {
+        raw_free(v.allocator, v.str_.data);
+    }
+
     case .Array {
         for elem: v.array_ {
             free(elem);
@@ -61,6 +66,7 @@ free :: (v: ^Value) do switch v.type {
 
     case .Object {
         for ^entry: v.object_ {
+            raw_free(v.allocator, entry.key.data);
             free(entry.value);
         }
         array.free(^v.object_);
index ae2b26dae0c9b3659b1e7b54d04699ac1d77da2b..6bdb5c896f152081bb520c7784b7cbe22566ec8f 100644 (file)
@@ -332,6 +332,14 @@ Type* type_build_from_ast(bh_allocator alloc, AstType* type_node) {
 
                 mem_alignment = type_alignment_of((*member)->type);
                 if (mem_alignment <= 0) {
+                    if ((*member)->type->kind == Type_Kind_Struct) {
+                        AstStructType* member_node = (AstStructType *) (*member)->type->ast_type;
+                        if (member_node->stcache_is_valid) {
+                            s_node->stcache_is_valid = 0;
+                            return NULL;
+                        }
+                    }
+
                     onyx_report_error((*member)->token->pos, "Invalid member type: %s", type_get_name((*member)->type)); 
                     return NULL;
                 }