[ffi] Format samples and benchmarks with Dart 3.8
Change-Id: I4f8e695ac648becc0de3466b6ba2058cbae7c408 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/422103 Commit-Queue: Bob Nystrom <rnystrom@google.com> Reviewed-by: Bob Nystrom <rnystrom@google.com> Auto-Submit: Daco Harkes <dacoharkes@google.com> Commit-Queue: Daco Harkes <dacoharkes@google.com>
This commit is contained in:
committed by
Commit Queue
parent
546ddc645e
commit
0768228750
@@ -28,8 +28,11 @@ main() async {
|
||||
print("Dart: Setup.");
|
||||
Expect.isTrue(NativeApi.majorVersion == 2);
|
||||
Expect.isTrue(NativeApi.minorVersion >= 2);
|
||||
final initializeApi = dl.lookupFunction<IntPtr Function(Pointer<Void>),
|
||||
int Function(Pointer<Void>)>("InitDartApiDL");
|
||||
final initializeApi = dl
|
||||
.lookupFunction<
|
||||
IntPtr Function(Pointer<Void>),
|
||||
int Function(Pointer<Void>)
|
||||
>("InitDartApiDL");
|
||||
Expect.isTrue(initializeApi(NativeApi.initializeApiDLData) == 0);
|
||||
|
||||
final interactiveCppRequests = ReceivePort()..listen(requestExecuteCallback);
|
||||
@@ -82,28 +85,42 @@ final callback2FP = Pointer.fromFunction<Void Function(IntPtr)>(callback2);
|
||||
|
||||
final dl = dlopenPlatformSpecific("ffi_test_functions");
|
||||
|
||||
final registerCallback1 = dl.lookupFunction<
|
||||
Void Function(Int64 sendPort,
|
||||
Pointer<NativeFunction<IntPtr Function(IntPtr)>> functionPointer),
|
||||
void Function(int sendPort,
|
||||
Pointer<NativeFunction<IntPtr Function(IntPtr)>> functionPointer)>(
|
||||
'RegisterMyCallbackBlocking');
|
||||
final registerCallback1 = dl
|
||||
.lookupFunction<
|
||||
Void Function(
|
||||
Int64 sendPort,
|
||||
Pointer<NativeFunction<IntPtr Function(IntPtr)>> functionPointer,
|
||||
),
|
||||
void Function(
|
||||
int sendPort,
|
||||
Pointer<NativeFunction<IntPtr Function(IntPtr)>> functionPointer,
|
||||
)
|
||||
>('RegisterMyCallbackBlocking');
|
||||
|
||||
final registerCallback2 = dl.lookupFunction<
|
||||
Void Function(Int64 sendPort,
|
||||
Pointer<NativeFunction<Void Function(IntPtr)>> functionPointer),
|
||||
void Function(int sendPort,
|
||||
Pointer<NativeFunction<Void Function(IntPtr)>> functionPointer)>(
|
||||
'RegisterMyCallbackNonBlocking');
|
||||
final registerCallback2 = dl
|
||||
.lookupFunction<
|
||||
Void Function(
|
||||
Int64 sendPort,
|
||||
Pointer<NativeFunction<Void Function(IntPtr)>> functionPointer,
|
||||
),
|
||||
void Function(
|
||||
int sendPort,
|
||||
Pointer<NativeFunction<Void Function(IntPtr)>> functionPointer,
|
||||
)
|
||||
>('RegisterMyCallbackNonBlocking');
|
||||
|
||||
final startWorkSimulator =
|
||||
dl.lookupFunction<Void Function(), void Function()>('StartWorkSimulator');
|
||||
final startWorkSimulator = dl.lookupFunction<Void Function(), void Function()>(
|
||||
'StartWorkSimulator',
|
||||
);
|
||||
|
||||
final stopWorkSimulator =
|
||||
dl.lookupFunction<Void Function(), void Function()>('StopWorkSimulator');
|
||||
final stopWorkSimulator = dl.lookupFunction<Void Function(), void Function()>(
|
||||
'StopWorkSimulator',
|
||||
);
|
||||
|
||||
final executeCallback = dl.lookupFunction<Void Function(Pointer<Work>),
|
||||
void Function(Pointer<Work>)>('ExecuteCallback');
|
||||
final executeCallback = dl
|
||||
.lookupFunction<Void Function(Pointer<Work>), void Function(Pointer<Work>)>(
|
||||
'ExecuteCallback',
|
||||
);
|
||||
|
||||
final class Work extends Opaque {}
|
||||
|
||||
|
||||
@@ -37,8 +37,11 @@ main() async {
|
||||
print("Dart: Setup.");
|
||||
Expect.isTrue(NativeApi.majorVersion == 2);
|
||||
Expect.isTrue(NativeApi.minorVersion >= 2);
|
||||
final initializeApi = dl.lookupFunction<IntPtr Function(Pointer<Void>),
|
||||
int Function(Pointer<Void>)>("InitDartApiDL");
|
||||
final initializeApi = dl
|
||||
.lookupFunction<
|
||||
IntPtr Function(Pointer<Void>),
|
||||
int Function(Pointer<Void>)
|
||||
>("InitDartApiDL");
|
||||
Expect.isTrue(initializeApi(NativeApi.initializeApiDLData) == 0);
|
||||
|
||||
final interactiveCppRequests = ReceivePort()..listen(handleCppRequests);
|
||||
@@ -111,8 +114,10 @@ void handleCppRequests(dynamic message) {
|
||||
// the argument to the function.
|
||||
final int argument = cppRequest.data[0];
|
||||
final int result = myCallback1(argument);
|
||||
final cppResponse =
|
||||
CppResponse(cppRequest.pendingCall!, Uint8List.fromList([result]));
|
||||
final cppResponse = CppResponse(
|
||||
cppRequest.pendingCall!,
|
||||
Uint8List.fromList([result]),
|
||||
);
|
||||
print('Dart: Responding: $cppResponse');
|
||||
cppRequest.replyPort!.send(cppResponse.toCppMessage());
|
||||
} else if (cppRequest.method == 'myCallback2') {
|
||||
@@ -123,14 +128,18 @@ void handleCppRequests(dynamic message) {
|
||||
|
||||
final dl = dlopenPlatformSpecific("ffi_test_functions");
|
||||
|
||||
final registerSendPort = dl.lookupFunction<Void Function(Int64 sendPort),
|
||||
void Function(int sendPort)>('RegisterSendPort');
|
||||
final registerSendPort = dl
|
||||
.lookupFunction<Void Function(Int64 sendPort), void Function(int sendPort)>(
|
||||
'RegisterSendPort',
|
||||
);
|
||||
|
||||
final startWorkSimulator2 =
|
||||
dl.lookupFunction<Void Function(), void Function()>('StartWorkSimulator2');
|
||||
final startWorkSimulator2 = dl.lookupFunction<Void Function(), void Function()>(
|
||||
'StartWorkSimulator2',
|
||||
);
|
||||
|
||||
final stopWorkSimulator2 =
|
||||
dl.lookupFunction<Void Function(), void Function()>('StopWorkSimulator2');
|
||||
final stopWorkSimulator2 = dl.lookupFunction<Void Function(), void Function()>(
|
||||
'StopWorkSimulator2',
|
||||
);
|
||||
|
||||
Future asyncSleep(int ms) {
|
||||
return new Future.delayed(Duration(milliseconds: ms), () => true);
|
||||
|
||||
@@ -16,11 +16,10 @@ import '../dylib_utils.dart';
|
||||
void main() {
|
||||
final receiveFromHelper = ReceivePort();
|
||||
|
||||
Isolate.spawn(helperIsolateMain, receiveFromHelper.sendPort)
|
||||
.then((helperIsolate) {
|
||||
helperIsolate.addOnExitListener(
|
||||
receiveFromHelper.sendPort,
|
||||
);
|
||||
Isolate.spawn(helperIsolateMain, receiveFromHelper.sendPort).then((
|
||||
helperIsolate,
|
||||
) {
|
||||
helperIsolate.addOnExitListener(receiveFromHelper.sendPort);
|
||||
print("Main: Helper started.");
|
||||
Pointer<SomeResource> resource = nullptr;
|
||||
receiveFromHelper.listen((message) {
|
||||
@@ -34,7 +33,8 @@ void main() {
|
||||
Expect.isNull(message);
|
||||
print("Main: Helper is shut down.");
|
||||
print(
|
||||
"Main: Trying to use resource after isolate that was supposed to free it was shut down.");
|
||||
"Main: Trying to use resource after isolate that was supposed to free it was shut down.",
|
||||
);
|
||||
useResource(resource);
|
||||
print("Main: Releasing resource manually.");
|
||||
releaseResource(resource);
|
||||
@@ -70,20 +70,27 @@ void helperIsolateMain(SendPort sendToMain) {
|
||||
});
|
||||
}
|
||||
|
||||
final ffiTestDynamicLibrary =
|
||||
dlopenPlatformSpecific("ffi_test_dynamic_library");
|
||||
final ffiTestDynamicLibrary = dlopenPlatformSpecific(
|
||||
"ffi_test_dynamic_library",
|
||||
);
|
||||
|
||||
final allocateResource = ffiTestDynamicLibrary.lookupFunction<
|
||||
Pointer<SomeResource> Function(),
|
||||
Pointer<SomeResource> Function()>("AllocateResource");
|
||||
final allocateResource = ffiTestDynamicLibrary
|
||||
.lookupFunction<
|
||||
Pointer<SomeResource> Function(),
|
||||
Pointer<SomeResource> Function()
|
||||
>("AllocateResource");
|
||||
|
||||
final useResource = ffiTestDynamicLibrary.lookupFunction<
|
||||
Void Function(Pointer<SomeResource>),
|
||||
void Function(Pointer<SomeResource>)>("UseResource");
|
||||
final useResource = ffiTestDynamicLibrary
|
||||
.lookupFunction<
|
||||
Void Function(Pointer<SomeResource>),
|
||||
void Function(Pointer<SomeResource>)
|
||||
>("UseResource");
|
||||
|
||||
final releaseResource = ffiTestDynamicLibrary.lookupFunction<
|
||||
Void Function(Pointer<SomeResource>),
|
||||
void Function(Pointer<SomeResource>)>("ReleaseResource");
|
||||
final releaseResource = ffiTestDynamicLibrary
|
||||
.lookupFunction<
|
||||
Void Function(Pointer<SomeResource>),
|
||||
void Function(Pointer<SomeResource>)
|
||||
>("ReleaseResource");
|
||||
|
||||
/// Represents some opaque resource being managed by a library.
|
||||
final class SomeResource extends Opaque {}
|
||||
|
||||
@@ -14,12 +14,15 @@ import 'utf8_helpers.dart';
|
||||
import '../dylib_utils.dart';
|
||||
|
||||
main() async {
|
||||
final ffiTestDynamicLibrary =
|
||||
dlopenPlatformSpecific("ffi_test_dynamic_library");
|
||||
final ffiTestDynamicLibrary = dlopenPlatformSpecific(
|
||||
"ffi_test_dynamic_library",
|
||||
);
|
||||
|
||||
final MemMove = ffiTestDynamicLibrary.lookupFunction<
|
||||
Void Function(Pointer<Void>, Pointer<Void>, IntPtr),
|
||||
void Function(Pointer<Void>, Pointer<Void>, int)>("MemMove");
|
||||
final MemMove = ffiTestDynamicLibrary
|
||||
.lookupFunction<
|
||||
Void Function(Pointer<Void>, Pointer<Void>, IntPtr),
|
||||
void Function(Pointer<Void>, Pointer<Void>, int)
|
||||
>("MemMove");
|
||||
|
||||
// To ensure resources are freed, wrap them in a [using] call.
|
||||
using((Arena arena) {
|
||||
@@ -79,17 +82,23 @@ main() async {
|
||||
print(p.contents());
|
||||
});
|
||||
|
||||
final allocateResource = ffiTestDynamicLibrary.lookupFunction<
|
||||
Pointer<SomeResource> Function(),
|
||||
Pointer<SomeResource> Function()>("AllocateResource");
|
||||
final allocateResource = ffiTestDynamicLibrary
|
||||
.lookupFunction<
|
||||
Pointer<SomeResource> Function(),
|
||||
Pointer<SomeResource> Function()
|
||||
>("AllocateResource");
|
||||
|
||||
final useResource = ffiTestDynamicLibrary.lookupFunction<
|
||||
Void Function(Pointer<SomeResource>),
|
||||
void Function(Pointer<SomeResource>)>("UseResource");
|
||||
final useResource = ffiTestDynamicLibrary
|
||||
.lookupFunction<
|
||||
Void Function(Pointer<SomeResource>),
|
||||
void Function(Pointer<SomeResource>)
|
||||
>("UseResource");
|
||||
|
||||
final releaseResource = ffiTestDynamicLibrary.lookupFunction<
|
||||
Void Function(Pointer<SomeResource>),
|
||||
void Function(Pointer<SomeResource>)>("ReleaseResource");
|
||||
final releaseResource = ffiTestDynamicLibrary
|
||||
.lookupFunction<
|
||||
Void Function(Pointer<SomeResource>),
|
||||
void Function(Pointer<SomeResource>)
|
||||
>("ReleaseResource");
|
||||
|
||||
// Using an FFI call to release a resource.
|
||||
using((Arena arena) {
|
||||
|
||||
@@ -13,12 +13,15 @@ import 'utf8_helpers.dart';
|
||||
import '../dylib_utils.dart';
|
||||
|
||||
main() async {
|
||||
final ffiTestDynamicLibrary =
|
||||
dlopenPlatformSpecific("ffi_test_dynamic_library");
|
||||
final ffiTestDynamicLibrary = dlopenPlatformSpecific(
|
||||
"ffi_test_dynamic_library",
|
||||
);
|
||||
|
||||
final MemMove = ffiTestDynamicLibrary.lookupFunction<
|
||||
Void Function(Pointer<Void>, Pointer<Void>, IntPtr),
|
||||
void Function(Pointer<Void>, Pointer<Void>, int)>("MemMove");
|
||||
final MemMove = ffiTestDynamicLibrary
|
||||
.lookupFunction<
|
||||
Void Function(Pointer<Void>, Pointer<Void>, IntPtr),
|
||||
void Function(Pointer<Void>, Pointer<Void>, int)
|
||||
>("MemMove");
|
||||
|
||||
// To ensure resources are freed, wrap them in a [withZoneArena] call.
|
||||
withZoneArena(() {
|
||||
@@ -77,17 +80,23 @@ main() async {
|
||||
print(p.contents());
|
||||
});
|
||||
|
||||
final allocateResource = ffiTestDynamicLibrary.lookupFunction<
|
||||
Pointer<SomeResource> Function(),
|
||||
Pointer<SomeResource> Function()>("AllocateResource");
|
||||
final allocateResource = ffiTestDynamicLibrary
|
||||
.lookupFunction<
|
||||
Pointer<SomeResource> Function(),
|
||||
Pointer<SomeResource> Function()
|
||||
>("AllocateResource");
|
||||
|
||||
final useResource = ffiTestDynamicLibrary.lookupFunction<
|
||||
Void Function(Pointer<SomeResource>),
|
||||
void Function(Pointer<SomeResource>)>("UseResource");
|
||||
final useResource = ffiTestDynamicLibrary
|
||||
.lookupFunction<
|
||||
Void Function(Pointer<SomeResource>),
|
||||
void Function(Pointer<SomeResource>)
|
||||
>("UseResource");
|
||||
|
||||
final releaseResource = ffiTestDynamicLibrary.lookupFunction<
|
||||
Void Function(Pointer<SomeResource>),
|
||||
void Function(Pointer<SomeResource>)>("ReleaseResource");
|
||||
final releaseResource = ffiTestDynamicLibrary
|
||||
.lookupFunction<
|
||||
Void Function(Pointer<SomeResource>),
|
||||
void Function(Pointer<SomeResource>)
|
||||
>("ReleaseResource");
|
||||
|
||||
// Using an FFI call to release a resource.
|
||||
withZoneArena(() {
|
||||
|
||||
@@ -13,12 +13,15 @@ import 'utf8_helpers.dart';
|
||||
import '../dylib_utils.dart';
|
||||
|
||||
main() {
|
||||
final ffiTestDynamicLibrary =
|
||||
dlopenPlatformSpecific("ffi_test_dynamic_library");
|
||||
final ffiTestDynamicLibrary = dlopenPlatformSpecific(
|
||||
"ffi_test_dynamic_library",
|
||||
);
|
||||
|
||||
final memMove = ffiTestDynamicLibrary.lookupFunction<
|
||||
Void Function(Pointer<Void>, Pointer<Void>, IntPtr),
|
||||
void Function(Pointer<Void>, Pointer<Void>, int)>("MemMove");
|
||||
final memMove = ffiTestDynamicLibrary
|
||||
.lookupFunction<
|
||||
Void Function(Pointer<Void>, Pointer<Void>, IntPtr),
|
||||
void Function(Pointer<Void>, Pointer<Void>, int)
|
||||
>("MemMove");
|
||||
|
||||
// To ensure resources are freed, call free manually.
|
||||
//
|
||||
|
||||
@@ -46,7 +46,8 @@ extension Utf8Helpers on Pointer<Utf8> {
|
||||
/// Returns a Dart string containing the decoded code points.
|
||||
String contents() {
|
||||
final int length = strlen;
|
||||
return utf8.decode(Uint8List.view(
|
||||
this.cast<Uint8>().asTypedList(length).buffer, 0, length));
|
||||
return utf8.decode(
|
||||
Uint8List.view(this.cast<Uint8>().asTypedList(length).buffer, 0, length),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,72 +18,121 @@ 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 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);
|
||||
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");
|
||||
DynamicLibrary ffiTestFunctions = dlopenPlatformSpecific(
|
||||
"ffi_test_functions",
|
||||
);
|
||||
|
||||
{
|
||||
// A int32 bin op.
|
||||
BinaryOp sumPlus42 =
|
||||
ffiTestFunctions.lookupFunction<NativeBinaryOp, BinaryOp>("SumPlus42");
|
||||
BinaryOp sumPlus42 = ffiTestFunctions
|
||||
.lookupFunction<NativeBinaryOp, BinaryOp>("SumPlus42");
|
||||
|
||||
var result = sumPlus42(3, 17);
|
||||
print(result);
|
||||
@@ -172,10 +221,22 @@ main() {
|
||||
|
||||
{
|
||||
// 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);
|
||||
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);
|
||||
}
|
||||
@@ -184,8 +245,28 @@ main() {
|
||||
// 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);
|
||||
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);
|
||||
}
|
||||
@@ -211,9 +292,10 @@ main() {
|
||||
|
||||
{
|
||||
// Passing in nullptr for a pointer argument results in a nullptr in c.
|
||||
Int64PointerUnOp nullableInt64ElemAt1 =
|
||||
ffiTestFunctions.lookupFunction<Int64PointerUnOp, Int64PointerUnOp>(
|
||||
"NullableInt64ElemAt1");
|
||||
Int64PointerUnOp nullableInt64ElemAt1 = ffiTestFunctions
|
||||
.lookupFunction<Int64PointerUnOp, Int64PointerUnOp>(
|
||||
"NullableInt64ElemAt1",
|
||||
);
|
||||
|
||||
Pointer<Int64> result = nullableInt64ElemAt1(nullptr);
|
||||
print(result);
|
||||
|
||||
@@ -11,19 +11,22 @@ import 'dylib_utils.dart';
|
||||
|
||||
typedef NativeCoordinateOp = Pointer<Coordinate> Function(Pointer<Coordinate>);
|
||||
|
||||
typedef CoordinateTrice = Pointer<Coordinate> Function(
|
||||
Pointer<NativeFunction<NativeCoordinateOp>>, Pointer<Coordinate>);
|
||||
typedef CoordinateTrice =
|
||||
Pointer<Coordinate> Function(
|
||||
Pointer<NativeFunction<NativeCoordinateOp>>,
|
||||
Pointer<Coordinate>,
|
||||
);
|
||||
|
||||
typedef BinaryOp = int Function(int, int);
|
||||
typedef NativeIntptrBinOp = IntPtr Function(IntPtr, IntPtr);
|
||||
typedef NativeIntptrBinOpLookup = Pointer<NativeFunction<NativeIntptrBinOp>>
|
||||
Function();
|
||||
typedef NativeIntptrBinOpLookup =
|
||||
Pointer<NativeFunction<NativeIntptrBinOp>> Function();
|
||||
|
||||
typedef NativeApplyTo42And74Type = IntPtr Function(
|
||||
Pointer<NativeFunction<NativeIntptrBinOp>>);
|
||||
typedef NativeApplyTo42And74Type =
|
||||
IntPtr Function(Pointer<NativeFunction<NativeIntptrBinOp>>);
|
||||
|
||||
typedef ApplyTo42And74Type = int Function(
|
||||
Pointer<NativeFunction<NativeIntptrBinOp>>);
|
||||
typedef ApplyTo42And74Type =
|
||||
int Function(Pointer<NativeFunction<NativeIntptrBinOp>>);
|
||||
|
||||
int myPlus(int a, int b) {
|
||||
print("myPlus");
|
||||
@@ -35,15 +38,17 @@ int myPlus(int a, int b) {
|
||||
main() {
|
||||
print('start main');
|
||||
|
||||
DynamicLibrary ffiTestFunctions =
|
||||
dlopenPlatformSpecific("ffi_test_functions");
|
||||
DynamicLibrary ffiTestFunctions = dlopenPlatformSpecific(
|
||||
"ffi_test_functions",
|
||||
);
|
||||
|
||||
{
|
||||
// Pass a c pointer to a c function as an argument to a c function.
|
||||
Pointer<NativeFunction<NativeCoordinateOp>> transposeCoordinatePointer =
|
||||
ffiTestFunctions.lookup("TransposeCoordinate");
|
||||
Pointer<NativeFunction<CoordinateTrice>> p2 =
|
||||
ffiTestFunctions.lookup("CoordinateUnOpTrice");
|
||||
Pointer<NativeFunction<CoordinateTrice>> p2 = ffiTestFunctions.lookup(
|
||||
"CoordinateUnOpTrice",
|
||||
);
|
||||
CoordinateTrice coordinateUnOpTrice = p2.asFunction();
|
||||
final c1 = calloc<Coordinate>()
|
||||
..ref.x = 10.0
|
||||
@@ -58,8 +63,8 @@ main() {
|
||||
|
||||
{
|
||||
// Return a c pointer to a c function from a c function.
|
||||
Pointer<NativeFunction<NativeIntptrBinOpLookup>> p14 =
|
||||
ffiTestFunctions.lookup("IntptrAdditionClosure");
|
||||
Pointer<NativeFunction<NativeIntptrBinOpLookup>> p14 = ffiTestFunctions
|
||||
.lookup("IntptrAdditionClosure");
|
||||
NativeIntptrBinOpLookup intptrAdditionClosure = p14.asFunction();
|
||||
|
||||
Pointer<NativeFunction<NativeIntptrBinOp>> intptrAdditionPointer =
|
||||
@@ -69,12 +74,14 @@ main() {
|
||||
}
|
||||
|
||||
{
|
||||
Pointer<NativeFunction<NativeIntptrBinOp>> pointer =
|
||||
Pointer.fromFunction(myPlus, 0);
|
||||
Pointer<NativeFunction<NativeIntptrBinOp>> pointer = Pointer.fromFunction(
|
||||
myPlus,
|
||||
0,
|
||||
);
|
||||
print(pointer);
|
||||
|
||||
Pointer<NativeFunction<NativeApplyTo42And74Type>> p17 =
|
||||
ffiTestFunctions.lookup("ApplyTo42And74");
|
||||
Pointer<NativeFunction<NativeApplyTo42And74Type>> p17 = ffiTestFunctions
|
||||
.lookup("ApplyTo42And74");
|
||||
ApplyTo42And74Type applyTo42And74 = p17.asFunction();
|
||||
|
||||
int result = applyTo42And74(pointer);
|
||||
|
||||
@@ -34,9 +34,10 @@ void main() {
|
||||
|
||||
final testLibrary = dlopenPlatformSpecific("ffi_test_functions");
|
||||
|
||||
final registerClosureCallback =
|
||||
testLibrary.lookupFunction<Void Function(Handle), void Function(Object)>(
|
||||
"RegisterClosureCallback");
|
||||
final registerClosureCallback = testLibrary
|
||||
.lookupFunction<Void Function(Handle), void Function(Object)>(
|
||||
"RegisterClosureCallback",
|
||||
);
|
||||
|
||||
final invokeClosureCallback = testLibrary
|
||||
.lookupFunction<Void Function(), void Function()>("InvokeClosureCallback");
|
||||
@@ -49,19 +50,23 @@ void doClosureCallback(Object callback) {
|
||||
callback_as_function();
|
||||
}
|
||||
|
||||
final closureCallbackPointer =
|
||||
Pointer.fromFunction<Void Function(Handle)>(doClosureCallback);
|
||||
final closureCallbackPointer = Pointer.fromFunction<Void Function(Handle)>(
|
||||
doClosureCallback,
|
||||
);
|
||||
|
||||
void doDynamicLinking() {
|
||||
Expect.isTrue(NativeApi.majorVersion == 2);
|
||||
Expect.isTrue(NativeApi.minorVersion >= 2);
|
||||
final initializeApi = testLibrary.lookupFunction<
|
||||
IntPtr Function(Pointer<Void>),
|
||||
int Function(Pointer<Void>)>("InitDartApiDL");
|
||||
final initializeApi = testLibrary
|
||||
.lookupFunction<
|
||||
IntPtr Function(Pointer<Void>),
|
||||
int Function(Pointer<Void>)
|
||||
>("InitDartApiDL");
|
||||
Expect.isTrue(initializeApi(NativeApi.initializeApiDLData) == 0);
|
||||
|
||||
final registerClosureCallback = testLibrary.lookupFunction<
|
||||
Void Function(Pointer),
|
||||
void Function(Pointer)>("RegisterClosureCallbackFP");
|
||||
final registerClosureCallback = testLibrary
|
||||
.lookupFunction<Void Function(Pointer), void Function(Pointer)>(
|
||||
"RegisterClosureCallbackFP",
|
||||
);
|
||||
registerClosureCallback(closureCallbackPointer);
|
||||
}
|
||||
|
||||
@@ -14,13 +14,15 @@ typedef NativeCoordinateOp = Pointer<Coordinate> Function(Pointer<Coordinate>);
|
||||
main() {
|
||||
print('start main');
|
||||
|
||||
DynamicLibrary ffiTestFunctions =
|
||||
dlopenPlatformSpecific("ffi_test_functions");
|
||||
DynamicLibrary ffiTestFunctions = dlopenPlatformSpecific(
|
||||
"ffi_test_functions",
|
||||
);
|
||||
|
||||
{
|
||||
// Pass a struct to a c function and get a struct as return value.
|
||||
Pointer<NativeFunction<NativeCoordinateOp>> p1 =
|
||||
ffiTestFunctions.lookup("TransposeCoordinate");
|
||||
Pointer<NativeFunction<NativeCoordinateOp>> p1 = ffiTestFunctions.lookup(
|
||||
"TransposeCoordinate",
|
||||
);
|
||||
NativeCoordinateOp f1 = p1.asFunction();
|
||||
|
||||
final c1 = calloc<Coordinate>()
|
||||
@@ -45,8 +47,9 @@ main() {
|
||||
|
||||
{
|
||||
// Pass an array of structs to a c function.
|
||||
Pointer<NativeFunction<NativeCoordinateOp>> p1 =
|
||||
ffiTestFunctions.lookup("CoordinateElemAt1");
|
||||
Pointer<NativeFunction<NativeCoordinateOp>> p1 = ffiTestFunctions.lookup(
|
||||
"CoordinateElemAt1",
|
||||
);
|
||||
NativeCoordinateOp f1 = p1.asFunction();
|
||||
|
||||
Pointer<Coordinate> c1 = calloc<Coordinate>(3);
|
||||
|
||||
Reference in New Issue
Block a user