This commit is contained in:
Yamozha
2021-04-02 02:24:13 +03:00
parent c23950b545
commit 7256d79e2c
31493 changed files with 3036630 additions and 0 deletions

View File

@ -0,0 +1,47 @@
/*
* 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.
*/
#include <glog/logging.h>
#include <React/RCTLog.h>
#include <cxxreact/MessageQueueThread.h>
namespace facebook {
namespace react {
// RCTNativeModule arranges for native methods to be invoked on a queue which
// is not the JS thread. C++ modules don't use RCTNativeModule, so this little
// adapter does the work.
class DispatchMessageQueueThread : public MessageQueueThread {
public:
DispatchMessageQueueThread(RCTModuleData *moduleData) : moduleData_(moduleData) {}
void runOnQueue(std::function<void()> &&func) override
{
dispatch_queue_t queue = moduleData_.methodQueue;
dispatch_block_t block = [func = std::move(func)] { func(); };
RCTAssert(block != nullptr, @"Invalid block generated in call to %@", moduleData_);
if (queue && block) {
dispatch_async(queue, block);
}
}
void runOnQueueSync(std::function<void()> &&__unused func) override
{
LOG(FATAL) << "Unsupported operation";
}
void quitSynchronous() override
{
LOG(FATAL) << "Unsupported operation";
}
private:
RCTModuleData *moduleData_;
};
}
}

View File

@ -0,0 +1,17 @@
/*
* 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 <Foundation/Foundation.h>
#import <React/RCTBridgeMethod.h>
#import <cxxreact/CxxModule.h>
@interface RCTCxxMethod : NSObject <RCTBridgeMethod>
- (instancetype)initWithCxxMethod:(const facebook::xplat::module::CxxModule::Method &)cxxMethod;
@end

View File

@ -0,0 +1,139 @@
/*
* 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 "RCTCxxMethod.h"
#import <React/RCTBridge+Private.h>
#import <React/RCTBridge.h>
#import <React/RCTConvert.h>
#import <React/RCTFollyConvert.h>
#import <cxxreact/JsArgumentHelpers.h>
#import "RCTCxxUtils.h"
#import <memory>
using facebook::xplat::module::CxxModule;
using namespace facebook::react;
@implementation RCTCxxMethod {
std::unique_ptr<CxxModule::Method> _method;
}
- (instancetype)initWithCxxMethod:(const CxxModule::Method &)method
{
if ((self = [super init])) {
_method = std::make_unique<CxxModule::Method>(method);
}
return self;
}
- (const char *)JSMethodName
{
return _method->name.c_str();
}
- (RCTFunctionType)functionType
{
std::string type(_method->getType());
if (type == "sync") {
return RCTFunctionTypeSync;
} else if (type == "async") {
return RCTFunctionTypeNormal;
} else {
return RCTFunctionTypePromise;
}
}
- (id)invokeWithBridge:(RCTBridge *)bridge module:(id)module arguments:(NSArray *)arguments
{
// module is unused except for printing errors. The C++ object it represents
// is also baked into _method.
// the last N arguments are callbacks, according to the Method data. The
// preceding arguments are values which have already been parsed from JS: they
// may be NSNumber (bool, int, double), NSString, NSArray, or NSObject.
CxxModule::Callback first;
CxxModule::Callback second;
if (arguments.count < _method->callbacks) {
RCTLogError(
@"Method %@.%s expects at least %zu arguments, but got %tu",
RCTBridgeModuleNameForClass([module class]),
_method->name.c_str(),
_method->callbacks,
arguments.count);
return nil;
}
if (_method->callbacks >= 1) {
if (![arguments[arguments.count - 1] isKindOfClass:[NSNumber class]]) {
RCTLogError(
@"Argument %tu (%@) of %@.%s should be a function",
arguments.count - 1,
arguments[arguments.count - 1],
RCTBridgeModuleNameForClass([module class]),
_method->name.c_str());
return nil;
}
NSNumber *id1;
if (_method->callbacks == 2) {
if (![arguments[arguments.count - 2] isKindOfClass:[NSNumber class]]) {
RCTLogError(
@"Argument %tu (%@) of %@.%s should be a function",
arguments.count - 2,
arguments[arguments.count - 2],
RCTBridgeModuleNameForClass([module class]),
_method->name.c_str());
return nil;
}
id1 = arguments[arguments.count - 2];
NSNumber *id2 = arguments[arguments.count - 1];
second = ^(std::vector<folly::dynamic> args) {
[bridge enqueueCallback:id2 args:convertFollyDynamicToId(folly::dynamic(args.begin(), args.end()))];
};
} else {
id1 = arguments[arguments.count - 1];
}
first = ^(std::vector<folly::dynamic> args) {
[bridge enqueueCallback:id1 args:convertFollyDynamicToId(folly::dynamic(args.begin(), args.end()))];
};
}
folly::dynamic args = convertIdToFollyDynamic(arguments);
args.resize(args.size() - _method->callbacks);
try {
if (_method->func) {
_method->func(std::move(args), first, second);
return nil;
} else {
auto result = _method->syncFunc(std::move(args));
// TODO: we should convert this to JSValue directly
return convertFollyDynamicToId(result);
}
} catch (const facebook::xplat::JsArgumentException &ex) {
RCTLogError(
@"Method %@.%s argument error: %s",
RCTBridgeModuleNameForClass([module class]),
_method->name.c_str(),
ex.what());
return nil;
}
}
- (NSString *)description
{
return [NSString stringWithFormat:@"<%@: %p; name = %s>", [self class], self, self.JSMethodName];
}
@end

View File

@ -0,0 +1,33 @@
/*
* 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 <memory>
#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>
namespace facebook {
namespace xplat {
namespace module {
class CxxModule;
}
}
}
/**
* Subclass RCTCxxModule to use cross-platform CxxModule on iOS.
*
* Subclasses must implement the createModule method to lazily produce the module. When running under the Cxx bridge
* modules will be accessed directly, under the Objective-C bridge method access is wrapped through RCTCxxMethod.
*/
@interface RCTCxxModule : NSObject <RCTBridgeModule>
// To be implemented by subclasses
- (std::unique_ptr<facebook::xplat::module::CxxModule>)createModule;
@end

