Specialize JSCValueEncoder for id instead of NSArray

Reviewed By: kathryngray

Differential Revision: D5355723

fbshipit-source-id: a992514c92143fcac52f8e35824c665a1cb44ea4
This commit is contained in:
Pieter De Baets 2017-08-07 06:41:15 -07:00 committed by Facebook Github Bot
parent 782453d35e
commit 1cd276ab5c
3 changed files with 19 additions and 15 deletions

View File

@ -1164,8 +1164,7 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithBundleURL:(__unused NSURL *)bundleUR
RCT_PROFILE_BEGIN_EVENT(0, @"callFunctionOnModule", (@{ @"module": module, @"method": method })); RCT_PROFILE_BEGIN_EVENT(0, @"callFunctionOnModule", (@{ @"module": module, @"method": method }));
__block JSValue *ret = nil; __block JSValue *ret = nil;
NSError *errorObj = tryAndReturnError(^{ NSError *errorObj = tryAndReturnError(^{
Value result = self->_reactInstance->callFunctionSync( Value result = self->_reactInstance->callFunctionSync([module UTF8String], [method UTF8String], (id)arguments);
[module UTF8String], [method UTF8String], arguments);
JSContext *context = contextForGlobalContextRef(JSC_JSContextGetGlobalContext(result.context())); JSContext *context = contextForGlobalContextRef(JSC_JSContextGetGlobalContext(result.context()));
ret = [JSC_JSValue(result.context()) valueWithJSValueRef:result inContext:context]; ret = [JSC_JSValue(result.context()) valueWithJSValueRef:result inContext:context];
}); });

View File

@ -26,16 +26,10 @@ std::vector<std::unique_ptr<NativeModule>> createNativeModules(NSArray<RCTModule
JSContext *contextForGlobalContextRef(JSGlobalContextRef contextRef); JSContext *contextForGlobalContextRef(JSGlobalContextRef contextRef);
/* template<>
* The ValueEncoder<NSArray *>::toValue is used by JSCExecutor callFunctionSync. struct JSCValueEncoder<id> {
* Note: Because the NSArray * is really a NSArray * __strong the toValue is static Value toJSCValue(JSGlobalContextRef ctx, id obj) {
* accepting NSArray *const __strong instead of NSArray *&&. JSValue *value = [JSC_JSValue(ctx) valueWithObject:obj inContext:contextForGlobalContextRef(ctx)];
*/
template <>
struct ValueEncoder<NSArray *> {
static Value toValue(JSGlobalContextRef ctx, NSArray *const __strong array)
{
JSValue *value = [JSC_JSValue(ctx) valueWithObject:array inContext:contextForGlobalContextRef(ctx)];
return {ctx, [value JSValueRef]}; return {ctx, [value JSValueRef]};
} }
}; };

View File

@ -36,8 +36,19 @@ private:
folly::dynamic m_jscConfig; folly::dynamic m_jscConfig;
}; };
template <typename T> template<typename T>
struct ValueEncoder; 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 { class RN_EXPORT JSCExecutor : public JSExecutor {
public: public:
@ -69,7 +80,7 @@ public:
Value callFunctionSync( Value callFunctionSync(
const std::string& module, const std::string& method, T&& args) { const std::string& module, const std::string& method, T&& args) {
return callFunctionSyncWithValue( 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))); m_context, std::forward<T>(args)));
} }