Skip to content

Commit 6ebf3f0

Browse files
jensjohacommit-bot@chromium.org
authored andcommitted
[kernel] Clear a few lists when loading dill
Prior to this change, loading the sdk dill twice for instance would result in additional exports as well as parameter counts doubling, essentially destroying the result. Change-Id: I4d694bec1926d6df6572e9bb36e8a88ec0822e27 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/120785 Commit-Queue: Jens Johansen <jensj@google.com> Reviewed-by: Johnni Winther <johnniwinther@google.com>
1 parent d1124c3 commit 6ebf3f0

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
export 'load_dill_twice_lib_2.dart';
6+
7+
// Use annotation to force proper printing.
8+
9+
typedef myTypedefWithNamed(@Foo int a, {@Foo int b});
10+
typedef myTypedefWithOptionalPositional(@Foo int a, [@Foo int b]);
11+
12+
const Foo = 42;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
int foo() => 42;
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:io' show Platform, exit;
6+
7+
import 'package:kernel/binary/ast_from_binary.dart' show BinaryBuilder;
8+
9+
import 'package:kernel/text/ast_to_text.dart' show componentToString;
10+
11+
import 'package:kernel/kernel.dart' show Component;
12+
13+
import 'incremental_load_from_dill_test.dart' show normalCompileToBytes;
14+
15+
main() async {
16+
List<int> bytes = await normalCompileToBytes(
17+
Platform.script.resolve("load_dill_twice_lib_1.dart"));
18+
19+
Component c = new Component();
20+
new BinaryBuilder(bytes).readComponent(c);
21+
String loadedOnceString = componentToString(c);
22+
new BinaryBuilder(bytes).readComponent(c);
23+
String loadedTwiceString = componentToString(c);
24+
25+
if (loadedOnceString != loadedTwiceString) {
26+
print("Loading the dill twice produces a different textual representation");
27+
List<String> linesOnce = loadedOnceString.split("\n");
28+
List<String> linesTwice = loadedTwiceString.split("\n");
29+
30+
if (linesOnce.length != linesTwice.length) {
31+
print("Number of lines differ! "
32+
"(${linesOnce.length} vs ${linesTwice.length})");
33+
}
34+
35+
// Do some simple and stupid diff.
36+
int i = 0;
37+
int j = 0;
38+
while (i < linesOnce.length || j < linesTwice.length) {
39+
if (i < linesOnce.length && j < linesTwice.length) {
40+
if (linesOnce[i] == linesTwice[j]) {
41+
i++;
42+
j++;
43+
} else {
44+
// Search for line from linesOnce in linesTwice
45+
bool good = false;
46+
for (int k = j + 1; k < linesTwice.length && k < j + 100; k++) {
47+
if (linesOnce[i] == linesTwice[k]) {
48+
// Inserted lines between j and k.
49+
for (int k2 = j; k2 < k; k2++) {
50+
print("+ ${linesTwice[k2]}");
51+
}
52+
i++;
53+
j = k + 1;
54+
good = true;
55+
break;
56+
}
57+
}
58+
if (!good) {
59+
// Assume lines deleted.
60+
print("- ${linesOnce[i]}");
61+
i++;
62+
}
63+
}
64+
} else if (i < linesOnce.length) {
65+
// Rest from linesOnce was deleted.
66+
for (; i < linesOnce.length; i++) {
67+
print("- ${linesOnce[i]}");
68+
}
69+
} else if (j < linesTwice.length) {
70+
// Rest from linesTwice was added.
71+
for (; j < linesTwice.length; j++) {
72+
print("+ ${linesTwice[j]}");
73+
}
74+
}
75+
}
76+
77+
exit(1);
78+
}
79+
}

pkg/kernel/lib/binary/ast_from_binary.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,7 @@ class BinaryBuilder {
956956
void _readAdditionalExports(Library library) {
957957
int numExportedReference = readUInt();
958958
if (numExportedReference != 0) {
959+
library.additionalExports.clear();
959960
for (int i = 0; i < numExportedReference; i++) {
960961
CanonicalName exportedName = readCanonicalNameReference();
961962
Reference reference = exportedName.getReference();
@@ -1012,7 +1013,9 @@ class BinaryBuilder {
10121013
readAndPushTypeParameterList(node.typeParameters, node);
10131014
var type = readDartType();
10141015
readAndPushTypeParameterList(node.typeParametersOfFunctionType, node);
1016+
node.positionalParameters.clear();
10151017
node.positionalParameters.addAll(readAndPushVariableDeclarationList());
1018+
node.namedParameters.clear();
10161019
node.namedParameters.addAll(readAndPushVariableDeclarationList());
10171020
typeParameterStack.length = 0;
10181021
variableStack.length = 0;

0 commit comments

Comments
 (0)