/* * 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 #include #include namespace facebook::react { template <> struct Bridging { static jsi::WeakObject fromJs(jsi::Runtime &rt, const jsi::Object &value) { return jsi::WeakObject(rt, value); } static jsi::Value toJs(jsi::Runtime &rt, jsi::WeakObject &value) { return value.lock(rt); } }; template struct Bridging< std::shared_ptr, std::enable_if_t>> { static std::shared_ptr fromJs(jsi::Runtime &rt, const jsi::Object &value) { return value.asHostObject(rt); } static jsi::Object toJs(jsi::Runtime &rt, std::shared_ptr value) { return jsi::Object::createFromHostObject(rt, std::move(value)); } }; namespace map_detail { template struct Bridging { static T fromJs( jsi::Runtime &rt, const jsi::Object &value, const std::shared_ptr &jsInvoker) { T result; auto propertyNames = value.getPropertyNames(rt); auto length = propertyNames.length(rt); for (size_t i = 0; i < length; i++) { auto propertyName = propertyNames.getValueAtIndex(rt, i); result.emplace( bridging::fromJs(rt, propertyName, jsInvoker), bridging::fromJs( rt, value.getProperty(rt, propertyName.asString(rt)), jsInvoker)); } return result; } static jsi::Object toJs( jsi::Runtime &rt, const T &map, const std::shared_ptr &jsInvoker) { auto resultObject = jsi::Object(rt); for (const auto &[key, value] : map) { resultObject.setProperty( rt, jsi::PropNameID::forUtf8(rt, key), bridging::toJs(rt, value, jsInvoker)); } return resultObject; } }; } // namespace map_detail #ifdef BUTTER_USE_FOLLY_CONTAINERS template struct Bridging> : map_detail::Bridging> {}; #endif template struct Bridging> : map_detail::Bridging> {}; template struct Bridging> : map_detail::Bridging> {}; } // namespace facebook::react