1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-30 15:18:11 +00:00

LibJS: Make Object::to_string() call the "toString" property if present

This commit is contained in:
Andreas Kling 2020-04-05 18:18:24 +02:00
parent a7b21ec0f4
commit 8bfee015bc
4 changed files with 13 additions and 2 deletions

View file

@ -205,6 +205,14 @@ Value Object::to_primitive(PreferredType preferred_type) const
Value Object::to_string() const
{
auto to_string_property = get("toString");
if (to_string_property.has_value()
&& to_string_property.value().is_object()
&& to_string_property.value().as_object().is_function()) {
auto& to_string_function = static_cast<Function&>(to_string_property.value().as_object());
return const_cast<Object*>(this)->interpreter().call(&to_string_function, const_cast<Object*>(this));
}
return js_string(heap(), String::format("[object %s]", class_name()));
}
}