1/*
2 * Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
3 * for details. All rights reserved. Use of this source code is governed by a
4 * BSD-style license that can be found in the LICENSE file.
5 */
6
7#include "include/dart_api_dl.h"
8#include "include/dart_version.h"
9#include "include/internal/dart_api_dl_impl.h"
10
11#include <string.h>
12
13#define DART_API_DL_DEFINITIONS(name, R, A) name##_Type name##_DL = NULL;
14
15DART_API_ALL_DL_SYMBOLS(DART_API_DL_DEFINITIONS)
16
17#undef DART_API_DL_DEFINITIONS
18
19typedef void (*DartApiEntry_function)();
20
21DartApiEntry_function FindFunctionPointer(const DartApiEntry* entries,
22 const char* name) {
23 while (entries->name != NULL) {
24 if (strcmp(entries->name, name) == 0) return entries->function;
25 entries++;
26 }
27 return NULL;
28}
29
30intptr_t Dart_InitializeApiDL(void* data) {
31 DartApi* dart_api_data = (DartApi*)data;
32
33 if (dart_api_data->major != DART_API_DL_MAJOR_VERSION) {
34 // If the DartVM we're running on does not have the same version as this
35 // file was compiled against, refuse to initialize. The symbols are not
36 // compatible.
37 return -1;
38 }
39 // Minor versions are allowed to be different.
40 // If the DartVM has a higher minor version, it will provide more symbols
41 // than we initialize here.
42 // If the DartVM has a lower minor version, it will not provide all symbols.
43 // In that case, we leave the missing symbols un-initialized. Those symbols
44 // should not be used by the Dart and native code. The client is responsible
45 // for checking the minor version number himself based on which symbols it
46 // is using.
47 // (If we would error out on this case, recompiling native code against a
48 // newer SDK would break all uses on older SDKs, which is too strict.)
49
50 const DartApiEntry* dart_api_function_pointers = dart_api_data->functions;
51
52#define DART_API_DL_INIT(name, R, A) \
53 name##_DL = \
54 (name##_Type)(FindFunctionPointer(dart_api_function_pointers, #name));
55 DART_API_ALL_DL_SYMBOLS(DART_API_DL_INIT)
56#undef DART_API_DL_INIT
57
58 return 0;
59}
60