Skip to content

Commit 16d1a83

Browse files
committed
Always add a null function at table index zero
This allows function references (a table index internally) to be nullable
1 parent 9613d29 commit 16d1a83

File tree

170 files changed

+971
-366
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

170 files changed

+971
-366
lines changed

src/compiler.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ export class Compiler extends DiagnosticEmitter {
286286
/** Map of already compiled static string segments. */
287287
stringSegments: Map<string,MemorySegment> = new Map();
288288
/** Function table being compiled. */
289-
functionTable: string[] = [];
289+
functionTable: string[] = [ "null" ];
290290
/** Argument count helper global. */
291291
argcVar: GlobalRef = 0;
292292
/** Argument count helper setter. */
@@ -394,10 +394,13 @@ export class Compiler extends DiagnosticEmitter {
394394
var functionTable = this.functionTable;
395395
var functionTableSize = functionTable.length;
396396
var functionTableExported = false;
397-
if (functionTableSize) {
398-
module.setFunctionTable(functionTable);
399-
module.addTableExport("0", "table");
400-
functionTableExported = true;
397+
module.setFunctionTable(functionTable);
398+
if (functionTableSize) { // index 0 is NULL
399+
module.addFunction("null", this.ensureFunctionType(null, Type.void), null, module.createBlock(null, []));
400+
if (functionTableSize > 1) {
401+
module.addTableExport("0", "table");
402+
functionTableExported = true;
403+
}
401404
}
402405

403406
// import table if requested (default table is named '0' by Binaryen)

tests/compiler/abi.optimized.wat

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,7 @@
5353
)
5454
)
5555
)
56+
(func $null (; 6 ;) (; has Stack IR ;) (type $v)
57+
(nop)
58+
)
5659
)

tests/compiler/abi.untouched.wat

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
(global $abi/condition (mut i32) (i32.const 0))
77
(global $abi/y (mut i32) (i32.const 0))
88
(global $HEAP_BASE i32 (i32.const 24))
9+
(table 1 1 anyfunc)
10+
(elem (i32.const 0) $null)
911
(memory $0 1)
1012
(data (i32.const 8) "\06\00\00\00a\00b\00i\00.\00t\00s\00")
1113
(export "memory" (memory $0))
@@ -283,4 +285,6 @@
283285
)
284286
)
285287
)
288+
(func $null (; 6 ;) (type $v)
289+
)
286290
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
(module
2+
(type $v (func))
23
(memory $0 0)
34
(export "memory" (memory $0))
5+
(func $start (; 0 ;) (; has Stack IR ;) (type $v)
6+
(nop)
7+
)
48
)

tests/compiler/asc-constants.untouched.wat

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
(global $ASC_FEATURE_MUTABLE_GLOBAL i32 (i32.const 0))
1010
(global $ASC_FEATURE_SIGN_EXTENSION i32 (i32.const 0))
1111
(global $HEAP_BASE i32 (i32.const 8))
12+
(table 1 1 anyfunc)
13+
(elem (i32.const 0) $null)
1214
(memory $0 0)
1315
(export "memory" (memory $0))
1416
(start $start)
@@ -38,4 +40,6 @@
3840
(i32.const 0)
3941
)
4042
)
43+
(func $null (; 1 ;) (type $v)
44+
)
4145
)

tests/compiler/assert.optimized.wat

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
(module
2+
(type $v (func))
23
(memory $0 1)
34
(data (i32.const 8) "\t\00\00\00a\00s\00s\00e\00r\00t\00.\00t\00s")
45
(data (i32.const 32) "\0c\00\00\00m\00u\00s\00t\00 \00b\00e\00 \00t\00r\00u\00e")
56
(export "memory" (memory $0))
7+
(func $start (; 0 ;) (; has Stack IR ;) (type $v)
8+
(nop)
9+
)
610
)

