dart-sdk/tests/ffi_2/abi_specific_int_test.dart
Daco Harkes 045b9c1715 Reland "[vm/ffi] Add common C types"
We're adding these types to `dart:ffi` rather than `package:ffi` so that
they can be used with `FfiNative`s.

Adds `NativeType`s for the following C types:

* unsigned char
* signed char
* short
* unsigned short
* int
* unsigned int
* long
* unsigned long
* long long
* unsigned long long
* uintptr_t
* size_t
* wchar_t

Because the C standard only defines minimum sizes for many of these
types, future platforms might diverge from the typical size even if all
platforms currently agree on a size. To avoid having to reification
later, we define all types as AbiSpecificIntegers rather than typedefs,
even if all current target platforms agree on the size.

Closes: https://github.com/dart-lang/sdk/issues/36140

TEST=tests/ffi/c_types_test.dart

Original patch in patchset 1.

* Removes `Char` for now until package:win32 has rolled to 2.3.8 in
  Flutter. https://pub.dev/packages/win32/versions/2.3.8/changelog
  https://logs.chromium.org/logs/flutter/buildbucket/cr-buildbucket/8824468064587445729/+/u/Android_Views_Integration_Tests/stdout
* Adds `c_type.dart` in `ffi_source.gni` which should fix `IntPtr`
  missing when analyzing `path_provider_linux`. (However, I was unable
  to reproduce the issue locally.)
  https://logs.chromium.org/logs/flutter/buildbucket/cr-buildbucket/8824468064571399025/+/u/run_test.dart_for_flutter_plugins_shard_and_subshard_analyze/test_stdout
  `/tmp/flutter_plugins.KZMNMC/packages/path_provider/path_provider_linux$ ~/flt/engine/src/out/host_debug/dart-sdk/bin/dart  analyze --fatal-infos`

Change-Id: I89130cccba285fc9c483bb53f5710a302f2b104f
Cq-Include-Trybots: luci.dart.try:dart-sdk-linux-try,dart-sdk-mac-try,dart-sdk-win-try,vm-ffi-android-debug-arm64c-try,vm-ffi-android-debug-arm-try,vm-canary-linux-debug-try,vm-fuchsia-release-x64-try,vm-kernel-gcc-linux-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-kernel-mac-debug-x64-try,vm-kernel-mac-release-arm64-try,vm-kernel-nnbd-win-release-ia32-try,vm-kernel-nnbd-win-release-x64-try,vm-precomp-ffi-qemu-linux-release-arm-try,vm-kernel-win-release-x64-try,flutter-analyze-try,flutter-engine-linux-try,flutter-frontend-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/229156
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Daco Harkes <dacoharkes@google.com>
2022-01-27 13:09:01 +00:00

113 lines
2.3 KiB
Dart

// 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.
// @dart = 2.9
import 'dart:ffi';
import 'dart:io';
import 'package:expect/expect.dart';
import 'package:ffi/ffi.dart';
void main() {
testSizeOf();
testStoreLoad();
testStoreLoadIndexed();
testStruct();
testInlineArray();
testInlineArray2();
}
void testSizeOf() {
final size = sizeOf<WChar>();
if (Platform.isWindows) {
Expect.equals(2, size);
} else {
Expect.equals(4, size);
}
}
void testStoreLoad() {
final p = calloc<WChar>();
p.value = 10;
Expect.equals(10, p.value);
calloc.free(p);
}
void testStoreLoadIndexed() {
final p = calloc<WChar>(2);
p[0] = 10;
p[1] = 3;
Expect.equals(10, p[0]);
Expect.equals(3, p[1]);
calloc.free(p);
}
class WCharStruct extends Struct {
@WChar()
int a0;
@WChar()
int a1;
}
void testStruct() {
final p = calloc<WCharStruct>();
p.ref.a0 = 1;
Expect.equals(1, p.ref.a0);
p.ref.a0 = 2;
Expect.equals(2, p.ref.a0);
calloc.free(p);
}
class WCharArrayStruct extends Struct {
@Array(100)
Array<WChar> a0;
}
void testInlineArray() {
final p = calloc<WCharArrayStruct>();
final array = p.ref.a0;
for (int i = 0; i < 100; i++) {
array[i] = i;
}
for (int i = 0; i < 100; i++) {
Expect.equals(i, array[i]);
}
calloc.free(p);
}
const _dim0 = 3;
const _dim1 = 8;
const _dim2 = 4;
class WCharArrayArrayStruct extends Struct {
@Array(_dim1, _dim2)
Array<Array<WChar>> a0;
}
void testInlineArray2() {
int someValue(int a, int b, int c) => a * 1337 + b * 42 + c;
final p = calloc<WCharArrayArrayStruct>(_dim0);
for (int i0 = 0; i0 < _dim0; i0++) {
final array = p.elementAt(i0).ref.a0;
for (int i1 = 0; i1 < _dim1; i1++) {
final array2 = array[i1];
for (int i2 = 0; i2 < _dim2; i2++) {
array2[i2] = someValue(i0, i1, i2);
}
}
}
for (int i0 = 0; i0 < _dim0; i0++) {
final array = p.elementAt(i0).ref.a0;
for (int i1 = 0; i1 < _dim1; i1++) {
final array2 = array[i1];
for (int i2 = 0; i2 < _dim2; i2++) {
Expect.equals(someValue(i0, i1, i2), array2[i2]);
}
}
}
calloc.free(p);
}