LibJS: Use Value::to_byte_string() in fewer places

This commit is contained in:
Andreas Kling 2025-03-28 14:32:54 +00:00 committed by Tim Flynn
commit 2462a6b0fa
Notes: github-actions[bot] 2025-03-28 16:32:47 +00:00
8 changed files with 28 additions and 29 deletions

View file

@ -87,7 +87,7 @@ JS_DEFINE_NATIVE_FUNCTION($262Object::detach_array_buffer)
JS_DEFINE_NATIVE_FUNCTION($262Object::eval_script) JS_DEFINE_NATIVE_FUNCTION($262Object::eval_script)
{ {
auto source_text = TRY(vm.argument(0).to_byte_string(vm)); auto source_text = TRY(vm.argument(0).to_string(vm));
// 1. Let hostDefined be any host-defined values for the provided sourceText (obtained in an implementation dependent manner) // 1. Let hostDefined be any host-defined values for the provided sourceText (obtained in an implementation dependent manner)

View file

@ -36,7 +36,7 @@ void GlobalObject::visit_edges(Cell::Visitor& visitor)
JS_DEFINE_NATIVE_FUNCTION(GlobalObject::print) JS_DEFINE_NATIVE_FUNCTION(GlobalObject::print)
{ {
auto string = TRY(vm.argument(0).to_byte_string(vm)); auto string = TRY(vm.argument(0).to_string(vm));
outln("{}", string); outln("{}", string);
return js_undefined(); return js_undefined();
} }

View file

@ -55,7 +55,7 @@ ThrowCompletionOr<GC::Ref<Object>> AggregateErrorConstructor::construct(Function
// 3. If message is not undefined, then // 3. If message is not undefined, then
if (!message.is_undefined()) { if (!message.is_undefined()) {
// a. Let msg be ? ToString(message). // a. Let msg be ? ToString(message).
auto msg = TRY(message.to_byte_string(vm)); auto msg = TRY(message.to_string(vm));
// b. Perform CreateNonEnumerableDataPropertyOrThrow(O, "message", msg). // b. Perform CreateNonEnumerableDataPropertyOrThrow(O, "message", msg).
aggregate_error->create_non_enumerable_data_property_or_throw(vm.names.message, PrimitiveString::create(vm, msg)); aggregate_error->create_non_enumerable_data_property_or_throw(vm.names.message, PrimitiveString::create(vm, msg));

View file

@ -92,7 +92,7 @@ JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_exponential)
// 4. If x is not finite, return Number::toString(x). // 4. If x is not finite, return Number::toString(x).
if (!number_value.is_finite_number()) if (!number_value.is_finite_number())
return PrimitiveString::create(vm, MUST(number_value.to_byte_string(vm))); return PrimitiveString::create(vm, MUST(number_value.to_string(vm)));
// 5. If f < 0 or f > 100, throw a RangeError exception. // 5. If f < 0 or f > 100, throw a RangeError exception.
if (fraction_digits < 0 || fraction_digits > 100) if (fraction_digits < 0 || fraction_digits > 100)
@ -220,7 +220,7 @@ JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_fixed)
// 6. If x is not finite, return Number::toString(x). // 6. If x is not finite, return Number::toString(x).
if (!number_value.is_finite_number()) if (!number_value.is_finite_number())
return PrimitiveString::create(vm, TRY(number_value.to_byte_string(vm))); return PrimitiveString::create(vm, TRY(number_value.to_string(vm)));
// 7. Set x to (x). // 7. Set x to (x).
auto number = number_value.as_double(); auto number = number_value.as_double();
@ -236,7 +236,7 @@ JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_fixed)
// 10. If x ≥ 10^21, then // 10. If x ≥ 10^21, then
// a. Let m be ! ToString(𝔽(x)). // a. Let m be ! ToString(𝔽(x)).
if (number >= 1e+21) if (number >= 1e+21)
return PrimitiveString::create(vm, MUST(number_value.to_byte_string(vm))); return PrimitiveString::create(vm, MUST(number_value.to_string(vm)));
// 11. Else, // 11. Else,
// a. Let n be an integer for which n / (10^f) - x is as close to zero as possible. If there are two such n, pick the larger n. // a. Let n be an integer for which n / (10^f) - x is as close to zero as possible. If there are two such n, pick the larger n.
@ -289,14 +289,14 @@ JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_precision)
// 2. If precision is undefined, return ! ToString(x). // 2. If precision is undefined, return ! ToString(x).
if (precision_value.is_undefined()) if (precision_value.is_undefined())
return PrimitiveString::create(vm, MUST(number_value.to_byte_string(vm))); return PrimitiveString::create(vm, MUST(number_value.to_string(vm)));
// 3. Let p be ? ToIntegerOrInfinity(precision). // 3. Let p be ? ToIntegerOrInfinity(precision).
auto precision = TRY(precision_value.to_integer_or_infinity(vm)); auto precision = TRY(precision_value.to_integer_or_infinity(vm));
// 4. If x is not finite, return Number::toString(x). // 4. If x is not finite, return Number::toString(x).
if (!number_value.is_finite_number()) if (!number_value.is_finite_number())
return PrimitiveString::create(vm, MUST(number_value.to_byte_string(vm))); return PrimitiveString::create(vm, MUST(number_value.to_string(vm)));
// 5. If p < 1 or p > 100, throw a RangeError exception. // 5. If p < 1 or p > 100, throw a RangeError exception.
if ((precision < 1) || (precision > 100)) if ((precision < 1) || (precision > 100))
@ -428,7 +428,7 @@ JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_string)
// 5. If radixMV = 10, return ! ToString(x). // 5. If radixMV = 10, return ! ToString(x).
if (radix_mv == 10) if (radix_mv == 10)
return PrimitiveString::create(vm, MUST(number_value.to_byte_string(vm))); return PrimitiveString::create(vm, MUST(number_value.to_string(vm)));
// 6. Return the String representation of this Number value using the radix specified by radixMV. Letters a-z are used for digits with values 10 through 35. The precise algorithm is implementation-defined, however the algorithm should be a generalization of that specified in 6.1.6.1.20. // 6. Return the String representation of this Number value using the radix specified by radixMV. Letters a-z are used for digits with values 10 through 35. The precise algorithm is implementation-defined, however the algorithm should be a generalization of that specified in 6.1.6.1.20.
if (number_value.is_positive_infinity()) if (number_value.is_positive_infinity())

