dart-sdk/tests/ffi/structs_nested_test.dart
Daco Harkes 92e5746494 Reland "[vm/ffi] Add class modifiers"
This is a reland of commit 1755f89092

Can land after (or with) the Flutter PR:
https://github.com/flutter/engine/pull/40434

Original change's description:
> [vm/ffi] Add class modifiers
>
> Adds class modifiers to `dart:ffi`.
>
> Migrates all user-defined subclasses of `Struct`, `Union`, `Opaque`,
> and `AbiSpecificInteger` to be `final class`es.
>
> Does not remove the manual error checking, so some errors will show up
> twice now in language version 3.0. In language version <3.0, only the
> FFI-specific error will show up.
>
> In a follow-up CL, we will try to make the language-errors to show up
> also <3.0 so that we can remove the FFI-specific errors.
>
> Examples of duplicated errors:
> pkg/analyzer/test/src/diagnostics/subtype_of_ffi_class_test.dart
>
> TEST=pkg/analyzer/test/ (for the analyzer)
> TEST=pkg/front_end/testcases/ (for the CFE)
> TEST=test/ffi/ (for the VM)
>
> CoreLibraryReviewExempt: No need for dart2js to review.
> Bug: https://github.com/dart-lang/sdk/issues/51683
> Change-Id: I2964ceccb7db59fbdaf6be5319f5e4ec2dabe0f3
> Cq-Include-Trybots: luci.dart.try:pkg-linux-debug-try,pkg-win-release-try,pkg-mac-release-try,vm-precomp-ffi-qemu-linux-release-riscv64-try,vm-precomp-ffi-qemu-linux-release-arm-try,vm-ffi-android-debug-arm64c-try,vm-ffi-android-debug-arm-try,vm-reload-rollback-linux-debug-x64-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try
> Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/289223
> Reviewed-by: Johnni Winther <johnniwinther@google.com>
> Reviewed-by: Devon Carew <devoncarew@google.com>
> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
> Reviewed-by: Jackson Gardner <jacksongardner@google.com>
> Reviewed-by: Lasse Nielsen <lrn@google.com>
> Commit-Queue: Daco Harkes <dacoharkes@google.com>

TEST=pkg/analyzer/test/ (for the analyzer)
TEST=pkg/front_end/testcases/ (for the CFE)
TEST=test/ffi/ (for the VM)
CoreLibraryReviewExempt: No need for dart2js to review.
Bug: https://github.com/dart-lang/sdk/issues/51683
Change-Id: I2ee3f0ac31d4162068a2346a06320029b2263ee2
Cq-Include-Trybots: luci.dart.try:pkg-linux-debug-try,pkg-win-release-try,pkg-mac-release-try,vm-precomp-ffi-qemu-linux-release-riscv64-try,vm-precomp-ffi-qemu-linux-release-arm-try,vm-ffi-android-debug-arm64c-try,vm-ffi-android-debug-arm-try,vm-reload-rollback-linux-debug-x64-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/289781
Reviewed-by: Devon Carew <devoncarew@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
2023-03-21 15:25:10 +00:00

104 lines
2.3 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.
//
// This tests the non trampoline aspects of nested structs.
//
// SharedObjects=ffi_test_functions
import 'dart:ffi';
import "package:expect/expect.dart";
import "package:ffi/ffi.dart";
import 'dylib_utils.dart';
final ffiTestFunctions = dlopenPlatformSpecific("ffi_test_functions");
void main() {
for (int i = 0; i < 10; ++i) {
testSizeOf();
testAllocate();
testRead();
testWrite();
testCopy();
}
}
final class Struct4BytesHomogeneousInt16 extends Struct {
@Int16()
external int a0;
@Int16()
external int a1;
String toString() => "(${a0}, ${a1})";
}
final class Struct8BytesNestedInt extends Struct {
external Struct4BytesHomogeneousInt16 a0;
external Struct4BytesHomogeneousInt16 a1;
String toString() => "(${a0}, ${a1})";
}
void testSizeOf() {
print(sizeOf<Struct8BytesNestedInt>());
Expect.equals(8, sizeOf<Struct8BytesNestedInt>());
}
void testAllocate() {
final p = calloc<Struct8BytesNestedInt>();
Expect.type<Pointer<Struct8BytesNestedInt>>(p);
print(p);
calloc.free(p);
}
/// Test that reading does not segfault, even uninitialized.
void testRead() {
print("read");
final p = calloc<Struct8BytesNestedInt>();
print(p);
print(p.ref.runtimeType);
print(p.address);
print(p.ref.a0.runtimeType);
print(p.ref.a0.a0);
calloc.free(p);
print("read");
}
void testWrite() {
print("write");
final p = calloc<Struct8BytesNestedInt>(2);
p[0].a0.a0 = 12;
p[0].a0.a1 = 13;
p[0].a1.a0 = 14;
p[0].a1.a1 = 15;
p[1].a0.a0 = 16;
p[1].a0.a1 = 17;
p[1].a1.a0 = 18;
p[1].a1.a1 = 19;
Expect.equals(12, p[0].a0.a0);
Expect.equals(13, p[0].a0.a1);
Expect.equals(14, p[0].a1.a0);
Expect.equals(15, p[0].a1.a1);
Expect.equals(16, p[1].a0.a0);
Expect.equals(17, p[1].a0.a1);
Expect.equals(18, p[1].a1.a0);
Expect.equals(19, p[1].a1.a1);
calloc.free(p);
print("written");
}
void testCopy() {
print("copy");
final p = calloc<Struct8BytesNestedInt>();
p.ref.a0.a0 = 12;
p.ref.a0.a1 = 13;
p.ref.a1 = p.ref.a0;
Expect.equals(12, p.ref.a1.a0);
Expect.equals(13, p.ref.a1.a1);
calloc.free(p);
print("copied");
}