Custom_Format tag; bugfix with polymorphic structure tags
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 30 Dec 2021 03:57:13 +0000 (21:57 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 30 Dec 2021 03:57:13 +0000 (21:57 -0600)
core/container/map.onyx
core/conv.onyx
src/wasm_type_table.h
tests/i32map.onyx
tests/struct_use_pointer_member

index af0d335d95da07db527754845249cc56afe58cb7..ff36f1b5284655b767769cefec02e451ec3a1b99 100644 (file)
@@ -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;
index af4f42fa12e69fbc11c239ad848ee11d98d6fbc3..87038084af30beeda747931ef245639b1290bfe5 100644 (file)
@@ -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
 
index b858c0ccfb9f0102654a3575ac858dc36920ecc8..18c08bca65f24aa63fff747b5c3e685a449bbfa7 100644 (file)
@@ -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);
index a43af90640e70627128c44c7dfb99b6323bc832f..d7a88f1215c3ff1ab1461e8389f8d81e5e4c448a 100644 (file)
@@ -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 {
index ac82b42b52ac2c1bf2fcc48f29b67c1d6002c0db..5bd48a51598d8feaab12907577dd6e6478f0302a 100644 (file)
@@ -1,2 +1,2 @@
 Hello, I am Billy!
-Go away!! func[3]
+Go away!! func[5]