Skip to content

Commit 287d33e

Browse files
authored
wasm: resolve fixed-array .len instead of aborting with "could not find field len" (#27518)
1 parent 1704098 commit 287d33e

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

vlib/v/gen/wasm/gen.v

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,13 @@ pub fn (mut g Gen) expr(node ast.Expr, expected ast.Type) {
10161016
g.get(v)
10171017
}
10181018
ast.SelectorExpr {
1019-
if v := g.get_var_from_expr(node) {
1019+
final := g.table.final_sym(node.expr_type)
1020+
if node.field_name == 'len' && final.info is ast.ArrayFixed {
1021+
// a fixed array's `.len` is a compile-time constant; the checker
1022+
// types it as `int` but emits no field, so resolve it here rather
1023+
// than aborting in get_field_offset ("could not find field len").
1024+
g.func.i32_const(i32(final.info.size))
1025+
} else if v := g.get_var_from_expr(node) {
10201026
if g.needs_address {
10211027
if !v.is_address {
10221028
g.v_error("cannot take the address of a value that doesn't live on the stack. this is a current limitation.",

vlib/v/gen/wasm/tests/arrays.vv

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ fn selector_index_assign() {
6262
println(app.frame[app.idx])
6363
}
6464

65+
fn fixed_array_len() {
66+
a := [10, 12, 150]!
67+
println(a.len)
68+
b := [8]int{}
69+
println(b.len)
70+
}
71+
6572
fn main() {
6673
println('--- static_arrays()')
6774
a, b, c := static_arrays()
@@ -83,4 +90,7 @@ fn main() {
8390

8491
println('--- selector_index_assign()')
8592
selector_index_assign()
93+
94+
println('--- fixed_array_len()')
95+
fixed_array_len()
8696
}

vlib/v/gen/wasm/tests/arrays.vv.out

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@
1616
0
1717
123
1818
123
19+
--- fixed_array_len()
20+
3
21+
8

0 commit comments

Comments
 (0)