1
0
mirror of https://github.com/dart-lang/sdk synced 2024-07-03 00:08:46 +00:00

[vm/ffi] Change dart_api_dl.cc to dart_api_dl.c

Adapted the solution from
https://github.com/mraleph/go_dart_ffi_example/blob/master/dart_api_dl/include/dart_api_dl.c
such that we only have one carbon copy of the signatures.

This breaks existing code because the file is renamed, which is
added to the changelog.

Closes: https://github.com/dart-lang/sdk/issues/42982

Change-Id: If9300cac513c6cf5dac9e524bfc069764bb1a3f8
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/157965
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Vyacheslav Egorov <vegorov@google.com>
This commit is contained in:
Daco Harkes 2020-08-10 15:11:21 +00:00 committed by commit-bot@chromium.org
parent e2a67723ca
commit d98f777da8
6 changed files with 110 additions and 170 deletions

View File

@ -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

View File

@ -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` -

View File

@ -10,25 +10,27 @@
#include <string.h>
#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<DartApi*>(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<name##Type>( \
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

View File

@ -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 */

View File

@ -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 */

View File

@ -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<intptr_t>(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<void (*)()>(name)},
#define ENTRY(name, R, A) \
DartApiEntry{#name, reinterpret_cast<void (*)()>(name)},
DART_API_ALL_DL_SYMBOLS(ENTRY)
#undef ENTRY
DartApiEntry{nullptr, nullptr}};