mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 12:05:15 +00:00
LibJS: Use Value::to_byte_string() in fewer places
This commit is contained in:
parent
c71772126f
commit
2462a6b0fa
Notes:
github-actions[bot]
2025-03-28 16:32:47 +00:00
Author: https://github.com/awesomekling Commit: https://github.com/LadybirdBrowser/ladybird/commit/2462a6b0fa2 Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4142 Reviewed-by: https://github.com/trflynn89
8 changed files with 28 additions and 29 deletions
|
@ -87,7 +87,7 @@ JS_DEFINE_NATIVE_FUNCTION($262Object::detach_array_buffer)
|
|||
|
||||
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)
|
||||
|
||||
|
|
|
@ -36,7 +36,7 @@ void GlobalObject::visit_edges(Cell::Visitor& visitor)
|
|||
|
||||
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);
|
||||
return js_undefined();
|
||||
}
|
||||
|
|
|
@ -55,7 +55,7 @@ ThrowCompletionOr<GC::Ref<Object>> AggregateErrorConstructor::construct(Function
|
|||
// 3. If message is not undefined, then
|
||||
if (!message.is_undefined()) {
|
||||
// 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).
|
||||
aggregate_error->create_non_enumerable_data_property_or_throw(vm.names.message, PrimitiveString::create(vm, msg));
|
||||
|
|
|
@ -92,7 +92,7 @@ JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_exponential)
|
|||
|
||||
// 4. If x is not finite, return Number::toString(x).
|
||||
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.
|
||||
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).
|
||||
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).
|
||||
auto number = number_value.as_double();
|
||||
|
@ -236,7 +236,7 @@ JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_fixed)
|
|||
// 10. If x ≥ 10^21, then
|
||||
// a. Let m be ! ToString(𝔽(x)).
|
||||
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,
|
||||
// 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).
|
||||
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).
|
||||
auto precision = TRY(precision_value.to_integer_or_infinity(vm));
|
||||
|
||||
// 4. If x is not finite, return Number::toString(x).
|
||||
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.
|
||||
if ((precision < 1) || (precision > 100))
|
||||
|
@ -428,7 +428,7 @@ JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_string)
|
|||
|
||||
// 5. If radixMV = 10, return ! ToString(x).
|
||||
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.
|
||||
if (number_value.is_positive_infinity())
|
||||
|
|
|
@ -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
|
||||
JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::flags)
|
||||
{
|
||||
|
||||
// 1. Let R be the this value.
|
||||
// 2. If Type(R) is not Object, throw a TypeError exception.
|
||||
auto regexp_object = TRY(this_object(vm));
|
||||
|
@ -513,7 +512,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::flags)
|
|||
#undef __JS_ENUMERATE
|
||||
|
||||
// 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
|
||||
|
@ -530,7 +529,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_match)
|
|||
|
||||
// 4. Let flags be ? ToString(? Get(rx, "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
|
||||
if (!flags.contains('g')) {
|
||||
|
@ -573,7 +572,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_match)
|
|||
|
||||
// 1. Let matchStr be ? ToString(? Get(result, "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).
|
||||
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")).
|
||||
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.
|
||||
|
||||
|
@ -651,13 +650,13 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_replace)
|
|||
// 6. If functionalReplace is false, then
|
||||
if (!replace_value.is_function()) {
|
||||
// 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));
|
||||
}
|
||||
|
||||
// 7. Let flags be ? ToString(? Get(rx, "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.
|
||||
bool global = flags.contains('g');
|
||||
|
@ -694,7 +693,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_replace)
|
|||
|
||||
// 1. Let matchStr be ? ToString(? Get(result, "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
|
||||
if (match_str.is_empty()) {
|
||||
|
@ -746,7 +745,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_replace)
|
|||
// ii. If capN is not undefined, then
|
||||
if (!capture.is_undefined()) {
|
||||
// 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.
|
||||
|
@ -810,13 +809,13 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_replace)
|
|||
|
||||
// 16. If nextSourcePosition ≥ lengthS, return accumulatedResult.
|
||||
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.
|
||||
auto substring = string.substring_view(next_source_position);
|
||||
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
|
||||
|
@ -901,7 +900,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_split)
|
|||
|
||||
// 5. Let flags be ? ToString(? Get(rx, "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.
|
||||
// 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.
|
||||
// 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 »).
|
||||
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")).
|
||||
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")).
|
||||
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.
|
||||
// 6. Return result.
|
||||
|
|
|
@ -53,7 +53,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpStringIteratorPrototype::next)
|
|||
|
||||
auto match_object = TRY(match.to_object(vm));
|
||||
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()) {
|
||||
auto last_index_value = TRY(iterator->regexp_object().get(vm.names.lastIndex));
|
||||
auto last_index = TRY(last_index_value.to_length(vm));
|
||||
|
|
|
@ -71,7 +71,7 @@ JS_DEFINE_NATIVE_FUNCTION(ConsoleGlobalEnvironmentExtensions::$_function)
|
|||
auto* console_global_object = TRY(get_console(vm));
|
||||
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) {
|
||||
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& 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();
|
||||
|
||||
|
|
|
@ -283,14 +283,14 @@ void WebAssemblyModule::initialize(JS::Realm& realm)
|
|||
|
||||
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 object = TRY(this_value.to_object(vm));
|
||||
if (!is<WebAssemblyModule>(*object))
|
||||
return vm.throw_completion<JS::TypeError>("Not a WebAssemblyModule"sv);
|
||||
auto& instance = static_cast<WebAssemblyModule&>(*object);
|
||||
for (auto& entry : instance.module_instance().exports()) {
|
||||
if (entry.name() == name) {
|
||||
if (entry.name() == name.to_byte_string()) {
|
||||
auto& value = entry.value();
|
||||
if (auto ptr = value.get_pointer<Wasm::FunctionAddress>())
|
||||
return JS::Value(static_cast<unsigned long>(ptr->value()));
|
||||
|
|
Loading…
Add table
Reference in a new issue