View File

@ -0,0 +1,87 @@
/*
* 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 "RCTCxxModule.h"
#import <React/RCTBridge.h>
#import <React/RCTFollyConvert.h>
#import <React/RCTLog.h>
#import <cxxreact/CxxModule.h>
#import "RCTCxxMethod.h"
using namespace facebook::react;
@implementation RCTCxxModule {
std::unique_ptr<facebook::xplat::module::CxxModule> _module;
}
+ (NSString *)moduleName
{
return @"";
}
+ (BOOL)requiresMainQueueSetup
{
return NO;
}
- (void)lazyInit
{
if (!_module) {
_module = [self createModule];
if (_module) {
RCTAssert(
[RCTBridgeModuleNameForClass([self class]) isEqualToString:@(_module->getName().c_str())],
@"CxxModule class name %@ does not match runtime name %s",
RCTBridgeModuleNameForClass([self class]),
_module->getName().c_str());
}
}
}
- (std::unique_ptr<facebook::xplat::module::CxxModule>)createModule
{
RCTAssert(NO, @"Subclass %@ must override createModule", [self class]);
return nullptr;
}
- (NSArray<id<RCTBridgeMethod>> *)methodsToExport
{
[self lazyInit];
if (!_module) {
return nil;
}
NSMutableArray *moduleMethods = [NSMutableArray new];
for (const auto &method : _module->getMethods()) {
[moduleMethods addObject:[[RCTCxxMethod alloc] initWithCxxMethod:method]];
}
return moduleMethods;
}
- (NSDictionary<NSString *, id> *)constantsToExport
{
return [self getConstants];
}
- (NSDictionary<NSString *, id> *)getConstants
{
[self lazyInit];
if (!_module) {
return nil;
}
NSMutableDictionary *moduleConstants = [NSMutableDictionary new];
for (const auto &c : _module->getConstants()) {
moduleConstants[@(c.first.c_str())] = convertFollyDynamicToId(c.second);
}
return moduleConstants;
}
@end

View File

@ -0,0 +1,29 @@
/*
* 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.
*/
#include <functional>
#include <memory>
#import <Foundation/Foundation.h>
@class RCTBridge;
@class RCTModuleData;
namespace facebook {
namespace react {
class Instance;
class NativeModule;
std::vector<std::unique_ptr<NativeModule>>
createNativeModules(NSArray<RCTModuleData *> *modules, RCTBridge *bridge, const std::shared_ptr<Instance> &instance);
NSError *tryAndReturnError(const std::function<void()> &func);
NSString *deriveSourceURL(NSURL *url);
}
}

View File

