mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 17:07:34 +00:00
LibJS: Implement spec-compliant OrdinaryToPrimitive
This renames Object::to_primitive() to Object::ordinary_to_primitive() for two reasons: - No confusion with Value::to_primitive() - To match the spec's name Also change existing uses of Object::to_primitive() to Value::to_primitive() when the spec uses the latter (which will still call Object::ordinary_to_primitive()). Object::to_string() has been removed as it's not needed anymore (and nothing the spec uses). This makes it possible to overwrite an object's toString and valueOf and have them provide results for anything that uses to_primitive() - e.g.: const o = { toString: undefined, valueOf: () => 42 }; Number(o) // 42, previously NaN ["foo", o].toString(); // "foo,42", previously "foo,[object Object]" ++o // 43, previously NaN etc.
This commit is contained in:
parent
e163db248d
commit
fb89c324c5
4 changed files with 50 additions and 42 deletions
|
@ -836,48 +836,30 @@ bool Object::has_own_property(const PropertyName& property_name) const
|
|||
return shape().lookup(property_name.to_string_or_symbol()).has_value();
|
||||
}
|
||||
|
||||
Value Object::to_primitive(Value::PreferredType preferred_type) const
|
||||
Value Object::ordinary_to_primitive(Value::PreferredType preferred_type) const
|
||||
{
|
||||
Value result = js_undefined();
|
||||
ASSERT(preferred_type == Value::PreferredType::String || preferred_type == Value::PreferredType::Number);
|
||||
|
||||
switch (preferred_type) {
|
||||
case Value::PreferredType::Default:
|
||||
case Value::PreferredType::Number: {
|
||||
result = value_of();
|
||||
if (result.is_object()) {
|
||||
result = to_string();
|
||||
}
|
||||
break;
|
||||
}
|
||||
case Value::PreferredType::String: {
|
||||
result = to_string();
|
||||
if (result.is_object())
|
||||
result = value_of();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ASSERT(!result.is_object());
|
||||
return result;
|
||||
}
|
||||
|
||||
Value Object::to_string() const
|
||||
{
|
||||
auto& vm = this->vm();
|
||||
auto to_string_property = get(vm.names.toString);
|
||||
if (to_string_property.is_function()) {
|
||||
auto& to_string_function = to_string_property.as_function();
|
||||
auto to_string_result = vm.call(to_string_function, const_cast<Object*>(this));
|
||||
if (to_string_result.is_object())
|
||||
vm.throw_exception<TypeError>(global_object(), ErrorType::Convert, "object", "string");
|
||||
|
||||
Vector<FlyString, 2> method_names;
|
||||
if (preferred_type == Value::PreferredType::String)
|
||||
method_names = { vm.names.toString, vm.names.valueOf };
|
||||
else
|
||||
method_names = { vm.names.valueOf, vm.names.toString };
|
||||
|
||||
for (auto& method_name : method_names) {
|
||||
auto method = get(method_name);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto* string = to_string_result.to_primitive_string(global_object());
|
||||
if (vm.exception())
|
||||
return {};
|
||||
return string;
|
||||
if (method.is_function()) {
|
||||
auto result = vm.call(method.as_function(), const_cast<Object*>(this));
|
||||
if (!result.is_object())
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return js_string(vm, String::formatted("[object {}]", class_name()));
|
||||
vm.throw_exception<TypeError>(global_object(), ErrorType::Convert, "object", preferred_type == Value::PreferredType::String ? "string" : "number");
|
||||
return {};
|
||||
}
|
||||
|
||||
Value Object::invoke(const StringOrSymbol& property_name, Optional<MarkedValueList> arguments)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue