-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRCTNativeModule.mm
239 lines (203 loc) · 7.59 KB
/
RCTNativeModule.mm
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
/*
* 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.
*/
#import "RCTNativeModule.h"
#import <Foundation/Foundation.h>
#import <React/RCTBridge.h>
#import <React/RCTBridgeMethod.h>
#import <React/RCTBridgeModule.h>
#import <React/RCTCxxUtils.h>
#import <React/RCTFollyConvert.h>
#import <React/RCTLog.h>
#import <React/RCTProfile.h>
#import <React/RCTUtils.h>
#import <reactperflogger/BridgeNativeModulePerfLogger.h>
#ifdef WITH_FBSYSTRACE
#include <fbsystrace.h>
#endif
namespace {
enum SchedulingContext { Sync, Async };
}
namespace facebook {
namespace react {
static MethodCallResult invokeInner(
RCTBridge *bridge,
RCTModuleData *moduleData,
unsigned int methodId,
const folly::dynamic ¶ms,
int callId,
SchedulingContext context);
RCTNativeModule::RCTNativeModule(RCTBridge *bridge, RCTModuleData *moduleData)
: m_bridge(bridge), m_moduleData(moduleData)
{
}
std::string RCTNativeModule::getName()
{
return [m_moduleData.name UTF8String];
}
std::string RCTNativeModule::getSyncMethodName(unsigned int methodId)
{
return m_moduleData.methods[methodId].JSMethodName;
}
std::vector<MethodDescriptor> RCTNativeModule::getMethods()
{
std::vector<MethodDescriptor> descs;
for (id<RCTBridgeMethod> method in m_moduleData.methods) {
descs.emplace_back(method.JSMethodName, RCTFunctionDescriptorFromType(method.functionType));
}
return descs;
}
folly::dynamic RCTNativeModule::getConstants()
{
RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, @"[RCTNativeModule getConstants] moduleData.exportedConstants", nil);
NSDictionary *constants = m_moduleData.exportedConstants;
folly::dynamic ret = convertIdToFollyDynamic(constants);
RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"");
return ret;
}
void RCTNativeModule::invoke(unsigned int methodId, folly::dynamic &¶ms, int callId)
{
id<RCTBridgeMethod> method = m_moduleData.methods[methodId];
if (method) {
RCT_PROFILE_BEGIN_EVENT(
RCTProfileTagAlways,
@"[RCTNativeModule invoke]",
@{@"method" : [NSString stringWithUTF8String:method.JSMethodName]});
RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"");
}
const char *moduleName = [m_moduleData.name UTF8String];
const char *methodName = m_moduleData.methods[methodId].JSMethodName;
dispatch_queue_t queue = m_moduleData.methodQueue;
const bool isSyncModule = queue == RCTJSThread;
if (isSyncModule) {
BridgeNativeModulePerfLogger::syncMethodCallStart(moduleName, methodName);
BridgeNativeModulePerfLogger::syncMethodCallArgConversionStart(moduleName, methodName);
} else {
BridgeNativeModulePerfLogger::asyncMethodCallStart(moduleName, methodName);
}
// capture by weak pointer so that we can safely use these variables in a callback
__weak RCTBridge *weakBridge = m_bridge;
__weak RCTModuleData *weakModuleData = m_moduleData;
// The BatchedBridge version of this buckets all the callbacks by thread, and
// queues one block on each. This is much simpler; we'll see how it goes and
// iterate.
dispatch_block_t block = [weakBridge, weakModuleData, methodId, params = std::move(params), callId, isSyncModule] {
#ifdef WITH_FBSYSTRACE
if (callId != -1) {
fbsystrace_end_async_flow(TRACE_TAG_REACT_APPS, "native", callId);
}
#else
(void)(callId);
#endif
@autoreleasepool {
invokeInner(weakBridge, weakModuleData, methodId, std::move(params), callId, isSyncModule ? Sync : Async);
}
};
if (isSyncModule) {
block();
BridgeNativeModulePerfLogger::syncMethodCallReturnConversionEnd(moduleName, methodName);
} else if (queue) {
BridgeNativeModulePerfLogger::asyncMethodCallDispatch(moduleName, methodName);
dispatch_async(queue, block);
}
#ifdef RCT_DEV
if (!queue) {
RCTLog(
@"Attempted to invoke `%u` (method ID) on `%@` (NativeModule name) without a method queue.",
methodId,
m_moduleData.name);
}
#endif
if (isSyncModule) {
BridgeNativeModulePerfLogger::syncMethodCallEnd(moduleName, methodName);
} else {
BridgeNativeModulePerfLogger::asyncMethodCallEnd(moduleName, methodName);
}
}
MethodCallResult RCTNativeModule::callSerializableNativeHook(unsigned int reactMethodId, folly::dynamic &¶ms)
{
return invokeInner(m_bridge, m_moduleData, reactMethodId, params, 0, Sync);
}
static MethodCallResult invokeInner(
RCTBridge *bridge,
RCTModuleData *moduleData,
unsigned int methodId,
const folly::dynamic ¶ms,
int callId,
SchedulingContext context)
{
if (!bridge || !bridge.valid || !moduleData) {
if (context == Sync) {
/**
* NOTE: moduleName and methodName are "". This shouldn't be an issue because there can only be one ongoing sync
* call at a time, and when we call syncMethodCallFail, that one call should terminate. This is also an
* exceptional scenario, so it shouldn't occur often.
*/
BridgeNativeModulePerfLogger::syncMethodCallFail("N/A", "N/A");
}
return std::nullopt;
}
id<RCTBridgeMethod> method = moduleData.methods[methodId];
if (RCT_DEBUG && !method) {
RCTLogError(@"Unknown methodID: %ud for module: %@", methodId, moduleData.name);
}
const char *moduleName = [moduleData.name UTF8String];
const char *methodName = moduleData.methods[methodId].JSMethodName;
if (context == Async) {
BridgeNativeModulePerfLogger::asyncMethodCallExecutionStart(moduleName, methodName, (int32_t)callId);
BridgeNativeModulePerfLogger::asyncMethodCallExecutionArgConversionStart(moduleName, methodName, (int32_t)callId);
}
NSArray *objcParams = convertFollyDynamicToId(params);
if (context == Sync) {
BridgeNativeModulePerfLogger::syncMethodCallArgConversionEnd(moduleName, methodName);
}
RCT_PROFILE_BEGIN_EVENT(
RCTProfileTagAlways,
@"[RCTNativeModule invokeInner]",
@{@"method" : [NSString stringWithUTF8String:method.JSMethodName]});
@try {
if (context == Sync) {
BridgeNativeModulePerfLogger::syncMethodCallExecutionStart(moduleName, methodName);
} else {
BridgeNativeModulePerfLogger::asyncMethodCallExecutionArgConversionEnd(moduleName, methodName, (int32_t)callId);
}
id result = [method invokeWithBridge:bridge module:moduleData.instance arguments:objcParams];
if (context == Sync) {
BridgeNativeModulePerfLogger::syncMethodCallExecutionEnd(moduleName, methodName);
BridgeNativeModulePerfLogger::syncMethodCallReturnConversionStart(moduleName, methodName);
} else {
BridgeNativeModulePerfLogger::asyncMethodCallExecutionEnd(moduleName, methodName, (int32_t)callId);
}
return convertIdToFollyDynamic(result);
} @catch (NSException *exception) {
if (context == Sync) {
BridgeNativeModulePerfLogger::syncMethodCallFail(moduleName, methodName);
} else {
BridgeNativeModulePerfLogger::asyncMethodCallExecutionFail(moduleName, methodName, (int32_t)callId);
}
// Pass on JS exceptions
if ([exception.name hasPrefix:RCTFatalExceptionName]) {
@throw exception;
}
#if RCT_DEBUG
NSString *message = [NSString
stringWithFormat:@"Exception '%@' was thrown while invoking %s on target %@ with params %@\ncallstack: %@",
exception,
method.JSMethodName,
moduleData.name,
objcParams,
exception.callStackSymbols];
RCTFatal(RCTErrorWithMessage(message));
#else
RCTFatalException(exception);
#endif
} @finally {
RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"");
}
return std::nullopt;
}
}
}