Skip to content

Commit

Permalink
cgen: fix array map to fixed array
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Dec 10, 2024
1 parent bc7a844 commit 5ebf0c1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
22 changes: 22 additions & 0 deletions vlib/v/gen/c/array.v
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,28 @@ fn (mut g Gen) gen_array_map(node ast.CallExpr) {
g.write('${ret_elem_styp} ${tmp_map_expr_result_name} = ')
g.expr(expr.expr)
}
ast.SelectorExpr {
if expr.typ != ast.void_type && g.table.final_sym(expr.typ).kind == .array_fixed {
atype := g.styp(expr.typ)
if closure_var_decl != '' {
g.write('memcpy(&${closure_var_decl}, &')
g.expr(expr)
g.write(', sizeof(${atype}))')
} else {
g.writeln('${ret_elem_styp} ${tmp_map_expr_result_name};')
g.write('memcpy(&${tmp_map_expr_result_name}, &')
g.expr(expr)
g.write(', sizeof(${atype}))')
}
} else {
if closure_var_decl != '' {
g.write('${closure_var_decl} = ')
} else {
g.write('${ret_elem_styp} ${tmp_map_expr_result_name} = ')
}
g.expr(expr)
}
}
else {
if closure_var_decl != '' {
g.write('${closure_var_decl} = ')
Expand Down
13 changes: 13 additions & 0 deletions vlib/v/tests/builtin_arrays/array_map_to_fixed_array_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type Mat4 = [16]f32

struct GameObject {
mut:
transform Mat4
children []&GameObject
}

fn test_array_map_to_fixed_array() {
mut v1 := &GameObject{}
eprintln('children: ${v1.children.map(it.transform)}')
assert '${v1.children.map(it.transform)}' == '[]'
}

0 comments on commit 5ebf0c1

Please sign in to comment.