1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix multi_array index issue

This commit is contained in:
yuyi 2020-06-12 07:24:25 +08:00 committed by GitHub
parent c9cfe9d40b
commit ff1437fc5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 7 deletions

View File

@ -43,6 +43,21 @@ fn __new_array_with_default(mylen int, cap int, elm_size int, val voidptr) array
return arr
}
fn __new_array_with_array_default(mylen int, cap int, elm_size int, val array) array {
cap_ := if cap < mylen { mylen } else { cap }
arr := array{
element_size: elm_size
data: vcalloc(cap_ * elm_size)
len: mylen
cap: cap_
}
for i in 0..arr.len {
val_clone := val.clone()
C.memcpy(charptr(arr.data) + i*elm_size, &val_clone, elm_size)
}
return arr
}
// Private function, used by V (`nums := [1, 2, 3]`)
fn new_array_from_c_array(len, cap, elm_size int, c_array voidptr) array {
cap_ := if cap < len { len } else { cap }

View File

@ -816,3 +816,9 @@ fn test_array_with_cap() {
assert a5.len == 1
assert a5.cap == 10
}
fn test_mutli_array_index() {
mut a := [][]int{len:2, init: []int{len:3, init:0}}
a[0][0] = 1
assert '$a' == '[[1, 0, 0], [0, 0, 0]]'
}

View File

@ -4320,10 +4320,16 @@ fn (mut g Gen) array_init(it ast.ArrayInit) {
g.write('}')
return
}
// elem_sym := g.table.get_type_symbol(it.elem_type)
elem_type_str := g.typ(it.elem_type)
if it.exprs.len == 0 {
elem_sym := g.table.get_type_symbol(it.elem_type)
is_default_array := elem_sym.kind == .array && it.has_default
if is_default_array {
g.write('__new_array_with_array_default(')
} else {
g.write('__new_array_with_default(')
}
if it.has_len {
g.expr(it.len_expr)
g.write(', ')
@ -4337,11 +4343,15 @@ fn (mut g Gen) array_init(it ast.ArrayInit) {
g.write('0, ')
}
g.write('sizeof($elem_type_str), ')
if is_default_array {
g.write('_val_$it.pos.pos)')
} else {
if it.has_default || (it.has_len && it.elem_type == table.string_type) {
g.write('&_val_$it.pos.pos)')
} else {
g.write('0)')
}
}
return
}
len := it.exprs.len