forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_dart_native_resolver.cc
127 lines (102 loc) · 3.95 KB
/
test_dart_native_resolver.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/testing/test_dart_native_resolver.h"
#include <mutex>
#include <vector>
#include "flutter/fml/logging.h"
#include "third_party/tonic/logging/dart_error.h"
#include "tonic/converter/dart_converter.h"
namespace flutter {
namespace testing {
TestDartNativeResolver::TestDartNativeResolver() = default;
TestDartNativeResolver::~TestDartNativeResolver() = default;
void TestDartNativeResolver::AddNativeCallback(std::string name,
Dart_NativeFunction callback) {
native_callbacks_[name] = callback;
}
void TestDartNativeResolver::AddFfiNativeCallback(std::string name,
void* callback_ptr) {
ffi_native_callbacks_[name] = callback_ptr;
}
Dart_NativeFunction TestDartNativeResolver::ResolveCallback(
std::string name) const {
auto found = native_callbacks_.find(name);
if (found == native_callbacks_.end()) {
return nullptr;
}
return found->second;
}
void* TestDartNativeResolver::ResolveFfiCallback(std::string name) const {
auto found = ffi_native_callbacks_.find(name);
if (found == ffi_native_callbacks_.end()) {
return nullptr;
}
return found->second;
}
static std::mutex gIsolateResolversMutex;
static std::map<Dart_Isolate, std::weak_ptr<TestDartNativeResolver>>
gIsolateResolvers;
Dart_NativeFunction TestDartNativeResolver::DartNativeEntryResolverCallback(
Dart_Handle dart_name,
int num_of_arguments,
bool* auto_setup_scope) {
auto name = tonic::StdStringFromDart(dart_name);
std::scoped_lock lock(gIsolateResolversMutex);
auto found = gIsolateResolvers.find(Dart_CurrentIsolate());
if (found == gIsolateResolvers.end()) {
FML_LOG(ERROR) << "Could not resolve native method for :" << name;
return nullptr;
}
if (auto resolver = found->second.lock()) {
return resolver->ResolveCallback(std::move(name));
} else {
gIsolateResolvers.erase(found);
}
FML_LOG(ERROR) << "Could not resolve native method for :" << name;
return nullptr;
}
static const uint8_t* DartNativeEntrySymbolCallback(
Dart_NativeFunction function) {
return reinterpret_cast<const uint8_t*>("¯\\_(ツ)_/¯");
}
void* TestDartNativeResolver::FfiNativeResolver(const char* name,
uintptr_t args_n) {
std::scoped_lock lock(gIsolateResolversMutex);
auto found = gIsolateResolvers.find(Dart_CurrentIsolate());
if (found == gIsolateResolvers.end()) {
FML_LOG(ERROR) << "Could not resolve native method for :" << name;
return nullptr;
}
if (auto resolver = found->second.lock()) {
return resolver->ResolveFfiCallback(name);
} else {
gIsolateResolvers.erase(found);
}
FML_LOG(ERROR) << "Could not resolve native method for :" << name;
return nullptr;
}
void TestDartNativeResolver::SetNativeResolverForIsolate() {
FML_CHECK(!Dart_IsError(Dart_RootLibrary()));
auto result = Dart_SetNativeResolver(Dart_RootLibrary(),
DartNativeEntryResolverCallback,
DartNativeEntrySymbolCallback);
FML_CHECK(!tonic::LogIfError(result))
<< "Could not set native resolver in test.";
result = Dart_SetFfiNativeResolver(Dart_RootLibrary(), &FfiNativeResolver);
FML_CHECK(!tonic::LogIfError(result))
<< "Could not set FFI native resolver in test.";
std::scoped_lock lock(gIsolateResolversMutex);
gIsolateResolvers[Dart_CurrentIsolate()] = shared_from_this();
std::vector<Dart_Isolate> isolates_with_dead_resolvers;
for (const auto& entry : gIsolateResolvers) {
if (!entry.second.lock()) {
isolates_with_dead_resolvers.push_back(entry.first);
}
}
for (const auto& dead_isolate : isolates_with_dead_resolvers) {
gIsolateResolvers.erase(dead_isolate);
}
}
} // namespace testing
} // namespace flutter