Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cli/asc.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ exports.main = function main(argv, options, callback) {
assemblyscript.setImportMemory(compilerOptions, args.importMemory);
assemblyscript.setSharedMemory(compilerOptions, args.sharedMemory);
assemblyscript.setImportTable(compilerOptions, args.importTable);
assemblyscript.setExportTable(compilerOptions, args.exportTable);
assemblyscript.setExplicitStart(compilerOptions, args.explicitStart);
assemblyscript.setMemoryBase(compilerOptions, args.memoryBase >>> 0);
assemblyscript.setSourceMap(compilerOptions, args.sourceMap != null);
Expand Down
9 changes: 7 additions & 2 deletions cli/asc.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
"default": false
},
"importMemory": {
"description": "Imports the memory instance provided by the embedder.",
"description": "Imports the memory provided as 'env.memory'.",
"type": "b",
"default": false
},
Expand All @@ -138,7 +138,12 @@
"default": 0
},
"importTable": {
"description": "Imports the function table instance provided by the embedder.",
"description": "Imports the function table provided as 'env.table'.",
"type": "b",
"default": false
},
"exportTable": {
"description": "Exports the function table as 'table'.",
"type": "b",
"default": false
},
Expand Down
5 changes: 4 additions & 1 deletion src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ export class Options {
sharedMemory: i32 = 0;
/** If true, imports the function table provided by the embedder. */
importTable: bool = false;
/** If true, exports the function table. */
exportTable: bool = false;
/** If true, generates information necessary for source maps. */
sourceMap: bool = false;
/** If true, generates an explicit start function. */
Expand Down Expand Up @@ -455,8 +457,9 @@ export class Compiler extends DiagnosticEmitter {
module.setFunctionTable(functionTable.length, Module.UNLIMITED_TABLE, functionTable, module.i32(0));
module.addFunction("null", NativeType.None, NativeType.None, null, module.unreachable());

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

// set up module exports
for (let file of this.program.filesByName.values()) {
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ export function setImportTable(options: Options, importTable: bool): void {
options.importTable = importTable;
}

/** Sets the `exportTable` option. */
export function setExportTable(options: Options, exportTable: bool): void {
options.exportTable = exportTable;
}

/** Sets the `sourceMap` option. */
export function setSourceMap(options: Options, sourceMap: bool): void {
options.sourceMap = sourceMap;
Expand Down
5 changes: 5 additions & 0 deletions tests/compiler/exportimport-table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
exports.preInstantiate = function(imports, exports) {
imports.env = {
table: new WebAssembly.Table({ element: "anyfunc", initial: 2 })
};
};
7 changes: 7 additions & 0 deletions tests/compiler/exportimport-table.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"asc_flags": [
"--runtime none",
"--importTable",
"--exportTable"
]
}
14 changes: 14 additions & 0 deletions tests/compiler/exportimport-table.optimized.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(module
(type $none_=>_none (func))
(import "env" "table" (table $0 2 funcref))
(elem (i32.const 0) $null $start:exportimport-table~anonymous|0)
(memory $0 0)
(export "memory" (memory $0))
(export "table" (table $0))
(func $start:exportimport-table~anonymous|0 (; 0 ;)
nop
)
(func $null (; 1 ;)
unreachable
)
)
2 changes: 2 additions & 0 deletions tests/compiler/exportimport-table.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var f = (): void => {};
f;
23 changes: 23 additions & 0 deletions tests/compiler/exportimport-table.untouched.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
(module
(type $none_=>_none (func))
(import "env" "table" (table $0 2 funcref))
(elem (i32.const 0) $null $start:exportimport-table~anonymous|0)
(memory $0 0)
(global $exportimport-table/f (mut i32) (i32.const 1))
(export "memory" (memory $0))
(export "table" (table $0))
(start $start)
(func $start:exportimport-table~anonymous|0 (; 0 ;)
nop
)
(func $start:exportimport-table (; 1 ;)
global.get $exportimport-table/f
drop
)
(func $start (; 2 ;)
call $start:exportimport-table
)
(func $null (; 3 ;)
unreachable
)
)