/* * 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::nullptr_t fromJs(jsi::Runtime &rt, const jsi::Value &value) { if (value.isNull() || value.isUndefined()) { return nullptr; } else { throw jsi::JSError(rt, "Cannot convert value to nullptr"); } } static std::nullptr_t toJs(jsi::Runtime &, std::nullptr_t) { return nullptr; } }; template struct Bridging> { static std::optional fromJs( jsi::Runtime &rt, const jsi::Value &value, const std::shared_ptr &jsInvoker) { if (value.isNull() || value.isUndefined()) { return {}; } return bridging::fromJs(rt, value, jsInvoker); } template static std::optional fromJs( jsi::Runtime &rt, const std::optional &value, const std::shared_ptr &jsInvoker) { if (value) { return bridging::fromJs(rt, *value, jsInvoker); } return {}; } static jsi::Value toJs( jsi::Runtime &rt, const std::optional &value, const std::shared_ptr &jsInvoker) { if (value) { return bridging::toJs(rt, *value, jsInvoker); } return jsi::Value::null(); } }; template struct Bridging< std::shared_ptr, std::enable_if_t>> { static jsi::Value toJs( jsi::Runtime &rt, const std::shared_ptr &ptr, const std::shared_ptr &jsInvoker) { if (ptr) { return bridging::toJs(rt, *ptr, jsInvoker); } return jsi::Value::null(); } }; template struct Bridging> { static jsi::Value toJs( jsi::Runtime &rt, const std::unique_ptr &ptr, const std::shared_ptr &jsInvoker) { if (ptr) { return bridging::toJs(rt, *ptr, jsInvoker); } return jsi::Value::null(); } }; template struct Bridging> { static jsi::Value toJs( jsi::Runtime &rt, const std::weak_ptr &weakPtr, const std::shared_ptr &jsInvoker) { if (auto ptr = weakPtr.lock()) { return bridging::toJs(rt, *ptr, jsInvoker); } return jsi::Value::null(); } }; } // namespace facebook::react