blob: d7021b39421fc3068b41b2a4231e7dc2c7e4ff3a [file] [log] [blame]
Daco Harkesb62e7152020-10-26 12:01:16 +00001// 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
9import 'dart:ffi';
10
11import "package:expect/expect.dart";
12import "package:ffi/ffi.dart";
13
14// Reuse the struct classes.
15import 'function_structs_by_value_generated_test.dart';
16
17void main() {
18 for (int i = 0; i < 10; i++) {
19 recursiveTest(10);
20 recursiveTest(11);
Daco Harkesb6b82dd2020-12-18 09:34:35 +000021 testCopyLogic();
Daco Harkesb62e7152020-10-26 12:01:16 +000022 }
23}
24
25void recursiveTest(int recursionCounter) {
Daco Harkes7fbbe4f2021-01-28 21:40:15 +000026 final pointer = calloc<Struct20BytesHomogeneousInt32>();
27 final struct = pointer.ref;
Daco Harkesb62e7152020-10-26 12:01:16 +000028 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 Harkes7fbbe4f2021-01-28 21:40:15 +000039 calloc.free(pointer);
Daco Harkesb62e7152020-10-26 12:01:16 +000040}
41
42Struct20BytesHomogeneousInt32 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
65final functionPointer = Pointer.fromFunction<
66 Struct20BytesHomogeneousInt32 Function(
67 Int64, Struct20BytesHomogeneousInt32)>(dartPassStructRecursive);
68
69final cPassStructRecursive = ffiTestFunctions.lookupFunction<
70 Struct20BytesHomogeneousInt32 Function(Int64 recursionCounter,
71 Struct20BytesHomogeneousInt32 struct, Pointer callbackAddress),
72 Struct20BytesHomogeneousInt32 Function(int recursionCounter,
73 Struct20BytesHomogeneousInt32, Pointer)>("PassStructRecursive");
Daco Harkesb6b82dd2020-12-18 09:34:35 +000074
75Struct8BytesNestedInt typedDataBackedStruct = Struct8BytesNestedInt();
76bool typedDataBackedStructSet = false;
77void _receiveStructByValue(Struct8BytesNestedInt struct) {
78 typedDataBackedStruct = struct;
79 typedDataBackedStructSet = true;
80}
81
82final _receiveStructByValuePointer =
83 Pointer.fromFunction<Void Function(Struct8BytesNestedInt)>(
84 _receiveStructByValue);
85
86final _invokeReceiveStructByValue = ffiTestFunctions.lookupFunction<
87 Void Function(
88 Pointer<NativeFunction<Void Function(Struct8BytesNestedInt)>>),
89 void Function(
90 Pointer<NativeFunction<Void Function(Struct8BytesNestedInt)>>)>(
91 "CallbackWithStruct");
92
93void testCopyLogic() {
94 _invokeReceiveStructByValue(_receiveStructByValuePointer);
95 Expect.isTrue(typedDataBackedStructSet);
96
Daco Harkes7fbbe4f2021-01-28 21:40:15 +000097 final pointer = calloc<Struct8BytesNestedInt>();
98 final pointerBackedStruct = pointer.ref;
Daco Harkesb6b82dd2020-12-18 09:34:35 +000099
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 Harkes7fbbe4f2021-01-28 21:40:15 +0000135 calloc.free(pointer);
Daco Harkesb6b82dd2020-12-18 09:34:35 +0000136}