2017-02-01 22:10:41 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "RCTNativeModule.h"
|
|
|
|
|
|
|
|
#import <React/RCTBridge.h>
|
|
|
|
#import <React/RCTBridgeMethod.h>
|
|
|
|
#import <React/RCTBridgeModule.h>
|
|
|
|
#import <React/RCTCxxUtils.h>
|
2017-02-16 14:45:40 +00:00
|
|
|
#import <React/RCTFollyConvert.h>
|
2017-07-10 12:21:03 +00:00
|
|
|
#import <React/RCTLog.h>
|
2017-02-01 22:10:41 +00:00
|
|
|
#import <React/RCTProfile.h>
|
|
|
|
#import <React/RCTUtils.h>
|
|
|
|
|
2017-05-13 00:57:12 +00:00
|
|
|
#ifdef WITH_FBSYSTRACE
|
|
|
|
#include <fbsystrace.h>
|
|
|
|
#endif
|
|
|
|
|
2017-02-01 22:10:41 +00:00
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
|
|
|
|
|
|
|
RCTNativeModule::RCTNativeModule(RCTBridge *bridge, RCTModuleData *moduleData)
|
|
|
|
: m_bridge(bridge)
|
|
|
|
, m_moduleData(moduleData) {}
|
|
|
|
|
|
|
|
std::string RCTNativeModule::getName() {
|
|
|
|
return [m_moduleData.name UTF8String];
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<MethodDescriptor> RCTNativeModule::getMethods() {
|
|
|
|
std::vector<MethodDescriptor> descs;
|
|
|
|
|
|
|
|
for (id<RCTBridgeMethod> method in m_moduleData.methods) {
|
|
|
|
descs.emplace_back(
|
2017-07-24 13:46:01 +00:00
|
|
|
method.JSMethodName,
|
2017-04-27 18:49:49 +00:00
|
|
|
RCTFunctionDescriptorFromType(method.functionType)
|
2017-02-01 22:10:41 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return descs;
|
|
|
|
}
|
|
|
|
|
|
|
|
folly::dynamic RCTNativeModule::getConstants() {
|
|
|
|
RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways,
|
2017-04-08 22:14:38 +00:00
|
|
|
@"[RCTNativeModule getConstants] moduleData.exportedConstants", nil);
|
|
|
|
NSDictionary *constants = m_moduleData.exportedConstants;
|
2017-07-10 12:21:03 +00:00
|
|
|
folly::dynamic ret = convertIdToFollyDynamic(constants);
|
2017-02-01 22:10:41 +00:00
|
|
|
RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-05-13 00:57:12 +00:00
|
|
|
void RCTNativeModule::invoke(unsigned int methodId, folly::dynamic &¶ms, int callId) {
|
2017-02-01 22:10:41 +00:00
|
|
|
// 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.
|
2017-05-13 00:57:12 +00:00
|
|
|
dispatch_block_t block = [this, methodId, params=std::move(params), callId] {
|
|
|
|
#ifdef WITH_FBSYSTRACE
|
|
|
|
if (callId != -1) {
|
|
|
|
fbsystrace_end_async_flow(TRACE_TAG_REACT_APPS, "native", callId);
|
2017-02-01 22:10:41 +00:00
|
|
|
}
|
2017-05-13 00:57:12 +00:00
|
|
|
#endif
|
2017-04-27 18:49:49 +00:00
|
|
|
invokeInner(methodId, std::move(params));
|
2017-02-01 22:10:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
dispatch_queue_t queue = m_moduleData.methodQueue;
|
|
|
|
if (queue == RCTJSThread) {
|
|
|
|
block();
|
|
|
|
} else if (queue) {
|
|
|
|
dispatch_async(queue, block);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-25 12:29:45 +00:00
|
|
|
MethodCallResult RCTNativeModule::callSerializableNativeHook(unsigned int reactMethodId, folly::dynamic &¶ms) {
|
2017-09-26 03:18:05 +00:00
|
|
|
return invokeInner(reactMethodId, params);
|
2017-02-01 22:10:41 +00:00
|
|
|
}
|
|
|
|
|
2017-09-26 03:18:05 +00:00
|
|
|
MethodCallResult RCTNativeModule::invokeInner(unsigned int methodId, const folly::dynamic ¶ms) {
|
2017-05-13 00:57:12 +00:00
|
|
|
if (!m_bridge.valid) {
|
|
|
|
return folly::none;
|
|
|
|
}
|
|
|
|
|
2017-04-27 18:49:49 +00:00
|
|
|
id<RCTBridgeMethod> method = m_moduleData.methods[methodId];
|
|
|
|
if (RCT_DEBUG && !method) {
|
|
|
|
RCTLogError(@"Unknown methodID: %ud for module: %@",
|
|
|
|
methodId, m_moduleData.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
NSArray *objcParams = convertFollyDynamicToId(params);
|
|
|
|
@try {
|
|
|
|
id result = [method invokeWithBridge:m_bridge module:m_moduleData.instance arguments:objcParams];
|
|
|
|
return convertIdToFollyDynamic(result);
|
|
|
|
}
|
|
|
|
@catch (NSException *exception) {
|
|
|
|
// Pass on JS exceptions
|
|
|
|
if ([exception.name hasPrefix:RCTFatalExceptionName]) {
|
|
|
|
@throw exception;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSString *message = [NSString stringWithFormat:
|
2017-09-27 16:52:20 +00:00
|
|
|
@"Exception '%@' was thrown while invoking %s on target %@ with params %@\ncallstack: %@",
|
|
|
|
exception, method.JSMethodName, m_moduleData.name, objcParams, exception.callStackSymbols];
|
2017-04-27 18:49:49 +00:00
|
|
|
RCTFatal(RCTErrorWithMessage(message));
|
|
|
|
}
|
|
|
|
}
|
2017-02-01 22:10:41 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|