Skip to content

Commit e2804df

Browse files
authored
Add an option to export the function table (AssemblyScript#1001)
1 parent 8e6a934 commit e2804df

9 files changed

+68
-3
lines changed

cli/asc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ exports.main = function main(argv, options, callback) {
217217
assemblyscript.setImportMemory(compilerOptions, args.importMemory);
218218
assemblyscript.setSharedMemory(compilerOptions, args.sharedMemory);
219219
assemblyscript.setImportTable(compilerOptions, args.importTable);
220+
assemblyscript.setExportTable(compilerOptions, args.exportTable);
220221
assemblyscript.setExplicitStart(compilerOptions, args.explicitStart);
221222
assemblyscript.setMemoryBase(compilerOptions, args.memoryBase >>> 0);
222223
assemblyscript.setSourceMap(compilerOptions, args.sourceMap != null);

cli/asc.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123
"default": false
124124
},
125125
"importMemory": {
126-
"description": "Imports the memory instance provided by the embedder.",
126+
"description": "Imports the memory provided as 'env.memory'.",
127127
"type": "b",
128128
"default": false
129129
},
@@ -138,7 +138,12 @@
138138
"default": 0
139139
},
140140
"importTable": {
141-
"description": "Imports the function table instance provided by the embedder.",
141+
"description": "Imports the function table provided as 'env.table'.",
142+
"type": "b",
143+
"default": false
144+
},
145+
"exportTable": {
146+
"description": "Exports the function table as 'table'.",
142147
"type": "b",
143148
"default": false
144149
},

src/compiler.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ export class Options {
198198
sharedMemory: i32 = 0;
199199
/** If true, imports the function table provided by the embedder. */
200200
importTable: bool = false;
201+
/** If true, exports the function table. */
202+
exportTable: bool = false;
201203
/** If true, generates information necessary for source maps. */
202204
sourceMap: bool = false;
203205
/** If true, generates an explicit start function. */
@@ -455,8 +457,9 @@ export class Compiler extends DiagnosticEmitter {
455457
module.setFunctionTable(functionTable.length, Module.UNLIMITED_TABLE, functionTable, module.i32(0));
456458
module.addFunction("null", NativeType.None, NativeType.None, null, module.unreachable());
457459

458-
// import table if requested (default table is named '0' by Binaryen)
460+
// import and/or export table if requested (default table is named '0' by Binaryen)
459461
if (options.importTable) module.addTableImport("0", "env", "table");
462+
if (options.exportTable) module.addTableExport("0", "table");
460463

461464
// set up module exports
462465
for (let file of this.program.filesByName.values()) {

src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ export function setImportTable(options: Options, importTable: bool): void {
4343
options.importTable = importTable;
4444
}
4545

46+
/** Sets the `exportTable` option. */
47+
export function setExportTable(options: Options, exportTable: bool): void {
48+
options.exportTable = exportTable;
49+
}
50+
4651
/** Sets the `sourceMap` option. */
4752
export function setSourceMap(options: Options, sourceMap: bool): void {
4853
options.sourceMap = sourceMap;

tests/compiler/exportimport-table.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
exports.preInstantiate = function(imports, exports) {
2+
imports.env = {
3+
table: new WebAssembly.Table({ element: "anyfunc", initial: 2 })
4+
};
5+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"asc_flags": [
3+
"--runtime none",
4+
"--importTable",
5+
"--exportTable"
6+
]
7+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
(module
2+
(type $none_=>_none (func))
3+
(import "env" "table" (table $0 2 funcref))
4+
(elem (i32.const 0) $null $start:exportimport-table~anonymous|0)
5+
(memory $0 0)
6+
(export "memory" (memory $0))
7+
(export "table" (table $0))
8+
(func $start:exportimport-table~anonymous|0 (; 0 ;)
9+
nop
10+
)
11+
(func $null (; 1 ;)
12+
unreachable
13+
)
14+
)

tests/compiler/exportimport-table.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
var f = (): void => {};
2+
f;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
(module
2+
(type $none_=>_none (func))
3+
(import "env" "table" (table $0 2 funcref))
4+
(elem (i32.const 0) $null $start:exportimport-table~anonymous|0)
5+
(memory $0 0)
6+
(global $exportimport-table/f (mut i32) (i32.const 1))
7+
(export "memory" (memory $0))
8+
(export "table" (table $0))
9+
(start $start)
10+
(func $start:exportimport-table~anonymous|0 (; 0 ;)
11+
nop
12+
)
13+
(func $start:exportimport-table (; 1 ;)
14+
global.get $exportimport-table/f
15+
drop
16+
)
17+
(func $start (; 2 ;)
18+
call $start:exportimport-table
19+
)
20+
(func $null (; 3 ;)
21+
unreachable
22+
)
23+
)

0 commit comments

Comments
 (0)