Skip to content

Commit be567d2

Browse files
mrousavymeta-codesync[bot]
authored andcommitted
Forward String::length in RuntimeDecorator and WithRuntimeDecorator (#58936)
Summary: Pull Request resolved: #58936 `IRuntime::length(const String&)` came in with "Add `String::length` API" (590396bd4 on static_h, D96030262), and that commit didn't touch `decorator.h`. Neither `RuntimeDecorator` nor `WithRuntimeDecorator` overrides it, so on a decorated runtime `length()` falls back to the default `utf16(str).size()` and never reaches the wrapped runtime. It looks like an oversight. The same stack added `push`, `tryGetMutableBuffer`, `detached` and the TypedArray methods, and each of those commits added decorator overrides, including one for this method's overload `length(const TypedArray&)`. The `createError*` commit (meta/hermes#2203) and this one didn't. For Hermes-backed decorators the value is already right, because Hermes' `utf16()` is exact, but every call copies the whole string instead of reading the length. Runtimes with a native `length()` and the default `utf16()` get wrong answers. React Native's `JSCRuntime` is one: built against the system JavaScriptCore and wrapped in a `RuntimeDecorator`, `'a\uD800b'` has length 1 instead of 3 and `'x\uDC00yz'` has 1 instead of 4. With this change both match the undecorated runtime. That setup lives outside this repo, so it isn't a test here. `WithRuntimeDecorator` needs the override too. Otherwise it would inherit the plain forward and `length()` would skip `Around`, which it currently goes through via `utf16()`. `TracingRuntime` no longer records an incidental `Utf16Record` when `length()` is called. That record only came from the `utf16()` fallback. I didn't add a record for `length()`: strings are immutable, and `TracingRuntime` already leaves pure queries like `size(Array)` untraced. X-link: meta/hermes#2205 Test Plan: Added `JSITest.DecoratorForwardsStringLength`. An inner decorator returns a sentinel from `length()` and counts `length()` and `utf16()` calls. The test checks that a `RuntimeDecorator` and a `WithRuntimeDecorator` over it both reach that `length()` without calling `utf16()`, and that `Around` runs once. It fails without the change (it gets 5 from the `utf16()` fallback) and passes with it. The full `APITests` binary passes (357 tests) in a Debug + ASan build, including the SynthTrace and TraceInterpreter tests. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Reviewed By: avp Differential Revision: D123943189 Pulled By: lavenzg fbshipit-source-id: 519fdde9f2ad46ce906567bfd1d94d787015915c
1 parent 4fec40e commit be567d2

11 files changed

Lines changed: 82 additions & 0 deletions

‎packages/react-native/ReactCommon/jsi/jsi/decorator.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,10 @@ class RuntimeDecorator : public Base, private jsi::Instrumentation {
243243
return plain_.utf16(sym);
244244
}
245245

246+
size_t length(const String& str) override {
247+
return plain_.length(str);
248+
}
249+
246250
void getStringData(
247251
const jsi::String& str,
248252
void* ctx,
@@ -822,6 +826,11 @@ class WithRuntimeDecorator : public RuntimeDecorator<Plain, Base> {
822826
return RD::utf16(sym);
823827
}
824828

829+
size_t length(const String& str) override {
830+
Around around{with_};
831+
return RD::length(str);
832+
}
833+
825834
void getStringData(
826835
const jsi::String& str,
827836
void* ctx,

‎packages/react-native/ReactCommon/jsi/jsi/test/testlib.cpp‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,61 @@ TEST_P(JSITest, StringLengthTest) {
138138
EXPECT_EQ(invalid.length(rt), 2);
139139
}
140140

141+
TEST_P(JSITest, DecoratorForwardsStringLength) {
142+
// Decorators should forward length(const String&) to the decorated runtime
143+
// instead of falling back to the default Runtime::length, which copies the
144+
// string via utf16() and bypasses the runtime's own implementation.
145+
class Inner : public RuntimeDecorator<Runtime, Runtime> {
146+
public:
147+
explicit Inner(Runtime& rt) : RuntimeDecorator(rt) {}
148+
149+
size_t length(const String&) override {
150+
++lengthCalls;
151+
return 42;
152+
}
153+
154+
std::u16string utf16(const String& str) override {
155+
++utf16Calls;
156+
return RuntimeDecorator::utf16(str);
157+
}
158+
159+
int lengthCalls = 0;
160+
int utf16Calls = 0;
161+
};
162+
163+
class Outer : public RuntimeDecorator<Runtime, Runtime> {
164+
public:
165+
explicit Outer(Runtime& rt) : RuntimeDecorator(rt) {}
166+
};
167+
168+
struct Count {
169+
void before() {
170+
++beforeCalls;
171+
}
172+
void after() {
173+
++afterCalls;
174+
}
175+
int beforeCalls = 0;
176+
int afterCalls = 0;
177+
};
178+
179+
Inner inner(rt);
180+
String str = String::createFromAscii(inner, "hello");
181+
182+
Outer outer(inner);
183+
EXPECT_EQ(str.length(outer), 42);
184+
EXPECT_EQ(inner.lengthCalls, 1);
185+
EXPECT_EQ(inner.utf16Calls, 0);
186+
187+
Count count;
188+
WithRuntimeDecorator<Count> with(inner, count);
189+
EXPECT_EQ(str.length(with), 42);
190+
EXPECT_EQ(inner.lengthCalls, 2);
191+
EXPECT_EQ(inner.utf16Calls, 0);
192+
EXPECT_EQ(count.beforeCalls, 1);
193+
EXPECT_EQ(count.afterCalls, 1);
194+
}
195+
141196
TEST_P(JSITest, ObjectTest) {
142197
eval("x = {1:2, '3':4, 5:'six', 'seven':['eight', 'nine']}");
143198
Object x = rt.global().getPropertyAsObject(rt, "x");

‎scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12784,6 +12784,7 @@ class facebook::jsi::RuntimeDecorator : public facebook::jsi::Runtime {
1278412784
protected virtual facebook::jsi::WeakObject createWeakObject(const facebook::jsi::Object& o) override;
1278512785
protected virtual size_t byteLength(const facebook::jsi::TypedArray& typedArray) override;
1278612786
protected virtual size_t byteOffset(const facebook::jsi::TypedArray& typedArray) override;
12787+
protected virtual size_t length(const facebook::jsi::String& str) override;
1278712788
protected virtual size_t length(const facebook::jsi::TypedArray& typedArray) override;
1278812789
protected virtual size_t push(const facebook::jsi::Array& a, const facebook::jsi::Value* elements, size_t count) override;
1278912790
protected virtual size_t size(const facebook::jsi::Array& a) override;
@@ -12902,6 +12903,7 @@ class facebook::jsi::WithRuntimeDecorator : public facebook::jsi::RuntimeDecorat
1290212903
protected virtual facebook::jsi::WeakObject createWeakObject(const facebook::jsi::Object& o) override;
1290312904
protected virtual size_t byteLength(const facebook::jsi::TypedArray& typedArray) override;
1290412905
protected virtual size_t byteOffset(const facebook::jsi::TypedArray& typedArray) override;
12906+
protected virtual size_t length(const facebook::jsi::String& str) override;
1290512907
protected virtual size_t length(const facebook::jsi::TypedArray& typedArray) override;
1290612908
protected virtual size_t push(const facebook::jsi::Array& a, const facebook::jsi::Value* elements, size_t count) override;
1290712909
protected virtual size_t size(const facebook::jsi::Array& a) override;

‎scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12400,6 +12400,7 @@ class facebook::jsi::RuntimeDecorator : public facebook::jsi::Runtime {
1240012400
protected virtual facebook::jsi::WeakObject createWeakObject(const facebook::jsi::Object& o) override;
1240112401
protected virtual size_t byteLength(const facebook::jsi::TypedArray& typedArray) override;
1240212402
protected virtual size_t byteOffset(const facebook::jsi::TypedArray& typedArray) override;
12403+
protected virtual size_t length(const facebook::jsi::String& str) override;
1240312404
protected virtual size_t length(const facebook::jsi::TypedArray& typedArray) override;
1240412405
protected virtual size_t push(const facebook::jsi::Array& a, const facebook::jsi::Value* elements, size_t count) override;
1240512406
protected virtual size_t size(const facebook::jsi::Array& a) override;
@@ -12518,6 +12519,7 @@ class facebook::jsi::WithRuntimeDecorator : public facebook::jsi::RuntimeDecorat
1251812519
protected virtual facebook::jsi::WeakObject createWeakObject(const facebook::jsi::Object& o) override;
1251912520
protected virtual size_t byteLength(const facebook::jsi::TypedArray& typedArray) override;
1252012521
protected virtual size_t byteOffset(const facebook::jsi::TypedArray& typedArray) override;
12522+
protected virtual size_t length(const facebook::jsi::String& str) override;
1252112523
protected virtual size_t length(const facebook::jsi::TypedArray& typedArray) override;
1252212524
protected virtual size_t push(const facebook::jsi::Array& a, const facebook::jsi::Value* elements, size_t count) override;
1252312525
protected virtual size_t size(const facebook::jsi::Array& a) override;

‎scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12631,6 +12631,7 @@ class facebook::jsi::RuntimeDecorator : public facebook::jsi::Runtime {
1263112631
protected virtual facebook::jsi::WeakObject createWeakObject(const facebook::jsi::Object& o) override;
1263212632
protected virtual size_t byteLength(const facebook::jsi::TypedArray& typedArray) override;
1263312633
protected virtual size_t byteOffset(const facebook::jsi::TypedArray& typedArray) override;
12634+
protected virtual size_t length(const facebook::jsi::String& str) override;
1263412635
protected virtual size_t length(const facebook::jsi::TypedArray& typedArray) override;
1263512636
protected virtual size_t push(const facebook::jsi::Array& a, const facebook::jsi::Value* elements, size_t count) override;
1263612637
protected virtual size_t size(const facebook::jsi::Array& a) override;
@@ -12749,6 +12750,7 @@ class facebook::jsi::WithRuntimeDecorator : public facebook::jsi::RuntimeDecorat
1274912750
protected virtual facebook::jsi::WeakObject createWeakObject(const facebook::jsi::Object& o) override;
1275012751
protected virtual size_t byteLength(const facebook::jsi::TypedArray& typedArray) override;
1275112752
protected virtual size_t byteOffset(const facebook::jsi::TypedArray& typedArray) override;
12753+
protected virtual size_t length(const facebook::jsi::String& str) override;
1275212754
protected virtual size_t length(const facebook::jsi::TypedArray& typedArray) override;
1275312755
protected virtual size_t push(const facebook::jsi::Array& a, const facebook::jsi::Value* elements, size_t count) override;
1275412756
protected virtual size_t size(const facebook::jsi::Array& a) override;

‎scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14545,6 +14545,7 @@ class facebook::jsi::RuntimeDecorator : public facebook::jsi::Runtime {
1454514545
protected virtual facebook::jsi::WeakObject createWeakObject(const facebook::jsi::Object& o) override;
1454614546
protected virtual size_t byteLength(const facebook::jsi::TypedArray& typedArray) override;
1454714547
protected virtual size_t byteOffset(const facebook::jsi::TypedArray& typedArray) override;
14548+
protected virtual size_t length(const facebook::jsi::String& str) override;
1454814549
protected virtual size_t length(const facebook::jsi::TypedArray& typedArray) override;
1454914550
protected virtual size_t push(const facebook::jsi::Array& a, const facebook::jsi::Value* elements, size_t count) override;
1455014551
protected virtual size_t size(const facebook::jsi::Array& a) override;
@@ -14663,6 +14664,7 @@ class facebook::jsi::WithRuntimeDecorator : public facebook::jsi::RuntimeDecorat
1466314664
protected virtual facebook::jsi::WeakObject createWeakObject(const facebook::jsi::Object& o) override;
1466414665
protected virtual size_t byteLength(const facebook::jsi::TypedArray& typedArray) override;
1466514666
protected virtual size_t byteOffset(const facebook::jsi::TypedArray& typedArray) override;
14667+
protected virtual size_t length(const facebook::jsi::String& str) override;
1466614668
protected virtual size_t length(const facebook::jsi::TypedArray& typedArray) override;
1466714669
protected virtual size_t push(const facebook::jsi::Array& a, const facebook::jsi::Value* elements, size_t count) override;
1466814670
protected virtual size_t size(const facebook::jsi::Array& a) override;

‎scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14227,6 +14227,7 @@ class facebook::jsi::RuntimeDecorator : public facebook::jsi::Runtime {
1422714227
protected virtual facebook::jsi::WeakObject createWeakObject(const facebook::jsi::Object& o) override;
1422814228
protected virtual size_t byteLength(const facebook::jsi::TypedArray& typedArray) override;
1422914229
protected virtual size_t byteOffset(const facebook::jsi::TypedArray& typedArray) override;
14230+
protected virtual size_t length(const facebook::jsi::String& str) override;
1423014231
protected virtual size_t length(const facebook::jsi::TypedArray& typedArray) override;
1423114232
protected virtual size_t push(const facebook::jsi::Array& a, const facebook::jsi::Value* elements, size_t count) override;
1423214233
protected virtual size_t size(const facebook::jsi::Array& a) override;
@@ -14345,6 +14346,7 @@ class facebook::jsi::WithRuntimeDecorator : public facebook::jsi::RuntimeDecorat
1434514346
protected virtual facebook::jsi::WeakObject createWeakObject(const facebook::jsi::Object& o) override;
1434614347
protected virtual size_t byteLength(const facebook::jsi::TypedArray& typedArray) override;
1434714348
protected virtual size_t byteOffset(const facebook::jsi::TypedArray& typedArray) override;
14349+
protected virtual size_t length(const facebook::jsi::String& str) override;
1434814350
protected virtual size_t length(const facebook::jsi::TypedArray& typedArray) override;
1434914351
protected virtual size_t push(const facebook::jsi::Array& a, const facebook::jsi::Value* elements, size_t count) override;
1435014352
protected virtual size_t size(const facebook::jsi::Array& a) override;

‎scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14402,6 +14402,7 @@ class facebook::jsi::RuntimeDecorator : public facebook::jsi::Runtime {
1440214402
protected virtual facebook::jsi::WeakObject createWeakObject(const facebook::jsi::Object& o) override;
1440314403
protected virtual size_t byteLength(const facebook::jsi::TypedArray& typedArray) override;
1440414404
protected virtual size_t byteOffset(const facebook::jsi::TypedArray& typedArray) override;
14405+
protected virtual size_t length(const facebook::jsi::String& str) override;
1440514406
protected virtual size_t length(const facebook::jsi::TypedArray& typedArray) override;
1440614407
protected virtual size_t push(const facebook::jsi::Array& a, const facebook::jsi::Value* elements, size_t count) override;
1440714408
protected virtual size_t size(const facebook::jsi::Array& a) override;
@@ -14520,6 +14521,7 @@ class facebook::jsi::WithRuntimeDecorator : public facebook::jsi::RuntimeDecorat
1452014521
protected virtual facebook::jsi::WeakObject createWeakObject(const facebook::jsi::Object& o) override;
1452114522
protected virtual size_t byteLength(const facebook::jsi::TypedArray& typedArray) override;
1452214523
protected virtual size_t byteOffset(const facebook::jsi::TypedArray& typedArray) override;
14524+
protected virtual size_t length(const facebook::jsi::String& str) override;
1452314525
protected virtual size_t length(const facebook::jsi::TypedArray& typedArray) override;
1452414526
protected virtual size_t push(const facebook::jsi::Array& a, const facebook::jsi::Value* elements, size_t count) override;
1452514527
protected virtual size_t size(const facebook::jsi::Array& a) override;

‎scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9731,6 +9731,7 @@ class facebook::jsi::RuntimeDecorator : public facebook::jsi::Runtime {
97319731
protected virtual facebook::jsi::WeakObject createWeakObject(const facebook::jsi::Object& o) override;
97329732
protected virtual size_t byteLength(const facebook::jsi::TypedArray& typedArray) override;
97339733
protected virtual size_t byteOffset(const facebook::jsi::TypedArray& typedArray) override;
9734+
protected virtual size_t length(const facebook::jsi::String& str) override;
97349735
protected virtual size_t length(const facebook::jsi::TypedArray& typedArray) override;
97359736
protected virtual size_t push(const facebook::jsi::Array& a, const facebook::jsi::Value* elements, size_t count) override;
97369737
protected virtual size_t size(const facebook::jsi::Array& a) override;
@@ -9849,6 +9850,7 @@ class facebook::jsi::WithRuntimeDecorator : public facebook::jsi::RuntimeDecorat
98499850
protected virtual facebook::jsi::WeakObject createWeakObject(const facebook::jsi::Object& o) override;
98509851
protected virtual size_t byteLength(const facebook::jsi::TypedArray& typedArray) override;
98519852
protected virtual size_t byteOffset(const facebook::jsi::TypedArray& typedArray) override;
9853+
protected virtual size_t length(const facebook::jsi::String& str) override;
98529854
protected virtual size_t length(const facebook::jsi::TypedArray& typedArray) override;
98539855
protected virtual size_t push(const facebook::jsi::Array& a, const facebook::jsi::Value* elements, size_t count) override;
98549856
protected virtual size_t size(const facebook::jsi::Array& a) override;

‎scripts/cxx-api/api-snapshots/ReactCommonNewarchCxx.api‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9555,6 +9555,7 @@ class facebook::jsi::RuntimeDecorator : public facebook::jsi::Runtime {
95559555
protected virtual facebook::jsi::WeakObject createWeakObject(const facebook::jsi::Object& o) override;
95569556
protected virtual size_t byteLength(const facebook::jsi::TypedArray& typedArray) override;
95579557
protected virtual size_t byteOffset(const facebook::jsi::TypedArray& typedArray) override;
9558+
protected virtual size_t length(const facebook::jsi::String& str) override;
95589559
protected virtual size_t length(const facebook::jsi::TypedArray& typedArray) override;
95599560
protected virtual size_t push(const facebook::jsi::Array& a, const facebook::jsi::Value* elements, size_t count) override;
95609561
protected virtual size_t size(const facebook::jsi::Array& a) override;
@@ -9673,6 +9674,7 @@ class facebook::jsi::WithRuntimeDecorator : public facebook::jsi::RuntimeDecorat
96739674
protected virtual facebook::jsi::WeakObject createWeakObject(const facebook::jsi::Object& o) override;
96749675
protected virtual size_t byteLength(const facebook::jsi::TypedArray& typedArray) override;
96759676
protected virtual size_t byteOffset(const facebook::jsi::TypedArray& typedArray) override;
9677+
protected virtual size_t length(const facebook::jsi::String& str) override;
96769678
protected virtual size_t length(const facebook::jsi::TypedArray& typedArray) override;
96779679
protected virtual size_t push(const facebook::jsi::Array& a, const facebook::jsi::Value* elements, size_t count) override;
96789680
protected virtual size_t size(const facebook::jsi::Array& a) override;

0 commit comments

Comments
 (0)