mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-01 16:02:53 +00:00
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:
parent
8155377b5f
commit
e83a2c2369
Notes:
github-actions[bot]
2025-03-24 22:28:59 +00:00
Author: https://github.com/awesomekling
Commit: e83a2c2369
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4067
Reviewed-by: https://github.com/trflynn89
6 changed files with 15 additions and 17 deletions
|
@ -243,10 +243,10 @@ ThrowCompletionOr<double> compare_array_elements(VM& vm, Value x, Value y, Funct
|
||||||
}
|
}
|
||||||
|
|
||||||
// 5. Let xString be ? ToString(x).
|
// 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).
|
// 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).
|
// 7. Let xSmaller be ! IsLessThan(xString, yString, true).
|
||||||
auto x_smaller = MUST(is_less_than(vm, x_string, y_string, true));
|
auto x_smaller = MUST(is_less_than(vm, x_string, y_string, true));
|
||||||
|
|
|
@ -838,9 +838,9 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::join)
|
||||||
};
|
};
|
||||||
|
|
||||||
auto length = TRY(length_of_array_like(vm, this_object));
|
auto length = TRY(length_of_array_like(vm, this_object));
|
||||||
ByteString separator = ",";
|
String separator = ","_string;
|
||||||
if (!vm.argument(0).is_undefined())
|
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;
|
StringBuilder builder;
|
||||||
for (size_t i = 0; i < length; ++i) {
|
for (size_t i = 0; i < length; ++i) {
|
||||||
if (i > 0)
|
if (i > 0)
|
||||||
|
@ -848,11 +848,11 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::join)
|
||||||
auto value = TRY(this_object->get(i));
|
auto value = TRY(this_object->get(i));
|
||||||
if (value.is_nullish())
|
if (value.is_nullish())
|
||||||
continue;
|
continue;
|
||||||
auto string = TRY(value.to_byte_string(vm));
|
auto string = TRY(value.to_string(vm));
|
||||||
builder.append(string);
|
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
|
// 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));
|
auto locale_string_result = TRY(value.invoke(vm, vm.names.toLocaleString, locales, options));
|
||||||
|
|
||||||
// ii. Set R to the string-concatenation of R and S.
|
// 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);
|
builder.append(string);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1658,7 +1658,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::to_locale_string)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 7. Return R.
|
// 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
|
// 23.1.3.33 Array.prototype.toReversed ( ), https://tc39.es/ecma262/#sec-array.prototype.toreversed
|
||||||
|
|
|
@ -167,13 +167,11 @@ JS_DEFINE_NATIVE_FUNCTION(StringConstructor::raw)
|
||||||
// 7. Let nextIndex be 0.
|
// 7. Let nextIndex be 0.
|
||||||
// 8. Repeat,
|
// 8. Repeat,
|
||||||
for (size_t i = 0; i < literal_count; ++i) {
|
for (size_t i = 0; i < literal_count; ++i) {
|
||||||
auto next_key = ByteString::number(i);
|
|
||||||
|
|
||||||
// a. Let nextLiteralVal be ? Get(literals, ! ToString(𝔽(nextIndex))).
|
// 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).
|
// 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.
|
// c. Set R to the string-concatenation of R and nextLiteral.
|
||||||
builder.append(next_literal);
|
builder.append(next_literal);
|
||||||
|
@ -188,7 +186,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringConstructor::raw)
|
||||||
auto next_substitution_value = vm.argument(i + 1);
|
auto next_substitution_value = vm.argument(i + 1);
|
||||||
|
|
||||||
// ii. Let nextSub be ? ToString(nextSubVal).
|
// 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.
|
// iii. Set R to the string-concatenation of R and nextSub.
|
||||||
builder.append(next_substitution);
|
builder.append(next_substitution);
|
||||||
|
|
|
@ -1987,7 +1987,7 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::to_locale_string)
|
||||||
if (!next_element.is_nullish()) {
|
if (!next_element.is_nullish()) {
|
||||||
// i. Let S be ? ToString(? Invoke(nextElement, "toLocaleString", « locales, options »)).
|
// 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_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.
|
// ii. Set R to the string-concatenation of R and S.
|
||||||
builder.append(locale_string);
|
builder.append(locale_string);
|
||||||
|
@ -1997,7 +1997,7 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::to_locale_string)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 7. Return R.
|
// 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
|
// 23.2.3.32 %TypedArray%.prototype.toReversed ( ), https://tc39.es/ecma262/#sec-%typedarray%.prototype.toreversed
|
||||||
|
|
|
@ -26,7 +26,7 @@ TESTJS_GLOBAL_FUNCTION(is_strict_mode, isStrictMode, 0)
|
||||||
|
|
||||||
TESTJS_GLOBAL_FUNCTION(can_parse_source, canParseSource)
|
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));
|
auto parser = JS::Parser(JS::Lexer(source));
|
||||||
(void)parser.parse_program();
|
(void)parser.parse_program();
|
||||||
return JS::Value(!parser.has_errors());
|
return JS::Value(!parser.has_errors());
|
||||||
|
|
|
@ -21,7 +21,7 @@ TESTJS_GLOBAL_FUNCTION(read_binary_wasm_file, readBinaryWasmFile)
|
||||||
return StringView { error_string, strlen(error_string) };
|
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);
|
auto file = Core::File::open(filename, Core::File::OpenMode::Read);
|
||||||
if (file.is_error())
|
if (file.is_error())
|
||||||
return vm.throw_completion<JS::TypeError>(error_code_to_string(file.error().code()));
|
return vm.throw_completion<JS::TypeError>(error_code_to_string(file.error().code()));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue