From 1d6b732496f396217e4dcb1c5861713b8d9cbcc7 Mon Sep 17 00:00:00 2001 From: Christoph Purrer Date: Tue, 25 Oct 2022 12:32:30 -0700 Subject: [PATCH] react-native bridging > Add support for std::set Summary: There are use cases in which we want to pass a set of unique elements from C++ to JS and back. We can do this simple by re-using a plain JS array for these purposes. Changelog: [Internal][Added] react-native bridging > Add support for std::set Reviewed By: cipolleschi Differential Revision: D40668244 fbshipit-source-id: d06603440569e5f760c2859a54cf6e4232c7d97a --- ReactCommon/react/bridging/Array.h | 19 +++++++++++++++++++ .../react/bridging/tests/BridgingTest.cpp | 6 ++++++ 2 files changed, 25 insertions(+) diff --git a/ReactCommon/react/bridging/Array.h b/ReactCommon/react/bridging/Array.h index 330e2e1a4f8b6f..af36c392d06af7 100644 --- a/ReactCommon/react/bridging/Array.h +++ b/ReactCommon/react/bridging/Array.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -105,4 +106,22 @@ struct Bridging> } }; +template +struct Bridging> : array_detail::BridgingDynamic> { + static std::set fromJs( + facebook::jsi::Runtime &rt, + const jsi::Array &array, + const std::shared_ptr &jsInvoker) { + size_t length = array.length(rt); + + std::set set; + for (size_t i = 0; i < length; i++) { + set.insert( + bridging::fromJs(rt, array.getValueAtIndex(rt, i), jsInvoker)); + } + + return set; + } +}; + } // namespace facebook::react diff --git a/ReactCommon/react/bridging/tests/BridgingTest.cpp b/ReactCommon/react/bridging/tests/BridgingTest.cpp index 0e2a99d38f3b72..a1f50fec988833 100644 --- a/ReactCommon/react/bridging/tests/BridgingTest.cpp +++ b/ReactCommon/react/bridging/tests/BridgingTest.cpp @@ -431,6 +431,8 @@ TEST_F(BridgingTest, supportTest) { EXPECT_TRUE((bridging::supportsFromJs)); EXPECT_TRUE((bridging::supportsFromJs)); EXPECT_TRUE((bridging::supportsFromJs)); + EXPECT_TRUE((bridging::supportsFromJs, jsi::Array>)); + EXPECT_TRUE((bridging::supportsFromJs, jsi::Array &>)); EXPECT_TRUE((bridging::supportsFromJs, jsi::Array>)); EXPECT_TRUE((bridging::supportsFromJs, jsi::Array &>)); EXPECT_TRUE( @@ -453,6 +455,8 @@ TEST_F(BridgingTest, supportTest) { EXPECT_FALSE((bridging::supportsFromJs)); EXPECT_FALSE((bridging::supportsFromJs)); EXPECT_FALSE((bridging::supportsFromJs)); + EXPECT_FALSE((bridging::supportsFromJs, jsi::String>)); + EXPECT_FALSE((bridging::supportsFromJs, jsi::String &>)); EXPECT_FALSE((bridging::supportsFromJs, jsi::String>)); EXPECT_FALSE((bridging::supportsFromJs, jsi::String &>)); @@ -494,6 +498,8 @@ TEST_F(BridgingTest, supportTest) { EXPECT_TRUE((bridging::supportsToJs)); EXPECT_TRUE((bridging::supportsToJs)); EXPECT_TRUE((bridging::supportsToJs)); + EXPECT_TRUE((bridging::supportsToJs>)); + EXPECT_TRUE((bridging::supportsToJs, jsi::Array>)); EXPECT_TRUE((bridging::supportsToJs>)); EXPECT_TRUE((bridging::supportsToJs, jsi::Array>)); EXPECT_TRUE((bridging::supportsToJs>));