Files
sdk/pkg/kernel/test/binary/component_mode_test.dart
T
Johnni Winther c5cd8ff349 [cfe] Remove agnostic mode
The agnostic mode was added to allow the platform dill embedded in
the VM to support both weak and strong mode. Since weak mode is no
longer supported in the VM, the agnostic mode can new be deleted.

All uses of the agnostic in Dart and Flutter have been removed prior
to this change.

Change-Id: Iff0f69d9cd64e887e01cd7e7d336a97761bd6d4b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/366801
Reviewed-by: Alexander Thomas <athom@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Jens Johansen <jensj@google.com>
2024-06-06 11:02:37 +00:00

148 lines
5.1 KiB
Dart

// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:kernel/binary/ast_from_binary.dart';
import 'utils.dart';
void main() {
void setCompileMode(Component c, NonNullableByDefaultCompiledMode mode) {
c.setMainMethodAndMode(null, true, mode);
}
void verifyMode(Component c, NonNullableByDefaultCompiledMode mode) {
if (c.mode != mode) {
throw "Serialized and re-read component had change in mode: "
"Expected $mode got ${c.mode}.";
}
}
const List<NonNullableByDefaultCompiledMode> modes = const [
NonNullableByDefaultCompiledMode.Weak,
NonNullableByDefaultCompiledMode.Strong,
];
int combination = 0;
for (NonNullableByDefaultCompiledMode c1Mode in modes) {
for (NonNullableByDefaultCompiledMode c2Mode in modes) {
combination++;
print("Checking combination #$combination ("
"c1Mode: $c1Mode; "
"c2Mode: $c2Mode; "
")");
// Try individually.
List<int> c1Serialized;
{
Uri uri = Uri.parse("foo://bar.dart");
Library lib1 = new Library(uri, fileUri: uri)
..nonNullableByDefaultCompiledMode = c1Mode;
Component c1 = new Component(libraries: [lib1]);
setCompileMode(c1, c1Mode);
c1Serialized = serializeComponent(c1);
Component c1RoundTrip = loadComponentFromBytes(c1Serialized);
verifyMode(c1RoundTrip, c1Mode);
}
List<int> c2Serialized;
{
Uri uri = Uri.parse("foo://baz.dart");
Library lib2 = new Library(uri, fileUri: uri)
..nonNullableByDefaultCompiledMode = c2Mode;
Component c2 = new Component(libraries: [lib2]);
setCompileMode(c2, c2Mode);
c2Serialized = serializeComponent(c2);
Component c2RoundTrip = loadComponentFromBytes(c2Serialized);
verifyMode(c2RoundTrip, c2Mode);
}
// Try with combined binary.
try {
List<int> combined = [];
combined.addAll(c1Serialized);
combined.addAll(c2Serialized);
Component combinedRoundTrip = loadComponentFromBytes(combined);
verifyMode(combinedRoundTrip, verifyOK(c1Mode, c2Mode));
print(" -> OK with $c1Mode and $c2Mode");
} on CompilationModeError catch (e) {
print(" -> Got $e with $c1Mode and $c2Mode");
verifyError(c1Mode, c2Mode);
}
// Try other order.
try {
List<int> combined = [];
combined.addAll(c2Serialized);
combined.addAll(c1Serialized);
Component combinedRoundTrip = loadComponentFromBytes(combined);
verifyMode(combinedRoundTrip, verifyOK(c1Mode, c2Mode));
print(" -> OK with $c1Mode and $c2Mode");
} on CompilationModeError catch (e) {
print(" -> Got $e with $c1Mode and $c2Mode");
verifyError(c1Mode, c2Mode);
}
// Try with individual binary, but loaded into same component.
try {
Component combinedRoundTrip = loadComponentFromBytes(c1Serialized);
combinedRoundTrip =
loadComponentFromBytes(c2Serialized, combinedRoundTrip);
verifyMode(combinedRoundTrip, verifyOK(c1Mode, c2Mode));
print(" -> OK with $c1Mode and $c2Mode");
} on CompilationModeError catch (e) {
print(" -> Got $e with $c1Mode and $c2Mode");
verifyError(c1Mode, c2Mode);
}
// Try other order.
try {
Component combinedRoundTrip = loadComponentFromBytes(c2Serialized);
combinedRoundTrip =
loadComponentFromBytes(c1Serialized, combinedRoundTrip);
verifyMode(combinedRoundTrip, verifyOK(c1Mode, c2Mode));
print(" -> OK with $c1Mode and $c2Mode");
} on CompilationModeError catch (e) {
print(" -> Got $e with $c1Mode and $c2Mode");
verifyError(c1Mode, c2Mode);
}
// Try with individual binary, but loaded into same component where
// component initially does not have a mode.
try {
Component combinedRoundTrip = new Component();
combinedRoundTrip =
loadComponentFromBytes(c1Serialized, combinedRoundTrip);
combinedRoundTrip =
loadComponentFromBytes(c2Serialized, combinedRoundTrip);
verifyMode(combinedRoundTrip, verifyOK(c1Mode, c2Mode));
print(" -> OK with $c1Mode and $c2Mode");
} on CompilationModeError catch (e) {
print(" -> Got $e with $c1Mode and $c2Mode");
verifyError(c1Mode, c2Mode);
}
}
}
print("Done: Everything looks good.");
}
bool isOK(NonNullableByDefaultCompiledMode c1Mode,
NonNullableByDefaultCompiledMode c2Mode) {
return c1Mode == c2Mode;
}
NonNullableByDefaultCompiledMode verifyOK(
NonNullableByDefaultCompiledMode c1Mode,
NonNullableByDefaultCompiledMode c2Mode) {
if (isOK(c1Mode, c2Mode)) {
return c1Mode;
}
throw "Not OK combination: $c1Mode and $c2Mode";
}
void verifyError(NonNullableByDefaultCompiledMode c1Mode,
NonNullableByDefaultCompiledMode c2Mode) {
if (isOK(c1Mode, c2Mode)) {
throw "Unexpected error for $c1Mode and $c2Mode";
}
}