mirror of
https://github.com/dart-lang/sdk
synced 2024-09-05 08:24:39 +00:00
![Daco Harkes](/assets/img/avatar_default.png)
Closes: https://github.com/dart-lang/sdk/issues/43600. TEST=samples/ffi Change-Id: I620e6a73c8e37dd72f8f3921fa37dd4f8966e45c Cq-Include-Trybots: luci.dart.try:vm-kernel-nnbd-linux-debug-x64-try,vm-kernel-precomp-nnbd-linux-debug-x64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-x64-try,vm-kernel-reload-linux-debug-x64-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-precomp-linux-debug-x64-try,dart-sdk-linux-try,analyzer-nnbd-linux-release-try,analyzer-linux-release-try,front-end-nnbd-linux-release-x64-try,front-end-linux-release-x64-try Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/173542 Commit-Queue: Daco Harkes <dacoharkes@google.com> Reviewed-by: Aske Simon Christensen <askesc@google.com>
68 lines
2 KiB
Dart
68 lines
2 KiB
Dart
// Copyright (c) 2020, 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:expect/expect.dart';
|
|
|
|
import 'dylib_utils.dart';
|
|
|
|
void main() {
|
|
print('start main');
|
|
|
|
doDynamicLinking();
|
|
|
|
int counter = 0;
|
|
void closure() {
|
|
counter++;
|
|
}
|
|
|
|
// C holds on to this closure through a `Dart_PersistenHandle`.
|
|
registerClosureCallback(closure);
|
|
|
|
// Some time later this closure can be invoked.
|
|
invokeClosureCallback();
|
|
Expect.equals(1, counter);
|
|
|
|
// When C is done it needs to stop holding on to the closure such that the
|
|
// Dart GC can collect the closure.
|
|
releaseClosureCallback();
|
|
|
|
print('end main');
|
|
}
|
|
|
|
final testLibrary = dlopenPlatformSpecific("ffi_test_functions");
|
|
|
|
final registerClosureCallback =
|
|
testLibrary.lookupFunction<Void Function(Handle), void Function(Object)>(
|
|
"RegisterClosureCallback");
|
|
|
|
final invokeClosureCallback = testLibrary
|
|
.lookupFunction<Void Function(), void Function()>("InvokeClosureCallback");
|
|
|
|
final releaseClosureCallback = testLibrary
|
|
.lookupFunction<Void Function(), void Function()>("ReleaseClosureCallback");
|
|
|
|
void doClosureCallback(Object callback) {
|
|
final callback_as_function = callback as void Function();
|
|
callback_as_function();
|
|
}
|
|
|
|
final closureCallbackPointer =
|
|
Pointer.fromFunction<Void Function(Handle)>(doClosureCallback);
|
|
|
|
void doDynamicLinking() {
|
|
Expect.isTrue(NativeApi.majorVersion == 2);
|
|
Expect.isTrue(NativeApi.minorVersion >= 0);
|
|
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");
|
|
registerClosureCallback(closureCallbackPointer);
|
|
}
|