Skip to content

Commit e81441a

Browse files
committed
[JSC] Add boolean/undefined/null handling to string join functions
https://bugs.webkit.org/show_bug.cgi?id=328769 rdar://190893702 Reviewed by Sosuke Suzuki. This patch extends string join functions with fast boolean/undefined/null handlings. We also rename JSOnlyStringsAndInt32sJoiner to JSExactLengthJoiner. Test: JSTests/stress/array-join-booleans-and-nullish.js * JSTests/stress/array-join-booleans-and-nullish.js: Added. (shouldBe): (join): * JSTests/stress/array-join-dfg-tostring-mutation.js: * Source/JavaScriptCore/dfg/DFGOperations.cpp: (JSC::DFG::arrayJoinWithStringSeparator): * Source/JavaScriptCore/runtime/ArrayPrototype.cpp: (JSC::JSC_DEFINE_HOST_FUNCTION): * Source/JavaScriptCore/runtime/ArrayPrototypeInlines.h: (JSC::fastArrayJoin): * Source/JavaScriptCore/runtime/JSStringJoiner.cpp: (JSC::joinStrings): (JSC::JSExactLengthJoiner::joinImpl): (JSC::JSOnlyStringsAndInt32sJoiner::joinImpl): Deleted. * Source/JavaScriptCore/runtime/JSStringJoiner.h: (JSC::JSExactLengthJoiner::JSExactLengthJoiner): (JSC::JSExactLengthJoiner::tryJoin): (JSC::JSOnlyStringsAndInt32sJoiner::JSOnlyStringsAndInt32sJoiner): Deleted. (JSC::JSOnlyStringsAndInt32sJoiner::tryJoin): Deleted. Canonical link: https://commits.webkit.org/324799@main
1 parent 0927910 commit e81441a

7 files changed

Lines changed: 82 additions & 16 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
function shouldBe(actual, expected) {
2+
if (actual !== expected)
3+
throw new Error('bad value: ' + actual + ', expected: ' + expected);
4+
}
5+
6+
function join(array, separator) {
7+
return array.join(separator);
8+
}
9+
noInline(join);
10+
11+
var rope = "ro" + String.fromCharCode(112) + "e";
12+
var cases = [
13+
[["a", true, 1, false, null, undefined, "b"], ",", "a,true,1,false,,,b"],
14+
[[true, false], "", "truefalse"],
15+
[[null, undefined], ",", ","],
16+
[[undefined], ",", ""],
17+
[[true], ",", "true"],
18+
[[false], "", "false"],
19+
[[null], "", ""],
20+
[[undefined], "", ""],
21+
[[42], "", "42"],
22+
[["x", true, rope, -1], "--", "x--true--rope---1"],
23+
[["\u3042", false, null, 2], ",", "\u3042,false,,2"],
24+
[["a", true], "\u3042", "a\u3042true"],
25+
[[1, 2, false], ",", "1,2,false"],
26+
[["a", 1.5, true], ",", "a,1.5,true"],
27+
[[null, undefined, null], "", ""],
28+
[[null, "\u3042", undefined], "", "\u3042"],
29+
[[undefined, "a", null], "\u3042", "\u3042a\u3042"],
30+
];
31+
32+
for (var i = 0; i < testLoopCount; ++i) {
33+
var [array, separator, expected] = cases[i % cases.length];
34+
shouldBe(join(array, separator), expected);
35+
}
36+
37+
var holey = ["a", , true];
38+
shouldBe(join(holey, ","), "a,,true");
39+
Array.prototype[1] = "proto";
40+
shouldBe(join(holey, ","), "a,proto,true");
41+
shouldBe(join(["a", undefined, true], ","), "a,,true");
42+
shouldBe(join(["a", null, true], ","), "a,,true");
43+
delete Array.prototype[1];

‎JSTests/stress/array-join-dfg-tostring-mutation.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ for (var i = 0; i < testLoopCount; ++i)
4848
shouldBe(joinHolePunch(), "13");
4949

