From a52596e1ce65e46506469ff9111c3e28d9818cf8 Mon Sep 17 00:00:00 2001 From: Chi Tsai Date: Wed, 15 Apr 2026 14:42:00 -0700 Subject: [PATCH] Add `String::length` API (#56160) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/56160 Add a JSI API to get the length of a JavaScript string in UTF-16 code units. This is equivalent to the "length" property of a JS string. New API: - `IRuntime::length(const String&)`: Returns the number of UTF-16 code units in the string. Default implementation: The default implementation in `jsi::Runtime` calls `utf16()` then returns the size. Note that the default `utf16()` implementation first converts the JS string to UTF-8, then converts that to UTF-16. This intermediate UTF-8 step does not handle lone surrogates correctly, causing code units to be "lost" in the conversion. Runtimes should provide optimized implementations when possible. The following optimized implementations are provided: JSC implementation: Uses the `JSStringGetLength` API for efficient retrieval. V8 implementation: Uses the `v8::String::Length()` API for efficient retrieval. Changelog: [Internal] Reviewed By: avp Differential Revision: D96030262 fbshipit-source-id: acb0404a68c5bccbcdab5c5acdcfb65c3d991d82 --- .../ReactCommon/jsc/JSCRuntime.cpp | 5 +++ .../react-native/ReactCommon/jsi/jsi/jsi.cpp | 4 ++ .../react-native/ReactCommon/jsi/jsi/jsi.h | 11 +++++ .../ReactCommon/jsi/jsi/test/testlib.cpp | 40 +++++++++++++++++++ 4 files changed, 60 insertions(+) diff --git a/packages/react-native/ReactCommon/jsc/JSCRuntime.cpp b/packages/react-native/ReactCommon/jsc/JSCRuntime.cpp index 7d6e3c4e8db..1f89827eccc 100644 --- a/packages/react-native/ReactCommon/jsc/JSCRuntime.cpp +++ b/packages/react-native/ReactCommon/jsc/JSCRuntime.cpp @@ -179,6 +179,7 @@ class JSCRuntime : public jsi::Runtime { jsi::String createStringFromAscii(const char* str, size_t length) override; jsi::String createStringFromUtf8(const uint8_t* utf8, size_t length) override; std::string utf8(const jsi::String& /*str*/) override; + size_t length(const jsi::String& /*str*/) override; jsi::Object createObject() override; jsi::Object createObject(std::shared_ptr ho) override; @@ -745,6 +746,10 @@ std::string JSCRuntime::utf8(const jsi::String& str) { return JSStringToSTLString(stringRef(str)); } +size_t JSCRuntime::length(const jsi::String& str) { + return JSStringGetLength(stringRef(str)); +} + jsi::Object JSCRuntime::createObject() { return createObject(static_cast(nullptr)); } diff --git a/packages/react-native/ReactCommon/jsi/jsi/jsi.cpp b/packages/react-native/ReactCommon/jsi/jsi/jsi.cpp index 03c3009eefe..792b5bf5a0b 100644 --- a/packages/react-native/ReactCommon/jsi/jsi/jsi.cpp +++ b/packages/react-native/ReactCommon/jsi/jsi/jsi.cpp @@ -602,6 +602,10 @@ Value Runtime::createURIError(const String& msg) { return callGlobalFunction(*this, "URIError", Value(*this, msg)); } +size_t Runtime::length(const String& str) { + return utf16(str).size(); +} + bool Runtime::detached(const ArrayBuffer& buffer) { Value prop = buffer.getProperty(*this, "detached"); if (!prop.isBool()) { diff --git a/packages/react-native/ReactCommon/jsi/jsi/jsi.h b/packages/react-native/ReactCommon/jsi/jsi/jsi.h index a7e64f4ab54..5110303f24d 100644 --- a/packages/react-native/ReactCommon/jsi/jsi/jsi.h +++ b/packages/react-native/ReactCommon/jsi/jsi/jsi.h @@ -675,6 +675,10 @@ class JSI_EXPORT IRuntime : public ICast { /// Create a new URIError object with the message property set to \p message. virtual Value createURIError(const String& msg) = 0; + /// Returns the number of code units in the string, equivalent to 'length' + /// property of a JS string. + virtual size_t length(const String& str) = 0; + protected: virtual ~IRuntime() = default; }; @@ -786,6 +790,8 @@ class JSI_EXPORT Runtime : public IRuntime { Value createTypeError(const String& msg) override; Value createURIError(const String& msg) override; + size_t length(const String& str) override; + protected: friend class Pointer; friend class PropNameID; @@ -1105,6 +1111,11 @@ class JSI_EXPORT String : public Pointer { return runtime.strictEquals(a, b); } + /// \return the 'length' property of this JS string. + size_t length(IRuntime& runtime) const { + return runtime.length(*this); + } + /// Copies the data in a JS string as utf8 into a C++ string. std::string utf8(IRuntime& runtime) const { return runtime.utf8(*this); diff --git a/packages/react-native/ReactCommon/jsi/jsi/test/testlib.cpp b/packages/react-native/ReactCommon/jsi/jsi/test/testlib.cpp index 62c8b0aac93..673e36cf261 100644 --- a/packages/react-native/ReactCommon/jsi/jsi/test/testlib.cpp +++ b/packages/react-native/ReactCommon/jsi/jsi/test/testlib.cpp @@ -98,6 +98,46 @@ TEST_P(JSITest, StringTest) { EXPECT_EQ(movedQuux.utf8(rt), "quux2"); } +TEST_P(JSITest, StringLengthTest) { + // Test ASCII string length + String ascii = String::createFromAscii(rt, "hello"); + EXPECT_EQ(ascii.length(rt), 5); + + // Test empty string + String empty = String::createFromAscii(rt, ""); + EXPECT_EQ(empty.length(rt), 0); + + // Test euro sign '€' (U+20AC) - BMP character, 1 code unit + String euro = eval("'\\u20AC'").getString(rt); + EXPECT_EQ(euro.length(rt), 1); + + // Test codepoint requiring 2 code units (surrogate pair) + // U+1F408 (🐈) is encoded as \uD83D\uDC08 in UTF-16 + String emoji = eval("'\\uD83D\\uDC08'").getString(rt); + EXPECT_EQ(emoji.length(rt), 2); + + // Test another surrogate pair: U+10000 (first supplementary character) + String supplementary = eval("'\\uD800\\uDC00'").getString(rt); + EXPECT_EQ(supplementary.length(rt), 2); + + // Test lone high surrogate (U+D800) + String loneHighSurrogate = eval("'\\uD800'").getString(rt); + EXPECT_EQ(loneHighSurrogate.length(rt), 1); + + // Test lone low surrogate (U+DC00) + String loneLowSurrogate = eval("'\\uDC00'").getString(rt); + EXPECT_EQ(loneLowSurrogate.length(rt), 1); + + // Test lone surrogate in the middle of a string + String mixedWithLoneSurrogate = eval("'a\\uD800b'").getString(rt); + EXPECT_EQ(mixedWithLoneSurrogate.length(rt), 3); + + // Unicode Max Value is U+10FFFF, U+11FFFF is invalid + // But it could be theoretically encoded as \uDBFF\uDFFF + String invalid = eval("'\\uDBFF\\uDFFF'").getString(rt); + EXPECT_EQ(invalid.length(rt), 2); +} + TEST_P(JSITest, ObjectTest) { eval("x = {1:2, '3':4, 5:'six', 'seven':['eight', 'nine']}"); Object x = rt.global().getPropertyAsObject(rt, "x");