Skip to content

Commit 61ba5de

Browse files
committed
Add a test for calling a function from an array
1 parent f70cfbc commit 61ba5de

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

tests/compiler/std/array-access.debug.wat

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
(global $~lib/shared/runtime/Runtime.Minimal i32 (i32.const 1))
1111
(global $~lib/shared/runtime/Runtime.Incremental i32 (i32.const 2))
1212
(global $~lib/native/ASC_SHRINK_LEVEL i32 (i32.const 0))
13+
(global $~argumentsLength (mut i32) (i32.const 0))
1314
(global $~lib/memory/__data_end i32 (i32.const 284))
1415
(global $~lib/memory/__stack_pointer (mut i32) (i32.const 33052))
1516
(global $~lib/memory/__heap_base i32 (i32.const 33052))
@@ -26,6 +27,7 @@
2627
(export "stringArrayMethodCall" (func $export:std/array-access/stringArrayMethodCall))
2728
(export "stringArrayArrayPropertyAccess" (func $export:std/array-access/stringArrayArrayPropertyAccess))
2829
(export "stringArrayArrayMethodCall" (func $export:std/array-access/stringArrayArrayMethodCall))
30+
(export "functionArrayElementCall" (func $export:std/array-access/functionArrayElementCall))
2931
(func $~lib/array/Array<~lib/array/Array<i32>>#get:length_ (type $i32_=>_i32) (param $this i32) (result i32)
3032
local.get $this
3133
i32.load $0 offset=12
@@ -249,6 +251,24 @@
249251
local.get $this
250252
i32.load $0 offset=4
251253
)
254+
(func $~lib/array/Array<%28i32%29=>i32>#get:length_ (type $i32_=>_i32) (param $this i32) (result i32)
255+
local.get $this
256+
i32.load $0 offset=12
257+
)
258+
(func $~lib/array/Array<%28i32%29=>i32>#get:dataStart (type $i32_=>_i32) (param $this i32) (result i32)
259+
local.get $this
260+
i32.load $0 offset=4
261+
)
262+
(func $std/array-access/functionArrayElementCall (type $i32_=>_i32) (param $a i32) (result i32)
263+
i32.const 123
264+
i32.const 1
265+
global.set $~argumentsLength
266+
local.get $a
267+
i32.const 0
268+
call $~lib/array/Array<%28i32%29=>i32>#__get
269+
i32.load $0
270+
call_indirect $0 (type $i32_=>_i32)
271+
)
252272
(func $~stack_check (type $none_=>_none)
253273
global.get $~lib/memory/__stack_pointer
254274
global.get $~lib/memory/__data_end
@@ -592,6 +612,62 @@
592612
global.set $~lib/memory/__stack_pointer
593613
local.get $3
594614
)
615+
(func $~lib/array/Array<%28i32%29=>i32>#__get (type $i32_i32_=>_i32) (param $this i32) (param $index i32) (result i32)
616+
(local $value i32)
617+
(local $3 i32)
618+
global.get $~lib/memory/__stack_pointer
619+
i32.const 4
620+
i32.sub
621+
global.set $~lib/memory/__stack_pointer
622+
call $~stack_check
623+
global.get $~lib/memory/__stack_pointer
624+
i32.const 0
625+
i32.store $0
626+
local.get $index
627+
local.get $this
628+
call $~lib/array/Array<%28i32%29=>i32>#get:length_
629+
i32.ge_u
630+
if
631+
i32.const 32
632+
i32.const 96
633+
i32.const 114
634+
i32.const 42
635+
call $~lib/builtins/abort
636+
unreachable
637+
end
638+
global.get $~lib/memory/__stack_pointer
639+
local.get $this
640+
call $~lib/array/Array<%28i32%29=>i32>#get:dataStart
641+
local.get $index
642+
i32.const 2
643+
i32.shl
644+
i32.add
645+
i32.load $0
646+
local.tee $value
647+
i32.store $0
648+
i32.const 1
649+
drop
650+
i32.const 0
651+
i32.eqz
652+
drop
653+
local.get $value
654+
i32.eqz
655+
if
656+
i32.const 144
657+
i32.const 96
658+
i32.const 118
659+
i32.const 40
660+
call $~lib/builtins/abort
661+
unreachable
662+
end
663+
local.get $value
664+
local.set $3
665+
global.get $~lib/memory/__stack_pointer
666+
i32.const 4
667+
i32.add
668+
global.set $~lib/memory/__stack_pointer
669+
local.get $3
670+
)
595671
(func $export:std/array-access/i32ArrayArrayElementAccess (type $i32_=>_i32) (param $0 i32) (result i32)
596672
(local $1 i32)
597673
global.get $~lib/memory/__stack_pointer
@@ -687,4 +763,23 @@
687763
global.set $~lib/memory/__stack_pointer
688764
local.get $1
689765
)
766+
(func $export:std/array-access/functionArrayElementCall (type $i32_=>_i32) (param $0 i32) (result i32)
767+
(local $1 i32)
768+
global.get $~lib/memory/__stack_pointer
769+
i32.const 4
770+
i32.sub
771+
global.set $~lib/memory/__stack_pointer
772+
call $~stack_check
773+
global.get $~lib/memory/__stack_pointer
774+
local.get $0
775+
i32.store $0
776+
local.get $0
777+
call $std/array-access/functionArrayElementCall
778+
local.set $1
779+
global.get $~lib/memory/__stack_pointer
780+
i32.const 4
781+
i32.add
782+
global.set $~lib/memory/__stack_pointer
783+
local.get $1
784+
)
690785
)

