From: Brendan Hansen Date: Tue, 15 Jun 2021 18:14:03 +0000 (-0500) Subject: working on json module; bugfix with struct constructing X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=2a9e8b50f7f350f164b880809abd4fa97aeec7cd;p=onyx.git working on json module; bugfix with struct constructing --- diff --git a/bin/onyx b/bin/onyx index 9c856b9f..383ec51d 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/modules/json/decoder.onyx b/modules/json/decoder.onyx index 003f04c6..37364374 100644 --- a/modules/json/decoder.onyx +++ b/modules/json/decoder.onyx @@ -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 diff --git a/modules/json/example.onyx b/modules/json/example.onyx index 04468ece..57d10d1f 100644 --- a/modules/json/example.onyx +++ b/modules/json/example.onyx @@ -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") diff --git a/modules/json/types.onyx b/modules/json/types.onyx index b4c4c72e..1e9d2b7a 100644 --- a/modules/json/types.onyx +++ b/modules/json/types.onyx @@ -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_); diff --git a/src/onyxtypes.c b/src/onyxtypes.c index ae2b26da..6bdb5c89 100644 --- a/src/onyxtypes.c +++ b/src/onyxtypes.c @@ -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; }