blob: c9950d51f69689974af7dd4a98086f4af5e48954 [file] [log] [blame]
// Copyright (c) 2022, 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 'package:kernel/binary/ast_to_binary.dart';
import 'binary/utils.dart';
void main() {
final Uri libUri = Uri.parse('org-dartlang:///lib.dart');
List<int> writtenBytesField;
Library lib = new Library(libUri, fileUri: libUri);
final Field field =
new Field.immutable(new Name("myGetter"), fileUri: libUri);
writtenBytesField = serialize(lib);
List<int> writtenBytesProcedure;
Library lib = new Library(libUri, fileUri: libUri);
final Block libProcedureBody = new Block([]);
final Procedure procedure = new Procedure(
new Name("myGetter"),
new FunctionNode(libProcedureBody, returnType: new DynamicType()),
fileUri: libUri);
writtenBytesProcedure = serialize(lib);
CanonicalName nameRoot = new CanonicalName.root();
for (int i = 0; i < 4; i++) {
// Load field version "on top of" (meant to replace old one if any).
Component componentWithField = new Component(nameRoot: nameRoot);
new BinaryBuilder(writtenBytesField,
disableLazyReading: false, alwaysCreateNewNamedNodes: true)
expect(componentWithField.libraries.single.members.single is Field, true);
// Load procedure version "on top of" (meant to replace old one if any).
Component componentWithProcedure = new Component(nameRoot: nameRoot);
new BinaryBuilder(writtenBytesProcedure,
disableLazyReading: false, alwaysCreateNewNamedNodes: true)
expect(componentWithProcedure.libraries.single.members.single is Procedure,
void expect(dynamic actual, dynamic expected) {
if (actual != expected) {
throw "Expected '$expected' but got '$actual'";
List<int> serialize(Library lib1) {
Component component = new Component(libraries: [lib1])
..setMainMethodAndMode(null, false, NonNullableByDefaultCompiledMode.Weak);
ByteSink sink = new ByteSink();
new BinaryPrinter(sink).writeComponentFile(component);
return sink.builder.takeBytes();