tests/compiler/assert.untouched.wat

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
(type $v (func))
44
(import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32)))
55
(global $HEAP_BASE i32 (i32.const 60))
6+
(table 1 1 anyfunc)
7+
(elem (i32.const 0) $null)
68
(memory $0 1)
79
(data (i32.const 8) "\t\00\00\00a\00s\00s\00e\00r\00t\00.\00t\00s\00")
810
(data (i32.const 32) "\0c\00\00\00m\00u\00s\00t\00 \00b\00e\00 \00t\00r\00u\00e\00")
@@ -139,4 +141,6 @@
139141
(unreachable)
140142
)
141143
)
144+
(func $null (; 2 ;) (type $v)
145+
)
142146
)

tests/compiler/binary.optimized.wat

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,4 +1148,7 @@
11481148
)
11491149
)
11501150
)
1151+
(func $null (; 5 ;) (; has Stack IR ;) (type $v)
1152+
(nop)
1153+
)
11511154
)

tests/compiler/binary.untouched.wat

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
(global $binary/f (mut f32) (f32.const 0))
1212
(global $binary/F (mut f64) (f64.const 0))
1313
(global $HEAP_BASE i32 (i32.const 8))
14+
(table 1 1 anyfunc)
15+
(elem (i32.const 0) $null)
1416
(memory $0 0)
1517
(export "memory" (memory $0))
1618
(start $start)
@@ -4920,4 +4922,6 @@
49204922
)
49214923
)
49224924
)
4925+
(func $null (; 7 ;) (type $v)
4926+
)
49234927
)

tests/compiler/builtins.optimized.wat

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@
1111
(global $builtins/u (mut i32) (i32.const 0))
1212
(global $builtins/U (mut i64) (i64.const 0))
1313
(global $builtins/s (mut i32) (i32.const 0))
14-
(global $builtins/fn (mut i32) (i32.const 0))
15-
(table 1 1 anyfunc)
16-
(elem (i32.const 0) $start~anonymous|0)
14+
(global $builtins/fn (mut i32) (i32.const 1))
15+
(table 2 2 anyfunc)
16+
(elem (i32.const 0) $builtins/test $start~anonymous|1)
1717
(memory $0 1)
1818
(data (i32.const 8) "\0b\00\00\00b\00u\00i\00l\00t\00i\00n\00s\00.\00t\00s")
1919
(data (i32.const 40) "\01\00\00\001")
2020
(export "memory" (memory $0))
2121
(export "table" (table $0))
2222
(export "test" (func $builtins/test))
2323
(start $start)
24-
(func $start~anonymous|0 (; 1 ;) (; has Stack IR ;) (type $iiv) (param $0 i32) (param $1 i32)
24+
(func $start~anonymous|1 (; 1 ;) (; has Stack IR ;) (type $iiv) (param $0 i32) (param $1 i32)
2525
(nop)
2626
)
2727
(func $builtins/test (; 2 ;) (; has Stack IR ;) (type $v)

tests/compiler/builtins.untouched.wat

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
(global $builtins/u (mut i32) (i32.const 0))
1515
(global $builtins/U (mut i64) (i64.const 0))
1616
(global $builtins/s (mut i32) (i32.const 0))
17-
(global $builtins/fn (mut i32) (i32.const 0))
17+
(global $builtins/fn (mut i32) (i32.const 1))
1818
(global $~lib/builtins/i8.MIN_VALUE i32 (i32.const -128))
1919
(global $~lib/builtins/i8.MAX_VALUE i32 (i32.const 127))
2020
(global $~lib/builtins/i16.MIN_VALUE i32 (i32.const -32768))
@@ -44,16 +44,16 @@
4444
(global $~lib/builtins/f64.MAX_SAFE_INTEGER f64 (f64.const 9007199254740991))
4545
(global $~lib/builtins/f64.EPSILON f64 (f64.const 2.220446049250313e-16))
4646
(global $HEAP_BASE i32 (i32.const 48))
47-
(table 1 1 anyfunc)
48-
(elem (i32.const 0) $start~anonymous|0)
47+
(table 2 2 anyfunc)
48+
(elem (i32.const 0) $null $start~anonymous|1)
4949
(memory $0 1)
5050
(data (i32.const 8) "\0b\00\00\00b\00u\00i\00l\00t\00i\00n\00s\00.\00t\00s\00")
5151
(data (i32.const 40) "\01\00\00\001\00")
5252
(export "memory" (memory $0))
5353
(export "table" (table $0))
5454
(export "test" (func $builtins/test))
5555
(start $start)
56-
(func $start~anonymous|0 (; 1 ;) (type $iiv) (param $0 i32) (param $1 i32)
56+
(func $start~anonymous|1 (; 1 ;) (type $iiv) (param $0 i32) (param $1 i32)
5757
(nop)
5858
)
5959
(func $builtins/test (; 2 ;) (type $v)
@@ -3123,4 +3123,6 @@
31233123
)
31243124
)
31253125
)
3126+
(func $null (; 4 ;) (type $v)
3127+
)
31263128
)

