From da4bf60a09563dcc545aad070b149a8a0b47cb84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Sinan=20A=C4=9Facan?= Date: Wed, 15 May 2024 09:49:02 +0000 Subject: [PATCH] [wasm_builder] Update incorrect docs, some minor refactoring MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Imported things (modules, globals, tables, functions) don't need to declared before defining things, as we don't assign indices to things before finalizing the IR, and when finalizing we assign ids to imported things before defined things, in `finalizeImportsAndBuilders`. Remove documentation saying imports should be declared before definitions. `FunctionsBuilder._functions` list was used in the name section, and required imports to come before definitions. This list is now removed, instead we pass `[...imported, ...defined]` to the names section. - Stop upcasting imported things to `Import` before we need to upcast. - Fix global lists passed to `NameSection`. These lists need to include imports as well (even though we never name them, wasm_builder doesn't allow naming them yet) otherwise the indices in the name section will be incorrect. wasm_builder doesn't allow importing types yet, so we don't need to do the same for the types list. Change-Id: Id05632c3af7937bd66d7581d89d538137020f6e6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/366601 Commit-Queue: Ömer Ağacan Reviewed-by: Martin Kustermann --- pkg/wasm_builder/lib/src/builder/functions.dart | 10 ++-------- pkg/wasm_builder/lib/src/builder/globals.dart | 5 +---- pkg/wasm_builder/lib/src/builder/memories.dart | 5 +---- pkg/wasm_builder/lib/src/builder/module.dart | 11 ++++++----- pkg/wasm_builder/lib/src/builder/tables.dart | 5 +---- pkg/wasm_builder/lib/src/ir/functions.dart | 7 ++----- pkg/wasm_builder/lib/src/ir/globals.dart | 2 +- pkg/wasm_builder/lib/src/ir/memories.dart | 2 +- pkg/wasm_builder/lib/src/ir/module.dart | 10 ++++++++-- pkg/wasm_builder/lib/src/ir/tables.dart | 2 +- 10 files changed, 24 insertions(+), 35 deletions(-) diff --git a/pkg/wasm_builder/lib/src/builder/functions.dart b/pkg/wasm_builder/lib/src/builder/functions.dart index 474a469a7fa..04c6e07f376 100644 --- a/pkg/wasm_builder/lib/src/builder/functions.dart +++ b/pkg/wasm_builder/lib/src/builder/functions.dart @@ -11,9 +11,8 @@ part 'function.dart'; /// The interface for the functions in a module. class FunctionsBuilder with Builder { final ModuleBuilder _module; - final _functions = []; final _functionBuilders = []; - final _importedFunctions = []; + final _importedFunctions = []; int _nameCount = 0; ir.BaseFunction? _start; @@ -28,7 +27,6 @@ class FunctionsBuilder with Builder { if (name != null) { _nameCount++; } - _functions.add(function); } /// Defines a new function in this module with the given function type. @@ -44,9 +42,6 @@ class FunctionsBuilder with Builder { } /// Import a function into the module. - /// - /// All imported functions must be specified before any functions are declared - /// using [FunctionsBuilder.define]. ir.ImportedFunction import(String module, String name, ir.FunctionType type, [String? functionName]) { final function = ir.ImportedFunction( @@ -60,7 +55,6 @@ class FunctionsBuilder with Builder { ir.Functions forceBuild() { final built = finalizeImportsAndBuilders( _importedFunctions, _functionBuilders); - return ir.Functions( - _start, _importedFunctions, built, _functions, _nameCount); + return ir.Functions(_start, _importedFunctions, built, _nameCount); } } diff --git a/pkg/wasm_builder/lib/src/builder/globals.dart b/pkg/wasm_builder/lib/src/builder/globals.dart index 6b72909aaec..23e2eed0af1 100644 --- a/pkg/wasm_builder/lib/src/builder/globals.dart +++ b/pkg/wasm_builder/lib/src/builder/globals.dart @@ -10,7 +10,7 @@ part 'global.dart'; class GlobalsBuilder with Builder { final ModuleBuilder _module; - final _importedGlobals = []; + final _importedGlobals = []; final _globalBuilders = []; /// Number of named globals. @@ -29,9 +29,6 @@ class GlobalsBuilder with Builder { } /// Imports a global variable into this module. - /// - /// All imported globals must be specified before any globals are declared - /// using [Globals.define]. ir.ImportedGlobal import(String module, String name, ir.GlobalType type) { final global = ir.ImportedGlobal(module, name, ir.FinalizableIndex(), type); _importedGlobals.add(global); diff --git a/pkg/wasm_builder/lib/src/builder/memories.dart b/pkg/wasm_builder/lib/src/builder/memories.dart index 1f9da580a73..ec037e19ce2 100644 --- a/pkg/wasm_builder/lib/src/builder/memories.dart +++ b/pkg/wasm_builder/lib/src/builder/memories.dart @@ -8,7 +8,7 @@ import 'util.dart'; class MemoriesBuilder with Builder { final _definedMemories = []; - final _importedMemories = []; + final _importedMemories = []; /// Add a new memory to the module. ir.DefinedMemory define(bool shared, int minSize, [int? maxSize]) { @@ -19,9 +19,6 @@ class MemoriesBuilder with Builder { } /// Imports a memory into this module. - /// - /// All imported memories must be specified before any memories are declared - /// using [defined]. ir.ImportedMemory import(String module, String name, bool shared, int minSize, [int? maxSize]) { final memory = ir.ImportedMemory( diff --git a/pkg/wasm_builder/lib/src/builder/module.dart b/pkg/wasm_builder/lib/src/builder/module.dart index 47ec9f5c4ce..ff96acef802 100644 --- a/pkg/wasm_builder/lib/src/builder/module.dart +++ b/pkg/wasm_builder/lib/src/builder/module.dart @@ -41,11 +41,12 @@ class ModuleBuilder with Builder { finalGlobals, types.build(), dataSegments.build(), - finalFunctions.imported - .followedBy(finalTables.imported) - .followedBy(finalMemories.imported) - .followedBy(finalGlobals.imported) - .toList(), + [ + ...finalFunctions.imported, + ...finalTables.imported, + ...finalMemories.imported, + ...finalGlobals.imported + ], watchPoints); } } diff --git a/pkg/wasm_builder/lib/src/builder/tables.dart b/pkg/wasm_builder/lib/src/builder/tables.dart index f98f29d9ce5..fe58dd5958a 100644 --- a/pkg/wasm_builder/lib/src/builder/tables.dart +++ b/pkg/wasm_builder/lib/src/builder/tables.dart @@ -11,7 +11,7 @@ part 'table.dart'; /// The interface for the tables in a module. class TablesBuilder with Builder { final _tableBuilders = []; - final _importedTables = []; + final _importedTables = []; /// Defines a new table in this module. TableBuilder define(ir.RefType type, int minSize, [int? maxSize]) { @@ -21,9 +21,6 @@ class TablesBuilder with Builder { } /// Imports a table into this module. - /// - /// All imported tables must be specified before any tables are declared - /// using [Tables.define]. ir.ImportedTable import( String module, String name, ir.RefType type, int minSize, [int? maxSize]) { diff --git a/pkg/wasm_builder/lib/src/ir/functions.dart b/pkg/wasm_builder/lib/src/ir/functions.dart index fb89092df88..759405ef512 100644 --- a/pkg/wasm_builder/lib/src/ir/functions.dart +++ b/pkg/wasm_builder/lib/src/ir/functions.dart @@ -13,16 +13,13 @@ class Functions { final BaseFunction? start; /// Imported functions. - final List imported; + final List imported; /// Defined functions. final List defined; - /// All functions, in the order they were emitted. - final List all; - /// Named functions. final int namedCount; - Functions(this.start, this.imported, this.defined, this.all, this.namedCount); + Functions(this.start, this.imported, this.defined, this.namedCount); } diff --git a/pkg/wasm_builder/lib/src/ir/globals.dart b/pkg/wasm_builder/lib/src/ir/globals.dart index 564d6a8da70..4a1fda01926 100644 --- a/pkg/wasm_builder/lib/src/ir/globals.dart +++ b/pkg/wasm_builder/lib/src/ir/globals.dart @@ -9,7 +9,7 @@ part 'global.dart'; class Globals { /// Imported globals. - final List imported; + final List imported; /// Defined globals. final List defined; diff --git a/pkg/wasm_builder/lib/src/ir/memories.dart b/pkg/wasm_builder/lib/src/ir/memories.dart index 40740632959..92aa3a599fe 100644 --- a/pkg/wasm_builder/lib/src/ir/memories.dart +++ b/pkg/wasm_builder/lib/src/ir/memories.dart @@ -9,7 +9,7 @@ part 'memory.dart'; class Memories { /// Imported memories. - final List imported; + final List imported; /// Defined memories. final List defined; diff --git a/pkg/wasm_builder/lib/src/ir/module.dart b/pkg/wasm_builder/lib/src/ir/module.dart index a188358c0cf..945a985413a 100644 --- a/pkg/wasm_builder/lib/src/ir/module.dart +++ b/pkg/wasm_builder/lib/src/ir/module.dart @@ -51,8 +51,14 @@ class Module implements Serializable { DataCountSection(dataSegments.defined, watchPoints).serialize(s); CodeSection(functions.defined, watchPoints).serialize(s); DataSection(dataSegments.defined, watchPoints).serialize(s); - if (functions.namedCount > 0 || types.namedCount > 0) { - NameSection(functions.all, types.defined, globals.defined, watchPoints, + if (functions.namedCount > 0 || + types.namedCount > 0 || + globals.namedCount > 0) { + NameSection( + [...functions.imported, ...functions.defined], + types.defined, + [...globals.imported, ...globals.defined], + watchPoints, functionNameCount: functions.namedCount, typeNameCount: types.namedCount, globalNameCount: globals.namedCount) diff --git a/pkg/wasm_builder/lib/src/ir/tables.dart b/pkg/wasm_builder/lib/src/ir/tables.dart index a253ea9349e..6fd243895b7 100644 --- a/pkg/wasm_builder/lib/src/ir/tables.dart +++ b/pkg/wasm_builder/lib/src/ir/tables.dart @@ -9,7 +9,7 @@ part 'table.dart'; class Tables { /// Imported tables. - final List imported; + final List imported; /// Defined tables. final List defined;