blob: 5aa0ee36924a952e094a1e7527586019c6b5530d [file] [log] [blame]
// Copyright (c) 2021, 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.
// OtherResources=print_object_layout_script.dart
// Test for --print-object-layout-to option of gen_snapshot.
import 'dart:convert' show jsonDecode;
import 'dart:math' show max;
import 'dart:io' show File, Platform;
import 'package:expect/expect.dart';
import 'package:path/path.dart' as path;
import 'snapshot_test_helper.dart';
verifyObjectLayout(String path) {
final classes = jsonDecode(File(path).readAsStringSync());
var sizeA, fieldsA;
var sizeB, fieldsB;
for (var cls in classes) {
if (cls['class'] == 'ClassA') {
sizeA = cls['size'].toInt();
fieldsA = cls['fields'];
print(cls);
} else if (cls['class'] == 'ClassB') {
sizeB = cls['size'].toInt();
fieldsB = cls['fields'];
print(cls);
}
}
Expect.isNotNull(sizeA);
Expect.isTrue(sizeA > 0);
Expect.isTrue(fieldsA.length == 2);
int maxOffsetA = 0;
for (var field in fieldsA) {
String fieldName = field['field'];
Expect.isTrue(fieldName == 'fieldA1' || fieldName == 'fieldA2');
int offset = field['offset'].toInt();
Expect.isTrue((offset > 0) && (offset < sizeA));
maxOffsetA = max(offset, maxOffsetA);
}
Expect.isNotNull(sizeB);
Expect.isTrue(sizeB > 0);
Expect.isTrue(sizeA < sizeB);
Expect.isTrue(fieldsB.length == 3);
for (var field in fieldsB) {
String fieldName = field['field'];
if (fieldName == 'staticB4') {
Expect.isTrue(field['static']);
} else {
Expect.isTrue(fieldName == 'fieldB1' || fieldName == 'fieldB2');
int offset = field['offset'].toInt();
Expect.isTrue((offset > 0) && (offset < sizeB));
Expect.isTrue(offset > maxOffsetA);
}
}
}
main() async {
// We don't have access to the SDK on Android.
if (Platform.isAndroid) {
print('Skipping test on Android');
return;
}
final testScriptUri =
Platform.script.resolve('print_object_layout_script.dart');
await withTempDir((String temp) async {
final appDillPath = path.join(temp, 'app.dill');
final snapshotPath = path.join(temp, 'aot.snapshot');
final objectLayoutPath = path.join(temp, 'layout.json');
await runGenKernel('BUILD DILL FILE', [
'--aot',
'--link-platform',
'--output=$appDillPath',
testScriptUri.toFilePath(),
]);
await runGenSnapshot('GENERATE SNAPSHOT', [
'--snapshot-kind=app-aot-elf',
'--elf=$snapshotPath',
'--print-object-layout-to=$objectLayoutPath',
appDillPath,
]);
verifyObjectLayout(objectLayoutPath);
});
}