tests/compiler/call-inferred.optimized.wat

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,7 @@
8080
)
8181
)
8282
)
83+
(func $null (; 5 ;) (; has Stack IR ;) (type $v)
84+
(nop)
85+
)
8386
)

tests/compiler/call-inferred.untouched.wat

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
(type $v (func))
77
(import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32)))
88
(global $HEAP_BASE i32 (i32.const 44))
9+
(table 1 1 anyfunc)
10+
(elem (i32.const 0) $null)
911
(memory $0 1)
1012
(data (i32.const 8) "\10\00\00\00c\00a\00l\00l\00-\00i\00n\00f\00e\00r\00r\00e\00d\00.\00t\00s\00")
1113
(export "memory" (memory $0))
@@ -100,4 +102,6 @@
100102
)
101103
)
102104
)
105+
(func $null (; 6 ;) (type $v)
106+
)
103107
)

tests/compiler/call-optional.optimized.wat

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
(type $v (func))
55
(import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32)))
66
(global $~argc (mut i32) (i32.const 0))
7-
(global $call-optional/optIndirect (mut i32) (i32.const 0))
8-
(table 1 1 anyfunc)
9-
(elem (i32.const 0) $call-optional/opt|trampoline)
7+
(global $call-optional/optIndirect (mut i32) (i32.const 1))
8+
(table 2 2 anyfunc)
9+
(elem (i32.const 0) $null $call-optional/opt|trampoline)
1010
(memory $0 1)
1111
(data (i32.const 8) "\10\00\00\00c\00a\00l\00l\00-\00o\00p\00t\00i\00o\00n\00a\00l\00.\00t\00s")
1212
(export "memory" (memory $0))
@@ -177,4 +177,7 @@
177177
)
178178
)
179179
)
180+
(func $null (; 4 ;) (; has Stack IR ;) (type $v)
181+
(nop)
182+
)
180183
)

tests/compiler/call-optional.untouched.wat

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
(type $v (func))
55
(import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32)))
66
(global $~argc (mut i32) (i32.const 0))
7-
(global $call-optional/optIndirect (mut i32) (i32.const 0))
7+
(global $call-optional/optIndirect (mut i32) (i32.const 1))
88
(global $HEAP_BASE i32 (i32.const 44))
9-
(table 1 1 anyfunc)
10-
(elem (i32.const 0) $call-optional/opt|trampoline)
9+
(table 2 2 anyfunc)
10+
(elem (i32.const 0) $null $call-optional/opt|trampoline)
1111
(memory $0 1)
1212
(data (i32.const 8) "\10\00\00\00c\00a\00l\00l\00-\00o\00p\00t\00i\00o\00n\00a\00l\00.\00t\00s\00")
1313
(export "memory" (memory $0))
@@ -206,4 +206,6 @@
206206
)
207207
)
208208
)
209+
(func $null (; 4 ;) (type $v)
210+
)
209211
)

tests/compiler/class-extends.optimized.wat

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(module
22
(type $iv (func (param i32)))
3+
(type $v (func))
34
(memory $0 0)
45
(export "memory" (memory $0))
56
(export "test" (func $class-extends/test))
@@ -23,4 +24,7 @@
2324
(i32.const 3)
2425
)
2526
)
27+
(func $null (; 1 ;) (; has Stack IR ;) (type $v)
28+
(nop)
29+
)
2630
)