5050
// 5. ToString replaces an Int32 element with a non-string/non-int32 value
51-
// (object). The empty-separator JSOnlyStringsAndInt32sJoiner must bail out
51+
// (object). The empty-separator JSExactLengthJoiner must bail out
5252
// of its fast path and the slow path must still produce spec-correct output.
5353
function joinNonStringElement() {
5454
var arr = [1, 2, 3];

‎Source/JavaScriptCore/dfg/DFGOperations.cpp‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1751,8 +1751,10 @@ static ALWAYS_INLINE JSString* arrayJoinWithStringSeparator(JSGlobalObject* glob
17511751
unsigned length = array->length();
17521752
if (!separator->length() && (array->indexingType() == ArrayWithContiguous || array->indexingType() == ArrayWithInt32)) {
17531753
auto* butterfly = array->butterfly();
1754-
JSOnlyStringsAndInt32sJoiner joiner(StringView { });
1755-
auto* joined = joiner.tryJoin<ContiguousShape>(globalObject, butterfly->contiguous().data(), length);
1754+
JSExactLengthJoiner joiner(StringView { });
1755+
auto* joined = array->indexingType() == ArrayWithInt32
1756+
? joiner.tryJoin<Int32Shape>(globalObject, butterfly->contiguous().data(), length)
1757+
: joiner.tryJoin<ContiguousShape>(globalObject, butterfly->contiguous().data(), length);
17561758
RETURN_IF_EXCEPTION(scope, { });
17571759
if (joined)
17581760
return joined;

‎Source/JavaScriptCore/runtime/ArrayPrototype.cpp‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,10 @@ JSC_DEFINE_HOST_FUNCTION(arrayProtoFuncJoin, (JSGlobalObject* globalObject, Call
443443
if (!separatorString->length() && (array->indexingType() == ArrayWithContiguous || array->indexingType() == ArrayWithInt32)) {
444444
auto* butterfly = array->butterfly();
445445
unsigned length = butterfly->publicLength();
446-
JSOnlyStringsAndInt32sJoiner joiner(StringView { });
447-
auto* joined = joiner.tryJoin<ContiguousShape>(globalObject, butterfly->contiguous().data(), length);
446+
JSExactLengthJoiner joiner(StringView { });
447+
auto* joined = array->indexingType() == ArrayWithInt32
448+
? joiner.tryJoin<Int32Shape>(globalObject, butterfly->contiguous().data(), length)
449+
: joiner.tryJoin<ContiguousShape>(globalObject, butterfly->contiguous().data(), length);
448450
RETURN_IF_EXCEPTION(scope, { });
449451
if (joined)
450452
return JSValue::encode(joined);

‎Source/JavaScriptCore/runtime/ArrayPrototypeInlines.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,8 @@ ALWAYS_INLINE JSString* fastArrayJoin(JSGlobalObject* globalObject, JSObject* th
317317
break;
318318
auto data = butterfly.contiguous().data();
319319

320-
JSOnlyStringsAndInt32sJoiner onlyInt32sJoiner(separator);
321-
if (auto joined = onlyInt32sJoiner.tryJoin<Int32Shape>(globalObject, data, length))
320+
JSExactLengthJoiner exactLengthJoiner(separator);
321+
if (auto joined = exactLengthJoiner.tryJoin<Int32Shape>(globalObject, data, length))
322322
RELEASE_AND_RETURN(scope, joined);
323323
RETURN_IF_EXCEPTION(scope, { });
324324

@@ -349,8 +349,8 @@ ALWAYS_INLINE JSString* fastArrayJoin(JSGlobalObject* globalObject, JSObject* th
349349
auto data = butterfly.contiguous().data();
350350
bool holesKnownToBeOK = false;
351351

352-
JSOnlyStringsAndInt32sJoiner onlyStringsJoiner(separator);
353-
if (auto joined = onlyStringsJoiner.tryJoin<ContiguousShape>(globalObject, data, length))
352+
JSExactLengthJoiner exactLengthJoiner(separator);
353+
if (auto joined = exactLengthJoiner.tryJoin<ContiguousShape>(globalObject, data, length))
354354
RELEASE_AND_RETURN(scope, joined);
355355
RETURN_IF_EXCEPTION(scope, { });
356356

‎Source/JavaScriptCore/runtime/JSStringJoiner.cpp‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@ static inline String joinStrings(JSGlobalObject* globalObject, const WriteBarrie
205205
data = data.subspan(length);
206206
return;
207207
}
208+
if (value.isBoolean()) {
209+
appendStringToData(data, StringView(value.isTrue() ? "true"_s : "false"_s));
210+
return;
211+
}
212+
if (value.isUndefinedOrNull())
213+
return;
208214
}
209215
ASSERT(value.isInt32());
210216
appendStringToData(data, value.asInt32());
@@ -284,7 +290,7 @@ JSString* JSStringJoiner::joinImpl(JSGlobalObject* globalObject)
284290
}
285291

286292
template<IndexingType indexingShape>
287-
JSString* JSOnlyStringsAndInt32sJoiner::joinImpl(JSGlobalObject* globalObject, const WriteBarrier<Unknown>* data, unsigned length)
293+
JSString* JSExactLengthJoiner::joinImpl(JSGlobalObject* globalObject, const WriteBarrier<Unknown>* data, unsigned length)
288294
{
289295
VM& vm = globalObject->vm();
290296
auto scope = DECLARE_THROW_SCOPE(vm);
@@ -315,8 +321,8 @@ JSString* JSOnlyStringsAndInt32sJoiner::joinImpl(JSGlobalObject* globalObject, c
315321
return jsString(vm, WTF::move(result));
316322
}
317323

318-
template JSString* JSOnlyStringsAndInt32sJoiner::joinImpl<Int32Shape>(JSGlobalObject*, const WriteBarrier<Unknown>*, unsigned);
319-
template JSString* JSOnlyStringsAndInt32sJoiner::joinImpl<ContiguousShape>(JSGlobalObject*, const WriteBarrier<Unknown>*, unsigned);
324+
template JSString* JSExactLengthJoiner::joinImpl<Int32Shape>(JSGlobalObject*, const WriteBarrier<Unknown>*, unsigned);
325+
template JSString* JSExactLengthJoiner::joinImpl<ContiguousShape>(JSGlobalObject*, const WriteBarrier<Unknown>*, unsigned);
320326

321327
} // namespace JSC
322328

‎Source/JavaScriptCore/runtime/JSStringJoiner.h‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,12 @@ ALWAYS_INLINE void JSStringJoiner::appendNumber(VM& vm, double value)
217217

218218
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
219219

220-
// Avoids the overhead of accumulating intermediate vectors of values when
221-
// we're only joining existing strings.
222-
class JSOnlyStringsAndInt32sJoiner {
220+
// Joins straight from array storage, without accumulating an intermediate vector of values, when
221+
// every element's string length is known without converting it: a string, an int32, a boolean,
222+
// undefined, or null. The result is allocated once at its exact length.
223+
class JSExactLengthJoiner {
223224
public:
224-
JSOnlyStringsAndInt32sJoiner(StringView separator)
225+
JSExactLengthJoiner(StringView separator)
225226
: m_separator(separator)
226227
, m_isAll8Bit(m_separator.is8Bit())
227228
{
@@ -239,6 +240,12 @@ class JSOnlyStringsAndInt32sJoiner {
239240
if constexpr (indexingShape == ContiguousShape) {
240241
if (value.isString())
241242
return asString(value);
243+
if (value.isBoolean()) {
244+
VM& vm = globalObject->vm();
245+
return value.isTrue() ? vm.smallStrings.trueString() : vm.smallStrings.falseString();
246+
}
247+
if (value.isUndefinedOrNull())
248+
return jsEmptyString(globalObject->vm());
242249
if (!value.isInt32())
243250
return nullptr;
244251
}
@@ -259,6 +266,12 @@ class JSOnlyStringsAndInt32sJoiner {
259266
m_isAll8Bit &= string->is8Bit();
260267
continue;
261268
}
269+
if (value.isBoolean()) {
270+
m_accumulatedStringsLength += value.isTrue() ? "true"_s.length() : "false"_s.length();
271+
continue;
272+
}
273+
if (value.isUndefinedOrNull())
274+
continue;
262275
if (!value.isInt32())
263276
return nullptr;
264277
}

0 commit comments

Comments
 (0)