// 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>, 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> transposeCoordinatePointer = ffiTestFunctions.lookup("TransposeCoordinate"); ffi.Pointer> 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> Function(); void testNativeFunctionWithFunctionPointer() { ffi.Pointer> p1 = ffiTestFunctions.lookup("IntptrAdditionClosure"); NativeIntptrBinOpLookup intptrAdditionClosure = p1.asFunction(); ffi.Pointer> 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>); typedef ApplyTo42And74Type = int Function( ffi.Pointer>);