Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 42dc617

Browse files
Anna Gringauzecommit-bot@chromium.org
authored andcommitted
Emit constants during expression compilation to js
Closes: dart-lang/sdk#43963 Change-Id: Ic8af046b2cf1506984b84ea66b2676b3cc0f1771 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/169580 Commit-Queue: Anna Gringauze <annagrin@google.com> Reviewed-by: Nicholas Shahan <nshahan@google.com>
1 parent 3eb34ef commit 42dc617

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed

pkg/dev_compiler/lib/src/kernel/compiler.dart

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3002,12 +3002,49 @@ class ProgramCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
30023002
// size on expression evaluation is better.
30033003
// Issue: https://github.com/dart-lang/sdk/issues/43288
30043004
var fun = _emitFunction(functionNode, name);
3005-
var items = _typeTable?.discharge();
3006-
var body = js_ast.Block([...?items, ...fun.body.statements]);
30073005

3006+
var types = _typeTable.discharge();
3007+
var constants = _dischargeConstTable();
3008+
3009+
var body = js_ast.Block([...?types, ...?constants, ...fun.body.statements]);
30083010
return js_ast.Fun(fun.params, body);
30093011
}
30103012

3013+
/// Emit all collected const symbols
3014+
///
3015+
/// This is similar to how constants are emitted during
3016+
/// initial compilation in emitModule
3017+
///
3018+
/// TODO: unify the code with emitModule.
3019+
List<js_ast.Statement> _dischargeConstTable() {
3020+
var items = <js_ast.Statement>[];
3021+
3022+
if (_constLazyAccessors.isNotEmpty) {
3023+
var constTableBody = runtimeStatement(
3024+
'defineLazy(#, { # }, false)', [_constTable, _constLazyAccessors]);
3025+
items.add(constTableBody);
3026+
_constLazyAccessors.clear();
3027+
}
3028+
3029+
_copyAndFlattenBlocks(items, moduleItems);
3030+
moduleItems.clear();
3031+
return items;
3032+
}
3033+
3034+
/// Flattens blocks in [items] to a single list.
3035+
///
3036+
/// This will not flatten blocks that are marked as being scopes.
3037+
void _copyAndFlattenBlocks(
3038+
List<js_ast.Statement> result, Iterable<js_ast.ModuleItem> items) {
3039+
for (var item in items) {
3040+
if (item is js_ast.Block && !item.isScope) {
3041+
_copyAndFlattenBlocks(result, item.statements);
3042+
} else {
3043+
result.add(item as js_ast.Statement);
3044+
}
3045+
}
3046+
}
3047+
30113048
js_ast.Fun _emitFunction(FunctionNode f, String name) {
30123049
// normal function (sync), vs (sync*, async, async*)
30133050
var isSync = f.asyncMarker == AsyncMarker.Sync;

pkg/dev_compiler/test/expression_compiler/expression_compiler_test.dart

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,28 @@ void main() {
409409
))
410410
''');
411411
});
412+
413+
test('function', () async {
414+
await driver.check(
415+
scope: <String, String>{'x': '1', 'y': '2', 'z': '3'},
416+
expression: 'main',
417+
expectedResult: '''
418+
(function(x, y, z) {
419+
var VoidTodynamic = () => (VoidTodynamic = dart.constFn(dart.fnType(dart.dynamic, [])))();
420+
dart.defineLazy(CT, {
421+
get C0() {
422+
return C0 = dart.fn(foo.main, VoidTodynamic());
423+
}
424+
}, false);
425+
var C0;
426+
return C0 || CT.C0;
427+
}(
428+
1,
429+
2,
430+
3
431+
))
432+
''');
433+
});
412434
});
413435

414436
group('Expression compiler tests in method:', () {
@@ -1313,7 +1335,16 @@ void main() {
13131335
expression: 'const MyClass(1)',
13141336
expectedResult: '''
13151337
(function(p) {
1316-
return C0 || CT.C0;
1338+
dart.defineLazy(CT, {
1339+
get C0() {
1340+
return C0 = dart.const({
1341+
__proto__: foo.MyClass.prototype,
1342+
[_t]: 1
1343+
});
1344+
}
1345+
}, false);
1346+
var C0;
1347+
return C0 || CT.C0;
13171348
}(
13181349
1
13191350
))
@@ -1353,6 +1384,15 @@ void main() {
13531384
expression: "const Key('t')",
13541385
expectedResult: '''
13551386
(function(p) {
1387+
dart.defineLazy(CT, {
1388+
get C0() {
1389+
return C0 = dart.const({
1390+
__proto__: foo.ValueKey.prototype,
1391+
[value]: "t"
1392+
});
1393+
}
1394+
}, false);
1395+
var C0;
13561396
return C0 || CT.C0;
13571397
}(
13581398
1

0 commit comments

Comments
 (0)