LibJS: Prefer Value::to_string() over to_byte_string() in more places

We should always prefer working with String, and Value::to_string() may
even return a cached String if the Value refers to a primitive string,
but no caching occurs for ByteString.
This commit is contained in:
Andreas Kling 2025-03-16 17:44:29 -05:00
parent a8285f255b
commit bafee55f9b
6 changed files with 15 additions and 17 deletions

View file

@ -243,10 +243,10 @@ ThrowCompletionOr<double> compare_array_elements(VM& vm, Value x, Value y, Funct
}
// 5. Let xString be ? ToString(x).
auto x_string = PrimitiveString::create(vm, TRY(x.to_byte_string(vm)));
auto x_string = PrimitiveString::create(vm, TRY(x.to_string(vm)));
// 6. Let yString be ? ToString(y).
auto y_string = PrimitiveString::create(vm, TRY(y.to_byte_string(vm)));
auto y_string = PrimitiveString::create(vm, TRY(y.to_string(vm)));
// 7. Let xSmaller be ! IsLessThan(xString, yString, true).
auto x_smaller = MUST(is_less_than(vm, x_string, y_string, true));

View file

@ -838,9 +838,9 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::join)
};
auto length = TRY(length_of_array_like(vm, this_object));
ByteString separator = ",";
String separator = ","_string;
if (!vm.argument(0).is_undefined())
separator = TRY(vm.argument(0).to_byte_string(vm));
separator = TRY(vm.argument(0).to_string(vm));
StringBuilder builder;
for (size_t i = 0; i < length; ++i) {
if (i > 0)
@ -848,11 +848,11 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::join)
auto value = TRY(this_object->get(i));
if (value.is_nullish())
continue;
auto string = TRY(value.to_byte_string(vm));
auto string = TRY(value.to_string(vm));
builder.append(string);
}
return PrimitiveString::create(vm, builder.to_byte_string());
return PrimitiveString::create(vm, builder.to_string_without_validation());
}
// 23.1.3.19 Array.prototype.keys ( ), https://tc39.es/ecma262/#sec-array.prototype.keys
@ -1650,7 +1650,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::to_locale_string)
auto locale_string_result = TRY(value.invoke(vm, vm.names.toLocaleString, locales, options));
// ii. Set R to the string-concatenation of R and S.
auto string = TRY(locale_string_result.to_byte_string(vm));
auto string = TRY(locale_string_result.to_string(vm));
builder.append(string);
}
@ -1658,7 +1658,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::to_locale_string)
}
// 7. Return R.
return PrimitiveString::create(vm, builder.to_byte_string());
return PrimitiveString::create(vm, builder.to_string_without_validation());
}
// 23.1.3.33 Array.prototype.toReversed ( ), https://tc39.es/ecma262/#sec-array.prototype.toreversed

View file

@ -167,13 +167,11 @@ JS_DEFINE_NATIVE_FUNCTION(StringConstructor::raw)
// 7. Let nextIndex be 0.
// 8. Repeat,
for (size_t i = 0; i < literal_count; ++i) {
auto next_key = ByteString::number(i);
// a. Let nextLiteralVal be ? Get(literals, ! ToString(𝔽(nextIndex))).
auto next_literal_value = TRY(literals->get(next_key));
auto next_literal_value = TRY(literals->get(PropertyKey(i)));
// b. Let nextLiteral be ? ToString(nextLiteralVal).
auto next_literal = TRY(next_literal_value.to_byte_string(vm));
auto next_literal = TRY(next_literal_value.to_string(vm));
// c. Set R to the string-concatenation of R and nextLiteral.
builder.append(next_literal);
@ -188,7 +186,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringConstructor::raw)
auto next_substitution_value = vm.argument(i + 1);
// ii. Let nextSub be ? ToString(nextSubVal).
auto next_substitution = TRY(next_substitution_value.to_byte_string(vm));
auto next_substitution = TRY(next_substitution_value.to_string(vm));
// iii. Set R to the string-concatenation of R and nextSub.
builder.append(next_substitution);

View file

@ -1987,7 +1987,7 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::to_locale_string)
if (!next_element.is_nullish()) {
// i. Let S be ? ToString(? Invoke(nextElement, "toLocaleString", « locales, options »)).
auto locale_string_value = TRY(next_element.invoke(vm, vm.names.toLocaleString, locales, options));
auto locale_string = TRY(locale_string_value.to_byte_string(vm));
auto locale_string = TRY(locale_string_value.to_string(vm));
// ii. Set R to the string-concatenation of R and S.
builder.append(locale_string);
@ -1997,7 +1997,7 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::to_locale_string)
}
// 7. Return R.
return PrimitiveString::create(vm, builder.to_byte_string());
return PrimitiveString::create(vm, builder.to_string_without_validation());
}
// 23.2.3.32 %TypedArray%.prototype.toReversed ( ), https://tc39.es/ecma262/#sec-%typedarray%.prototype.toreversed

View file

@ -26,7 +26,7 @@ TESTJS_GLOBAL_FUNCTION(is_strict_mode, isStrictMode, 0)
TESTJS_GLOBAL_FUNCTION(can_parse_source, canParseSource)
{
auto source = TRY(vm.argument(0).to_byte_string(vm));
auto source = TRY(vm.argument(0).to_string(vm));
auto parser = JS::Parser(JS::Lexer(source));
(void)parser.parse_program();
return JS::Value(!parser.has_errors());

View file

@ -21,7 +21,7 @@ TESTJS_GLOBAL_FUNCTION(read_binary_wasm_file, readBinaryWasmFile)
return StringView { error_string, strlen(error_string) };
};
auto filename = TRY(vm.argument(0).to_byte_string(vm));
auto filename = TRY(vm.argument(0).to_string(vm));
auto file = Core::File::open(filename, Core::File::OpenMode::Read);
if (file.is_error())
return vm.throw_completion<JS::TypeError>(error_code_to_string(file.error().code()));