tests/compiler/class-extends.untouched.wat

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
(module
22
(type $iv (func (param i32)))
3+
(type $v (func))
34
(global $HEAP_BASE i32 (i32.const 8))
5+
(table 1 1 anyfunc)
6+
(elem (i32.const 0) $null)
47
(memory $0 0)
58
(export "memory" (memory $0))
69
(export "test" (func $class-extends/test))
@@ -24,4 +27,6 @@
2427
(i32.const 3)
2528
)
2629
)
30+
(func $null (; 1 ;) (type $v)
31+
)
2732
)

tests/compiler/class-overloading.optimized.wat

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@
1717
(i32.const 0)
1818
)
1919
)
20+
(func $null (; 3 ;) (; has Stack IR ;) (type $v)
21+
(nop)
22+
)
2023
)

tests/compiler/class-overloading.untouched.wat

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
(type $iv (func (param i32)))
33
(type $v (func))
44
(global $HEAP_BASE i32 (i32.const 8))
5+
(table 1 1 anyfunc)
6+
(elem (i32.const 0) $null)
57
(memory $0 0)
68
(export "memory" (memory $0))
79
(export "test" (func $class-overloading/test))
@@ -19,4 +21,6 @@
1921
(i32.const 0)
2022
)
2123
)
24+
(func $null (; 3 ;) (type $v)
25+
)
2226
)

tests/compiler/class-with-boolean-field.optimized.wat

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(module
22
(type $i (func (result i32)))
3+
(type $v (func))
34
(memory $0 0)
45
(export "memory" (memory $0))
56
(export "test" (func $class-with-boolean-field/test))
@@ -12,4 +13,7 @@
1213
(i32.const 0)
1314
)
1415
)
16+
(func $null (; 1 ;) (; has Stack IR ;) (type $v)
17+
(nop)
18+
)
1519
)

tests/compiler/class-with-boolean-field.untouched.wat

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
(module
22
(type $i (func (result i32)))
3+
(type $v (func))
34
(global $HEAP_BASE i32 (i32.const 8))
5+
(table 1 1 anyfunc)
6+
(elem (i32.const 0) $null)
47
(memory $0 0)
58
(export "memory" (memory $0))
69
(export "test" (func $class-with-boolean-field/test))
@@ -14,4 +17,6 @@
1417
(get_local $0)
1518
)
1619
)
20+
(func $null (; 1 ;) (type $v)
21+
)
1722
)

tests/compiler/class.optimized.wat

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,7 @@
6767
(call $class/Animal.sub<f32>)
6868
)
6969
)
70+
(func $null (; 4 ;) (; has Stack IR ;) (type $v)
71+
(nop)
72+
)
7073
)

tests/compiler/class.untouched.wat

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
(import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32)))
1010
(global $class/Animal.ONE (mut i32) (i32.const 1))
1111
(global $HEAP_BASE i32 (i32.const 28))
12+
(table 1 1 anyfunc)
13+
(elem (i32.const 0) $null)
1214
(memory $0 1)
1315
(data (i32.const 8) "\08\00\00\00c\00l\00a\00s\00s\00.\00t\00s\00")
1416
(export "memory" (memory $0))
@@ -152,4 +154,6 @@
152154
)
153155
)
154156
)
157+
(func $null (; 7 ;) (type $v)
158+
)
155159
)

tests/compiler/closure.optimized.wat

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
(module
2+
(type $v (func))
23
(memory $0 0)
34
(export "memory" (memory $0))
5+
(func $null (; 0 ;) (; has Stack IR ;) (type $v)
6+
(nop)
7+
)
48
)

tests/compiler/closure.untouched.wat

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
(module
2+
(type $v (func))
23
(global $HEAP_BASE i32 (i32.const 8))
4+
(table 1 1 anyfunc)
5+
(elem (i32.const 0) $null)
36
(memory $0 0)
47
(export "memory" (memory $0))
8+
(func $null (; 0 ;) (type $v)
9+
)
510
)

tests/compiler/comma.optimized.wat

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,7 @@
216216
)
217217
)
218218
)
219+
(func $null (; 2 ;) (; has Stack IR ;) (type $v)
220+
(nop)
221+
)
219222
)

0 commit comments

Comments
 (0)