|
35 | 35 | #include "include/dart_api.h" |
36 | 36 | #include "include/dart_native_api.h" |
37 | 37 |
|
| 38 | +#include "include/dart_api_dl.h" |
| 39 | + |
38 | 40 | namespace dart { |
39 | 41 |
|
40 | 42 | #define CHECK(X) \ |
@@ -270,31 +272,9 @@ DART_EXPORT intptr_t TestCallbackWrongIsolate(void (*fn)()) { |
270 | 272 | #endif // defined(TARGET_OS_LINUX) |
271 | 273 |
|
272 | 274 | //////////////////////////////////////////////////////////////////////////////// |
273 | | -// Dynamic linking of dart_native_api.h for the next two samples. |
274 | | -typedef bool (*Dart_PostCObjectType)(Dart_Port port_id, Dart_CObject* message); |
275 | | -Dart_PostCObjectType Dart_PostCObject_ = nullptr; |
276 | | - |
277 | | -DART_EXPORT void RegisterDart_PostCObject( |
278 | | - Dart_PostCObjectType function_pointer) { |
279 | | - Dart_PostCObject_ = function_pointer; |
280 | | -} |
281 | | - |
282 | | -typedef Dart_Port (*Dart_NewNativePortType)(const char* name, |
283 | | - Dart_NativeMessageHandler handler, |
284 | | - bool handle_concurrently); |
285 | | -Dart_NewNativePortType Dart_NewNativePort_ = nullptr; |
286 | | - |
287 | | -DART_EXPORT void RegisterDart_NewNativePort( |
288 | | - Dart_NewNativePortType function_pointer) { |
289 | | - Dart_NewNativePort_ = function_pointer; |
290 | | -} |
291 | | - |
292 | | -typedef bool (*Dart_CloseNativePortType)(Dart_Port native_port_id); |
293 | | -Dart_CloseNativePortType Dart_CloseNativePort_ = nullptr; |
294 | | - |
295 | | -DART_EXPORT void RegisterDart_CloseNativePort( |
296 | | - Dart_CloseNativePortType function_pointer) { |
297 | | - Dart_CloseNativePort_ = function_pointer; |
| 275 | +// Initialize `dart_api_dl.h` |
| 276 | +DART_EXPORT intptr_t InitDartApiDL(void* data) { |
| 277 | + return Dart_InitializeApiDL(data); |
298 | 278 | } |
299 | 279 |
|
300 | 280 | //////////////////////////////////////////////////////////////////////////////// |
@@ -342,7 +322,7 @@ void NotifyDart(Dart_Port send_port, const Work* work) { |
342 | 322 | dart_object.type = Dart_CObject_kInt64; |
343 | 323 | dart_object.value.as_int64 = work_addr; |
344 | 324 |
|
345 | | - const bool result = Dart_PostCObject_(send_port, &dart_object); |
| 325 | + const bool result = Dart_PostCObject_DL(send_port, &dart_object); |
346 | 326 | if (!result) { |
347 | 327 | FATAL("C : Posting message to port failed."); |
348 | 328 | } |
@@ -504,16 +484,16 @@ class PendingCall { |
504 | 484 | PendingCall(void** buffer, size_t* length) |
505 | 485 | : response_buffer_(buffer), response_length_(length) { |
506 | 486 | receive_port_ = |
507 | | - Dart_NewNativePort_("cpp-response", &PendingCall::HandleResponse, |
508 | | - /*handle_concurrently=*/false); |
| 487 | + Dart_NewNativePort_DL("cpp-response", &PendingCall::HandleResponse, |
| 488 | + /*handle_concurrently=*/false); |
509 | 489 | } |
510 | | - ~PendingCall() { Dart_CloseNativePort_(receive_port_); } |
| 490 | + ~PendingCall() { Dart_CloseNativePort_DL(receive_port_); } |
511 | 491 |
|
512 | 492 | Dart_Port port() const { return receive_port_; } |
513 | 493 |
|
514 | 494 | void PostAndWait(Dart_Port port, Dart_CObject* object) { |
515 | 495 | std::unique_lock<std::mutex> lock(mutex); |
516 | | - const bool success = Dart_PostCObject_(send_port_, object); |
| 496 | + const bool success = Dart_PostCObject_DL(send_port_, object); |
517 | 497 | if (!success) FATAL("Failed to send message, invalid port or isolate died"); |
518 | 498 |
|
519 | 499 | printf("C : Waiting for result.\n"); |
@@ -668,7 +648,7 @@ void MyCallback2(uint8_t a) { |
668 | 648 | printf("C : Dart_PostCObject_(request: %" Px ", call: %" Px ").\n", |
669 | 649 | reinterpret_cast<intptr_t>(&c_request), |
670 | 650 | reinterpret_cast<intptr_t>(&c_pending_call)); |
671 | | - Dart_PostCObject_(send_port_, &c_request); |
| 651 | + Dart_PostCObject_DL(send_port_, &c_request); |
672 | 652 | } |
673 | 653 |
|
674 | 654 | // Simulated work for Thread #1. |
@@ -793,7 +773,8 @@ DART_EXPORT void ThreadPoolTest_BarrierSync( |
793 | 773 | //////////////////////////////////////////////////////////////////////////////// |
794 | 774 | // Functions for handle tests. |
795 | 775 | // |
796 | | -// vmspecific_handle_test.dart |
| 776 | +// vmspecific_handle_test.dart (statically linked). |
| 777 | +// vmspecific_handle_dynamically_linked_test.dart (dynamically linked). |
797 | 778 |
|
798 | 779 | static void RunFinalizer(void* isolate_callback_data, |
799 | 780 | Dart_WeakPersistentHandle handle, |
@@ -912,4 +893,46 @@ DART_EXPORT Dart_Handle TrueHandle() { |
912 | 893 | return Dart_True(); |
913 | 894 | } |
914 | 895 |
|
| 896 | +DART_EXPORT Dart_Handle PassObjectToCUseDynamicLinking(Dart_Handle h) { |
| 897 | + auto persistent_handle = Dart_NewPersistentHandle_DL(h); |
| 898 | + |
| 899 | + Dart_Handle handle_2 = Dart_HandleFromPersistent_DL(persistent_handle); |
| 900 | + Dart_SetPersistentHandle_DL(persistent_handle, h); |
| 901 | + Dart_DeletePersistentHandle_DL(persistent_handle); |
| 902 | + |
| 903 | + auto weak_handle = Dart_NewWeakPersistentHandle_DL( |
| 904 | + handle_2, reinterpret_cast<void*>(0x1234), 64, RunFinalizer); |
| 905 | + Dart_Handle return_value = Dart_HandleFromWeakPersistent_DL(weak_handle); |
| 906 | + |
| 907 | + Dart_DeleteWeakPersistentHandle_DL(weak_handle); |
| 908 | + |
| 909 | + return return_value; |
| 910 | +} |
| 911 | + |
| 912 | +//////////////////////////////////////////////////////////////////////////////// |
| 913 | +// Example for doing closure callbacks with help of `dart_api.h`. |
| 914 | +// |
| 915 | +// sample_ffi_functions_callbacks_closures.dart |
| 916 | + |
| 917 | +void (*callback_)(Dart_Handle); |
| 918 | +Dart_PersistentHandle closure_to_callback_; |
| 919 | + |
| 920 | +DART_EXPORT void RegisterClosureCallbackFP(void (*callback)(Dart_Handle)) { |
| 921 | + callback_ = callback; |
| 922 | +} |
| 923 | + |
| 924 | +DART_EXPORT void RegisterClosureCallback(Dart_Handle h) { |
| 925 | + closure_to_callback_ = Dart_NewPersistentHandle_DL(h); |
| 926 | +} |
| 927 | + |
| 928 | +DART_EXPORT void InvokeClosureCallback() { |
| 929 | + Dart_Handle closure_handle = |
| 930 | + Dart_HandleFromPersistent_DL(closure_to_callback_); |
| 931 | + callback_(closure_handle); |
| 932 | +} |
| 933 | + |
| 934 | +DART_EXPORT void ReleaseClosureCallback() { |
| 935 | + Dart_DeletePersistentHandle_DL(closure_to_callback_); |
| 936 | +} |
| 937 | + |
915 | 938 | } // namespace dart |
0 commit comments