7eac9f355e
This CL introduces dart_api_dl.h which exposes a subset of dart_api.h and dart_native_api.h for dynamic linking at runtime through the FFI. Dynamic linking is done through including dart_api_dl.cc in a shared library and passing NativeApi.initializeApiDLData to the init function. This CL also includes Native API versioning to deal with possible version skew between native api version against which native libraries are compiled and the version in the DartVM the code is run on. The subset of symbols in the CL includes handle related symbols, error related symbols, handle scope symbols, and native port sumbols. Design: http://go/dart-ffi-expose-dart-api Closes: https://github.com/dart-lang/sdk/issues/40607 Closes: https://github.com/dart-lang/sdk/issues/36858 Closes: https://github.com/dart-lang/sdk/issues/41319 Closes: https://github.com/flutter/flutter/issues/46887 Closes: https://github.com/flutter/flutter/issues/47061 Misc: Closes: https://github.com/dart-lang/sdk/issues/42260 Change-Id: I9e557808dbc99b341f23964cbddbb05f26d7a6c5 Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-kernel-precomp-linux-debug-x64-try,vm-dartkb-linux-release-x64-abi-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-msan-linux-release-x64-try,vm-kernel-precomp-msan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,dart-sdk-linux-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,front-end-linux-release-x64-try,vm-kernel-precomp-win-release-x64-try,vm-kernel-mac-debug-x64-try,vm-precomp-ffi-qemu-linux-release-arm-try,vm-kernel-nnbd-linux-debug-x64-try,analyzer-nnbd-linux-release-try,front-end-nnbd-linux-release-x64-try Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/145592 Commit-Queue: Daco Harkes <dacoharkes@google.com> Reviewed-by: Martin Kustermann <kustermann@google.com> Reviewed-by: Alexander Markov <alexmarkov@google.com>
60 lines
2.3 KiB
C++
60 lines
2.3 KiB
C++
/*
|
|
* Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
|
|
* for details. All rights reserved. Use of this source code is governed by a
|
|
* BSD-style license that can be found in the LICENSE file.
|
|
*/
|
|
|
|
#include "include/dart_api_dl.h"
|
|
#include "include/dart_version.h"
|
|
#include "include/internal/dart_api_dl_impl.h"
|
|
|
|
#include <string.h>
|
|
|
|
#define DART_API_DL_DEFINITIONS(name) \
|
|
using name##Type = decltype(&name); \
|
|
name##Type name##_DL = nullptr;
|
|
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) {
|
|
if (strcmp(entries->name, name) == 0) return entries->function;
|
|
entries++;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
intptr_t Dart_InitializeApiDL(void* data) {
|
|
DartApi* dart_api_data = reinterpret_cast<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
|
|
// file was compiled against, refuse to initialize. The symbols are not
|
|
// compatible.
|
|
return -1;
|
|
}
|
|
// Minor versions are allowed to be different.
|
|
// If the DartVM has a higher minor version, it will provide more symbols
|
|
// than we initialize here.
|
|
// If the DartVM has a lower minor version, it will not provide all symbols.
|
|
// In that case, we leave the missing symbols un-initialized. Those symbols
|
|
// should not be used by the Dart and native code. The client is responsible
|
|
// for checking the minor version number himself based on which symbols it
|
|
// is using.
|
|
// (If we would error out on this case, recompiling native code against a
|
|
// newer SDK would break all uses on older SDKs, which is too strict.)
|
|
|
|
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));
|
|
DART_API_ALL_DL_SYMBOLS(DART_API_DL_INIT)
|
|
#undef DART_API_DL_INIT
|
|
|
|
return 0;
|
|
}
|