mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 07:17:35 +00:00
LibJS: Add spec comments to Value::to_string()
This commit is contained in:
parent
9a961af0b0
commit
596b30df5f
1 changed files with 23 additions and 8 deletions
|
@ -379,22 +379,37 @@ ThrowCompletionOr<DeprecatedString> Value::to_string(VM& vm) const
|
||||||
return number_to_string(m_value.as_double);
|
return number_to_string(m_value.as_double);
|
||||||
|
|
||||||
switch (m_value.tag) {
|
switch (m_value.tag) {
|
||||||
case UNDEFINED_TAG:
|
// 1. If argument is a String, return argument.
|
||||||
return "undefined"sv;
|
|
||||||
case NULL_TAG:
|
|
||||||
return "null"sv;
|
|
||||||
case BOOLEAN_TAG:
|
|
||||||
return as_bool() ? "true"sv : "false"sv;
|
|
||||||
case INT32_TAG:
|
|
||||||
return DeprecatedString::number(as_i32());
|
|
||||||
case STRING_TAG:
|
case STRING_TAG:
|
||||||
return as_string().deprecated_string();
|
return as_string().deprecated_string();
|
||||||
|
// 2. If argument is a Symbol, throw a TypeError exception.
|
||||||
case SYMBOL_TAG:
|
case SYMBOL_TAG:
|
||||||
return vm.throw_completion<TypeError>(ErrorType::Convert, "symbol", "string");
|
return vm.throw_completion<TypeError>(ErrorType::Convert, "symbol", "string");
|
||||||
|
// 3. If argument is undefined, return "undefined".
|
||||||
|
case UNDEFINED_TAG:
|
||||||
|
return "undefined"sv;
|
||||||
|
// 4. If argument is null, return "null".
|
||||||
|
case NULL_TAG:
|
||||||
|
return "null"sv;
|
||||||
|
// 5. If argument is true, return "true".
|
||||||
|
// 6. If argument is false, return "false".
|
||||||
|
case BOOLEAN_TAG:
|
||||||
|
return as_bool() ? "true"sv : "false"sv;
|
||||||
|
// 7. If argument is a Number, return Number::toString(argument, 10).
|
||||||
|
case INT32_TAG:
|
||||||
|
return DeprecatedString::number(as_i32());
|
||||||
|
// 8. If argument is a BigInt, return BigInt::toString(argument, 10).
|
||||||
case BIGINT_TAG:
|
case BIGINT_TAG:
|
||||||
return as_bigint().big_integer().to_base(10);
|
return as_bigint().big_integer().to_base(10);
|
||||||
|
// 9. Assert: argument is an Object.
|
||||||
case OBJECT_TAG: {
|
case OBJECT_TAG: {
|
||||||
|
// 10. Let primValue be ? ToPrimitive(argument, string).
|
||||||
auto primitive_value = TRY(to_primitive(vm, PreferredType::String));
|
auto primitive_value = TRY(to_primitive(vm, PreferredType::String));
|
||||||
|
|
||||||
|
// 11. Assert: primValue is not an Object.
|
||||||
|
VERIFY(!primitive_value.is_object());
|
||||||
|
|
||||||
|
// 12. Return ? ToString(primValue).
|
||||||
return primitive_value.to_string(vm);
|
return primitive_value.to_string(vm);
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue