From 5d9cc9e4079b74cf36e216edd1efb39521b34be2 Mon Sep 17 00:00:00 2001 From: Ruslan Lesiutin Date: Fri, 16 May 2025 12:20:42 -0700 Subject: [PATCH] Define Bridging protocol for HighResTimeStamp and HighResTimeDuration (#51253) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/51253 # Changelog: [Internal] Defines bridging template for `HighResTimeStamp` and `HighResTimeDuration`. When these values are passed to JavaScript over bridge, they will be converted to [`DOMHighResTimeStamp`](https://developer.mozilla.org/en-US/docs/Web/API/DOMHighResTimeStamp). Also the other way around, when we list `HighResTimeStamp` or `HighResTimeDuration` as a type of TurboModule spec, it will expect number value from JavaScript. Reviewed By: rubennorte Differential Revision: D74506516 fbshipit-source-id: b5893cef4525793eb6fb6326055c261c7b84a724 --- .../ReactCommon/react/bridging/Bridging.h | 1 + .../ReactCommon/react/bridging/CMakeLists.txt | 2 +- .../react/bridging/HighResTimeStamp.h | 41 +++++++++++++++++++ .../react/bridging/tests/BridgingTest.cpp | 30 ++++++++++++++ 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 packages/react-native/ReactCommon/react/bridging/HighResTimeStamp.h diff --git a/packages/react-native/ReactCommon/react/bridging/Bridging.h b/packages/react-native/ReactCommon/react/bridging/Bridging.h index 2ea53d588ae..558d91335f6 100644 --- a/packages/react-native/ReactCommon/react/bridging/Bridging.h +++ b/packages/react-native/ReactCommon/react/bridging/Bridging.h @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include diff --git a/packages/react-native/ReactCommon/react/bridging/CMakeLists.txt b/packages/react-native/ReactCommon/react/bridging/CMakeLists.txt index 9083644e908..39f8b9c65dd 100644 --- a/packages/react-native/ReactCommon/react/bridging/CMakeLists.txt +++ b/packages/react-native/ReactCommon/react/bridging/CMakeLists.txt @@ -14,6 +14,6 @@ add_library(react_bridging OBJECT ${react_bridging_SRC}) target_include_directories(react_bridging PUBLIC ${REACT_COMMON_DIR}) -target_link_libraries(react_bridging jsi callinvoker) +target_link_libraries(react_bridging jsi callinvoker react_timing) target_compile_reactnative_options(react_bridging PRIVATE "ReactNative") target_compile_options(react_bridging PRIVATE -Wpedantic) diff --git a/packages/react-native/ReactCommon/react/bridging/HighResTimeStamp.h b/packages/react-native/ReactCommon/react/bridging/HighResTimeStamp.h new file mode 100644 index 00000000000..ea3aa83bc62 --- /dev/null +++ b/packages/react-native/ReactCommon/react/bridging/HighResTimeStamp.h @@ -0,0 +1,41 @@ +/* + * 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 + +namespace facebook::react { + +template <> +struct Bridging { + static HighResTimeStamp fromJs( + jsi::Runtime& /*rt*/, + const jsi::Value& jsiValue) { + return HighResTimeStamp::fromDOMHighResTimeStamp(jsiValue.asNumber()); + } + + static double toJs(jsi::Runtime& /*rt*/, const HighResTimeStamp& value) { + return value.toDOMHighResTimeStamp(); + } +}; + +template <> +struct Bridging { + static HighResDuration fromJs( + jsi::Runtime& /*rt*/, + const jsi::Value& jsiValue) { + return HighResDuration::fromDOMHighResTimeStamp(jsiValue.asNumber()); + } + + static double toJs(jsi::Runtime& /*rt*/, const HighResDuration& value) { + return value.toDOMHighResTimeStamp(); + } +}; + +} // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/bridging/tests/BridgingTest.cpp b/packages/react-native/ReactCommon/react/bridging/tests/BridgingTest.cpp index 3021a663b96..db7287e79d1 100644 --- a/packages/react-native/ReactCommon/react/bridging/tests/BridgingTest.cpp +++ b/packages/react-native/ReactCommon/react/bridging/tests/BridgingTest.cpp @@ -654,6 +654,13 @@ TEST_F(BridgingTest, supportTest) { EXPECT_FALSE((bridging::supportsFromJs)); EXPECT_FALSE((bridging::supportsFromJs)); + // Ensure we can create HighResTimeStamp and HighResDuration from JSI + // values. + EXPECT_TRUE((bridging::supportsFromJs)); + EXPECT_TRUE((bridging::supportsFromJs)); + EXPECT_TRUE((bridging::supportsFromJs)); + EXPECT_TRUE((bridging::supportsFromJs)); + // Ensure we can convert some basic types to JSI values. EXPECT_TRUE((bridging::supportsToJs)); EXPECT_TRUE((bridging::supportsToJs)); @@ -677,6 +684,11 @@ TEST_F(BridgingTest, supportTest) { EXPECT_FALSE((bridging::supportsToJs)); EXPECT_FALSE((bridging::supportsToJs)); EXPECT_FALSE((bridging::supportsToJs, jsi::Function>)); + + // Ensure we can convert HighResTimeStamp and HighResDuration to + // DOMHighResTimeStamp (double). + EXPECT_TRUE((bridging::supportsToJs)); + EXPECT_TRUE((bridging::supportsToJs)); } TEST_F(BridgingTest, dynamicTest) { @@ -765,4 +777,22 @@ TEST_F(BridgingTest, dynamicTest) { EXPECT_TRUE(undefinedFromJsResult.isNull()); } +TEST_F(BridgingTest, highResTimeStampTest) { + HighResTimeStamp timestamp = HighResTimeStamp::now(); + EXPECT_EQ( + timestamp, + bridging::fromJs( + rt, bridging::toJs(rt, timestamp), invoker)); + + HighResDuration duration = HighResDuration::fromNanoseconds(1); + EXPECT_EQ( + duration, + bridging::fromJs( + rt, bridging::toJs(rt, duration), invoker)); + + EXPECT_EQ(1.0, bridging::toJs(rt, HighResDuration::fromNanoseconds(1e6))); + EXPECT_EQ( + 1.000001, bridging::toJs(rt, HighResDuration::fromNanoseconds(1e6 + 1))); +} + } // namespace facebook::react