From b9f9f3284e2e93358b57c7fb956d2c0129e39c0c Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Tue, 6 Nov 2018 10:58:51 -0800 Subject: [PATCH] Fabric: A bunch of functions for converting JSI primitives to Fabric and vice-versa Summary: We need to decouple this from actual JSI/UIManagerBinding implementation to make them more maintainable. Reviewed By: sahrens Differential Revision: D12876742 fbshipit-source-id: 30cad69d0a9761e2aa82f31d180e4b5a40cedb61 --- ReactCommon/fabric/uimanager/primitives.h | 60 +++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/ReactCommon/fabric/uimanager/primitives.h b/ReactCommon/fabric/uimanager/primitives.h index 7f7c4b8e1..71b2d8606 100644 --- a/ReactCommon/fabric/uimanager/primitives.h +++ b/ReactCommon/fabric/uimanager/primitives.h @@ -58,5 +58,65 @@ struct ShadowNodeListWrapper : public jsi::HostObject { SharedShadowNodeUnsharedList shadowNodeList; }; +inline static SharedShadowNode shadowNodeFromValue( + jsi::Runtime &runtime, + const jsi::Value &value) { + return value.getObject(runtime) + .getHostObject(runtime) + ->shadowNode; +} + +inline static jsi::Value valueFromShadowNode( + jsi::Runtime &runtime, + const SharedShadowNode &shadowNode) { + return jsi::Object::createFromHostObject( + runtime, std::make_shared(shadowNode)); +} + +inline static SharedShadowNodeUnsharedList shadowNodeListFromValue( + jsi::Runtime &runtime, + const jsi::Value &value) { + return value.getObject(runtime) + .getHostObject(runtime) + ->shadowNodeList; +} + +inline static jsi::Value valueFromShadowNodeList( + jsi::Runtime &runtime, + const SharedShadowNodeUnsharedList &shadowNodeList) { + return jsi::Object::createFromHostObject( + runtime, std::make_unique(shadowNodeList)); +} + +inline static RawProps rawPropsFromValue( + jsi::Runtime &runtime, + const jsi::Value &value) { + return rawPropsFromDynamic(folly::dynamic{ + value.isNull() ? nullptr : jsi::dynamicFromValue(runtime, value)}); +} + +inline static SharedEventTarget eventTargetFromValue( + jsi::Runtime &runtime, + const jsi::Value &value) { + return std::make_shared( + jsi::WeakObject(runtime, value.getObject(runtime))); +} + +inline static Tag tagFromValue(jsi::Runtime &runtime, const jsi::Value &value) { + return (Tag)value.getNumber(); +} + +inline static SurfaceId surfaceIdFromValue( + jsi::Runtime &runtime, + const jsi::Value &value) { + return (SurfaceId)value.getNumber(); +} + +inline static ComponentName componentNameFromValue( + jsi::Runtime &runtime, + const jsi::Value &value) { + return value.getString(runtime).utf8(runtime); +} + } // namespace react } // namespace facebook