Skip to content

Commit 42f6c85

Browse files
authored
Implicitly resolve to callables when resolving call expressions (#1320)
1 parent a27cc54 commit 42f6c85

File tree

4 files changed

+431
-4
lines changed

4 files changed

+431
-4
lines changed

src/resolver.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2384,6 +2384,17 @@ export class Resolver extends DiagnosticEmitter {
23842384
if (!instance) return null;
23852385
return instance.signature.returnType;
23862386
}
2387+
case ElementKind.GLOBAL:
2388+
case ElementKind.LOCAL:
2389+
case ElementKind.FIELD: {
2390+
let varType = (<VariableLikeElement>target).type;
2391+
let varElement = this.getElementOfType(varType);
2392+
if (!varElement || varElement.kind != ElementKind.FUNCTION_TARGET) {
2393+
break;
2394+
}
2395+
target = varElement;
2396+
// fall-through
2397+
}
23872398
case ElementKind.FUNCTION_TARGET: {
23882399
return (<FunctionTarget>target).signature.returnType;
23892400
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,116 @@
11
(module
2+
(type $none_=>_i32 (func (result i32)))
3+
(type $i32_=>_i32 (func (param i32) (result i32)))
4+
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
5+
(type $none_=>_none (func))
6+
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
7+
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
28
(memory $0 1)
39
(data (i32.const 1024) ",\00\00\00\01\00\00\00\01\00\00\00,\00\00\00f\00u\00n\00c\00t\00i\00o\00n\00-\00e\00x\00p\00r\00e\00s\00s\00i\00o\00n\00.\00t\00s")
10+
(table $0 18 funcref)
11+
(elem (i32.const 1) $start:function-expression~anonymous|0 $start:function-expression~anonymous|0 $start:function-expression~someName $start:function-expression~anonymous|2 $start:function-expression~anonymous|3 $start:function-expression~anonymous|4 $start:function-expression~anonymous|5 $start:function-expression~anonymous|3 $start:function-expression~anonymous|4 $start:function-expression~anonymous|5 $start:function-expression~anonymous|2 $function-expression/testGlobal~anonymous|0~anonymous|0 $function-expression/testGlobal~anonymous|0 $function-expression/testGlobal~anonymous|0~anonymous|0 $function-expression/testLocal~anonymous|0 $function-expression/testGlobal~anonymous|0~anonymous|0 $function-expression/testField~anonymous|0)
412
(export "memory" (memory $0))
13+
(start $~start)
14+
(func $start:function-expression~anonymous|0 (param $0 i32) (result i32)
15+
local.get $0
16+
)
17+
(func $start:function-expression~someName
18+
nop
19+
)
20+
(func $start:function-expression~anonymous|2 (result i32)
21+
i32.const 1
22+
)
23+
(func $start:function-expression~anonymous|3 (param $0 i32) (param $1 i32) (result i32)
24+
local.get $0
25+
local.get $1
26+
i32.add
27+
)
28+
(func $start:function-expression~anonymous|4 (param $0 i32) (param $1 i32) (result i32)
29+
local.get $0
30+
)
31+
(func $start:function-expression~anonymous|5 (param $0 i32) (param $1 i32) (result i32)
32+
i32.const 42
33+
)
34+
(func $function-expression/testGlobal~anonymous|0~anonymous|0 (param $0 i32) (result i32)
35+
local.get $0
36+
i32.const 24
37+
i32.add
38+
)
39+
(func $function-expression/testGlobal~anonymous|0 (result i32)
40+
i32.const 12
41+
)
42+
(func $function-expression/testLocal~anonymous|0 (result i32)
43+
i32.const 14
44+
)
45+
(func $function-expression/testField~anonymous|0 (result i32)
46+
i32.const 16
47+
)
48+
(func $~start
49+
(local $0 i32)
50+
(local $1 i32)
51+
i32.const 1120
52+
memory.size
53+
local.tee $1
54+
i32.const 16
55+
i32.shl
56+
local.tee $0
57+
i32.gt_u
58+
if
59+
local.get $1
60+
i32.const 66655
61+
local.get $0
62+
i32.sub
63+
i32.const -65536
64+
i32.and
65+
i32.const 16
66+
i32.shr_u
67+
local.tee $0
68+
local.get $1
69+
local.get $0
70+
i32.gt_s
71+
select
72+
memory.grow
73+
i32.const 0
74+
i32.lt_s
75+
if
76+
local.get $0
77+
memory.grow
78+
i32.const 0
79+
i32.lt_s
80+
if
81+
unreachable
82+
end
83+
end
84+
end
85+
i32.const 1088
86+
i32.const 16
87+
i32.store
88+
i32.const 1092
89+
i32.const 1
90+
i32.store
91+
i32.const 1096
92+
i32.const 3
93+
i32.store
94+
i32.const 1100
95+
i32.const 4
96+
i32.store
97+
i32.const 1104
98+
i32.const 17
99+
i32.store
100+
i32.const 1
101+
i32.const 1104
102+
i32.load
103+
call_indirect (type $none_=>_i32)
104+
call_indirect (type $i32_=>_i32)
105+
i32.const 25
106+
i32.ne
107+
if
108+
i32.const 0
109+
i32.const 1040
110+
i32.const 82
111+
i32.const 3
112+
call $~lib/builtins/abort
113+
unreachable
114+
end
115+
)
5116
)

tests/compiler/function-expression.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,42 @@ function testNullable(b: boolean): (() => i32) | null {
4343
}
4444
}
4545
assert(testNullable(false) == null);
46+
47+
// see: https://github.com/AssemblyScript/assemblyscript/issues/1289
48+
49+
var globalFunc: () => (x: i32) => i32;
50+
function testGlobal(): void {
51+
globalFunc = (): (x:i32) => i32 => {
52+
let myFunc = (x: i32): i32 => {
53+
return 24 + x;
54+
};
55+
return myFunc;
56+
};
57+
assert(globalFunc()(1) == 25);
58+
}
59+
testGlobal();
60+
61+
function testLocal(): void {
62+
let localFunc = (): (x:i32) => i32 => {
63+
let myFunc = (x: i32): i32 => {
64+
return 24 + x;
65+
};
66+
return myFunc;
67+
};
68+
assert(localFunc()(1) == 25);
69+
}
70+
testLocal();
71+
72+
class FieldClass {
73+
constructor(public fieldFunc: () => (x: i32) => i32) {}
74+
}
75+
function testField(): void {
76+
let fieldInst = new FieldClass((): (x:i32) => i32 => {
77+
let myFunc = (x: i32): i32 => {
78+
return 24 + x;
79+
};
80+
return myFunc;
81+
});
82+
assert(fieldInst.fieldFunc()(1) == 25);
83+
}
84+
testField();

0 commit comments

Comments
 (0)