View file

@ -481,7 +481,6 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::exec)
// 22.2.6.4 get RegExp.prototype.flags, https://tc39.es/ecma262/#sec-get-regexp.prototype.flags // 22.2.6.4 get RegExp.prototype.flags, https://tc39.es/ecma262/#sec-get-regexp.prototype.flags
JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::flags) JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::flags)
{ {
// 1. Let R be the this value. // 1. Let R be the this value.
// 2. If Type(R) is not Object, throw a TypeError exception. // 2. If Type(R) is not Object, throw a TypeError exception.
auto regexp_object = TRY(this_object(vm)); auto regexp_object = TRY(this_object(vm));
@ -513,7 +512,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::flags)
#undef __JS_ENUMERATE #undef __JS_ENUMERATE
// 20. Return result. // 20. Return result.
return PrimitiveString::create(vm, builder.to_byte_string()); return PrimitiveString::create(vm, builder.to_string_without_validation());
} }
// 22.2.6.8 RegExp.prototype [ @@match ] ( string ), https://tc39.es/ecma262/#sec-regexp.prototype-@@match // 22.2.6.8 RegExp.prototype [ @@match ] ( string ), https://tc39.es/ecma262/#sec-regexp.prototype-@@match
@ -530,7 +529,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_match)
// 4. Let flags be ? ToString(? Get(rx, "flags")). // 4. Let flags be ? ToString(? Get(rx, "flags")).
auto flags_value = TRY(regexp_object->get(vm.names.flags)); auto flags_value = TRY(regexp_object->get(vm.names.flags));
auto flags = TRY(flags_value.to_byte_string(vm)); auto flags = TRY(flags_value.to_string(vm));
// 5. If flags does not contain "g", then // 5. If flags does not contain "g", then
if (!flags.contains('g')) { if (!flags.contains('g')) {
@ -573,7 +572,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_match)
// 1. Let matchStr be ? ToString(? Get(result, "0")). // 1. Let matchStr be ? ToString(? Get(result, "0")).
auto match_value = TRY(result.get(0)); auto match_value = TRY(result.get(0));
auto match_str = TRY(match_value.to_byte_string(vm)); auto match_str = TRY(match_value.to_string(vm));
// 2. Perform ! CreateDataPropertyOrThrow(A, ! ToString(𝔽(n)), matchStr). // 2. Perform ! CreateDataPropertyOrThrow(A, ! ToString(𝔽(n)), matchStr).
MUST(array->create_data_property_or_throw(n, PrimitiveString::create(vm, match_str))); MUST(array->create_data_property_or_throw(n, PrimitiveString::create(vm, match_str)));
@ -606,7 +605,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_match_all)
// 5. Let flags be ? ToString(? Get(R, "flags")). // 5. Let flags be ? ToString(? Get(R, "flags")).
auto flags_value = TRY(regexp_object->get(vm.names.flags)); auto flags_value = TRY(regexp_object->get(vm.names.flags));
auto flags = TRY(flags_value.to_byte_string(vm)); auto flags = TRY(flags_value.to_string(vm));
// Steps 9-12 are performed early so that flags can be moved. // Steps 9-12 are performed early so that flags can be moved.
@ -651,13 +650,13 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_replace)
// 6. If functionalReplace is false, then // 6. If functionalReplace is false, then
if (!replace_value.is_function()) { if (!replace_value.is_function()) {
// a. Set replaceValue to ? ToString(replaceValue). // a. Set replaceValue to ? ToString(replaceValue).
auto replace_string = TRY(replace_value.to_byte_string(vm)); auto replace_string = TRY(replace_value.to_string(vm));
replace_value = PrimitiveString::create(vm, move(replace_string)); replace_value = PrimitiveString::create(vm, move(replace_string));
} }
// 7. Let flags be ? ToString(? Get(rx, "flags")). // 7. Let flags be ? ToString(? Get(rx, "flags")).
auto flags_value = TRY(regexp_object->get(vm.names.flags)); auto flags_value = TRY(regexp_object->get(vm.names.flags));
auto flags = TRY(flags_value.to_byte_string(vm)); auto flags = TRY(flags_value.to_string(vm));
// 8. If flags contains "g", let global be true. Otherwise, let global be false. // 8. If flags contains "g", let global be true. Otherwise, let global be false.
bool global = flags.contains('g'); bool global = flags.contains('g');
@ -694,7 +693,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_replace)
// 1. Let matchStr be ? ToString(? Get(result, "0")). // 1. Let matchStr be ? ToString(? Get(result, "0")).
auto match_value = TRY(result.get(vm, 0)); auto match_value = TRY(result.get(vm, 0));
auto match_str = TRY(match_value.to_byte_string(vm)); auto match_str = TRY(match_value.to_string(vm));
// 2. If matchStr is the empty String, then // 2. If matchStr is the empty String, then
if (match_str.is_empty()) { if (match_str.is_empty()) {
@ -746,7 +745,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_replace)
// ii. If capN is not undefined, then // ii. If capN is not undefined, then
if (!capture.is_undefined()) { if (!capture.is_undefined()) {
// 1. Set capN to ? ToString(capN). // 1. Set capN to ? ToString(capN).
capture = PrimitiveString::create(vm, TRY(capture.to_byte_string(vm))); capture = PrimitiveString::create(vm, TRY(capture.to_string(vm)));
} }
// iii. Append capN as the last element of captures. // iii. Append capN as the last element of captures.
@ -810,13 +809,13 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_replace)
// 16. If nextSourcePosition ≥ lengthS, return accumulatedResult. // 16. If nextSourcePosition ≥ lengthS, return accumulatedResult.
if (next_source_position >= string.length_in_code_units()) if (next_source_position >= string.length_in_code_units())
return PrimitiveString::create(vm, accumulated_result.to_byte_string()); return PrimitiveString::create(vm, accumulated_result.to_string_without_validation());
// 17. Return the string-concatenation of accumulatedResult and the substring of S from nextSourcePosition. // 17. Return the string-concatenation of accumulatedResult and the substring of S from nextSourcePosition.
auto substring = string.substring_view(next_source_position); auto substring = string.substring_view(next_source_position);
accumulated_result.append(substring); accumulated_result.append(substring);
return PrimitiveString::create(vm, accumulated_result.to_byte_string()); return PrimitiveString::create(vm, accumulated_result.to_string_without_validation());
} }
// 22.2.6.12 RegExp.prototype [ @@search ] ( string ), https://tc39.es/ecma262/#sec-regexp.prototype-@@search // 22.2.6.12 RegExp.prototype [ @@search ] ( string ), https://tc39.es/ecma262/#sec-regexp.prototype-@@search
@ -901,7 +900,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_split)
// 5. Let flags be ? ToString(? Get(rx, "flags")). // 5. Let flags be ? ToString(? Get(rx, "flags")).
auto flags_value = TRY(regexp_object->get(vm.names.flags)); auto flags_value = TRY(regexp_object->get(vm.names.flags));
auto flags = TRY(flags_value.to_byte_string(vm)); auto flags = TRY(flags_value.to_string(vm));
// 6. If flags contains "u" or flags contains "v", let unicodeMatching be true. // 6. If flags contains "u" or flags contains "v", let unicodeMatching be true.
// 7. Else, let unicodeMatching be false. // 7. Else, let unicodeMatching be false.
@ -909,7 +908,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_split)
// 8. If flags contains "y", let newFlags be flags. // 8. If flags contains "y", let newFlags be flags.
// 9. Else, let newFlags be the string-concatenation of flags and "y". // 9. Else, let newFlags be the string-concatenation of flags and "y".
auto new_flags = flags.find('y').has_value() ? move(flags) : ByteString::formatted("{}y", flags); auto new_flags = flags.bytes_as_string_view().find('y').has_value() ? move(flags) : MUST(String::formatted("{}y", flags));
// 10. Let splitter be ? Construct(C, « rx, newFlags »). // 10. Let splitter be ? Construct(C, « rx, newFlags »).
auto splitter = TRY(construct(vm, *constructor, regexp_object, PrimitiveString::create(vm, move(new_flags)))); auto splitter = TRY(construct(vm, *constructor, regexp_object, PrimitiveString::create(vm, move(new_flags))));
@ -1066,11 +1065,11 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::to_string)
// 3. Let pattern be ? ToString(? Get(R, "source")). // 3. Let pattern be ? ToString(? Get(R, "source")).
auto source_attr = TRY(regexp_object->get(vm.names.source)); auto source_attr = TRY(regexp_object->get(vm.names.source));
auto pattern = TRY(source_attr.to_byte_string(vm)); auto pattern = TRY(source_attr.to_string(vm));
// 4. Let flags be ? ToString(? Get(R, "flags")). // 4. Let flags be ? ToString(? Get(R, "flags")).
auto flags_attr = TRY(regexp_object->get(vm.names.flags)); auto flags_attr = TRY(regexp_object->get(vm.names.flags));
auto flags = TRY(flags_attr.to_byte_string(vm)); auto flags = TRY(flags_attr.to_string(vm));
// 5. Let result be the string-concatenation of "/", pattern, "/", and flags. // 5. Let result be the string-concatenation of "/", pattern, "/", and flags.
// 6. Return result. // 6. Return result.

