diff --git a/CHANGELOG.md b/CHANGELOG.md index a03d3766ed6..c3e87cc00db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,9 +9,13 @@ ### Dart VM +* **Breaking Change** [#42982][]: `dart_api_dl.cc` is renamed to + `dart_api_dl.c` and changed to a pure C file. * Introduces `Dart_FinalizableHandle`s. They do auto-delete, and the weakly referred object cannot be accessed through them. +[#42982]: https://github.com/dart-lang/sdk/issues/42982 + ### Tools #### Linter diff --git a/runtime/bin/BUILD.gn b/runtime/bin/BUILD.gn index 1b44cd3b529..2341506c45c 100644 --- a/runtime/bin/BUILD.gn +++ b/runtime/bin/BUILD.gn @@ -1122,7 +1122,7 @@ shared_library("ffi_test_functions") { sources = [ # This file must be compiled in for dynamic linking. - "../include/dart_api_dl.cc", + "../include/dart_api_dl.c", # The two files here do not depend on each other. # flutter/flutter integration tests will only use `ffi_test_functions.cc` - diff --git a/runtime/include/dart_api_dl.cc b/runtime/include/dart_api_dl.c similarity index 79% rename from runtime/include/dart_api_dl.cc rename to runtime/include/dart_api_dl.c index 5ab685d921e..84e5ce2a094 100644 --- a/runtime/include/dart_api_dl.cc +++ b/runtime/include/dart_api_dl.c @@ -10,25 +10,27 @@ #include -#define DART_API_DL_DEFINITIONS(name) \ - using name##Type = decltype(&name); \ - name##Type name##_DL = nullptr; +#define DART_API_DL_DEFINITIONS(name, R, A) \ + typedef R(*name##_Type) A; \ + name##_Type name##_DL = NULL; + DART_API_ALL_DL_SYMBOLS(DART_API_DL_DEFINITIONS) + #undef DART_API_DL_DEFINITIONS typedef void (*DartApiEntry_function)(); DartApiEntry_function FindFunctionPointer(const DartApiEntry* entries, const char* name) { - while (entries->name != nullptr) { + while (entries->name != NULL) { if (strcmp(entries->name, name) == 0) return entries->function; entries++; } - return nullptr; + return NULL; } intptr_t Dart_InitializeApiDL(void* data) { - DartApi* dart_api_data = reinterpret_cast(data); + DartApi* dart_api_data = (DartApi*)data; if (dart_api_data->major != DART_API_DL_MAJOR_VERSION) { // If the DartVM we're running on does not have the same version as this @@ -49,9 +51,9 @@ intptr_t Dart_InitializeApiDL(void* data) { const DartApiEntry* dart_api_function_pointers = dart_api_data->functions; -#define DART_API_DL_INIT(name) \ - name##_DL = reinterpret_cast( \ - FindFunctionPointer(dart_api_function_pointers, #name)); +#define DART_API_DL_INIT(name, R, A) \ + name##_DL = \ + (name##_Type)(FindFunctionPointer(dart_api_function_pointers, #name)); DART_API_ALL_DL_SYMBOLS(DART_API_DL_INIT) #undef DART_API_DL_INIT diff --git a/runtime/include/dart_api_dl.h b/runtime/include/dart_api_dl.h index 19fd0f3b0cb..85460c8a426 100644 --- a/runtime/include/dart_api_dl.h +++ b/runtime/include/dart_api_dl.h @@ -18,128 +18,110 @@ * All symbols are postfixed with _DL to indicate that they are dynamically * linked and to prevent conflicts with the original symbol. * - * Link `dart_api_dl.cc` file into your library and invoke + * Link `dart_api_dl.c` file into your library and invoke * `Dart_InitializeApiDL` with `NativeApi.initializeApiDLData`. */ -intptr_t Dart_InitializeApiDL(void* data); +#ifdef __cplusplus +#define DART_EXTERN extern "C" +#else +#define DART_EXTERN extern +#endif +DART_EXTERN intptr_t Dart_InitializeApiDL(void* data); + +// ============================================================================ // IMPORTANT! Never update these signatures without properly updating // DART_API_DL_MAJOR_VERSION and DART_API_DL_MINOR_VERSION. // -// Verbatim copy of `dart_native_api.h` and `dart_api.h` symbols to trigger -// compile-time errors if the sybols in those files are updated without -// updating these. +// Verbatim copy of `dart_native_api.h` and `dart_api.h` symbol names and types +// to trigger compile-time errors if the sybols in those files are updated +// without updating these. // -// Function signatures and typedefs are carbon copied. Structs are typechecked -// nominally in C/C++, so they are not copied, instead a comment is added to -// their definition. +// Function return and argument types, and typedefs are carbon copied. Structs +// are typechecked nominally in C/C++, so they are not copied, instead a +// comment is added to their definition. typedef int64_t Dart_Port_DL; typedef void (*Dart_NativeMessageHandler_DL)(Dart_Port_DL dest_port_id, Dart_CObject* message); -DART_EXTERN_C bool (*Dart_PostCObject_DL)(Dart_Port_DL port_id, - Dart_CObject* message); +// dart_native_api.h symbols can be called on any thread. +#define DART_NATIVE_API_DL_SYMBOLS(F) \ + /***** dart_native_api.h *****/ \ + /* Dart_Port */ \ + F(Dart_PostCObject, bool, (Dart_Port_DL port_id, Dart_CObject * message)) \ + F(Dart_PostInteger, bool, (Dart_Port_DL port_id, int64_t message)) \ + F(Dart_NewNativePort, Dart_Port_DL, \ + (const char* name, Dart_NativeMessageHandler_DL handler, \ + bool handle_concurrently)) \ + F(Dart_CloseNativePort, bool, (Dart_Port_DL native_port_id)) -DART_EXTERN_C bool (*Dart_PostInteger_DL)(Dart_Port_DL port_id, - int64_t message); +// dart_api.h symbols can only be called on Dart threads. +#define DART_API_DL_SYMBOLS(F) \ + /***** dart_api.h *****/ \ + /* Errors */ \ + F(Dart_IsError, bool, (Dart_Handle handle)) \ + F(Dart_IsApiError, bool, (Dart_Handle handle)) \ + F(Dart_IsUnhandledExceptionError, bool, (Dart_Handle handle)) \ + F(Dart_IsCompilationError, bool, (Dart_Handle handle)) \ + F(Dart_IsFatalError, bool, (Dart_Handle handle)) \ + F(Dart_GetError, const char*, (Dart_Handle handle)) \ + F(Dart_ErrorHasException, bool, (Dart_Handle handle)) \ + F(Dart_ErrorGetException, Dart_Handle, (Dart_Handle handle)) \ + F(Dart_ErrorGetStackTrace, Dart_Handle, (Dart_Handle handle)) \ + F(Dart_NewApiError, Dart_Handle, (const char* error)) \ + F(Dart_NewCompilationError, Dart_Handle, (const char* error)) \ + F(Dart_NewUnhandledExceptionError, Dart_Handle, (Dart_Handle exception)) \ + F(Dart_PropagateError, void, (Dart_Handle handle)) \ + /* Dart_Handle, Dart_PersistentHandle, Dart_WeakPersistentHandle */ \ + F(Dart_HandleFromPersistent, Dart_Handle, (Dart_PersistentHandle object)) \ + F(Dart_HandleFromWeakPersistent, Dart_Handle, \ + (Dart_WeakPersistentHandle object)) \ + F(Dart_NewPersistentHandle, Dart_PersistentHandle, (Dart_Handle object)) \ + F(Dart_SetPersistentHandle, void, \ + (Dart_PersistentHandle obj1, Dart_Handle obj2)) \ + F(Dart_DeletePersistentHandle, void, (Dart_PersistentHandle object)) \ + F(Dart_NewWeakPersistentHandle, Dart_WeakPersistentHandle, \ + (Dart_Handle object, void* peer, intptr_t external_allocation_size, \ + Dart_WeakPersistentHandleFinalizer callback)) \ + F(Dart_DeleteWeakPersistentHandle, void, (Dart_WeakPersistentHandle object)) \ + F(Dart_UpdateExternalSize, void, \ + (Dart_WeakPersistentHandle object, intptr_t external_allocation_size)) \ + F(Dart_NewFinalizableHandle, Dart_FinalizableHandle, \ + (Dart_Handle object, void* peer, intptr_t external_allocation_size, \ + Dart_HandleFinalizer callback)) \ + F(Dart_DeleteFinalizableHandle, void, \ + (Dart_FinalizableHandle object, Dart_Handle strong_ref_to_object)) \ + F(Dart_UpdateFinalizableExternalSize, void, \ + (Dart_FinalizableHandle object, Dart_Handle strong_ref_to_object, \ + intptr_t external_allocation_size)) \ + /* Dart_Port */ \ + F(Dart_Post, bool, (Dart_Port_DL port_id, Dart_Handle object)) \ + F(Dart_NewSendPort, Dart_Handle, (Dart_Port_DL port_id)) \ + F(Dart_SendPortGetId, Dart_Handle, \ + (Dart_Handle port, Dart_Port_DL * port_id)) \ + /* Scopes */ \ + F(Dart_EnterScope, void, ()) \ + F(Dart_ExitScope, void, ()) -DART_EXTERN_C Dart_Port_DL (*Dart_NewNativePort_DL)( - const char* name, - Dart_NativeMessageHandler_DL handler, - bool handle_concurrently); - -DART_EXTERN_C bool (*Dart_CloseNativePort_DL)(Dart_Port_DL native_port_id); - -DART_EXTERN_C bool (*Dart_IsError_DL)(Dart_Handle handle); - -DART_EXTERN_C bool (*Dart_IsApiError_DL)(Dart_Handle handle); - -DART_EXTERN_C bool (*Dart_IsUnhandledExceptionError_DL)(Dart_Handle handle); - -DART_EXTERN_C bool (*Dart_IsCompilationError_DL)(Dart_Handle handle); - -DART_EXTERN_C bool (*Dart_IsFatalError_DL)(Dart_Handle handle); - -DART_EXTERN_C const char* (*Dart_GetError_DL)(Dart_Handle handle); - -DART_EXTERN_C bool (*Dart_ErrorHasException_DL)(Dart_Handle handle); - -DART_EXTERN_C Dart_Handle (*Dart_ErrorGetException_DL)(Dart_Handle handle); - -DART_EXTERN_C Dart_Handle (*Dart_ErrorGetStackTrace_DL)(Dart_Handle handle); - -DART_EXTERN_C Dart_Handle (*Dart_NewApiError_DL)(const char* error); - -DART_EXTERN_C Dart_Handle (*Dart_NewCompilationError_DL)(const char* error); - -DART_EXTERN_C Dart_Handle (*Dart_NewUnhandledExceptionError_DL)( - Dart_Handle exception); - -DART_EXTERN_C void (*Dart_PropagateError_DL)(Dart_Handle handle); - -DART_EXTERN_C Dart_Handle (*Dart_ToString_DL)(Dart_Handle object); - -DART_EXTERN_C bool (*Dart_IdentityEquals_DL)(Dart_Handle obj1, - Dart_Handle obj2); - -DART_EXTERN_C Dart_Handle (*Dart_HandleFromPersistent_DL)( - Dart_PersistentHandle object); - -DART_EXTERN_C Dart_Handle (*Dart_HandleFromWeakPersistent_DL)( - Dart_WeakPersistentHandle object); - -DART_EXTERN_C Dart_PersistentHandle (*Dart_NewPersistentHandle_DL)( - Dart_Handle object); - -DART_EXTERN_C void (*Dart_SetPersistentHandle_DL)(Dart_PersistentHandle obj1, - Dart_Handle obj2); - -DART_EXTERN_C void (*Dart_DeletePersistentHandle_DL)( - Dart_PersistentHandle object); - -DART_EXTERN_C Dart_WeakPersistentHandle (*Dart_NewWeakPersistentHandle_DL)( - Dart_Handle object, - void* peer, - intptr_t external_allocation_size, - Dart_WeakPersistentHandleFinalizer callback); - -DART_EXTERN_C void (*Dart_DeleteWeakPersistentHandle_DL)( - Dart_WeakPersistentHandle object); - -DART_EXTERN_C void (*Dart_UpdateExternalSize_DL)( - Dart_WeakPersistentHandle object, - intptr_t external_allocation_size); - -DART_EXTERN_C Dart_FinalizableHandle (*Dart_NewFinalizableHandle_DL)( - Dart_Handle object, - void* peer, - intptr_t external_allocation_size, - Dart_HandleFinalizer callback); - -DART_EXTERN_C void (*Dart_DeleteFinalizableHandle_DL)( - Dart_FinalizableHandle object, - Dart_Handle strong_ref_to_object); - -DART_EXTERN_C void (*Dart_UpdateFinalizableExternalSize_DL)( - Dart_FinalizableHandle object, - Dart_Handle strong_ref_to_object, - intptr_t external_allocation_size); - -DART_EXTERN_C bool (*Dart_Post_DL)(Dart_Port_DL port_id, Dart_Handle object); - -DART_EXTERN_C Dart_Handle (*Dart_NewSendPort_DL)(Dart_Port_DL port_id); - -DART_EXTERN_C Dart_Handle (*Dart_SendPortGetId_DL)(Dart_Handle port, - Dart_Port_DL* port_id); - -DART_EXTERN_C void (*Dart_EnterScope_DL)(); - -DART_EXTERN_C void (*Dart_ExitScope_DL)(); +#define DART_API_ALL_DL_SYMBOLS(F) \ + DART_NATIVE_API_DL_SYMBOLS(F) \ + DART_API_DL_SYMBOLS(F) // IMPORTANT! Never update these signatures without properly updating // DART_API_DL_MAJOR_VERSION and DART_API_DL_MINOR_VERSION. // // End of verbatim copy. +// ============================================================================ + +#define DART_API_DL_DECLARATIONS(name, R, A) \ + typedef R(*name##_Type) A; \ + DART_EXTERN name##_Type name##_DL; + +DART_API_ALL_DL_SYMBOLS(DART_API_DL_DECLARATIONS) + +#undef DART_API_DL_DEFINITIONS + +#undef DART_EXTERN #endif /* RUNTIME_INCLUDE_DART_API_DL_H_ */ /* NOLINT */ diff --git a/runtime/include/internal/dart_api_dl_impl.h b/runtime/include/internal/dart_api_dl_impl.h index a306e2d3a18..ad13a4b8115 100644 --- a/runtime/include/internal/dart_api_dl_impl.h +++ b/runtime/include/internal/dart_api_dl_impl.h @@ -7,65 +7,15 @@ #ifndef RUNTIME_INCLUDE_INTERNAL_DART_API_DL_IMPL_H_ #define RUNTIME_INCLUDE_INTERNAL_DART_API_DL_IMPL_H_ -// dart_native_api.h symbols can be called on any thread. -#define DART_NATIVE_API_DL_SYMBOLS(F) \ - /***** dart_native_api.h *****/ \ - /* Dart_Port */ \ - F(Dart_PostCObject) \ - F(Dart_PostInteger) \ - F(Dart_NewNativePort) \ - F(Dart_CloseNativePort) - -// dart_api.h symbols can only be called on Dart threads. -#define DART_API_DL_SYMBOLS(F) \ - /***** dart_api.h *****/ \ - /* Errors */ \ - F(Dart_IsError) \ - F(Dart_IsApiError) \ - F(Dart_IsUnhandledExceptionError) \ - F(Dart_IsCompilationError) \ - F(Dart_IsFatalError) \ - F(Dart_GetError) \ - F(Dart_ErrorHasException) \ - F(Dart_ErrorGetException) \ - F(Dart_ErrorGetStackTrace) \ - F(Dart_NewApiError) \ - F(Dart_NewCompilationError) \ - F(Dart_NewUnhandledExceptionError) \ - F(Dart_PropagateError) \ - /* Dart_Handle, Dart_PersistentHandle, Dart_WeakPersistentHandle */ \ - F(Dart_NewPersistentHandle) \ - F(Dart_SetPersistentHandle) \ - F(Dart_HandleFromPersistent) \ - F(Dart_DeletePersistentHandle) \ - F(Dart_NewWeakPersistentHandle) \ - F(Dart_HandleFromWeakPersistent) \ - F(Dart_DeleteWeakPersistentHandle) \ - F(Dart_UpdateExternalSize) \ - F(Dart_NewFinalizableHandle) \ - F(Dart_DeleteFinalizableHandle) \ - F(Dart_UpdateFinalizableExternalSize) \ - /* Dart_Port */ \ - F(Dart_Post) \ - F(Dart_NewSendPort) \ - F(Dart_SendPortGetId) \ - /* Scopes */ \ - F(Dart_EnterScope) \ - F(Dart_ExitScope) - -#define DART_API_ALL_DL_SYMBOLS(F) \ - DART_NATIVE_API_DL_SYMBOLS(F) \ - DART_API_DL_SYMBOLS(F) - -struct DartApiEntry { +typedef struct { const char* name; void (*function)(); -}; +} DartApiEntry; -struct DartApi { +typedef struct { const int major; const int minor; const DartApiEntry* const functions; -}; +} DartApi; #endif /* RUNTIME_INCLUDE_INTERNAL_DART_API_DL_IMPL_H_ */ /* NOLINT */ diff --git a/runtime/lib/ffi.cc b/runtime/lib/ffi.cc index 1a8bc20facf..3c4a07cbc2f 100644 --- a/runtime/lib/ffi.cc +++ b/runtime/lib/ffi.cc @@ -3,6 +3,7 @@ // BSD-style license that can be found in the LICENSE file. #include "include/dart_api.h" +#include "include/dart_api_dl.h" #include "include/dart_native_api.h" #include "include/dart_version.h" #include "include/internal/dart_api_dl_impl.h" @@ -501,7 +502,7 @@ DEFINE_NATIVE_ENTRY(DartNativeApiFunctionPointer, 0, 1) { GET_NON_NULL_NATIVE_ARGUMENT(String, name_dart, arguments->NativeArgAt(0)); const char* name = name_dart.ToCString(); -#define RETURN_FUNCTION_ADDRESS(function_name) \ +#define RETURN_FUNCTION_ADDRESS(function_name, R, A) \ if (strcmp(name, #function_name) == 0) { \ return Integer::New(reinterpret_cast(function_name)); \ } @@ -522,7 +523,8 @@ DEFINE_NATIVE_ENTRY(DartApiDLMinorVersion, 0, 0) { } static const DartApiEntry dart_api_entries[] = { -#define ENTRY(name) DartApiEntry{#name, reinterpret_cast(name)}, +#define ENTRY(name, R, A) \ + DartApiEntry{#name, reinterpret_cast(name)}, DART_API_ALL_DL_SYMBOLS(ENTRY) #undef ENTRY DartApiEntry{nullptr, nullptr}};