Skip to content

Commit

Permalink
Use a map to look up dispatchers for Dart FFI function names (flutter…
Browse files Browse the repository at this point in the history
  • Loading branch information
jason-simmons authored Jul 20, 2022
1 parent 16fe58d commit f353726
Showing 1 changed file with 31 additions and 14 deletions.
45 changes: 31 additions & 14 deletions lib/ui/dart_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit f353726

Please sign in to comment.