From 81b4bbfbbf2c2c8747386fecb2505a4bca997f8c Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 28 Sep 2024 20:43:27 -0300 Subject: [PATCH 1/5] fix --- vlib/v/gen/c/cgen.v | 3 ++- vlib/v/tests/global_init_test.v | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/global_init_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index c278393be48b52..8648048f3b3589 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -7347,7 +7347,8 @@ fn (mut g Gen) type_default(typ_ ast.Type) string { .struct_ { mut has_none_zero := false info := sym.info as ast.Struct - mut init_str := if info.is_anon && !g.inside_global_decl { + mut init_str := if (info.is_anon && !g.inside_global_decl) + || g.pref.ccompiler_type == .tinyc { '(${g.typ(typ)}){' } else { '{' diff --git a/vlib/v/tests/global_init_test.v b/vlib/v/tests/global_init_test.v new file mode 100644 index 00000000000000..675b1d8a20dfee --- /dev/null +++ b/vlib/v/tests/global_init_test.v @@ -0,0 +1,23 @@ +@[has_globals] +module main + +interface IGameObject { +mut: + name string +} + +struct Game { +mut: + objects []IGameObject + delete_objects []IGameObject +} + +__global ( + game Game +) + +fn test_main() { + println('game: ${game}') + assert game.objects.len == 0 + assert game.delete_objects.len == 0 +} From 7e4466a5c1c439eef7359aae8c1f1fa95ce1b4e4 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 28 Sep 2024 20:57:24 -0300 Subject: [PATCH 2/5] fix --- vlib/v/gen/c/cgen.v | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 8648048f3b3589..440be8262d2687 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -7348,7 +7348,7 @@ fn (mut g Gen) type_default(typ_ ast.Type) string { mut has_none_zero := false info := sym.info as ast.Struct mut init_str := if (info.is_anon && !g.inside_global_decl) - || g.pref.ccompiler_type == .tinyc { + || (g.inside_global_decl && g.pref.ccompiler_type == .tinyc) { '(${g.typ(typ)}){' } else { '{' From d8f18fb1107aa65bb0d3392c45796874c1f10228 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sun, 29 Sep 2024 14:24:25 -0300 Subject: [PATCH 3/5] fix --- vlib/v/gen/c/cgen.v | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 440be8262d2687..cfb93f6e490ae8 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -7347,12 +7347,17 @@ fn (mut g Gen) type_default(typ_ ast.Type) string { .struct_ { mut has_none_zero := false info := sym.info as ast.Struct - mut init_str := if (info.is_anon && !g.inside_global_decl) - || (g.inside_global_decl && g.pref.ccompiler_type == .tinyc) { + mut init_str := if (info.is_anon && !g.inside_global_decl) { '(${g.typ(typ)}){' } else { '{' } + $if windows { + if g.pref.ccompiler_type == .tinyc && !typ.has_flag(.shared_f) + && g.inside_global_decl { + init_str = '(${g.typ(typ)}){' + } + } if sym.language in [.c, .v] { for field in info.fields { field_sym := g.table.sym(field.typ) From d66e924a2d8b7e749a59c66e8d74414f06d5f347 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sun, 29 Sep 2024 14:53:37 -0300 Subject: [PATCH 4/5] fix --- vlib/v/gen/c/cgen.v | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index cfb93f6e490ae8..20b2fe40c9aae0 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -7347,7 +7347,7 @@ fn (mut g Gen) type_default(typ_ ast.Type) string { .struct_ { mut has_none_zero := false info := sym.info as ast.Struct - mut init_str := if (info.is_anon && !g.inside_global_decl) { + mut init_str := if info.is_anon && !g.inside_global_decl { '(${g.typ(typ)}){' } else { '{' From 3ee57d065fa8d80ae1ca1cfa179c33a7cddc24d5 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Mon, 30 Sep 2024 08:19:23 -0300 Subject: [PATCH 5/5] fix --- vlib/v/gen/c/cgen.v | 3 +-- ...c.must_have => global_initializer_nix.c.must_have} | 0 ...lobal_initializer.vv => global_initializer_nix.vv} | 0 .../c/testdata/global_initializer_windows.must_have | 1 + vlib/v/gen/c/testdata/global_initializer_windows.vv | 11 +++++++++++ 5 files changed, 13 insertions(+), 2 deletions(-) rename vlib/v/gen/c/testdata/{global_initializer.c.must_have => global_initializer_nix.c.must_have} (100%) rename vlib/v/gen/c/testdata/{global_initializer.vv => global_initializer_nix.vv} (100%) create mode 100644 vlib/v/gen/c/testdata/global_initializer_windows.must_have create mode 100644 vlib/v/gen/c/testdata/global_initializer_windows.vv diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 20b2fe40c9aae0..2ffa2beaafa58e 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -7353,8 +7353,7 @@ fn (mut g Gen) type_default(typ_ ast.Type) string { '{' } $if windows { - if g.pref.ccompiler_type == .tinyc && !typ.has_flag(.shared_f) - && g.inside_global_decl { + if !typ.has_flag(.shared_f) && g.inside_global_decl { init_str = '(${g.typ(typ)}){' } } diff --git a/vlib/v/gen/c/testdata/global_initializer.c.must_have b/vlib/v/gen/c/testdata/global_initializer_nix.c.must_have similarity index 100% rename from vlib/v/gen/c/testdata/global_initializer.c.must_have rename to vlib/v/gen/c/testdata/global_initializer_nix.c.must_have diff --git a/vlib/v/gen/c/testdata/global_initializer.vv b/vlib/v/gen/c/testdata/global_initializer_nix.vv similarity index 100% rename from vlib/v/gen/c/testdata/global_initializer.vv rename to vlib/v/gen/c/testdata/global_initializer_nix.vv diff --git a/vlib/v/gen/c/testdata/global_initializer_windows.must_have b/vlib/v/gen/c/testdata/global_initializer_windows.must_have new file mode 100644 index 00000000000000..383d042d59b133 --- /dev/null +++ b/vlib/v/gen/c/testdata/global_initializer_windows.must_have @@ -0,0 +1 @@ +Array_fixed_main__Foo_3 g_test_foo = {(main__Foo){.foo = 0,.bar = (main___VAnonStruct1){.a = 0,.b = 0,},}, (main__Foo){.foo = 0,.bar = (main___VAnonStruct1){.a = 0,.b = 0,},}, (main__Foo){.foo = 0,.bar = (main___VAnonStruct1){.a = 0,.b = 0,},}}; // global4 \ No newline at end of file diff --git a/vlib/v/gen/c/testdata/global_initializer_windows.vv b/vlib/v/gen/c/testdata/global_initializer_windows.vv new file mode 100644 index 00000000000000..6f2ad48dc3a8bb --- /dev/null +++ b/vlib/v/gen/c/testdata/global_initializer_windows.vv @@ -0,0 +1,11 @@ +// vtest vflags: -enable-globals + +struct Foo { + foo int + bar struct { + a int + b f64 + } +} + +__global g_test_foo = [3]Foo{}