Skip to content

Commit

Permalink
Specialize JSCValueEncoder for id instead of NSArray
Browse files Browse the repository at this point in the history
Reviewed By: kathryngray

Differential Revision: D5355723

fbshipit-source-id: a992514c92143fcac52f8e35824c665a1cb44ea4
  • Loading branch information
javache authored and facebook-github-bot committed Aug 7, 2017
1 parent 782453d commit 1cd276a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
3 changes: 1 addition & 2 deletions React/CxxBridge/RCTCxxBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1164,8 +1164,7 @@ - (JSValue *)callFunctionOnModule:(NSString *)module
RCT_PROFILE_BEGIN_EVENT(0, @"callFunctionOnModule", (@{ @"module": module, @"method": method }));
__block JSValue *ret = nil;
NSError *errorObj = tryAndReturnError(^{
Value result = self->_reactInstance->callFunctionSync(
[module UTF8String], [method UTF8String], arguments);
Value result = self->_reactInstance->callFunctionSync([module UTF8String], [method UTF8String], (id)arguments);
JSContext *context = contextForGlobalContextRef(JSC_JSContextGetGlobalContext(result.context()));
ret = [JSC_JSValue(result.context()) valueWithJSValueRef:result inContext:context];
});
Expand Down
14 changes: 4 additions & 10 deletions React/CxxModule/RCTCxxUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,10 @@ std::vector<std::unique_ptr<NativeModule>> createNativeModules(NSArray<RCTModule

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 = [JSC_JSValue(ctx) valueWithObject:array inContext:contextForGlobalContextRef(ctx)];
template<>
struct JSCValueEncoder<id> {
static Value toJSCValue(JSGlobalContextRef ctx, id obj) {
JSValue *value = [JSC_JSValue(ctx) valueWithObject:obj inContext:contextForGlobalContextRef(ctx)];
return {ctx, [value JSValueRef]};
}
};
Expand Down
17 changes: 14 additions & 3 deletions ReactCommon/cxxreact/JSCExecutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,19 @@ class RN_EXPORT JSCExecutorFactory : public JSExecutorFactory {
folly::dynamic m_jscConfig;
};

template <typename T>
struct ValueEncoder;
template<typename T>
struct JSCValueEncoder {
// If you get a build error here, it means the compiler can't see the template instantation of toJSCValue
// applicable to your type.
static const Value toJSCValue(JSGlobalContextRef ctx, T&& value);
};

template<>
struct JSCValueEncoder<folly::dynamic> {
static const Value toJSCValue(JSGlobalContextRef ctx, const folly::dynamic &&value) {
return Value::fromDynamic(ctx, value);
}
};

class RN_EXPORT JSCExecutor : public JSExecutor {
public:
Expand Down Expand Up @@ -69,7 +80,7 @@ class RN_EXPORT JSCExecutor : public JSExecutor {
Value callFunctionSync(
const std::string& module, const std::string& method, T&& args) {
return callFunctionSyncWithValue(
module, method, ValueEncoder<typename std::decay<T>::type>::toValue(
module, method, JSCValueEncoder<typename std::decay<T>::type>::toJSCValue(
m_context, std::forward<T>(args)));
}

Expand Down

0 comments on commit 1cd276a

Please sign in to comment.