Files
sdk/tests/ffi/function_callbacks_test.dart
T
Samir Jindel ed169c4bb0 Revert "[vm/ffi] FFI callbacks on X64."
This reverts commit be209f7846.

Reason for revert: failures on dartkb and windows bots

Original change's description:
> [vm/ffi] FFI callbacks on X64.
> 
> For context on the design, see go/dart-ffi-callbacks
> 
> Change-Id: I2482e3c932e73f9a4c00fa7e218ff85f9328fc51
> Cq-Include-Trybots: luci.dart.try:vm-kernel-linux-debug-simdbc64-try, vm-kernel-linux-release-simdbc64-try, vm-kernel-mac-debug-simdbc64-try, vm-kernel-mac-release-simdbc64-try, vm-kernel-reload-mac-debug-simdbc64-try, vm-kernel-reload-mac-release-simdbc64-try, vm-kernel-linux-debug-ia32-try, vm-dartkb-linux-debug-simarm64-try
> Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/100240
> Commit-Queue: Samir Jindel <sjindel@google.com>
> Reviewed-by: Daco Harkes <dacoharkes@google.com>

TBR=sjindel@google.com,ajcbik@google.com,dacoharkes@google.com

Change-Id: I4cb3d93675d68a51ac9e125ad1d572c47e8b5903
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Cq-Include-Trybots: luci.dart.try:vm-kernel-linux-debug-simdbc64-try, vm-kernel-linux-release-simdbc64-try, vm-kernel-mac-debug-simdbc64-try, vm-kernel-mac-release-simdbc64-try, vm-kernel-reload-mac-debug-simdbc64-try, vm-kernel-reload-mac-release-simdbc64-try, vm-kernel-linux-debug-ia32-try, vm-dartkb-linux-debug-simarm64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/102983
Reviewed-by: Samir Jindel <sjindel@google.com>
Commit-Queue: Samir Jindel <sjindel@google.com>
2019-05-18 15:29:19 +00:00

79 lines
2.4 KiB
Dart

// Copyright (c) 2019, 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 test program for testing dart:ffi function pointers with callbacks.
//
// SharedObjects=ffi_test_functions
library FfiTest;
import 'dart:ffi' as ffi;
import 'dylib_utils.dart';
import "package:expect/expect.dart";
import 'coordinate.dart';
typedef NativeCoordinateOp = Coordinate Function(Coordinate);
typedef CoordinateTrice = Coordinate Function(
ffi.Pointer<ffi.NativeFunction<NativeCoordinateOp>>, Coordinate);
void main() {
testFunctionWithFunctionPointer();
testNativeFunctionWithFunctionPointer();
}
ffi.DynamicLibrary ffiTestFunctions =
dlopenPlatformSpecific("ffi_test_functions");
/// pass a pointer to a c function as an argument to a c function
void testFunctionWithFunctionPointer() {
ffi.Pointer<ffi.NativeFunction<NativeCoordinateOp>>
transposeCoordinatePointer =
ffiTestFunctions.lookup("TransposeCoordinate");
ffi.Pointer<ffi.NativeFunction<CoordinateTrice>> p2 =
ffiTestFunctions.lookup("CoordinateUnOpTrice");
CoordinateTrice coordinateUnOpTrice = p2.asFunction();
Coordinate c1 = Coordinate(10.0, 20.0, null);
c1.next = c1;
Coordinate result = coordinateUnOpTrice(transposeCoordinatePointer, c1);
print(result.runtimeType);
print(result.x);
print(result.y);
c1.free();
}
typedef BinaryOp = int Function(int, int);
typedef NativeIntptrBinOp = ffi.IntPtr Function(ffi.IntPtr, ffi.IntPtr);
typedef NativeIntptrBinOpLookup
= ffi.Pointer<ffi.NativeFunction<NativeIntptrBinOp>> Function();
void testNativeFunctionWithFunctionPointer() {
ffi.Pointer<ffi.NativeFunction<NativeIntptrBinOpLookup>> p1 =
ffiTestFunctions.lookup("IntptrAdditionClosure");
NativeIntptrBinOpLookup intptrAdditionClosure = p1.asFunction();
ffi.Pointer<ffi.NativeFunction<NativeIntptrBinOp>> intptrAdditionPointer =
intptrAdditionClosure();
BinaryOp intptrAddition = intptrAdditionPointer.asFunction();
Expect.equals(37, intptrAddition(10, 27));
}
int myPlus(int a, int b) => a + b;
typedef NativeApplyTo42And74Type = ffi.IntPtr Function(
ffi.Pointer<ffi.NativeFunction<NativeIntptrBinOp>>);
typedef ApplyTo42And74Type = int Function(
ffi.Pointer<ffi.NativeFunction<NativeIntptrBinOp>>);