-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
sample_ffi_functions.dart
221 lines (198 loc) · 6.33 KB
/
sample_ffi_functions.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// 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.
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'calloc.dart';
import 'dylib_utils.dart';
typedef NativeUnaryOp = Int32 Function(Int32);
typedef NativeBinaryOp = Int32 Function(Int32, Int32);
typedef UnaryOp = int Function(int);
typedef BinaryOp = int Function(int, int);
typedef GenericBinaryOp<T> = int Function(int, T);
typedef NativeQuadOpSigned = Int64 Function(Int8, Int16, Int32, Int64);
typedef NativeQuadOpUnsigned = Uint64 Function(Uint8, Uint16, Uint32, Uint64);
typedef NativeFunc4 = IntPtr Function(IntPtr);
typedef NativeDoubleUnaryOp = Double Function(Double);
typedef NativeFloatUnaryOp = Float Function(Float);
typedef NativeDecenaryOp = IntPtr Function(IntPtr, IntPtr, IntPtr, IntPtr,
IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr);
typedef NativeDecenaryOp2 = Int16 Function(
Int8, Int16, Int8, Int16, Int8, Int16, Int8, Int16, Int8, Int16);
typedef NativeDoubleDecenaryOp = Double Function(Double, Double, Double, Double,
Double, Double, Double, Double, Double, Double);
typedef NativeVigesimalOp = Double Function(
IntPtr,
Float,
IntPtr,
Double,
IntPtr,
Float,
IntPtr,
Double,
IntPtr,
Float,
IntPtr,
Double,
IntPtr,
Float,
IntPtr,
Double,
IntPtr,
Float,
IntPtr,
Double);
typedef Int64PointerUnOp = Pointer<Int64> Function(Pointer<Int64>);
typedef QuadOp = int Function(int, int, int, int);
typedef DoubleUnaryOp = double Function(double);
typedef DecenaryOp = int Function(
int, int, int, int, int, int, int, int, int, int);
typedef DoubleDecenaryOp = double Function(double, double, double, double,
double, double, double, double, double, double);
typedef VigesimalOp = double Function(
int,
double,
int,
double,
int,
double,
int,
double,
int,
double,
int,
double,
int,
double,
int,
double,
int,
double,
int,
double);
main() {
print('start main');
DynamicLibrary ffiTestFunctions =
dlopenPlatformSpecific("ffi_test_functions");
{
// A int32 bin op.
BinaryOp sumPlus42 =
ffiTestFunctions.lookupFunction<NativeBinaryOp, BinaryOp>("SumPlus42");
var result = sumPlus42(3, 17);
print(result);
print(result.runtimeType);
}
{
// Various size arguments.
QuadOp intComputation = ffiTestFunctions
.lookupFunction<NativeQuadOpSigned, QuadOp>("IntComputation");
var result = intComputation(125, 250, 500, 1000);
print(result);
print(result.runtimeType);
var mint = 0x7FFFFFFFFFFFFFFF; // 2 ^ 63 - 1
result = intComputation(1, 1, 0, mint);
print(result);
print(result.runtimeType);
}
{
// Unsigned int parameters.
QuadOp uintComputation = ffiTestFunctions
.lookupFunction<NativeQuadOpUnsigned, QuadOp>("UintComputation");
var result = uintComputation(0xFF, 0xFFFF, 0xFFFFFFFF, -1);
result = uintComputation(1, 1, 0, -1);
print(result);
print(result.runtimeType);
print(-0xFF + 0xFFFF - 0xFFFFFFFF);
}
{
// Architecture size argument.
Pointer<NativeFunction<NativeFunc4>> p = ffiTestFunctions.lookup("Times3");
UnaryOp f6 = p.asFunction();
var result = f6(1337);
print(result);
print(result.runtimeType);
}
{
// Function with double.
DoubleUnaryOp times1_337Double = ffiTestFunctions
.lookupFunction<NativeDoubleUnaryOp, DoubleUnaryOp>("Times1_337Double");
var result = times1_337Double(2.0);
print(result);
print(result.runtimeType);
}
{
// Function with float.
DoubleUnaryOp times1_337Float = ffiTestFunctions
.lookupFunction<NativeFloatUnaryOp, DoubleUnaryOp>("Times1_337Float");
var result = times1_337Float(1000.0);
print(result);
print(result.runtimeType);
}
{
// Function with many arguments: arguments get passed in registers and stack.
DecenaryOp sumManyInts = ffiTestFunctions
.lookupFunction<NativeDecenaryOp, DecenaryOp>("SumManyInts");
var result = sumManyInts(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
print(result);
print(result.runtimeType);
}
{
// Function with many arguments: arguments get passed in registers and stack.
DecenaryOp sumManyInts = ffiTestFunctions
.lookupFunction<NativeDecenaryOp2, DecenaryOp>("SumManySmallInts");
var result = sumManyInts(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
print(result);
print(result.runtimeType);
}
{
// Function with many double arguments.
DoubleDecenaryOp sumManyDoubles = ffiTestFunctions.lookupFunction<
NativeDoubleDecenaryOp, DoubleDecenaryOp>("SumManyDoubles");
var result =
sumManyDoubles(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0);
print(result);
print(result.runtimeType);
}
{
// Function with many arguments, ints and doubles mixed.
VigesimalOp sumManyNumbers = ffiTestFunctions
.lookupFunction<NativeVigesimalOp, VigesimalOp>("SumManyNumbers");
var result = sumManyNumbers(1, 2.0, 3, 4.0, 5, 6.0, 7, 8.0, 9, 10.0, 11,
12.0, 13, 14.0, 15, 16.0, 17, 18.0, 19, 20.0);
print(result);
print(result.runtimeType);
}
{
// pass an array / pointer as argument
Int64PointerUnOp assign1337Index1 = ffiTestFunctions
.lookupFunction<Int64PointerUnOp, Int64PointerUnOp>("Assign1337Index1");
Pointer<Int64> p2 = calloc(2);
p2.value = 42;
p2[1] = 1000;
print(p2.elementAt(1).address.toRadixString(16));
print(p2[1]);
Pointer<Int64> result = assign1337Index1(p2);
print(p2[1]);
print(assign1337Index1);
print(assign1337Index1.runtimeType);
print(result);
print(result.runtimeType);
print(result.address.toRadixString(16));
print(result.value);
}
{
// Passing in nullptr for a pointer argument results in a nullptr in c.
Int64PointerUnOp nullableInt64ElemAt1 =
ffiTestFunctions.lookupFunction<Int64PointerUnOp, Int64PointerUnOp>(
"NullableInt64ElemAt1");
Pointer<Int64> result = nullableInt64ElemAt1(nullptr);
print(result);
print(result.runtimeType);
Pointer<Int64> p2 = calloc(2);
result = nullableInt64ElemAt1(p2);
print(result);
print(result.runtimeType);
calloc.free(p2);
}
print("end main");
}