|
| 1 | +// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | +// |
| 5 | +// SharedObjects=ffi_test_functions |
| 6 | + |
| 7 | +import 'dart:ffi'; |
| 8 | + |
| 9 | +import 'dylib_utils.dart'; |
| 10 | + |
| 11 | +// ============================================ |
| 12 | +// Tests checks on Dart to native (asFunction). |
| 13 | +// ============================================ |
| 14 | + |
| 15 | +typedef Int64PointerParamOpDart = void Function(Pointer<Int64>); |
| 16 | +typedef Int64PointerParamOp = Void Function(Pointer<Int64>); |
| 17 | +typedef Int64PointerReturnOp = Pointer<Int64> Function(); |
| 18 | +typedef NaTyPointerReturnOp = Pointer<NativeType> Function(); |
| 19 | + |
| 20 | +final paramOpName = "NativeTypePointerParam"; |
| 21 | +final returnOpName = "NativeTypePointerReturn"; |
| 22 | + |
| 23 | +final DynamicLibrary ffiTestFunctions = |
| 24 | + dlopenPlatformSpecific("ffi_test_functions"); |
| 25 | + |
| 26 | +final p1 = |
| 27 | + ffiTestFunctions.lookup<NativeFunction<Int64PointerParamOp>>(paramOpName); |
| 28 | +final nonInvariantBinding1 = //# 1: compile-time error |
| 29 | + p1.asFunction<NaTyPointerParamOpDart>(); //# 1: continued |
| 30 | + |
| 31 | +final p2 = |
| 32 | + ffiTestFunctions.lookup<NativeFunction<NaTyPointerReturnOp>>(returnOpName); |
| 33 | +final nonInvariantBinding2 = //# 2: compile-time error |
| 34 | + p2.asFunction<Int64PointerReturnOp>(); //# 2: continued |
| 35 | + |
| 36 | +final p3 = Pointer< |
| 37 | + NativeFunction< |
| 38 | + Pointer<NativeFunction<Pointer<NativeType> Function()>> |
| 39 | + Function()>>.fromAddress(0x1234); |
| 40 | +final f3 = p3 //# 10: compile-time error |
| 41 | + .asFunction< //# 10: continued |
| 42 | + Pointer< //# 10: continued |
| 43 | + NativeFunction< //# 10: continued |
| 44 | + Pointer<Int8> Function()>> //# 10: continued |
| 45 | + Function()>(); //# 10: continued |
| 46 | + |
| 47 | +// =========================================================== |
| 48 | +// Test check on callbacks from native to Dart (fromFunction). |
| 49 | +// =========================================================== |
| 50 | + |
| 51 | +void naTyPointerParamOp(Pointer<NativeType> p) { |
| 52 | + final Pointer<Int8> asInt8 = p.cast(); |
| 53 | + asInt8.value = 42; |
| 54 | +} |
| 55 | + |
| 56 | +Pointer<Int64> int64PointerReturnOp() { |
| 57 | + return Pointer.fromAddress(0x13370000); |
| 58 | +} |
| 59 | + |
| 60 | +final implictDowncast1 = //# 3: compile-time error |
| 61 | + Pointer.fromFunction<Int64PointerParamOp>(//# 3: continued |
| 62 | + naTyPointerParamOp); //# 3: continued |
| 63 | +final implictDowncast2 = //# 4: compile-time error |
| 64 | + Pointer.fromFunction<NaTyPointerReturnOp>(//# 4: continued |
| 65 | + int64PointerReturnOp); //# 4: continued |
| 66 | + |
| 67 | +void main() {} |
0 commit comments