cgen: fix array of fns init (#5884)

pull/5888/head
Ruofan XU 2020-07-20 03:44:03 +08:00 committed by GitHub
parent bb60fe2ccf
commit 9f6aacb739
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 1 deletions

View File

@ -4784,7 +4784,13 @@ fn (mut g Gen) array_init(it ast.ArrayInit) {
return
}
len := it.exprs.len
g.write('new_array_from_c_array($len, $len, sizeof($elem_type_str), _MOV(($elem_type_str[$len]){')
elem_sym := g.table.get_type_symbol(it.elem_type)
if elem_sym.kind == .function {
g.write('new_array_from_c_array($len, $len, sizeof(voidptr), _MOV((voidptr[$len]){')
}
else {
g.write('new_array_from_c_array($len, $len, sizeof($elem_type_str), _MOV(($elem_type_str[$len]){')
}
if len > 8 {
g.writeln('')
g.write('\t\t')

View File

@ -194,3 +194,22 @@ fn test_array_init_direct_call() {
assert []int{len: 2, init: 0}.len == 2
assert []int{len: 3, init: 1}.map(it*2) == [2,2,2]
}
fn foo(a string) int {
return 10 + a.len
}
fn foo2(a string) int {
return 20 + a.len
}
type FnFoo fn(a string) int
fn test_array_of_fns_init() {
a := [FnFoo(foo), foo2]
assert a.len == 2
f0 := a[0]
assert f0('xx') == 12
f1 := a[1]
assert f1('yyy') == 23
}