From 5844e63c612f46c31d8e52e59f39032912caf213 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Wed, 29 Dec 2021 21:57:13 -0600 Subject: [PATCH] Custom_Format tag; bugfix with polymorphic structure tags --- core/container/map.onyx | 4 +++- core/conv.onyx | 23 +++++++++++++++++++++++ src/wasm_type_table.h | 11 +++++++++-- tests/i32map.onyx | 4 ---- tests/struct_use_pointer_member | 2 +- 5 files changed, 36 insertions(+), 8 deletions(-) diff --git a/core/container/map.onyx b/core/container/map.onyx index af0d335d..ff36f1b5 100644 --- a/core/container/map.onyx +++ b/core/container/map.onyx @@ -21,7 +21,9 @@ package core.map } } -Map :: struct (K: type_expr, V: type_expr) where ValidKey(K) { +Map :: struct (K: type_expr, V: type_expr) where ValidKey(K) + [conv.Custom_Format.{ #solidify format_map {K=K, V=V} }] +{ allocator : Allocator; hashes : [] i32; diff --git a/core/conv.onyx b/core/conv.onyx index af4f42fa..87038084 100644 --- a/core/conv.onyx +++ b/core/conv.onyx @@ -1,5 +1,7 @@ package core.conv +Enable_Custom_Formatters :: true + #local { map :: package core.map custom_formatters: Map(type_expr, (^Format_Output, ^Format, rawptr) -> void); @@ -7,12 +9,33 @@ package core.conv custom_formatters_initialized :: #init () { map.init(^custom_formatters, default=null_proc); + + #if Enable_Custom_Formatters { + use type_info; + + for type_idx: type_table.count { + type := type_table[type_idx]; + if type.kind != .Struct do continue; + + s_info := cast(^Type_Info_Struct) type; + for s_info.tags { + if it.type != Custom_Format do continue; + + custom_format := cast(^Custom_Format) it.data; + custom_formatters[cast(type_expr) type_idx] = custom_format.format; + } + } + } } register_custom_formatter :: (formatter: (^Format_Output, ^Format, ^$T) -> void) { custom_formatters[T] = formatter; } +Custom_Format :: struct { + format: (^Format_Output, ^Format, rawptr) -> void; +} + str_to_i64 :: (s: str) -> i64 { use package core diff --git a/src/wasm_type_table.h b/src/wasm_type_table.h index b858c0cc..18c08bca 100644 --- a/src/wasm_type_table.h +++ b/src/wasm_type_table.h @@ -404,12 +404,20 @@ u64 build_type_table(OnyxWasmModule* module) { u32 name_length = strlen(type->PolyStruct.name); bh_buffer_append(&table_buffer, type->PolyStruct.name, name_length); + u32 tags_count = bh_arr_length(type->PolyStruct.meta_tags); i32 i = 0; bh_arr_each(AstTyped *, tag, type->PolyStruct.meta_tags) { AstTyped* value = *tag; - assert(value->flags & Ast_Flag_Comptime); assert(value->type); + // Polymorphic structs are weird in this case, because the tag might not be constructed generically for + // the polymorphic structure so it should only be constructed for actual solidified structures. + // See core/containers/map.onyx with Custom_Format for an example. + if (!(value->flags & Ast_Flag_Comptime)) { + tags_count--; + continue; + } + u32 size = type_size_of(value->type); bh_buffer_align(&table_buffer, type_alignment_of(value->type)); tag_locations[i] = table_buffer.length; @@ -425,7 +433,6 @@ u64 build_type_table(OnyxWasmModule* module) { bh_buffer_align(&table_buffer, 8); u32 tags_base = table_buffer.length; - u32 tags_count = bh_arr_length(type->PolyStruct.meta_tags); fori (i, 0, tags_count) { WRITE_SLICE(tag_locations[i], type->PolyStruct.meta_tags[i]->type->id); diff --git a/tests/i32map.onyx b/tests/i32map.onyx index a43af906..d7a88f12 100644 --- a/tests/i32map.onyx +++ b/tests/i32map.onyx @@ -5,10 +5,6 @@ package main use package core main :: (args: [] cstr) { - conv.register_custom_formatter( - #solidify map.format_map { K=i32, V=str } - ); - imap : Map(i32, str); map.init(^imap, ""); defer { diff --git a/tests/struct_use_pointer_member b/tests/struct_use_pointer_member index ac82b42b..5bd48a51 100644 --- a/tests/struct_use_pointer_member +++ b/tests/struct_use_pointer_member @@ -1,2 +1,2 @@ Hello, I am Billy! -Go away!! func[3] +Go away!! func[5] -- 2.25.1