RCTCxxUtils.mm
4.14 KB
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
/**
* 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 "RCTCxxUtils.h"
#import <React/RCTFollyConvert.h>
#import <React/RCTModuleData.h>
#import <React/RCTUtils.h>
#import <cxxreact/CxxNativeModule.h>
#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;
}
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];
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 ?: @"";
}
} }