Skip to content

Commit d98f777

Browse files
dcharkescommit-bot@chromium.org
authored andcommitted
[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: #42982 Change-Id: If9300cac513c6cf5dac9e524bfc069764bb1a3f8 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/157965 Commit-Queue: Daco Harkes <[email protected]> Reviewed-by: Vyacheslav Egorov <[email protected]>
1 parent e2a6772 commit d98f777

File tree

6 files changed

+112
-172
lines changed

6 files changed

+112
-172
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@
99

1010
### Dart VM
1111

12+
* **Breaking Change** [#42982][]: `dart_api_dl.cc` is renamed to
13+
`dart_api_dl.c` and changed to a pure C file.
1214
* Introduces `Dart_FinalizableHandle`s. They do auto-delete, and the weakly
1315
referred object cannot be accessed through them.
1416

17+
[#42982]: https://github.com/dart-lang/sdk/issues/42982
18+
1519
### Tools
1620

1721
#### Linter

runtime/bin/BUILD.gn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,7 @@ shared_library("ffi_test_functions") {
11221122

11231123
sources = [
11241124
# This file must be compiled in for dynamic linking.
1125-
"../include/dart_api_dl.cc",
1125+
"../include/dart_api_dl.c",
11261126

11271127
# The two files here do not depend on each other.
11281128
# flutter/flutter integration tests will only use `ffi_test_functions.cc` -

runtime/include/dart_api_dl.cc renamed to runtime/include/dart_api_dl.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,27 @@
1010

1111
#include <string.h>
1212

13-
#define DART_API_DL_DEFINITIONS(name) \
14-
using name##Type = decltype(&name); \
15-
name##Type name##_DL = nullptr;
13+
#define DART_API_DL_DEFINITIONS(name, R, A) \
14+
typedef R(*name##_Type) A; \
15+
name##_Type name##_DL = NULL;
16+
1617
DART_API_ALL_DL_SYMBOLS(DART_API_DL_DEFINITIONS)
18+
1719
#undef DART_API_DL_DEFINITIONS
1820

1921
typedef void (*DartApiEntry_function)();
2022

2123
DartApiEntry_function FindFunctionPointer(const DartApiEntry* entries,
2224
const char* name) {
23-
while (entries->name != nullptr) {
25+
while (entries->name != NULL) {
2426
if (strcmp(entries->name, name) == 0) return entries->function;
2527
entries++;
2628
}
27-
return nullptr;
29+
return NULL;
2830
}
2931

3032
intptr_t Dart_InitializeApiDL(void* data) {
31-
DartApi* dart_api_data = reinterpret_cast<DartApi*>(data);
33+
DartApi* dart_api_data = (DartApi*)data;
3234

3335
if (dart_api_data->major != DART_API_DL_MAJOR_VERSION) {
3436
// 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) {
4951

5052
const DartApiEntry* dart_api_function_pointers = dart_api_data->functions;
5153

52-
#define DART_API_DL_INIT(name) \
53-
name##_DL = reinterpret_cast<name##Type>( \
54-
FindFunctionPointer(dart_api_function_pointers, #name));
54+
#define DART_API_DL_INIT(name, R, A) \
55+
name##_DL = \
56+
(name##_Type)(FindFunctionPointer(dart_api_function_pointers, #name));
5557
DART_API_ALL_DL_SYMBOLS(DART_API_DL_INIT)
5658
#undef DART_API_DL_INIT
5759

runtime/include/dart_api_dl.h

Lines changed: 88 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -18,128 +18,110 @@
1818
* All symbols are postfixed with _DL to indicate that they are dynamically
1919
* linked and to prevent conflicts with the original symbol.
2020
*
21-
* Link `dart_api_dl.cc` file into your library and invoke
21+
* Link `dart_api_dl.c` file into your library and invoke
2222
* `Dart_InitializeApiDL` with `NativeApi.initializeApiDLData`.
2323
*/
2424

25-
intptr_t Dart_InitializeApiDL(void* data);
25+
#ifdef __cplusplus
26+
#define DART_EXTERN extern "C"
27+
#else
28+
#define DART_EXTERN extern
29+
#endif
2630

31+
DART_EXTERN intptr_t Dart_InitializeApiDL(void* data);
32+
33+
// ============================================================================
2734
// IMPORTANT! Never update these signatures without properly updating
2835
// DART_API_DL_MAJOR_VERSION and DART_API_DL_MINOR_VERSION.
2936
//
30-
// Verbatim copy of `dart_native_api.h` and `dart_api.h` symbols to trigger
31-
// compile-time errors if the sybols in those files are updated without
32-
// updating these.
37+
// Verbatim copy of `dart_native_api.h` and `dart_api.h` symbol names and types
38+
// to trigger compile-time errors if the sybols in those files are updated
39+
// without updating these.
3340
//
34-
// Function signatures and typedefs are carbon copied. Structs are typechecked
35-
// nominally in C/C++, so they are not copied, instead a comment is added to
36-
// their definition.
41+
// Function return and argument types, and typedefs are carbon copied. Structs
42+
// are typechecked nominally in C/C++, so they are not copied, instead a
43+
// comment is added to their definition.
3744
typedef int64_t Dart_Port_DL;
3845

3946
typedef void (*Dart_NativeMessageHandler_DL)(Dart_Port_DL dest_port_id,
4047
Dart_CObject* message);
4148

42-
DART_EXTERN_C bool (*Dart_PostCObject_DL)(Dart_Port_DL port_id,
43-
Dart_CObject* message);
44-
45-
DART_EXTERN_C bool (*Dart_PostInteger_DL)(Dart_Port_DL port_id,
46-
int64_t message);
47-
48-
DART_EXTERN_C Dart_Port_DL (*Dart_NewNativePort_DL)(
49-
const char* name,
50-
Dart_NativeMessageHandler_DL handler,
51-
bool handle_concurrently);
52-
53-
DART_EXTERN_C bool (*Dart_CloseNativePort_DL)(Dart_Port_DL native_port_id);
54-
55-
DART_EXTERN_C bool (*Dart_IsError_DL)(Dart_Handle handle);
56-
57-
DART_EXTERN_C bool (*Dart_IsApiError_DL)(Dart_Handle handle);
58-
59-
DART_EXTERN_C bool (*Dart_IsUnhandledExceptionError_DL)(Dart_Handle handle);
60-
61-
DART_EXTERN_C bool (*Dart_IsCompilationError_DL)(Dart_Handle handle);
62-
63-
DART_EXTERN_C bool (*Dart_IsFatalError_DL)(Dart_Handle handle);
64-
65-
DART_EXTERN_C const char* (*Dart_GetError_DL)(Dart_Handle handle);
66-
67-
DART_EXTERN_C bool (*Dart_ErrorHasException_DL)(Dart_Handle handle);
68-
69-
DART_EXTERN_C Dart_Handle (*Dart_ErrorGetException_DL)(Dart_Handle handle);
70-
71-
DART_EXTERN_C Dart_Handle (*Dart_ErrorGetStackTrace_DL)(Dart_Handle handle);
72-
73-
DART_EXTERN_C Dart_Handle (*Dart_NewApiError_DL)(const char* error);
74-
75-
DART_EXTERN_C Dart_Handle (*Dart_NewCompilationError_DL)(const char* error);
76-
77-
DART_EXTERN_C Dart_Handle (*Dart_NewUnhandledExceptionError_DL)(
78-
Dart_Handle exception);
79-
80-
DART_EXTERN_C void (*Dart_PropagateError_DL)(Dart_Handle handle);
81-
82-
DART_EXTERN_C Dart_Handle (*Dart_ToString_DL)(Dart_Handle object);
83-
84-
DART_EXTERN_C bool (*Dart_IdentityEquals_DL)(Dart_Handle obj1,
85-
Dart_Handle obj2);
86-
87-
DART_EXTERN_C Dart_Handle (*Dart_HandleFromPersistent_DL)(
88-
Dart_PersistentHandle object);
89-
90-
DART_EXTERN_C Dart_Handle (*Dart_HandleFromWeakPersistent_DL)(
91-
Dart_WeakPersistentHandle object);
92-
93-
DART_EXTERN_C Dart_PersistentHandle (*Dart_NewPersistentHandle_DL)(
94-
Dart_Handle object);
95-
96-
DART_EXTERN_C void (*Dart_SetPersistentHandle_DL)(Dart_PersistentHandle obj1,
97-
Dart_Handle obj2);
98-
99-
DART_EXTERN_C void (*Dart_DeletePersistentHandle_DL)(
100-
Dart_PersistentHandle object);
101-
102-
DART_EXTERN_C Dart_WeakPersistentHandle (*Dart_NewWeakPersistentHandle_DL)(
103-
Dart_Handle object,
104-
void* peer,
105-
intptr_t external_allocation_size,
106-
Dart_WeakPersistentHandleFinalizer callback);
107-
108-
DART_EXTERN_C void (*Dart_DeleteWeakPersistentHandle_DL)(
109-
Dart_WeakPersistentHandle object);
110-
111-
DART_EXTERN_C void (*Dart_UpdateExternalSize_DL)(
112-
Dart_WeakPersistentHandle object,
113-
intptr_t external_allocation_size);
114-
115-
DART_EXTERN_C Dart_FinalizableHandle (*Dart_NewFinalizableHandle_DL)(
116-
Dart_Handle object,
117-
void* peer,
118-
intptr_t external_allocation_size,
119-
Dart_HandleFinalizer callback);
120-
121-
DART_EXTERN_C void (*Dart_DeleteFinalizableHandle_DL)(
122-
Dart_FinalizableHandle object,
123-
Dart_Handle strong_ref_to_object);
124-
125-
DART_EXTERN_C void (*Dart_UpdateFinalizableExternalSize_DL)(
126-
Dart_FinalizableHandle object,
127-
Dart_Handle strong_ref_to_object,
128-
intptr_t external_allocation_size);
129-
130-
DART_EXTERN_C bool (*Dart_Post_DL)(Dart_Port_DL port_id, Dart_Handle object);
131-
132-
DART_EXTERN_C Dart_Handle (*Dart_NewSendPort_DL)(Dart_Port_DL port_id);
133-
134-
DART_EXTERN_C Dart_Handle (*Dart_SendPortGetId_DL)(Dart_Handle port,
135-
Dart_Port_DL* port_id);
136-
137-
DART_EXTERN_C void (*Dart_EnterScope_DL)();
138-
139-
DART_EXTERN_C void (*Dart_ExitScope_DL)();
49+
// dart_native_api.h symbols can be called on any thread.
50+
#define DART_NATIVE_API_DL_SYMBOLS(F) \
51+
/***** dart_native_api.h *****/ \
52+
/* Dart_Port */ \
53+
F(Dart_PostCObject, bool, (Dart_Port_DL port_id, Dart_CObject * message)) \
54+
F(Dart_PostInteger, bool, (Dart_Port_DL port_id, int64_t message)) \
55+
F(Dart_NewNativePort, Dart_Port_DL, \
56+
(const char* name, Dart_NativeMessageHandler_DL handler, \
57+
bool handle_concurrently)) \
58+
F(Dart_CloseNativePort, bool, (Dart_Port_DL native_port_id))
59+
60+
// dart_api.h symbols can only be called on Dart threads.
61+
#define DART_API_DL_SYMBOLS(F) \
62+
/***** dart_api.h *****/ \
63+
/* Errors */ \
64+
F(Dart_IsError, bool, (Dart_Handle handle)) \
65+
F(Dart_IsApiError, bool, (Dart_Handle handle)) \
66+
F(Dart_IsUnhandledExceptionError, bool, (Dart_Handle handle)) \
67+
F(Dart_IsCompilationError, bool, (Dart_Handle handle)) \
68+
F(Dart_IsFatalError, bool, (Dart_Handle handle)) \
69+
F(Dart_GetError, const char*, (Dart_Handle handle)) \
70+
F(Dart_ErrorHasException, bool, (Dart_Handle handle)) \
71+
F(Dart_ErrorGetException, Dart_Handle, (Dart_Handle handle)) \
72+
F(Dart_ErrorGetStackTrace, Dart_Handle, (Dart_Handle handle)) \
73+
F(Dart_NewApiError, Dart_Handle, (const char* error)) \
74+
F(Dart_NewCompilationError, Dart_Handle, (const char* error)) \
75+
F(Dart_NewUnhandledExceptionError, Dart_Handle, (Dart_Handle exception)) \
76+
F(Dart_PropagateError, void, (Dart_Handle handle)) \
77+
/* Dart_Handle, Dart_PersistentHandle, Dart_WeakPersistentHandle */ \
78+
F(Dart_HandleFromPersistent, Dart_Handle, (Dart_PersistentHandle object)) \
79+
F(Dart_HandleFromWeakPersistent, Dart_Handle, \
80+
(Dart_WeakPersistentHandle object)) \
81+
F(Dart_NewPersistentHandle, Dart_PersistentHandle, (Dart_Handle object)) \
82+
F(Dart_SetPersistentHandle, void, \
83+
(Dart_PersistentHandle obj1, Dart_Handle obj2)) \
84+
F(Dart_DeletePersistentHandle, void, (Dart_PersistentHandle object)) \
85+
F(Dart_NewWeakPersistentHandle, Dart_WeakPersistentHandle, \
86+
(Dart_Handle object, void* peer, intptr_t external_allocation_size, \
87+
Dart_WeakPersistentHandleFinalizer callback)) \
88+
F(Dart_DeleteWeakPersistentHandle, void, (Dart_WeakPersistentHandle object)) \
89+
F(Dart_UpdateExternalSize, void, \
90+
(Dart_WeakPersistentHandle object, intptr_t external_allocation_size)) \
91+
F(Dart_NewFinalizableHandle, Dart_FinalizableHandle, \
92+
(Dart_Handle object, void* peer, intptr_t external_allocation_size, \
93+
Dart_HandleFinalizer callback)) \
94+
F(Dart_DeleteFinalizableHandle, void, \
95+
(Dart_FinalizableHandle object, Dart_Handle strong_ref_to_object)) \
96+
F(Dart_UpdateFinalizableExternalSize, void, \
97+
(Dart_FinalizableHandle object, Dart_Handle strong_ref_to_object, \
98+
intptr_t external_allocation_size)) \
99+
/* Dart_Port */ \
100+
F(Dart_Post, bool, (Dart_Port_DL port_id, Dart_Handle object)) \
101+
F(Dart_NewSendPort, Dart_Handle, (Dart_Port_DL port_id)) \
102+
F(Dart_SendPortGetId, Dart_Handle, \
103+
(Dart_Handle port, Dart_Port_DL * port_id)) \
104+
/* Scopes */ \
105+
F(Dart_EnterScope, void, ()) \
106+
F(Dart_ExitScope, void, ())
107+
108+
#define DART_API_ALL_DL_SYMBOLS(F) \
109+
DART_NATIVE_API_DL_SYMBOLS(F) \
110+
DART_API_DL_SYMBOLS(F)
140111
// IMPORTANT! Never update these signatures without properly updating
141112
// DART_API_DL_MAJOR_VERSION and DART_API_DL_MINOR_VERSION.
142113
//
143114
// End of verbatim copy.
115+
// ============================================================================
116+
117+
#define DART_API_DL_DECLARATIONS(name, R, A) \
118+
typedef R(*name##_Type) A; \
119+
DART_EXTERN name##_Type name##_DL;
120+
121+
DART_API_ALL_DL_SYMBOLS(DART_API_DL_DECLARATIONS)
122+
123+
#undef DART_API_DL_DEFINITIONS
124+
125+
#undef DART_EXTERN
144126

145127
#endif /* RUNTIME_INCLUDE_DART_API_DL_H_ */ /* NOLINT */

runtime/include/internal/dart_api_dl_impl.h

Lines changed: 4 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,65 +7,15 @@
77
#ifndef RUNTIME_INCLUDE_INTERNAL_DART_API_DL_IMPL_H_
88
#define RUNTIME_INCLUDE_INTERNAL_DART_API_DL_IMPL_H_
99

10-
// dart_native_api.h symbols can be called on any thread.
11-
#define DART_NATIVE_API_DL_SYMBOLS(F) \
12-
/***** dart_native_api.h *****/ \
13-
/* Dart_Port */ \
14-
F(Dart_PostCObject) \
15-
F(Dart_PostInteger) \
16-
F(Dart_NewNativePort) \
17-
F(Dart_CloseNativePort)
18-
19-
// dart_api.h symbols can only be called on Dart threads.
20-
#define DART_API_DL_SYMBOLS(F) \
21-
/***** dart_api.h *****/ \
22-
/* Errors */ \
23-
F(Dart_IsError) \
24-
F(Dart_IsApiError) \
25-
F(Dart_IsUnhandledExceptionError) \
26-
F(Dart_IsCompilationError) \
27-
F(Dart_IsFatalError) \
28-
F(Dart_GetError) \
29-
F(Dart_ErrorHasException) \
30-
F(Dart_ErrorGetException) \
31-
F(Dart_ErrorGetStackTrace) \
32-
F(Dart_NewApiError) \
33-
F(Dart_NewCompilationError) \
34-
F(Dart_NewUnhandledExceptionError) \
35-
F(Dart_PropagateError) \
36-
/* Dart_Handle, Dart_PersistentHandle, Dart_WeakPersistentHandle */ \
37-
F(Dart_NewPersistentHandle) \
38-
F(Dart_SetPersistentHandle) \
39-
F(Dart_HandleFromPersistent) \
40-
F(Dart_DeletePersistentHandle) \
41-
F(Dart_NewWeakPersistentHandle) \
42-
F(Dart_HandleFromWeakPersistent) \
43-
F(Dart_DeleteWeakPersistentHandle) \
44-
F(Dart_UpdateExternalSize) \
45-
F(Dart_NewFinalizableHandle) \
46-
F(Dart_DeleteFinalizableHandle) \
47-
F(Dart_UpdateFinalizableExternalSize) \
48-
/* Dart_Port */ \
49-
F(Dart_Post) \
50-
F(Dart_NewSendPort) \
51-
F(Dart_SendPortGetId) \
52-
/* Scopes */ \
53-
F(Dart_EnterScope) \
54-
F(Dart_ExitScope)
55-
56-
#define DART_API_ALL_DL_SYMBOLS(F) \
57-
DART_NATIVE_API_DL_SYMBOLS(F) \
58-
DART_API_DL_SYMBOLS(F)
59-
60-
struct DartApiEntry {
10+
typedef struct {
6111
const char* name;
6212
void (*function)();
63-
};
13+
} DartApiEntry;
6414

65-
struct DartApi {
15+
typedef struct {
6616
const int major;
6717
const int minor;
6818
const DartApiEntry* const functions;
69-
};
19+
} DartApi;
7020

7121
#endif /* RUNTIME_INCLUDE_INTERNAL_DART_API_DL_IMPL_H_ */ /* NOLINT */

runtime/lib/ffi.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
#include "include/dart_api.h"
6+
#include "include/dart_api_dl.h"
67
#include "include/dart_native_api.h"
78
#include "include/dart_version.h"
89
#include "include/internal/dart_api_dl_impl.h"
@@ -501,7 +502,7 @@ DEFINE_NATIVE_ENTRY(DartNativeApiFunctionPointer, 0, 1) {
501502
GET_NON_NULL_NATIVE_ARGUMENT(String, name_dart, arguments->NativeArgAt(0));
502503
const char* name = name_dart.ToCString();
503504

504-
#define RETURN_FUNCTION_ADDRESS(function_name) \
505+
#define RETURN_FUNCTION_ADDRESS(function_name, R, A) \
505506
if (strcmp(name, #function_name) == 0) { \
506507
return Integer::New(reinterpret_cast<intptr_t>(function_name)); \
507508
}
@@ -522,7 +523,8 @@ DEFINE_NATIVE_ENTRY(DartApiDLMinorVersion, 0, 0) {
522523
}
523524

524525
static const DartApiEntry dart_api_entries[] = {
525-
#define ENTRY(name) DartApiEntry{#name, reinterpret_cast<void (*)()>(name)},
526+
#define ENTRY(name, R, A) \
527+
DartApiEntry{#name, reinterpret_cast<void (*)()>(name)},
526528
DART_API_ALL_DL_SYMBOLS(ENTRY)
527529
#undef ENTRY
528530
DartApiEntry{nullptr, nullptr}};

0 commit comments

Comments
 (0)