Daco Harkes | b62e715 | 2020-10-26 12:01:16 +0000 | [diff] [blame] | 1 | // Copyright (c) 2020, 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 | // VMOptions=--deterministic --optimization-counter-threshold=5 --use-slow-path --stacktrace-every=100 |
| 8 | |
| 9 | import 'dart:ffi'; |
| 10 | |
| 11 | import "package:expect/expect.dart"; |
| 12 | import "package:ffi/ffi.dart"; |
| 13 | |
| 14 | // Reuse the struct classes. |
| 15 | import 'function_structs_by_value_generated_test.dart'; |
| 16 | |
| 17 | void main() { |
| 18 | for (int i = 0; i < 10; i++) { |
| 19 | recursiveTest(10); |
| 20 | recursiveTest(11); |
Daco Harkes | b6b82dd | 2020-12-18 09:34:35 +0000 | [diff] [blame] | 21 | testCopyLogic(); |
Daco Harkes | b62e715 | 2020-10-26 12:01:16 +0000 | [diff] [blame] | 22 | } |
| 23 | } |
| 24 | |
| 25 | void recursiveTest(int recursionCounter) { |
Daco Harkes | 7fbbe4f | 2021-01-28 21:40:15 +0000 | [diff] [blame] | 26 | final pointer = calloc<Struct20BytesHomogeneousInt32>(); |
| 27 | final struct = pointer.ref; |
Daco Harkes | b62e715 | 2020-10-26 12:01:16 +0000 | [diff] [blame] | 28 | struct.a0 = 1; |
| 29 | struct.a1 = 2; |
| 30 | struct.a2 = 3; |
| 31 | struct.a3 = 4; |
| 32 | struct.a4 = 5; |
| 33 | final result = dartPassStructRecursive(recursionCounter, struct); |
| 34 | Expect.equals(struct.a0 + recursionCounter * 2, result.a0); |
| 35 | Expect.equals(struct.a1, result.a1); |
| 36 | Expect.equals(struct.a2, result.a2); |
| 37 | Expect.equals(struct.a3, result.a3); |
| 38 | Expect.equals(struct.a4, result.a4); |
Daco Harkes | 7fbbe4f | 2021-01-28 21:40:15 +0000 | [diff] [blame] | 39 | calloc.free(pointer); |
Daco Harkes | b62e715 | 2020-10-26 12:01:16 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | Struct20BytesHomogeneousInt32 dartPassStructRecursive( |
| 43 | int recursionCounter, Struct20BytesHomogeneousInt32 struct) { |
| 44 | print("callbackPassStructRecurisive($recursionCounter, $struct)"); |
| 45 | struct.a0++; |
| 46 | final structA0Saved = struct.a0; |
| 47 | if (recursionCounter <= 0) { |
| 48 | print("returning"); |
| 49 | return struct; |
| 50 | } |
| 51 | |
| 52 | final result = |
| 53 | cPassStructRecursive(recursionCounter - 1, struct, functionPointer); |
| 54 | result.a0++; |
| 55 | |
| 56 | // Check struct.a0 is not modified by Dart->C call. |
| 57 | Expect.equals(structA0Saved, struct.a0); |
| 58 | |
| 59 | // Check struct.a0 is not modified by C->Dart callback, if so struct.a4 == 0. |
| 60 | Expect.notEquals(0, struct.a4); |
| 61 | |
| 62 | return result; |
| 63 | } |
| 64 | |
| 65 | final functionPointer = Pointer.fromFunction< |
| 66 | Struct20BytesHomogeneousInt32 Function( |
| 67 | Int64, Struct20BytesHomogeneousInt32)>(dartPassStructRecursive); |
| 68 | |
| 69 | final cPassStructRecursive = ffiTestFunctions.lookupFunction< |
| 70 | Struct20BytesHomogeneousInt32 Function(Int64 recursionCounter, |
| 71 | Struct20BytesHomogeneousInt32 struct, Pointer callbackAddress), |
| 72 | Struct20BytesHomogeneousInt32 Function(int recursionCounter, |
| 73 | Struct20BytesHomogeneousInt32, Pointer)>("PassStructRecursive"); |
Daco Harkes | b6b82dd | 2020-12-18 09:34:35 +0000 | [diff] [blame] | 74 | |
| 75 | Struct8BytesNestedInt typedDataBackedStruct = Struct8BytesNestedInt(); |
| 76 | bool typedDataBackedStructSet = false; |
| 77 | void _receiveStructByValue(Struct8BytesNestedInt struct) { |
| 78 | typedDataBackedStruct = struct; |
| 79 | typedDataBackedStructSet = true; |
| 80 | } |
| 81 | |
| 82 | final _receiveStructByValuePointer = |
| 83 | Pointer.fromFunction<Void Function(Struct8BytesNestedInt)>( |
| 84 | _receiveStructByValue); |
| 85 | |
| 86 | final _invokeReceiveStructByValue = ffiTestFunctions.lookupFunction< |
| 87 | Void Function( |
| 88 | Pointer<NativeFunction<Void Function(Struct8BytesNestedInt)>>), |
| 89 | void Function( |
| 90 | Pointer<NativeFunction<Void Function(Struct8BytesNestedInt)>>)>( |
| 91 | "CallbackWithStruct"); |
| 92 | |
| 93 | void testCopyLogic() { |
| 94 | _invokeReceiveStructByValue(_receiveStructByValuePointer); |
| 95 | Expect.isTrue(typedDataBackedStructSet); |
| 96 | |
Daco Harkes | 7fbbe4f | 2021-01-28 21:40:15 +0000 | [diff] [blame] | 97 | final pointer = calloc<Struct8BytesNestedInt>(); |
| 98 | final pointerBackedStruct = pointer.ref; |
Daco Harkes | b6b82dd | 2020-12-18 09:34:35 +0000 | [diff] [blame] | 99 | |
| 100 | void reset() { |
| 101 | pointerBackedStruct.a0.a0 = 1; |
| 102 | pointerBackedStruct.a0.a1 = 2; |
| 103 | pointerBackedStruct.a1.a0 = 3; |
| 104 | pointerBackedStruct.a1.a1 = 4; |
| 105 | typedDataBackedStruct.a0.a0 = 5; |
| 106 | typedDataBackedStruct.a0.a1 = 6; |
| 107 | typedDataBackedStruct.a1.a0 = 7; |
| 108 | typedDataBackedStruct.a1.a1 = 8; |
| 109 | } |
| 110 | |
| 111 | // Pointer -> Pointer. |
| 112 | reset(); |
| 113 | pointerBackedStruct.a1 = pointerBackedStruct.a0; |
| 114 | Expect.equals(1, pointerBackedStruct.a1.a0); |
| 115 | Expect.equals(2, pointerBackedStruct.a1.a1); |
| 116 | |
| 117 | // Pointer -> TypedData. |
| 118 | reset(); |
| 119 | typedDataBackedStruct.a1 = pointerBackedStruct.a0; |
| 120 | Expect.equals(1, typedDataBackedStruct.a1.a0); |
| 121 | Expect.equals(2, typedDataBackedStruct.a1.a1); |
| 122 | |
| 123 | // TypedData -> Pointer. |
| 124 | reset(); |
| 125 | pointerBackedStruct.a1 = typedDataBackedStruct.a0; |
| 126 | Expect.equals(5, pointerBackedStruct.a1.a0); |
| 127 | Expect.equals(6, pointerBackedStruct.a1.a1); |
| 128 | |
| 129 | // TypedData -> TypedData. |
| 130 | reset(); |
| 131 | typedDataBackedStruct.a1 = typedDataBackedStruct.a0; |
| 132 | Expect.equals(5, typedDataBackedStruct.a1.a0); |
| 133 | Expect.equals(6, typedDataBackedStruct.a1.a1); |
| 134 | |
Daco Harkes | 7fbbe4f | 2021-01-28 21:40:15 +0000 | [diff] [blame] | 135 | calloc.free(pointer); |
Daco Harkes | b6b82dd | 2020-12-18 09:34:35 +0000 | [diff] [blame] | 136 | } |