tests/compiler/std/array-access.release.wat

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
(data (i32.const 1160) "\01\00\00\00^\00\00\00E\00l\00e\00m\00e\00n\00t\00 \00t\00y\00p\00e\00 \00m\00u\00s\00t\00 \00b\00e\00 \00n\00u\00l\00l\00a\00b\00l\00e\00 \00i\00f\00 \00a\00r\00r\00a\00y\00 \00i\00s\00 \00h\00o\00l\00e\00y")
1414
(data (i32.const 1276) "\1c")
1515
(data (i32.const 1288) "\01")
16+
(table $0 1 1 funcref)
1617
(export "memory" (memory $0))
1718
(export "i32ArrayArrayElementAccess" (func $export:std/array-access/i32ArrayArrayElementAccess))
1819
(export "stringArrayPropertyAccess" (func $export:std/array-access/stringArrayPropertyAccess))
1920
(export "stringArrayMethodCall" (func $export:std/array-access/stringArrayMethodCall))
2021
(export "stringArrayArrayPropertyAccess" (func $export:std/array-access/stringArrayArrayPropertyAccess))
2122
(export "stringArrayArrayMethodCall" (func $export:std/array-access/stringArrayArrayMethodCall))
23+
(export "functionArrayElementCall" (func $export:std/array-access/functionArrayElementCall))
2224
(func $~lib/string/String#startsWith (type $i32_=>_i32) (param $0 i32) (result i32)
2325
(local $1 i32)
2426
(local $2 i32)
@@ -489,4 +491,32 @@
489491
call $~lib/builtins/abort
490492
unreachable
491493
)
494+
(func $export:std/array-access/functionArrayElementCall (type $i32_=>_i32) (param $0 i32) (result i32)
495+
global.get $~lib/memory/__stack_pointer
496+
i32.const 4
497+
i32.sub
498+
global.set $~lib/memory/__stack_pointer
499+
global.get $~lib/memory/__stack_pointer
500+
i32.const 1308
501+
i32.lt_s
502+
if
503+
i32.const 34096
504+
i32.const 34144
505+
i32.const 1
506+
i32.const 1
507+
call $~lib/builtins/abort
508+
unreachable
509+
end
510+
global.get $~lib/memory/__stack_pointer
511+
local.get $0
512+
i32.store $0
513+
i32.const 123
514+
local.get $0
515+
i32.const 0
516+
call $~lib/array/Array<~lib/array/Array<i32>>#__get
517+
i32.load $0
518+
call_indirect $0 (type $i32_=>_i32)
519+
drop
520+
unreachable
521+
)
492522
)

tests/compiler/std/array-access.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,9 @@ export function stringArrayArrayPropertyAccess(a: string[][]): i32 {
1717
export function stringArrayArrayMethodCall(a: string[][]): i32 {
1818
return a[0][1].startsWith("");
1919
}
20+
21+
// FIXME: Parenthesizing types is not supported
22+
type T = (x: i32) => i32
23+
export function functionArrayElementCall(a: T[]): i32 {
24+
return a[0](123);
25+
}

0 commit comments

Comments
 (0)