Skip to content

Commit 3140b09

Browse files
osa1Commit Queue
authored andcommitted
[dart2wasm] Name globals and functions for static fields
Adds new names: - Globals for static field initialization flags are named as "$memberName initialized". - Globals for static field values are named as "$memberName". - Static field initialization functions are named as "$memberName initializer". Also documents fields of `Globals` and removes unused field `_globalInitializers`. Change-Id: Id54c6e679b2d028917e86184e2913bb51e6b5a97 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/396240 Reviewed-by: Martin Kustermann <[email protected]> Commit-Queue: Ömer Ağacan <[email protected]>
1 parent e2c17b9 commit 3140b09

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

pkg/dart2wasm/lib/functions.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ class FunctionCollector {
173173
if (memberName.endsWith('.')) {
174174
memberName = memberName.substring(0, memberName.length - 1);
175175
}
176+
176177
if (target.isTypeCheckerReference) {
177178
if (member is Field || (member is Procedure && member.isSetter)) {
178179
return '$memberName setter type checker';
@@ -181,6 +182,10 @@ class FunctionCollector {
181182
}
182183
}
183184

185+
if (member is Field) {
186+
return '$memberName initializer';
187+
}
188+
184189
if (target.isInitializerReference) {
185190
return 'new $memberName (initializer)';
186191
} else if (target.isConstructorBodyReference) {

pkg/dart2wasm/lib/globals.dart

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,16 @@ import 'translator.dart';
1212
class Globals {
1313
final Translator translator;
1414

15+
/// Maps a static field to its global holding the field value.
1516
final Map<Field, w.GlobalBuilder> _globals = {};
17+
18+
/// When a global is read from a module other than the module defining it,
19+
/// this maps the global to the getter function defined and exported in
20+
/// the defining module.
1621
final Map<w.Global, w.BaseFunction> _globalGetters = {};
17-
final Map<Field, w.BaseFunction> _globalInitializers = {};
22+
1823
final Map<Field, w.Global> _globalInitializedFlag = {};
24+
1925
late final WasmGlobalImporter _globalsModuleMap =
2026
WasmGlobalImporter(translator, 'global');
2127

@@ -68,37 +74,42 @@ class Globals {
6874
assert(!field.isLate);
6975
return _globals.putIfAbsent(field, () {
7076
final Constant? init = _getConstantInitializer(field);
71-
w.ValueType type = translator.translateTypeOfField(field);
77+
w.ValueType fieldType = translator.translateTypeOfField(field);
7278
final module = translator.moduleForReference(field.fieldReference);
79+
final memberName = field.toString();
7380
if (init != null &&
7481
!(translator.constants.ensureConstant(init)?.isLazy ?? false)) {
7582
// Initialized to a constant
76-
final global =
77-
module.globals.define(w.GlobalType(type, mutable: !field.isFinal));
83+
final global = module.globals.define(
84+
w.GlobalType(fieldType, mutable: !field.isFinal), memberName);
7885
translator.constants
79-
.instantiateConstant(global.initializer, init, type);
86+
.instantiateConstant(global.initializer, init, fieldType);
8087
global.initializer.end();
8188
return global;
8289
} else {
83-
if (type is w.RefType && !type.nullable) {
90+
final w.ValueType globalType;
91+
if (fieldType is w.RefType && !fieldType.nullable) {
8492
// Null signals uninitialized
85-
type = type.withNullability(true);
93+
globalType = fieldType.withNullability(true);
8694
} else {
8795
// Explicit initialization flag
88-
final flag = module.globals.define(w.GlobalType(w.NumType.i32));
96+
globalType = fieldType;
97+
final flag = module.globals
98+
.define(w.GlobalType(w.NumType.i32), "$memberName initialized");
8999
flag.initializer.i32_const(0);
90100
flag.initializer.end();
91101
_globalInitializedFlag[field] = flag;
92102
}
93103

94-
final global = module.globals.define(w.GlobalType(type));
104+
final global =
105+
module.globals.define(w.GlobalType(globalType), memberName);
95106
translator
96107
.getDummyValuesCollectorForModule(module)
97-
.instantiateDummyValue(global.initializer, type);
108+
.instantiateDummyValue(global.initializer, globalType);
98109
global.initializer.end();
99110

100-
_globalInitializers[field] =
101-
translator.functions.getFunction(field.fieldReference);
111+
// Add initializer function to the compilation queue.
112+
translator.functions.getFunction(field.fieldReference);
102113
return global;
103114
}
104115
});
@@ -108,7 +119,6 @@ class Globals {
108119
/// field has been initialized, if such a flag global is needed.
109120
///
110121
/// Note that [getGlobalForStaticField] must have been called for the field beforehand.
111-
w.Global? getGlobalInitializedFlag(Field variable) {
112-
return _globalInitializedFlag[variable];
113-
}
122+
w.Global? getGlobalInitializedFlag(Field variable) =>
123+
_globalInitializedFlag[variable];
114124
}

0 commit comments

Comments
 (0)