Skip to content

Commit 5323e64

Browse files
committed
Implement array functions with just an expression body
1 parent 0fef69e commit 5323e64

File tree

6 files changed

+74
-18
lines changed

6 files changed

+74
-18
lines changed

dist/assemblyscript.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/assemblyscript.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/compiler.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -836,20 +836,27 @@ export class Compiler extends DiagnosticEmitter {
836836
var typeRef = this.ensureFunctionType(instance.signature);
837837
var module = this.module;
838838
if (body) {
839+
let returnType = instance.signature.returnType;
839840

840841
// compile body
841842
let previousFunction = this.currentFunction;
842843
this.currentFunction = instance;
843-
let stmt = this.compileStatement(body);
844-
845-
// make sure all branches return
846-
let allBranchesReturn = instance.flow.finalize();
847-
let returnType = instance.signature.returnType;
848-
if (returnType != Type.void && !allBranchesReturn) {
849-
this.error(
850-
DiagnosticCode.A_function_whose_declared_type_is_not_void_must_return_a_value,
851-
assert(declaration.signature.returnType, "return type expected").range
852-
);
844+
let flow = instance.flow;
845+
let stmt: ExpressionRef;
846+
if (body.kind == NodeKind.EXPRESSION) { // () => expression
847+
stmt = this.compileExpression((<ExpressionStatement>body).expression, returnType);
848+
flow.set(FlowFlags.RETURNS);
849+
} else {
850+
assert(body.kind == NodeKind.BLOCK);
851+
stmt = this.compileStatement(body);
852+
// make sure all branches return
853+
let allBranchesReturn = flow.finalize();
854+
if (returnType != Type.void && !allBranchesReturn) {
855+
this.error(
856+
DiagnosticCode.A_function_whose_declared_type_is_not_void_must_return_a_value,
857+
assert(declaration.signature.returnType, "return type expected").range
858+
);
859+
}
853860
}
854861
this.currentFunction = previousFunction;
855862

tests/compiler/function-expression.optimized.wat

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
(type $ii (func (param i32) (result i32)))
33
(type $iiiiv (func (param i32 i32 i32 i32)))
44
(type $v (func))
5+
(type $i (func (result i32)))
56
(import "env" "abort" (func $abort (param i32 i32 i32 i32)))
67
(global $function-expression/f1 (mut i32) (i32.const 0))
78
(global $function-expression/f2 (mut i32) (i32.const 1))
89
(global $function-expression/f3 (mut i32) (i32.const 2))
9-
(table 3 3 anyfunc)
10-
(elem (i32.const 0) $start~anonymous|0 $start~anonymous|0 $start~someName|2)
10+
(global $function-expression/f4 (mut i32) (i32.const 3))
11+
(table 4 4 anyfunc)
12+
(elem (i32.const 0) $start~anonymous|0 $start~anonymous|0 $start~someName|2 $start~anonymous|3)
1113
(memory $0 1)
1214
(data (i32.const 4) "\16\00\00\00f\00u\00n\00c\00t\00i\00o\00n\00-\00e\00x\00p\00r\00e\00s\00s\00i\00o\00n\00.\00t\00s")
1315
(export "memory" (memory $0))
@@ -18,7 +20,10 @@
1820
(func $start~someName|2 (; 2 ;) (type $v)
1921
(nop)
2022
)
21-
(func $start (; 3 ;) (type $v)
23+
(func $start~anonymous|3 (; 3 ;) (type $i) (result i32)
24+
(i32.const 1)
25+
)
26+
(func $start (; 4 ;) (type $v)
2227
(if
2328
(i32.ne
2429
(call_indirect (type $ii)
@@ -58,5 +63,22 @@
5863
(call_indirect (type $v)
5964
(get_global $function-expression/f3)
6065
)
66+
(if
67+
(i32.ne
68+
(call_indirect (type $i)
69+
(get_global $function-expression/f4)
70+
)
71+
(i32.const 1)
72+
)
73+
(block
74+
(call $abort
75+
(i32.const 0)
76+
(i32.const 4)
77+
(i32.const 16)
78+
(i32.const 0)
79+
)
80+
(unreachable)
81+
)
82+
)
6183
)
6284
)

tests/compiler/function-expression.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ assert(f2(2) == 2);
1111
var f3 = function someName(): void {
1212
};
1313
f3();
14+
15+
var f4 = (): i32 => 1;
16+
assert(f4() == 1);

tests/compiler/function-expression.untouched.wat

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
(type $ii (func (param i32) (result i32)))
33
(type $iiiiv (func (param i32 i32 i32 i32)))
44
(type $v (func))
5+
(type $i (func (result i32)))
56
(import "env" "abort" (func $abort (param i32 i32 i32 i32)))
67
(global $function-expression/f1 (mut i32) (i32.const 0))
78
(global $function-expression/f2 (mut i32) (i32.const 1))
89
(global $function-expression/f3 (mut i32) (i32.const 2))
10+
(global $function-expression/f4 (mut i32) (i32.const 3))
911
(global $HEAP_BASE i32 (i32.const 52))
10-
(table 3 3 anyfunc)
11-
(elem (i32.const 0) $start~anonymous|0 $start~anonymous|1 $start~someName|2)
12+
(table 4 4 anyfunc)
13+
(elem (i32.const 0) $start~anonymous|0 $start~anonymous|1 $start~someName|2 $start~anonymous|3)
1214
(memory $0 1)
1315
(data (i32.const 4) "\16\00\00\00f\00u\00n\00c\00t\00i\00o\00n\00-\00e\00x\00p\00r\00e\00s\00s\00i\00o\00n\00.\00t\00s\00")
1416
(export "memory" (memory $0))
@@ -25,7 +27,10 @@
2527
)
2628
(func $start~someName|2 (; 3 ;) (type $v)
2729
)
28-
(func $start (; 4 ;) (type $v)
30+
(func $start~anonymous|3 (; 4 ;) (type $i) (result i32)
31+
(i32.const 1)
32+
)
33+
(func $start (; 5 ;) (type $v)
2934
(if
3035
(i32.eqz
3136
(i32.eq
@@ -69,5 +74,24 @@
6974
(call_indirect (type $v)
7075
(get_global $function-expression/f3)
7176
)
77+
(if
78+
(i32.eqz
79+
(i32.eq
80+
(call_indirect (type $i)
81+
(get_global $function-expression/f4)
82+
)
83+
(i32.const 1)
84+
)
85+
)
86+
(block
87+
(call $abort
88+
(i32.const 0)
89+
(i32.const 4)
90+
(i32.const 16)
91+
(i32.const 0)
92+
)
93+
(unreachable)
94+
)
95+
)
7296
)
7397
)

0 commit comments

Comments
 (0)