/* * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ #pragma once #include #include #include namespace facebook::react { template <> struct Bridging> { static std::shared_ptr fromJs( jsi::Runtime& rt, const jsi::Value& jsiValue) { auto object = jsiValue.asObject(rt); // Using `jsi::NativeState` instead of `ShadowNodeWrapper` to avoid doing a // dynamic pointer cast twice (as we're calling `hasNativeState` and then // `getNativeState`). When we use `NativeState`, JSI doesn't need to do any // dynamic casts and we can do a single one on our own after the checks. if (!object.hasNativeState(rt)) { throw jsi::JSINativeException("Value is not a ShadowNode reference"); } auto nativeState = object.getNativeState(rt); auto shadowNodeWrapper = std::dynamic_pointer_cast(nativeState); if (shadowNodeWrapper == nullptr || shadowNodeWrapper->shadowNode == nullptr) { throw jsi::JSINativeException( "Value state is nullptr, expected a ShadowNode reference"); } return shadowNodeWrapper->shadowNode; } static jsi::Value toJs( jsi::Runtime& rt, const std::shared_ptr& value) { jsi::Object obj(rt); obj.setNativeState(rt, std::make_shared(value)); return obj; } }; } // namespace facebook::react