Skip to content

Commit da4bf60

Browse files
osa1Commit Queue
authored and
Commit Queue
committed
[wasm_builder] Update incorrect docs, some minor refactoring
- 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 <[email protected]> Reviewed-by: Martin Kustermann <[email protected]>
1 parent 0299c05 commit da4bf60

File tree

10 files changed

+24
-35
lines changed

10 files changed

+24
-35
lines changed

pkg/wasm_builder/lib/src/builder/functions.dart

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ part 'function.dart';
1111
/// The interface for the functions in a module.
1212
class FunctionsBuilder with Builder<ir.Functions> {
1313
final ModuleBuilder _module;
14-
final _functions = <ir.BaseFunction>[];
1514
final _functionBuilders = <FunctionBuilder>[];
16-
final _importedFunctions = <ir.Import>[];
15+
final _importedFunctions = <ir.ImportedFunction>[];
1716
int _nameCount = 0;
1817
ir.BaseFunction? _start;
1918

@@ -28,7 +27,6 @@ class FunctionsBuilder with Builder<ir.Functions> {
2827
if (name != null) {
2928
_nameCount++;
3029
}
31-
_functions.add(function);
3230
}
3331

3432
/// Defines a new function in this module with the given function type.
@@ -44,9 +42,6 @@ class FunctionsBuilder with Builder<ir.Functions> {
4442
}
4543

4644
/// Import a function into the module.
47-
///
48-
/// All imported functions must be specified before any functions are declared
49-
/// using [FunctionsBuilder.define].
5045
ir.ImportedFunction import(String module, String name, ir.FunctionType type,
5146
[String? functionName]) {
5247
final function = ir.ImportedFunction(
@@ -60,7 +55,6 @@ class FunctionsBuilder with Builder<ir.Functions> {
6055
ir.Functions forceBuild() {
6156
final built = finalizeImportsAndBuilders<ir.DefinedFunction>(
6257
_importedFunctions, _functionBuilders);
63-
return ir.Functions(
64-
_start, _importedFunctions, built, _functions, _nameCount);
58+
return ir.Functions(_start, _importedFunctions, built, _nameCount);
6559
}
6660
}

pkg/wasm_builder/lib/src/builder/globals.dart

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ part 'global.dart';
1010

1111
class GlobalsBuilder with Builder<ir.Globals> {
1212
final ModuleBuilder _module;
13-
final _importedGlobals = <ir.Import>[];
13+
final _importedGlobals = <ir.ImportedGlobal>[];
1414
final _globalBuilders = <GlobalBuilder>[];
1515

1616
/// Number of named globals.
@@ -29,9 +29,6 @@ class GlobalsBuilder with Builder<ir.Globals> {
2929
}
3030

3131
/// Imports a global variable into this module.
32-
///
33-
/// All imported globals must be specified before any globals are declared
34-
/// using [Globals.define].
3532
ir.ImportedGlobal import(String module, String name, ir.GlobalType type) {
3633
final global = ir.ImportedGlobal(module, name, ir.FinalizableIndex(), type);
3734
_importedGlobals.add(global);

pkg/wasm_builder/lib/src/builder/memories.dart

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import 'util.dart';
88

99
class MemoriesBuilder with Builder<ir.Memories> {
1010
final _definedMemories = <ir.DefinedMemory>[];
11-
final _importedMemories = <ir.Import>[];
11+
final _importedMemories = <ir.ImportedMemory>[];
1212

1313
/// Add a new memory to the module.
1414
ir.DefinedMemory define(bool shared, int minSize, [int? maxSize]) {
@@ -19,9 +19,6 @@ class MemoriesBuilder with Builder<ir.Memories> {
1919
}
2020

2121
/// Imports a memory into this module.
22-
///
23-
/// All imported memories must be specified before any memories are declared
24-
/// using [defined].
2522
ir.ImportedMemory import(String module, String name, bool shared, int minSize,
2623
[int? maxSize]) {
2724
final memory = ir.ImportedMemory(

pkg/wasm_builder/lib/src/builder/module.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,12 @@ class ModuleBuilder with Builder<ir.Module> {
4141
finalGlobals,
4242
types.build(),
4343
dataSegments.build(),
44-
finalFunctions.imported
45-
.followedBy(finalTables.imported)
46-
.followedBy(finalMemories.imported)
47-
.followedBy(finalGlobals.imported)
48-
.toList(),
44+
<ir.Import>[
45+
...finalFunctions.imported,
46+
...finalTables.imported,
47+
...finalMemories.imported,
48+
...finalGlobals.imported
49+
],
4950
watchPoints);
5051
}
5152
}

pkg/wasm_builder/lib/src/builder/tables.dart

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ part 'table.dart';
1111
/// The interface for the tables in a module.
1212
class TablesBuilder with Builder<ir.Tables> {
1313
final _tableBuilders = <TableBuilder>[];
14-
final _importedTables = <ir.Import>[];
14+
final _importedTables = <ir.ImportedTable>[];
1515

1616
/// Defines a new table in this module.
1717
TableBuilder define(ir.RefType type, int minSize, [int? maxSize]) {
@@ -21,9 +21,6 @@ class TablesBuilder with Builder<ir.Tables> {
2121
}
2222

2323
/// Imports a table into this module.
24-
///
25-
/// All imported tables must be specified before any tables are declared
26-
/// using [Tables.define].
2724
ir.ImportedTable import(
2825
String module, String name, ir.RefType type, int minSize,
2926
[int? maxSize]) {

pkg/wasm_builder/lib/src/ir/functions.dart

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,13 @@ class Functions {
1313
final BaseFunction? start;
1414

1515
/// Imported functions.
16-
final List<Import> imported;
16+
final List<ImportedFunction> imported;
1717

1818
/// Defined functions.
1919
final List<DefinedFunction> defined;
2020

21-
/// All functions, in the order they were emitted.
22-
final List<BaseFunction> all;
23-
2421
/// Named functions.
2522
final int namedCount;
2623

27-
Functions(this.start, this.imported, this.defined, this.all, this.namedCount);
24+
Functions(this.start, this.imported, this.defined, this.namedCount);
2825
}

pkg/wasm_builder/lib/src/ir/globals.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ part 'global.dart';
99

1010
class Globals {
1111
/// Imported globals.
12-
final List<Import> imported;
12+
final List<ImportedGlobal> imported;
1313

1414
/// Defined globals.
1515
final List<DefinedGlobal> defined;

pkg/wasm_builder/lib/src/ir/memories.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ part 'memory.dart';
99

1010
class Memories {
1111
/// Imported memories.
12-
final List<Import> imported;
12+
final List<ImportedMemory> imported;
1313

1414
/// Defined memories.
1515
final List<DefinedMemory> defined;

pkg/wasm_builder/lib/src/ir/module.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,14 @@ class Module implements Serializable {
5151
DataCountSection(dataSegments.defined, watchPoints).serialize(s);
5252
CodeSection(functions.defined, watchPoints).serialize(s);
5353
DataSection(dataSegments.defined, watchPoints).serialize(s);
54-
if (functions.namedCount > 0 || types.namedCount > 0) {
55-
NameSection(functions.all, types.defined, globals.defined, watchPoints,
54+
if (functions.namedCount > 0 ||
55+
types.namedCount > 0 ||
56+
globals.namedCount > 0) {
57+
NameSection(
58+
<BaseFunction>[...functions.imported, ...functions.defined],
59+
types.defined,
60+
<Global>[...globals.imported, ...globals.defined],
61+
watchPoints,
5662
functionNameCount: functions.namedCount,
5763
typeNameCount: types.namedCount,
5864
globalNameCount: globals.namedCount)

pkg/wasm_builder/lib/src/ir/tables.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ part 'table.dart';
99

1010
class Tables {
1111
/// Imported tables.
12-
final List<Import> imported;
12+
final List<ImportedTable> imported;
1313

1414
/// Defined tables.
1515
final List<DefinedTable> defined;

0 commit comments

Comments
 (0)