Repository navigation
Expand file tree
/
Copy pathTurboModuleManager.cpp
More file actions
353 lines (304 loc) · 12.4 KB
/
Copy pathTurboModuleManager.cpp
File metadata and controls
353 lines (304 loc) · 12.4 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "TurboModuleManager.h"
#include <memory>
#include <stdexcept>
#include <string>
#include <fbjni/fbjni.h>
#include <jsi/jsi.h>
#include <ReactCommon/BindingsInstallerHolder.h>
#include <ReactCommon/CxxTurboModuleUtils.h>
#include <ReactCommon/JavaInteropTurboModule.h>
#include <ReactCommon/TurboModuleBinding.h>
#include <ReactCommon/TurboModulePerfLogger.h>
#include <react/featureflags/ReactNativeFeatureFlags.h>
#include <react/jni/CxxModuleWrapperBase.h>
namespace facebook::react {
#ifndef RCT_REMOVE_LEGACY_MODULE_INTEROP
namespace {
class JMethodDescriptor : public jni::JavaClass<JMethodDescriptor> {
public:
static auto constexpr kJavaDescriptor =
"Lcom/facebook/react/internal/turbomodule/core/TurboModuleInteropUtils$MethodDescriptor;";
JavaInteropTurboModule::MethodDescriptor toMethodDescriptor() {
return JavaInteropTurboModule::MethodDescriptor{
.methodName = getMethodName(),
.jniSignature = getJNISignature(),
.jsiReturnKind = getJSIReturnKind(),
.jsArgCount = getJSArgCount(),
};
}
private:
std::string getMethodName() {
static const auto field =
javaClassStatic()->getField<jstring>("methodName");
return getFieldValue(field)->toStdString();
}
std::string getJNISignature() {
static const auto field =
javaClassStatic()->getField<jstring>("jniSignature");
return getFieldValue(field)->toStdString();
}
TurboModuleMethodValueKind getJSIReturnKind() {
static const auto field =
javaClassStatic()->getField<jstring>("jsiReturnKind");
const std::string jsiReturnKind = getFieldValue(field)->toStdString();
if (jsiReturnKind == "VoidKind") {
return VoidKind;
}
if (jsiReturnKind == "BooleanKind") {
return BooleanKind;
}
if (jsiReturnKind == "NumberKind") {
return NumberKind;
}
if (jsiReturnKind == "StringKind") {
return StringKind;
}
if (jsiReturnKind == "ObjectKind") {
return ObjectKind;
}
if (jsiReturnKind == "ArrayKind") {
return ArrayKind;
}
if (jsiReturnKind == "FunctionKind") {
return FunctionKind;
}
if (jsiReturnKind == "PromiseKind") {
return PromiseKind;
}
throw new std::runtime_error(
std::string("Failed to convert jsiReturnKind \"") + jsiReturnKind +
"\" to TurboModuleMethodValueKind");
}
int getJSArgCount() {
static const auto field = javaClassStatic()->getField<int>("jsArgCount");
return getFieldValue(field);
}
};
} // namespace
#endif // RCT_REMOVE_LEGACY_MODULE_INTEROP
TurboModuleManager::TurboModuleManager(
std::shared_ptr<CallInvoker> jsCallInvoker,
std::shared_ptr<NativeMethodCallInvoker> nativeMethodCallInvoker,
jni::alias_ref<TurboModuleManagerDelegate::javaobject> delegate)
: jsCallInvoker_(std::move(jsCallInvoker)),
nativeMethodCallInvoker_(std::move(nativeMethodCallInvoker)),
delegate_(jni::make_global(delegate)) {}
jni::local_ref<TurboModuleManager::jhybriddata> TurboModuleManager::initHybrid(
jni::alias_ref<jhybridobject> /* unused */,
jni::alias_ref<CallInvokerHolder::javaobject> jsCallInvokerHolder,
jni::alias_ref<NativeMethodCallInvokerHolder::javaobject>
nativeMethodCallInvokerHolder,
jni::alias_ref<TurboModuleManagerDelegate::javaobject> delegate) {
return makeCxxInstance(
jsCallInvokerHolder->cthis()->getCallInvoker(),
nativeMethodCallInvokerHolder->cthis()->getNativeMethodCallInvoker(),
delegate);
}
void TurboModuleManager::registerNatives() {
registerHybrid({
makeNativeMethod("initHybrid", TurboModuleManager::initHybrid),
makeNativeMethod(
"dispatchJSBindingInstall",
TurboModuleManager::dispatchJSBindingInstall),
});
}
TurboModuleProviderFunctionTypeWithRuntime
TurboModuleManager::createTurboModuleProvider(
jni::alias_ref<jhybridobject> javaPart) {
return [weakJavaPart = jni::make_weak(javaPart)](
jsi::Runtime& runtime,
const std::string& name) -> std::shared_ptr<TurboModule> {
auto javaPart = weakJavaPart.lockLocal();
if (!javaPart) {
return nullptr;
}
auto cxxPart = javaPart->cthis();
if (cxxPart == nullptr) {
return nullptr;
}
return cxxPart->getTurboModule(javaPart, name, runtime);
};
}
std::shared_ptr<TurboModule> TurboModuleManager::getTurboModule(
jni::alias_ref<jhybridobject> javaPart,
const std::string& name,
jsi::Runtime& runtime) {
const char* moduleName = name.c_str();
TurboModulePerfLogger::moduleJSRequireBeginningStart(moduleName);
auto turboModuleLookup = turboModuleCache_.find(name);
if (turboModuleLookup != turboModuleCache_.end()) {
TurboModulePerfLogger::moduleJSRequireBeginningCacheHit(moduleName);
TurboModulePerfLogger::moduleJSRequireBeginningEnd(moduleName);
return turboModuleLookup->second;
}
TurboModulePerfLogger::moduleJSRequireBeginningEnd(moduleName);
auto cxxDelegate = delegate_->cthis();
auto cxxModule = cxxDelegate->getTurboModule(name, jsCallInvoker_);
if (cxxModule) {
turboModuleCache_.insert({name, cxxModule});
return cxxModule;
}
auto& cxxTurboModuleMapProvider = globalExportedCxxTurboModuleMap();
auto it = cxxTurboModuleMapProvider.find(name);
if (it != cxxTurboModuleMapProvider.end()) {
auto turboModule = it->second(jsCallInvoker_);
turboModuleCache_.insert({name, turboModule});
return turboModule;
}
// TODO(T248203434): Remove this workaround once fixed in fbjni
// NOTE: We use jstring instead of std::string for the method signature to
// work around a bug in fbjni's exception handling. When a Java method throws
// an exception, fbjni's JMethod::operator() needs to check for pending
// exceptions via FACEBOOK_JNI_THROW_PENDING_EXCEPTION(). However, if we pass
// std::string, fbjni creates a temporary local_ref<JString> for the argument.
// C++ destroys temporaries at the end of the full-expression, which happens
// AFTER the JNI call returns but BEFORE the exception check. The destructor
// calls JNI functions (GetObjectRefType) while there's a pending exception,
// which violates JNI rules and causes ART's CheckJNI to abort the process.
//
// By pre-converting to jstring here, we control the lifetime of the
// local_ref<JString> so it extends past the exception check.
static auto getTurboJavaModule =
javaPart->getClass()->getMethod<jni::alias_ref<JTurboModule>(jstring)>(
"getTurboJavaModule");
auto jname = jni::make_jstring(name);
auto moduleInstance = getTurboJavaModule(javaPart.get(), jname.get());
if (moduleInstance) {
TurboModulePerfLogger::moduleJSRequireEndingStart(moduleName);
JavaTurboModule::InitParams params = {
.moduleName = name,
.instance = moduleInstance,
.jsInvoker = jsCallInvoker_,
.nativeMethodCallInvoker = nativeMethodCallInvoker_};
auto turboModule = cxxDelegate->getTurboModule(name, params);
if (moduleInstance->isInstanceOf(
JTurboModuleWithJSIBindings::javaClassStatic())) {
static auto getBindingsInstaller =
JTurboModuleWithJSIBindings::javaClassStatic()
->getMethod<BindingsInstallerHolder::javaobject()>(
"getBindingsInstaller");
auto installer = getBindingsInstaller(moduleInstance);
if (installer) {
installer->cthis()->installBindings(runtime, jsCallInvoker_);
}
}
turboModuleCache_.insert({name, turboModule});
TurboModulePerfLogger::moduleJSRequireEndingEnd(moduleName);
return turboModule;
}
return nullptr;
}
#ifndef RCT_REMOVE_LEGACY_MODULE_INTEROP
TurboModuleProviderFunctionTypeWithRuntime
TurboModuleManager::createLegacyModuleProvider(
jni::alias_ref<jhybridobject> javaPart) {
bool shouldCreateLegacyModules =
ReactNativeFeatureFlags::enableBridgelessArchitecture() &&
ReactNativeFeatureFlags::useTurboModuleInterop();
if (!shouldCreateLegacyModules) {
return nullptr;
}
return [weakJavaPart = jni::make_weak(javaPart)](
jsi::Runtime& /*runtime*/,
const std::string& name) -> std::shared_ptr<TurboModule> {
auto javaPart = weakJavaPart.lockLocal();
if (!javaPart) {
return nullptr;
}
auto cxxPart = javaPart->cthis();
if (cxxPart == nullptr) {
return nullptr;
}
return cxxPart->getLegacyModule(javaPart, name);
};
}
std::shared_ptr<TurboModule> TurboModuleManager::getLegacyModule(
jni::alias_ref<jhybridobject> javaPart,
const std::string& name) {
const char* moduleName = name.c_str();
TurboModulePerfLogger::moduleJSRequireBeginningStart(moduleName);
auto legacyModuleLookup = legacyModuleCache_.find(name);
if (legacyModuleLookup != legacyModuleCache_.end()) {
TurboModulePerfLogger::moduleJSRequireBeginningCacheHit(moduleName);
TurboModulePerfLogger::moduleJSRequireBeginningEnd(moduleName);
return legacyModuleLookup->second;
}
TurboModulePerfLogger::moduleJSRequireBeginningEnd(moduleName);
// TODO(T248203434): Remove this workaround once fixed in fbjni
// NOTE: We use jstring instead of std::string for the method signature to
// work around a bug in fbjni's exception handling. When a Java method throws
// an exception, fbjni's JMethod::operator() needs to check for pending
// exceptions via FACEBOOK_JNI_THROW_PENDING_EXCEPTION(). However, if we pass
// std::string, fbjni creates a temporary local_ref<JString> for the argument.
// C++ destroys temporaries at the end of the full-expression, which happens
// AFTER the JNI call returns but BEFORE the exception check. The destructor
// calls JNI functions (GetObjectRefType) while there's a pending exception,
// which violates JNI rules and causes ART's CheckJNI to abort the process.
//
// By pre-converting to jstring here, we control the lifetime of the
// local_ref<JString> so it extends past the exception check.
static auto getLegacyJavaModule =
javaPart->getClass()->getMethod<jni::alias_ref<JNativeModule>(jstring)>(
"getLegacyJavaModule");
auto jname = jni::make_jstring(name);
auto moduleInstance = getLegacyJavaModule(javaPart.get(), jname.get());
if (moduleInstance) {
TurboModulePerfLogger::moduleJSRequireEndingStart(moduleName);
JavaTurboModule::InitParams params = {
.moduleName = name,
.instance = moduleInstance,
.jsInvoker = jsCallInvoker_,
.nativeMethodCallInvoker = nativeMethodCallInvoker_};
static auto getMethodDescriptorsFromModule =
javaPart->getClass()
->getStaticMethod<jni::alias_ref<
jni::JList<JMethodDescriptor::javaobject>::javaobject>(
jni::alias_ref<JNativeModule>)>(
"getMethodDescriptorsFromModule");
auto javaMethodDescriptors =
getMethodDescriptorsFromModule(javaPart->getClass(), moduleInstance);
std::vector<JavaInteropTurboModule::MethodDescriptor> methodDescriptors;
for (jni::alias_ref<JMethodDescriptor> javaMethodDescriptor :
*javaMethodDescriptors) {
methodDescriptors.push_back(javaMethodDescriptor->toMethodDescriptor());
}
auto turboModule =
std::make_shared<JavaInteropTurboModule>(params, methodDescriptors);
legacyModuleCache_.insert({name, turboModule});
TurboModulePerfLogger::moduleJSRequireEndingEnd(moduleName);
return turboModule;
}
return nullptr;
}
#endif // RCT_REMOVE_LEGACY_MODULE_INTEROP
void TurboModuleManager::installJSBindings(
jsi::Runtime& runtime,
jni::alias_ref<jhybridobject> javaPart) {
#ifndef RCT_REMOVE_LEGACY_MODULE_INTEROP
TurboModuleBinding::install(
runtime,
createTurboModuleProvider(javaPart),
createLegacyModuleProvider(javaPart));
#else
TurboModuleBinding::install(runtime, createTurboModuleProvider(javaPart));
#endif // RCT_REMOVE_LEGACY_MODULE_INTEROP
}
void TurboModuleManager::dispatchJSBindingInstall(
jni::alias_ref<jhybridobject> javaPart,
jni::alias_ref<JRuntimeExecutor::javaobject> runtimeExecutor) {
auto cxxPart = javaPart->cthis();
if (cxxPart == nullptr || !cxxPart->jsCallInvoker_) {
return; // Runtime doesn't exist when attached to Chrome debugger.
}
runtimeExecutor->cthis()->get()(
[javaPart = jni::make_global(javaPart)](jsi::Runtime& runtime) {
installJSBindings(runtime, javaPart);
});
}
} // namespace facebook::react