@ -0,0 +1,106 @@
/*
* 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 "RCTCxxUtils.h"
#import <React/RCTFollyConvert.h>
#import <React/RCTModuleData.h>
#import <React/RCTUtils.h>
#import <cxxreact/CxxNativeModule.h>
#import <jsi/jsi.h>
#import "DispatchMessageQueueThread.h"
#import "RCTCxxModule.h"
#import "RCTNativeModule.h"
namespace facebook {
namespace react {
using facebook::jsi::JSError;
std::vector<std::unique_ptr<NativeModule>>
createNativeModules(NSArray<RCTModuleData *> *modules, RCTBridge *bridge, const std::shared_ptr<Instance> &instance)
{
std::vector<std::unique_ptr<NativeModule>> nativeModules;
for (RCTModuleData *moduleData in modules) {
if ([moduleData.moduleClass isSubclassOfClass:[RCTCxxModule class]]) {
nativeModules.emplace_back(std::make_unique<CxxNativeModule>(
instance,
[moduleData.name UTF8String],
[moduleData] { return [(RCTCxxModule *)(moduleData.instance) createModule]; },
std::make_shared<DispatchMessageQueueThread>(moduleData)));
} else {
nativeModules.emplace_back(std::make_unique<RCTNativeModule>(bridge, moduleData));
}
}
return nativeModules;
}
static NSError *errorWithException(const std::exception &e)
{
NSString *msg = @(e.what());
NSMutableDictionary *errorInfo = [NSMutableDictionary dictionary];
const auto *jsError = dynamic_cast<const JSError *>(&e);
if (jsError) {
errorInfo[RCTJSRawStackTraceKey] = @(jsError->getStack().c_str());
msg = [@"Unhandled JS Exception: " stringByAppendingString:msg];
}
NSError *nestedError;
try {
std::rethrow_if_nested(e);
} catch (const std::exception &e) {
nestedError = errorWithException(e);
} catch (...) {
}
if (nestedError) {
msg = [NSString stringWithFormat:@"%@\n\n%@", msg, [nestedError localizedDescription]];
}
errorInfo[NSLocalizedDescriptionKey] = msg;
return [NSError errorWithDomain:RCTErrorDomain code:1 userInfo:errorInfo];
}
NSError *tryAndReturnError(const std::function<void()> &func)
{
try {
@try {
func();
return nil;
} @catch (NSException *exception) {
NSString *message = [NSString stringWithFormat:@"Exception '%@' was thrown from JS thread", exception];
return RCTErrorWithMessage(message);
} @catch (id exception) {
// This will catch any other ObjC exception, but no C++ exceptions
return RCTErrorWithMessage(@"non-std ObjC Exception");
}
} catch (const std::exception &ex) {
return errorWithException(ex);
} catch (...) {
// On a 64-bit platform, this would catch ObjC exceptions, too, but not on
// 32-bit platforms, so we catch those with id exceptions above.
return RCTErrorWithMessage(@"non-std C++ exception");
}
}
NSString *deriveSourceURL(NSURL *url)
{
NSString *sourceUrl;
if (url.isFileURL) {
// Url will contain only path to resource (i.g. file:// will be removed)
sourceUrl = url.path;
} else {
// Url will include protocol (e.g. http://)
sourceUrl = url.absoluteString;
}
return sourceUrl ?: @"";
}
}
}

View File

@ -0,0 +1,33 @@
/*
* 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 <React/RCTModuleData.h>
#import <cxxreact/NativeModule.h>
namespace facebook {
namespace react {
class RCTNativeModule : public NativeModule {
public:
RCTNativeModule(RCTBridge *bridge, RCTModuleData *moduleData);
std::string getName() override;
std::vector<MethodDescriptor> getMethods() override;
folly::dynamic getConstants() override;
void invoke(unsigned int methodId, folly::dynamic &&params, int callId)
override;
MethodCallResult callSerializableNativeHook(
unsigned int reactMethodId,
folly::dynamic &&params) override;
private:
__weak RCTBridge *m_bridge;
RCTModuleData *m_moduleData;
};
} // namespace react
} // namespace facebook

View File

@ -0,0 +1,140 @@
/*
* 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>
#ifdef WITH_FBSYSTRACE
#include <fbsystrace.h>
#endif
namespace facebook {
namespace react {
static MethodCallResult
invokeInner(RCTBridge *bridge, RCTModuleData *moduleData, unsigned int methodId, const folly::dynamic &params);
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(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 &&params, int callId)
{
// 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] {
#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));
};
dispatch_queue_t queue = m_moduleData.methodQueue;
if (queue == RCTJSThread) {
block();
} else if (queue) {
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
}
MethodCallResult RCTNativeModule::callSerializableNativeHook(unsigned int reactMethodId, folly::dynamic &&params)
{
return invokeInner(m_bridge, m_moduleData, reactMethodId, params);
}
static MethodCallResult
invokeInner(RCTBridge *bridge, RCTModuleData *moduleData, unsigned int methodId, const folly::dynamic &params)
{
if (!bridge || !bridge.valid || !moduleData) {
return folly::none;
}
id<RCTBridgeMethod> method = moduleData.methods[methodId];
if (RCT_DEBUG && !method) {
RCTLogError(@"Unknown methodID: %ud for module: %@", methodId, moduleData.name);
}
NSArray *objcParams = convertFollyDynamicToId(params);
@try {
id result = [method invokeWithBridge:bridge module:moduleData.instance arguments:objcParams];
return convertIdToFollyDynamic(result);
} @catch (NSException *exception) {
// 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;
}
}
}