From 43fecdbb1da8850284fd9ea9840a032bcb45b0a6 Mon Sep 17 00:00:00 2001 From: dcode Date: Tue, 10 Dec 2019 08:09:55 +0100 Subject: [PATCH 1/3] Add an option to export the function table --- cli/asc.js | 1 + cli/asc.json | 9 ++++++-- src/compiler.ts | 5 +++- src/index.ts | 5 ++++ tests/compiler/exportimport-table.js | 5 ++++ tests/compiler/exportimport-table.json | 7 ++++++ .../compiler/exportimport-table.optimized.wat | 14 +++++++++++ tests/compiler/exportimport-table.ts | 2 ++ .../compiler/exportimport-table.untouched.wat | 23 +++++++++++++++++++ 9 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 tests/compiler/exportimport-table.js create mode 100644 tests/compiler/exportimport-table.json create mode 100644 tests/compiler/exportimport-table.optimized.wat create mode 100644 tests/compiler/exportimport-table.ts create mode 100644 tests/compiler/exportimport-table.untouched.wat diff --git a/cli/asc.js b/cli/asc.js index fbe5c07235..32ca6ceffb 100644 --- a/cli/asc.js +++ b/cli/asc.js @@ -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); diff --git a/cli/asc.json b/cli/asc.json index bb5b2e1134..93d3953833 100644 --- a/cli/asc.json +++ b/cli/asc.json @@ -123,7 +123,7 @@ "default": false }, "importMemory": { - "description": "Imports the memory instance provided by the embedder.", + "description": "Imports the memory provided by the embedder.", "type": "b", "default": false }, @@ -138,7 +138,12 @@ "default": 0 }, "importTable": { - "description": "Imports the function table instance provided by the embedder.", + "description": "Imports the function table provided by the embedder.", + "type": "b", + "default": false + }, + "exportTable": { + "description": "Exports the function table.", "type": "b", "default": false }, diff --git a/src/compiler.ts b/src/compiler.ts index 158fa472cf..9cbb1a5fdd 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -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. */ @@ -458,8 +460,9 @@ export class Compiler extends DiagnosticEmitter { module.setFunctionTable(functionTable.length, Module.UNLIMITED_TABLE, functionTable, module.i32(0)); module.addFunction("null", this.ensureFunctionType(null, Type.void), 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()) { diff --git a/src/index.ts b/src/index.ts index 7689e7fec0..436193a6a7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; diff --git a/tests/compiler/exportimport-table.js b/tests/compiler/exportimport-table.js new file mode 100644 index 0000000000..ce8963366a --- /dev/null +++ b/tests/compiler/exportimport-table.js @@ -0,0 +1,5 @@ +exports.preInstantiate = function(imports, exports) { + imports.env = { + table: new WebAssembly.Table({ element: "anyfunc", initial: 2 }) + }; +}; diff --git a/tests/compiler/exportimport-table.json b/tests/compiler/exportimport-table.json new file mode 100644 index 0000000000..99ea3c7483 --- /dev/null +++ b/tests/compiler/exportimport-table.json @@ -0,0 +1,7 @@ +{ + "asc_flags": [ + "--runtime none", + "--importTable", + "--exportTable" + ] +} \ No newline at end of file diff --git a/tests/compiler/exportimport-table.optimized.wat b/tests/compiler/exportimport-table.optimized.wat new file mode 100644 index 0000000000..e0b58d7604 --- /dev/null +++ b/tests/compiler/exportimport-table.optimized.wat @@ -0,0 +1,14 @@ +(module + (type $FUNCSIG$v (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 ;) (type $FUNCSIG$v) + nop + ) + (func $null (; 1 ;) (type $FUNCSIG$v) + unreachable + ) +) diff --git a/tests/compiler/exportimport-table.ts b/tests/compiler/exportimport-table.ts new file mode 100644 index 0000000000..3abb0a8275 --- /dev/null +++ b/tests/compiler/exportimport-table.ts @@ -0,0 +1,2 @@ +var f = (): void => {}; +f; diff --git a/tests/compiler/exportimport-table.untouched.wat b/tests/compiler/exportimport-table.untouched.wat new file mode 100644 index 0000000000..b860dacde8 --- /dev/null +++ b/tests/compiler/exportimport-table.untouched.wat @@ -0,0 +1,23 @@ +(module + (type $FUNCSIG$v (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 ;) (type $FUNCSIG$v) + nop + ) + (func $start:exportimport-table (; 1 ;) (type $FUNCSIG$v) + global.get $exportimport-table/f + drop + ) + (func $start (; 2 ;) (type $FUNCSIG$v) + call $start:exportimport-table + ) + (func $null (; 3 ;) (type $FUNCSIG$v) + unreachable + ) +) From 88d707a88197aee69d55a6560b382aea5f0e1aaf Mon Sep 17 00:00:00 2001 From: dcode Date: Fri, 13 Dec 2019 08:31:23 +0100 Subject: [PATCH 2/3] indicate import/export names in --help --- cli/asc.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cli/asc.json b/cli/asc.json index 93d3953833..a5be82478d 100644 --- a/cli/asc.json +++ b/cli/asc.json @@ -123,7 +123,7 @@ "default": false }, "importMemory": { - "description": "Imports the memory provided by the embedder.", + "description": "Imports the memory provided as 'env.memory'.", "type": "b", "default": false }, @@ -138,12 +138,12 @@ "default": 0 }, "importTable": { - "description": "Imports the function table provided by the embedder.", + "description": "Imports the function table provided as 'env.table'.", "type": "b", "default": false }, "exportTable": { - "description": "Exports the function table.", + "description": "Exports the function table as 'table'.", "type": "b", "default": false }, From e9ca58ae0df4341e397a13bf9b6c52329f22f5cc Mon Sep 17 00:00:00 2001 From: dcode Date: Wed, 18 Dec 2019 14:35:20 +0100 Subject: [PATCH 3/3] update test --- tests/compiler/exportimport-table.optimized.wat | 6 +++--- tests/compiler/exportimport-table.untouched.wat | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/compiler/exportimport-table.optimized.wat b/tests/compiler/exportimport-table.optimized.wat index e0b58d7604..17e119d655 100644 --- a/tests/compiler/exportimport-table.optimized.wat +++ b/tests/compiler/exportimport-table.optimized.wat @@ -1,14 +1,14 @@ (module - (type $FUNCSIG$v (func)) + (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 ;) (type $FUNCSIG$v) + (func $start:exportimport-table~anonymous|0 (; 0 ;) nop ) - (func $null (; 1 ;) (type $FUNCSIG$v) + (func $null (; 1 ;) unreachable ) ) diff --git a/tests/compiler/exportimport-table.untouched.wat b/tests/compiler/exportimport-table.untouched.wat index b860dacde8..4ab706c47c 100644 --- a/tests/compiler/exportimport-table.untouched.wat +++ b/tests/compiler/exportimport-table.untouched.wat @@ -1,5 +1,5 @@ (module - (type $FUNCSIG$v (func)) + (type $none_=>_none (func)) (import "env" "table" (table $0 2 funcref)) (elem (i32.const 0) $null $start:exportimport-table~anonymous|0) (memory $0 0) @@ -7,17 +7,17 @@ (export "memory" (memory $0)) (export "table" (table $0)) (start $start) - (func $start:exportimport-table~anonymous|0 (; 0 ;) (type $FUNCSIG$v) + (func $start:exportimport-table~anonymous|0 (; 0 ;) nop ) - (func $start:exportimport-table (; 1 ;) (type $FUNCSIG$v) + (func $start:exportimport-table (; 1 ;) global.get $exportimport-table/f drop ) - (func $start (; 2 ;) (type $FUNCSIG$v) + (func $start (; 2 ;) call $start:exportimport-table ) - (func $null (; 3 ;) (type $FUNCSIG$v) + (func $null (; 3 ;) unreachable ) )