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
This commit is contained in:
Ruslan Lesiutin authored and Facebook GitHub Bot committed 2025-05-16 12:20:42 -07:00
1 parent a882f19974
commit 5d9cc9e407
4 files changed
+73 -1

No files matched your search

@@ -15,6 +15,7 @@
#include <react/bridging/Error.h>
#include <react/bridging/EventEmitter.h>
#include <react/bridging/Function.h>
#include <react/bridging/HighResTimeStamp.h>
#include <react/bridging/Number.h>
#include <react/bridging/Object.h>
#include <react/bridging/Promise.h>
@@ -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)
@@ -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 <react/bridging/Base.h>
#include <react/timing/primitives.h>
namespace facebook::react {
template <>
struct Bridging<HighResTimeStamp> {
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<HighResDuration> {
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
@@ -654,6 +654,13 @@ TEST_F(BridgingTest, supportTest) {
EXPECT_FALSE((bridging::supportsFromJs<jsi::Function, jsi::Array>));
EXPECT_FALSE((bridging::supportsFromJs<jsi::Function, jsi::Array&>));
// Ensure we can create HighResTimeStamp and HighResDuration from JSI
// values.
EXPECT_TRUE((bridging::supportsFromJs<HighResTimeStamp, jsi::Value>));
EXPECT_TRUE((bridging::supportsFromJs<HighResTimeStamp, jsi::Value&>));
EXPECT_TRUE((bridging::supportsFromJs<HighResDuration, jsi::Value>));
EXPECT_TRUE((bridging::supportsFromJs<HighResDuration, jsi::Value&>));
// Ensure we can convert some basic types to JSI values.
EXPECT_TRUE((bridging::supportsToJs<bool>));
EXPECT_TRUE((bridging::supportsToJs<int>));
@@ -677,6 +684,11 @@ TEST_F(BridgingTest, supportTest) {
EXPECT_FALSE((bridging::supportsToJs<double, jsi::Object>));
EXPECT_FALSE((bridging::supportsToJs<std::string, jsi::Object>));
EXPECT_FALSE((bridging::supportsToJs<std::vector<int>, jsi::Function>));
// Ensure we can convert HighResTimeStamp and HighResDuration to
// DOMHighResTimeStamp (double).
EXPECT_TRUE((bridging::supportsToJs<HighResTimeStamp, double>));
EXPECT_TRUE((bridging::supportsToJs<HighResDuration, double>));
}
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<HighResTimeStamp>(
rt, bridging::toJs(rt, timestamp), invoker));
HighResDuration duration = HighResDuration::fromNanoseconds(1);
EXPECT_EQ(
duration,
bridging::fromJs<HighResDuration>(
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