mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
eb2a561ecb
Summary: ## Motivation This rename will fix the following CircleCI build failures: - [test_ios_unit_frameworks](https://circleci.com/gh/facebook/react-native/150473?utm_campaign=vcs-integration-link&utm_medium=referral&utm_source=github-build-link) - [test_ios_detox_frameworks](https://circleci.com/gh/facebook/react-native/150474?utm_campaign=vcs-integration-link&utm_medium=referral&utm_source=github-build-link) ## Investigation We have 4 podspec targets that map to the same header namespace (i.e: `header_dir`) `ReactCommon`: - **New:** `React-perflogger`: Directory is `ReactCommon/preflogger`, and contains `NativeModulePerfLogger.{h,cpp}`. - `React-runtimeexecutor`: Directory is `ReactCommon/runtimeexecutor`, and contains only `RuntimeExecutor.h` - `React-callinvoker`: Directory is `ReactCommon/callinvoker`, and contains only `CallInvoker.h` - `ReactCommon/turbomodule/core`: Directory is `ReactCommon/turbomodule`, and contains C++ files, as well has header files. **The problem:** We couldn't import headers from `React-perflogger` in `ReactCommon/turbomodule/core` files. **The cause:** I'm not entirely sure why, but I was able to discern the following two rules by playing around with the podspecs: 1. If your podspec target has a cpp file, it'll generate a framework when `USE_FRAMEWORKS=1`. 2. Two different frameworks cannot map to the same `module_name` or `header_dir`. (Why? No clue. But something breaks silently when this is the case). So, this is what happened when I landed `React-perflogger` (D21443610): 1. The TurboModules code generates the `ReactCommon` framework that uses the `ReactCommon` header namespace. 2. `React-runtimeexecutor` and `React-callinvoker` also used the `ReactCommon` header namespace. However, neither generate a framework because of Rule 1. 3. When I comitted `React-perflogger`, I introduced a second framework that competed with the `ReactCommon` framework (i.e: TurboModules code) for the `ReactCommon` header namespace. Rule 2 violation. ## Thoughts on renaming - `<perflogger/NativeModulePerfLogger.h>` is too generic, and the `perflogger` namepsace is used internally within FB. - `<react/perflogger/NativeModulePerfLogger.h>` matches our fabric header format, but I'm pretty sure that slashes aren't allowed in `header_dir`: I tested this and it didn't work. IIRC, only alphanumeric and underscore are valid characters for `header_dir` or `module_name`. So, I opted to just use `reactperflogger`. Changelog: [Internal] Reviewed By: fkgozali Differential Revision: D21598852 fbshipit-source-id: 60da5d0f7758eaf13907a080b7d8756688f40723
224 lines
7.1 KiB
Plaintext
224 lines
7.1 KiB
Plaintext
/*
|
|
* Copyright (c) Facebook, Inc. and its 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 <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/NativeModulePerfLogger.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)
|
|
{
|
|
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) {
|
|
NativeModulePerfLogger::getInstance().syncMethodCallStart(moduleName, methodName);
|
|
NativeModulePerfLogger::getInstance().syncMethodCallArgConversionStart(moduleName, methodName);
|
|
} else {
|
|
NativeModulePerfLogger::getInstance().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
|
|
invokeInner(weakBridge, weakModuleData, methodId, std::move(params), callId, isSyncModule ? Sync : Async);
|
|
};
|
|
|
|
if (isSyncModule) {
|
|
block();
|
|
NativeModulePerfLogger::getInstance().syncMethodCallReturnConversionEnd(moduleName, methodName);
|
|
} else if (queue) {
|
|
NativeModulePerfLogger::getInstance().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) {
|
|
NativeModulePerfLogger::getInstance().syncMethodCallEnd(moduleName, methodName);
|
|
} else {
|
|
NativeModulePerfLogger::getInstance().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.
|
|
*/
|
|
NativeModulePerfLogger::getInstance().syncMethodCallFail("N/A", "N/A");
|
|
}
|
|
return folly::none;
|
|
}
|
|
|
|
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) {
|
|
NativeModulePerfLogger::getInstance().asyncMethodCallExecutionStart(moduleName, methodName, (int32_t)callId);
|
|
NativeModulePerfLogger::getInstance().asyncMethodCallExecutionArgConversionStart(
|
|
moduleName, methodName, (int32_t)callId);
|
|
}
|
|
|
|
NSArray *objcParams = convertFollyDynamicToId(params);
|
|
|
|
if (context == Sync) {
|
|
NativeModulePerfLogger::getInstance().syncMethodCallArgConversionEnd(moduleName, methodName);
|
|
}
|
|
|
|
@try {
|
|
if (context == Sync) {
|
|
NativeModulePerfLogger::getInstance().syncMethodCallExecutionStart(moduleName, methodName);
|
|
} else {
|
|
NativeModulePerfLogger::getInstance().asyncMethodCallExecutionArgConversionEnd(
|
|
moduleName, methodName, (int32_t)callId);
|
|
}
|
|
|
|
id result = [method invokeWithBridge:bridge module:moduleData.instance arguments:objcParams];
|
|
|
|
if (context == Sync) {
|
|
NativeModulePerfLogger::getInstance().syncMethodCallExecutionEnd(moduleName, methodName);
|
|
NativeModulePerfLogger::getInstance().syncMethodCallReturnConversionStart(moduleName, methodName);
|
|
} else {
|
|
NativeModulePerfLogger::getInstance().asyncMethodCallExecutionEnd(moduleName, methodName, (int32_t)callId);
|
|
}
|
|
|
|
return convertIdToFollyDynamic(result);
|
|
} @catch (NSException *exception) {
|
|
if (context == Sync) {
|
|
NativeModulePerfLogger::getInstance().syncMethodCallFail(moduleName, methodName);
|
|
} else {
|
|
NativeModulePerfLogger::getInstance().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
|
|
}
|
|
|
|
return folly::none;
|
|
}
|
|
|
|
}
|
|
}
|