From 11e59e3ff4c286b455f851cecd1f976652e3c539 Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Thu, 16 Feb 2017 13:56:26 -0800 Subject: [PATCH] Move some utilities to RCTCxxUtils Reviewed By: mhorowitz Differential Revision: D4551173 fbshipit-source-id: db01d3205ec1feb54c815c0f0fc688fc7a4823d0 --- React/CxxBridge/RCTCxxBridge.mm | 37 --------------------------------- React/CxxModule/RCTCxxUtils.h | 19 +++++++++++++++++ React/CxxModule/RCTCxxUtils.mm | 25 ++++++++++++++++++++++ 3 files changed, 44 insertions(+), 37 deletions(-) diff --git a/React/CxxBridge/RCTCxxBridge.mm b/React/CxxBridge/RCTCxxBridge.mm index 8fc006362..e4a634498 100644 --- a/React/CxxBridge/RCTCxxBridge.mm +++ b/React/CxxBridge/RCTCxxBridge.mm @@ -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 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::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 { - 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 diff --git a/React/CxxModule/RCTCxxUtils.h b/React/CxxModule/RCTCxxUtils.h index 0891f20e5..0f3612338 100644 --- a/React/CxxModule/RCTCxxUtils.h +++ b/React/CxxModule/RCTCxxUtils.h @@ -8,7 +8,10 @@ */ #import +#include +#include #include +#include @interface RCTConvert (folly) @@ -19,6 +22,22 @@ namespace facebook { namespace react { +JSContext *contextForGlobalContextRef(JSGlobalContextRef contextRef); + +/* + * The ValueEncoder::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 { + 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& func); } } diff --git a/React/CxxModule/RCTCxxUtils.mm b/React/CxxModule/RCTCxxUtils.mm index caa986609..3283f8203 100644 --- a/React/CxxModule/RCTCxxUtils.mm +++ b/React/CxxModule/RCTCxxUtils.mm @@ -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 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());