react-native-macos/React/CxxModule/RCTCxxUtils.mm

130 строки
4.0 KiB
Plaintext
Исходник Обычный вид История

/**
Squashed commit of the following: commit 4c43e301d97042e9eec4401dd81b8ebce053f691 Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:49:02 2019 -0700 Squashed commit of the following: commit 862f3fb5d9e3161feab6e5e0e61c7b28e8ac1ef9 Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:46:37 2019 -0700 Revert "Merge 1b12e2caaadf094ce9327deb7e5fe49297e8a115 into 9f1e90947421ad0ea2de1b534117c500dc8c9744" This reverts commit d036bc4c5768d6a3327c5766e1791719cb11693f, reversing changes made to 9f1e90947421ad0ea2de1b534117c500dc8c9744. commit 41477c33c1bcc78f40da00ac7e86ab4dd3f7d062 Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:46:23 2019 -0700 Revert "Applying package update to 0.58.6-microsoft.1" This reverts commit 852df1af0e489063db15d74f0d241ea930778b9c. commit 4993963205e737849e4880309ef86aeac9f2a765 Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:46:16 2019 -0700 Revert "Applying package update to 0.58.6-microsoft.2" This reverts commit 774513fec948ee5595543e23ab4e7b9cccbdcbf1. commit 41f8beadb2030593208ffc6ff29339991dde3798 Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:46:07 2019 -0700 Revert "Merge 4f3c37886c3f8d0af9571dbed0ec1361d51219a6 into 774513fec948ee5595543e23ab4e7b9cccbdcbf1" This reverts commit a84049078437ce6e0852a30367b4c2d56be7b884, reversing changes made to 774513fec948ee5595543e23ab4e7b9cccbdcbf1. commit 143e70ec6228e110cd94a5ae9c56309040c1f1eb Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:45:57 2019 -0700 Revert "Applying package update to 0.58.6-microsoft.3" This reverts commit 54dfee820d107cb897c5aca14131cf7fbde616d7. commit 138191b0d6bf9e4370fb5126dc2095a3fb122a57 Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:45:16 2019 -0700 Revert "Merge d8be239facee73f3ba7b664154766dbfb8049050 into 54dfee820d107cb897c5aca14131cf7fbde616d7" This reverts commit faae1e56f805e2fd953218be92b2c0d1753a7c3c, reversing changes made to 54dfee820d107cb897c5aca14131cf7fbde616d7. commit 40a77c8365c5273df0bba4fc2b0439db17d247d0 Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:34:44 2019 -0700 Revert "Applying package update to 0.58.6-microsoft.4" This reverts commit 043f6330e7a7fe7a73ac4e406c157f61cf1e7f1c. commit adf5c26c8f7132a6643ecfea116226632173447b Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:34:42 2019 -0700 Revert "Applying package update to 0.58.6-microsoft.5" This reverts commit 4004f889c28f42535b9cc08d439311b2f5802359. commit e68d497a58e32d26f0d04130895954d7b654c2bf Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:34:39 2019 -0700 Revert "Fix the builds... (#20)" This reverts commit 745eeda26fafa79fd9110e20647ace54c80d3dab. commit 4cdef1eef22edbcdc9ca86e31846a175b944999e Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:34:31 2019 -0700 Revert "Applying package update to 0.58.6-microsoft.6" This reverts commit 34a307738806da39704d7f45428920144e0bed53.
2019-03-26 20:54:53 +03:00
* Copyright (c) 2015-present, Facebook, Inc.
*
* 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>
Squashed commit of the following: commit 4c43e301d97042e9eec4401dd81b8ebce053f691 Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:49:02 2019 -0700 Squashed commit of the following: commit 862f3fb5d9e3161feab6e5e0e61c7b28e8ac1ef9 Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:46:37 2019 -0700 Revert "Merge 1b12e2caaadf094ce9327deb7e5fe49297e8a115 into 9f1e90947421ad0ea2de1b534117c500dc8c9744" This reverts commit d036bc4c5768d6a3327c5766e1791719cb11693f, reversing changes made to 9f1e90947421ad0ea2de1b534117c500dc8c9744. commit 41477c33c1bcc78f40da00ac7e86ab4dd3f7d062 Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:46:23 2019 -0700 Revert "Applying package update to 0.58.6-microsoft.1" This reverts commit 852df1af0e489063db15d74f0d241ea930778b9c. commit 4993963205e737849e4880309ef86aeac9f2a765 Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:46:16 2019 -0700 Revert "Applying package update to 0.58.6-microsoft.2" This reverts commit 774513fec948ee5595543e23ab4e7b9cccbdcbf1. commit 41f8beadb2030593208ffc6ff29339991dde3798 Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:46:07 2019 -0700 Revert "Merge 4f3c37886c3f8d0af9571dbed0ec1361d51219a6 into 774513fec948ee5595543e23ab4e7b9cccbdcbf1" This reverts commit a84049078437ce6e0852a30367b4c2d56be7b884, reversing changes made to 774513fec948ee5595543e23ab4e7b9cccbdcbf1. commit 143e70ec6228e110cd94a5ae9c56309040c1f1eb Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:45:57 2019 -0700 Revert "Applying package update to 0.58.6-microsoft.3" This reverts commit 54dfee820d107cb897c5aca14131cf7fbde616d7. commit 138191b0d6bf9e4370fb5126dc2095a3fb122a57 Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:45:16 2019 -0700 Revert "Merge d8be239facee73f3ba7b664154766dbfb8049050 into 54dfee820d107cb897c5aca14131cf7fbde616d7" This reverts commit faae1e56f805e2fd953218be92b2c0d1753a7c3c, reversing changes made to 54dfee820d107cb897c5aca14131cf7fbde616d7. commit 40a77c8365c5273df0bba4fc2b0439db17d247d0 Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:34:44 2019 -0700 Revert "Applying package update to 0.58.6-microsoft.4" This reverts commit 043f6330e7a7fe7a73ac4e406c157f61cf1e7f1c. commit adf5c26c8f7132a6643ecfea116226632173447b Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:34:42 2019 -0700 Revert "Applying package update to 0.58.6-microsoft.5" This reverts commit 4004f889c28f42535b9cc08d439311b2f5802359. commit e68d497a58e32d26f0d04130895954d7b654c2bf Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:34:39 2019 -0700 Revert "Fix the builds... (#20)" This reverts commit 745eeda26fafa79fd9110e20647ace54c80d3dab. commit 4cdef1eef22edbcdc9ca86e31846a175b944999e Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:34:31 2019 -0700 Revert "Applying package update to 0.58.6-microsoft.6" This reverts commit 34a307738806da39704d7f45428920144e0bed53.
2019-03-26 20:54:53 +03:00
#import <jschelpers/Value.h>
#import "DispatchMessageQueueThread.h"
#import "RCTCxxModule.h"
#import "RCTNativeModule.h"
namespace facebook {
namespace react {
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;
}
Squashed commit of the following: commit 4c43e301d97042e9eec4401dd81b8ebce053f691 Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:49:02 2019 -0700 Squashed commit of the following: commit 862f3fb5d9e3161feab6e5e0e61c7b28e8ac1ef9 Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:46:37 2019 -0700 Revert "Merge 1b12e2caaadf094ce9327deb7e5fe49297e8a115 into 9f1e90947421ad0ea2de1b534117c500dc8c9744" This reverts commit d036bc4c5768d6a3327c5766e1791719cb11693f, reversing changes made to 9f1e90947421ad0ea2de1b534117c500dc8c9744. commit 41477c33c1bcc78f40da00ac7e86ab4dd3f7d062 Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:46:23 2019 -0700 Revert "Applying package update to 0.58.6-microsoft.1" This reverts commit 852df1af0e489063db15d74f0d241ea930778b9c. commit 4993963205e737849e4880309ef86aeac9f2a765 Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:46:16 2019 -0700 Revert "Applying package update to 0.58.6-microsoft.2" This reverts commit 774513fec948ee5595543e23ab4e7b9cccbdcbf1. commit 41f8beadb2030593208ffc6ff29339991dde3798 Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:46:07 2019 -0700 Revert "Merge 4f3c37886c3f8d0af9571dbed0ec1361d51219a6 into 774513fec948ee5595543e23ab4e7b9cccbdcbf1" This reverts commit a84049078437ce6e0852a30367b4c2d56be7b884, reversing changes made to 774513fec948ee5595543e23ab4e7b9cccbdcbf1. commit 143e70ec6228e110cd94a5ae9c56309040c1f1eb Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:45:57 2019 -0700 Revert "Applying package update to 0.58.6-microsoft.3" This reverts commit 54dfee820d107cb897c5aca14131cf7fbde616d7. commit 138191b0d6bf9e4370fb5126dc2095a3fb122a57 Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:45:16 2019 -0700 Revert "Merge d8be239facee73f3ba7b664154766dbfb8049050 into 54dfee820d107cb897c5aca14131cf7fbde616d7" This reverts commit faae1e56f805e2fd953218be92b2c0d1753a7c3c, reversing changes made to 54dfee820d107cb897c5aca14131cf7fbde616d7. commit 40a77c8365c5273df0bba4fc2b0439db17d247d0 Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:34:44 2019 -0700 Revert "Applying package update to 0.58.6-microsoft.4" This reverts commit 043f6330e7a7fe7a73ac4e406c157f61cf1e7f1c. commit adf5c26c8f7132a6643ecfea116226632173447b Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:34:42 2019 -0700 Revert "Applying package update to 0.58.6-microsoft.5" This reverts commit 4004f889c28f42535b9cc08d439311b2f5802359. commit e68d497a58e32d26f0d04130895954d7b654c2bf Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:34:39 2019 -0700 Revert "Fix the builds... (#20)" This reverts commit 745eeda26fafa79fd9110e20647ace54c80d3dab. commit 4cdef1eef22edbcdc9ca86e31846a175b944999e Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:34:31 2019 -0700 Revert "Applying package update to 0.58.6-microsoft.6" This reverts commit 34a307738806da39704d7f45428920144e0bed53.
2019-03-26 20:54:53 +03:00
JSContext *contextForGlobalContextRef(JSGlobalContextRef contextRef)
{
static std::mutex s_mutex;
static NSMapTable *s_contextCache;
if (!contextRef) {
return nil;
}
// Adding our own lock here, since JSC internal ones are insufficient
std::lock_guard<std::mutex> lock(s_mutex);
if (!s_contextCache) {
NSPointerFunctionsOptions keyOptions = NSPointerFunctionsOpaqueMemory | NSPointerFunctionsOpaquePersonality;
NSPointerFunctionsOptions valueOptions = NSPointerFunctionsWeakMemory | NSPointerFunctionsObjectPersonality;
s_contextCache = [[NSMapTable alloc] initWithKeyOptions:keyOptions valueOptions:valueOptions capacity:0];
}
JSContext *ctx = [s_contextCache objectForKey:(__bridge id)contextRef];
if (!ctx) {
ctx = [JSC_JSContext(contextRef) contextWithJSGlobalContextRef:contextRef];
[s_contextCache setObject:ctx forKey:(__bridge id)contextRef];
}
return ctx;
}
static NSError *errorWithException(const std::exception &e)
{
NSString *msg = @(e.what());
NSMutableDictionary *errorInfo = [NSMutableDictionary dictionary];
Squashed commit of the following: commit 4c43e301d97042e9eec4401dd81b8ebce053f691 Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:49:02 2019 -0700 Squashed commit of the following: commit 862f3fb5d9e3161feab6e5e0e61c7b28e8ac1ef9 Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:46:37 2019 -0700 Revert "Merge 1b12e2caaadf094ce9327deb7e5fe49297e8a115 into 9f1e90947421ad0ea2de1b534117c500dc8c9744" This reverts commit d036bc4c5768d6a3327c5766e1791719cb11693f, reversing changes made to 9f1e90947421ad0ea2de1b534117c500dc8c9744. commit 41477c33c1bcc78f40da00ac7e86ab4dd3f7d062 Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:46:23 2019 -0700 Revert "Applying package update to 0.58.6-microsoft.1" This reverts commit 852df1af0e489063db15d74f0d241ea930778b9c. commit 4993963205e737849e4880309ef86aeac9f2a765 Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:46:16 2019 -0700 Revert "Applying package update to 0.58.6-microsoft.2" This reverts commit 774513fec948ee5595543e23ab4e7b9cccbdcbf1. commit 41f8beadb2030593208ffc6ff29339991dde3798 Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:46:07 2019 -0700 Revert "Merge 4f3c37886c3f8d0af9571dbed0ec1361d51219a6 into 774513fec948ee5595543e23ab4e7b9cccbdcbf1" This reverts commit a84049078437ce6e0852a30367b4c2d56be7b884, reversing changes made to 774513fec948ee5595543e23ab4e7b9cccbdcbf1. commit 143e70ec6228e110cd94a5ae9c56309040c1f1eb Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:45:57 2019 -0700 Revert "Applying package update to 0.58.6-microsoft.3" This reverts commit 54dfee820d107cb897c5aca14131cf7fbde616d7. commit 138191b0d6bf9e4370fb5126dc2095a3fb122a57 Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:45:16 2019 -0700 Revert "Merge d8be239facee73f3ba7b664154766dbfb8049050 into 54dfee820d107cb897c5aca14131cf7fbde616d7" This reverts commit faae1e56f805e2fd953218be92b2c0d1753a7c3c, reversing changes made to 54dfee820d107cb897c5aca14131cf7fbde616d7. commit 40a77c8365c5273df0bba4fc2b0439db17d247d0 Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:34:44 2019 -0700 Revert "Applying package update to 0.58.6-microsoft.4" This reverts commit 043f6330e7a7fe7a73ac4e406c157f61cf1e7f1c. commit adf5c26c8f7132a6643ecfea116226632173447b Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:34:42 2019 -0700 Revert "Applying package update to 0.58.6-microsoft.5" This reverts commit 4004f889c28f42535b9cc08d439311b2f5802359. commit e68d497a58e32d26f0d04130895954d7b654c2bf Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:34:39 2019 -0700 Revert "Fix the builds... (#20)" This reverts commit 745eeda26fafa79fd9110e20647ace54c80d3dab. commit 4cdef1eef22edbcdc9ca86e31846a175b944999e Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:34:31 2019 -0700 Revert "Applying package update to 0.58.6-microsoft.6" This reverts commit 34a307738806da39704d7f45428920144e0bed53.
2019-03-26 20:54:53 +03:00
const JSException *jsException = dynamic_cast<const JSException*>(&e);
if (jsException) {
errorInfo[RCTJSRawStackTraceKey] = @(jsException->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 ?: @"";
}
} }