Use a map to look up dispatchers for Dart FFI function names (flutter/engine#34771)

This commit is contained in:
Jason Simmons 2022-07-20 09:30:05 -07:00 committed by GitHub
parent d7267e65ce
commit 51470b497b

View File

@ -4,6 +4,8 @@
#include "flutter/lib/ui/dart_ui.h"
#include <string_view>
#include "flutter/common/settings.h"
#include "flutter/fml/build_config.h"
#include "flutter/lib/ui/compositing/scene.h"
@ -279,26 +281,41 @@ typedef CanvasPath Path;
V(SemanticsUpdateBuilder, updateNode, 36) \
V(SemanticsUpdate, dispose, 1)
#define FFI_FUNCTION_MATCH(FUNCTION, ARGS) \
if (strcmp(name, #FUNCTION) == 0 && args == ARGS) { \
return reinterpret_cast<void*>( \
tonic::FfiDispatcher<void, decltype(&FUNCTION), &FUNCTION>::Call); \
}
#define FFI_FUNCTION_INSERT(FUNCTION, ARGS) \
function_dispatchers.insert(std::make_pair( \
std::string_view(#FUNCTION), \
reinterpret_cast<void*>( \
tonic::FfiDispatcher<void, decltype(&FUNCTION), &FUNCTION>::Call)));
#define FFI_METHOD_MATCH(CLASS, METHOD, ARGS) \
if (strcmp(name, #CLASS "::" #METHOD) == 0 && args == ARGS) { \
return reinterpret_cast<void*>( \
tonic::FfiDispatcher<CLASS, decltype(&CLASS::METHOD), \
&CLASS::METHOD>::Call); \
}
#define FFI_METHOD_INSERT(CLASS, METHOD, ARGS) \
function_dispatchers.insert( \
std::make_pair(std::string_view(#CLASS "::" #METHOD), \
reinterpret_cast<void*>( \
tonic::FfiDispatcher<CLASS, decltype(&CLASS::METHOD), \
&CLASS::METHOD>::Call)));
namespace {
std::unordered_map<std::string_view, void*> function_dispatchers;
void* ResolveFfiNativeFunction(const char* name, uintptr_t args) {
FFI_FUNCTION_LIST(FFI_FUNCTION_MATCH)
FFI_METHOD_LIST(FFI_METHOD_MATCH)
return nullptr;
auto it = function_dispatchers.find(name);
return (it != function_dispatchers.end()) ? it->second : nullptr;
}
void InitDispatcherMap() {
if (!function_dispatchers.empty()) {
return;
}
FFI_FUNCTION_LIST(FFI_FUNCTION_INSERT)
FFI_METHOD_LIST(FFI_METHOD_INSERT)
}
} // anonymous namespace
void DartUI::InitForIsolate(const Settings& settings) {
InitDispatcherMap();
auto dart_ui = Dart_LookupLibrary(ToDart("dart:ui"));
if (Dart_IsError(dart_ui)) {
Dart_PropagateError(dart_ui);