Skip to content

Commit

Permalink
cgen: fix global struct init on tcc (fix #22300) (#22350)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Oct 1, 2024
1 parent ce95787 commit 1b812f6
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -7353,6 +7353,11 @@ fn (mut g Gen) type_default(typ_ ast.Type) string {
} else {
'{'
}
$if windows {
if !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)
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions vlib/v/gen/c/testdata/global_initializer_windows.must_have
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions vlib/v/gen/c/testdata/global_initializer_windows.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// vtest vflags: -enable-globals

struct Foo {
foo int
bar struct {
a int
b f64
}
}

__global g_test_foo = [3]Foo{}
23 changes: 23 additions & 0 deletions vlib/v/tests/global_init_test.v
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 1b812f6

Please sign in to comment.