1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 12:07:45 +00:00

LibJS: Teach String.prototype.concat() to create rope strings

Defer serialization of the concatenated strings until later. This is
used heavily in SunSpider's string-validate-input subtest, which
sees a small progression.
This commit is contained in:
Andreas Kling 2022-08-06 00:48:22 +02:00 committed by Linus Groh
parent 8ed28890e4
commit e0d7d3c3d5

View file

@ -525,14 +525,26 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::trim_end)
// 22.1.3.5 String.prototype.concat ( ...args ), https://tc39.es/ecma262/#sec-string.prototype.concat // 22.1.3.5 String.prototype.concat ( ...args ), https://tc39.es/ecma262/#sec-string.prototype.concat
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::concat) JS_DEFINE_NATIVE_FUNCTION(StringPrototype::concat)
{ {
auto string = TRY(ak_string_from(vm, global_object)); // 1. Let O be ? RequireObjectCoercible(this value).
StringBuilder builder; auto object = TRY(require_object_coercible(global_object, vm.this_value(global_object)));
builder.append(string);
// 2. Let S be ? ToString(O).
auto* string = TRY(object.to_primitive_string(global_object));
// 3. Let R be S.
auto* result = string;
// 4. For each element next of args, do
for (size_t i = 0; i < vm.argument_count(); ++i) { for (size_t i = 0; i < vm.argument_count(); ++i) {
auto string_argument = TRY(vm.argument(i).to_string(global_object)); // a. Let nextString be ? ToString(next).
builder.append(string_argument); auto* next_string = TRY(vm.argument(i).to_primitive_string(global_object));
// b. Set R to the string-concatenation of R and nextString.
result = js_rope_string(vm, *result, *next_string);
} }
return js_string(vm, builder.to_string());
// 5. Return R.
return result;
} }
// 22.1.3.24 String.prototype.substring ( start, end ), https://tc39.es/ecma262/#sec-string.prototype.substring // 22.1.3.24 String.prototype.substring ( start, end ), https://tc39.es/ecma262/#sec-string.prototype.substring