View file

@ -53,7 +53,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpStringIteratorPrototype::next)
auto match_object = TRY(match.to_object(vm)); auto match_object = TRY(match.to_object(vm));
auto match_string_value = TRY(match_object->get(0)); auto match_string_value = TRY(match_object->get(0));
auto match_string = TRY(match_string_value.to_byte_string(vm)); auto match_string = TRY(match_string_value.to_string(vm));
if (match_string.is_empty()) { if (match_string.is_empty()) {
auto last_index_value = TRY(iterator->regexp_object().get(vm.names.lastIndex)); auto last_index_value = TRY(iterator->regexp_object().get(vm.names.lastIndex));
auto last_index = TRY(last_index_value.to_length(vm)); auto last_index = TRY(last_index_value.to_length(vm));

View file

@ -71,7 +71,7 @@ JS_DEFINE_NATIVE_FUNCTION(ConsoleGlobalEnvironmentExtensions::$_function)
auto* console_global_object = TRY(get_console(vm)); auto* console_global_object = TRY(get_console(vm));
auto& window = *console_global_object->m_window_object; auto& window = *console_global_object->m_window_object;
auto selector = TRY(vm.argument(0).to_byte_string(vm)); auto selector = TRY(vm.argument(0).to_string(vm));
if (vm.argument_count() > 1) { if (vm.argument_count() > 1) {
auto element_value = vm.argument(1); auto element_value = vm.argument(1);
@ -95,7 +95,7 @@ JS_DEFINE_NATIVE_FUNCTION(ConsoleGlobalEnvironmentExtensions::$$_function)
auto* console_global_object = TRY(get_console(vm)); auto* console_global_object = TRY(get_console(vm));
auto& window = *console_global_object->m_window_object; auto& window = *console_global_object->m_window_object;
auto selector = TRY(vm.argument(0).to_byte_string(vm)); auto selector = TRY(vm.argument(0).to_string(vm));
Web::DOM::ParentNode* element = &window.associated_document(); Web::DOM::ParentNode* element = &window.associated_document();

View file

@ -283,14 +283,14 @@ void WebAssemblyModule::initialize(JS::Realm& realm)
JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::get_export) JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::get_export)
{ {
auto name = TRY(vm.argument(0).to_byte_string(vm)); auto name = TRY(vm.argument(0).to_string(vm));
auto this_value = vm.this_value(); auto this_value = vm.this_value();
auto object = TRY(this_value.to_object(vm)); auto object = TRY(this_value.to_object(vm));
if (!is<WebAssemblyModule>(*object)) if (!is<WebAssemblyModule>(*object))
return vm.throw_completion<JS::TypeError>("Not a WebAssemblyModule"sv); return vm.throw_completion<JS::TypeError>("Not a WebAssemblyModule"sv);
auto& instance = static_cast<WebAssemblyModule&>(*object); auto& instance = static_cast<WebAssemblyModule&>(*object);
for (auto& entry : instance.module_instance().exports()) { for (auto& entry : instance.module_instance().exports()) {
if (entry.name() == name) { if (entry.name() == name.to_byte_string()) {
auto& value = entry.value(); auto& value = entry.value();
if (auto ptr = value.get_pointer<Wasm::FunctionAddress>()) if (auto ptr = value.get_pointer<Wasm::FunctionAddress>())
return JS::Value(static_cast<unsigned long>(ptr->value())); return JS::Value(static_cast<unsigned long>(ptr->value()));