Move some utilities to RCTCxxUtils

Reviewed By: mhorowitz

Differential Revision: D4551173

fbshipit-source-id: db01d3205ec1feb54c815c0f0fc688fc7a4823d0
This commit is contained in:
Pieter De Baets 2017-02-16 13:56:26 -08:00 committed by Facebook Github Bot
parent be5235f86d
commit 11e59e3ff4
3 changed files with 44 additions and 37 deletions

View File

@ -1221,44 +1221,7 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithBundleURL:(__unused NSURL *)bundleUR
}];
}
static 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;
}
/*
* The ValueEncoder<NSArray *>::toValue is used by callFunctionSync below.
* Note: Because the NSArray * is really a NSArray * __strong the toValue is
* accepting NSArray *const __strong instead of NSArray *&&.
*/
template <>
struct ValueEncoder<NSArray *> {
static Value toValue(JSGlobalContextRef ctx, NSArray *const __strong array)
{
JSValue *value = [JSValue valueWithObject:array inContext:contextForGlobalContextRef(ctx)];
return {ctx, [value JSValueRef]};
}
};
- (JSValue *)callFunctionOnModule:(NSString *)module
method:(NSString *)method

View File

@ -8,7 +8,10 @@
*/
#import <React/RCTConvert.h>
#include <JavaScriptCore/JavaScriptCore.h>
#include <cxxreact/JSCExecutor.h>
#include <folly/dynamic.h>
#include <jschelpers/JavaScriptCore.h>
@interface RCTConvert (folly)
@ -19,6 +22,22 @@
namespace facebook {
namespace react {
JSContext *contextForGlobalContextRef(JSGlobalContextRef contextRef);
/*
* The ValueEncoder<NSArray *>::toValue is used by JSCExecutor callFunctionSync.
* Note: Because the NSArray * is really a NSArray * __strong the toValue is
* accepting NSArray *const __strong instead of NSArray *&&.
*/
template <>
struct ValueEncoder<NSArray *> {
static Value toValue(JSGlobalContextRef ctx, NSArray *const __strong array)
{
JSValue *value = [JSValue valueWithObject:array inContext:contextForGlobalContextRef(ctx)];
return {ctx, [value JSValueRef]};
}
};
NSError *tryAndReturnError(const std::function<void()>& func);
} }

View File

@ -36,6 +36,31 @@ using namespace facebook::react;
namespace facebook {
namespace react {
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());