1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 01:17:34 +00:00

LibJS: Implement spec-compliant Object.prototype.toString

This commit is contained in:
Matthew Olsson 2020-07-11 10:11:37 -07:00 committed by Andreas Kling
parent caa11503b1
commit 5ecd504f4e
4 changed files with 53 additions and 12 deletions

View file

@ -68,10 +68,43 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::has_own_property)
JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::to_string)
{
auto* this_object = interpreter.this_value(global_object).to_object(interpreter, global_object);
auto this_value = interpreter.this_value(global_object);
if (this_value.is_undefined())
return js_string(interpreter, "[object Undefined]");
if (this_value.is_null())
return js_string(interpreter, "[object Null]");
auto* this_object = this_value.to_object(interpreter, global_object);
if (!this_object)
return {};
return js_string(interpreter, String::format("[object %s]", this_object->class_name()));
String tag;
auto to_string_tag = this_object->get(interpreter.well_known_symbol_to_string_tag());
if (to_string_tag.is_string()) {
tag = to_string_tag.as_string().string();
} else if (this_object->is_array()) {
tag = "Array";
} else if (this_object->is_function()) {
tag = "Function";
} else if (this_object->is_error()) {
tag = "Error";
} else if (this_object->is_boolean_object()) {
tag = "Boolean";
} else if (this_object->is_number_object()) {
tag = "Number";
} else if (this_object->is_string_object()) {
tag = "String";
} else if (this_object->is_date()) {
tag = "Date";
} else if (this_object->is_regexp_object()) {
tag = "RegExp";
} else {
tag = "Object";
}
return js_string(interpreter, String::format("[object %s]", tag.characters()));
}
JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::to_locale_string)

View file

@ -64,8 +64,8 @@ public:
private:
virtual void visit_children(Visitor&) override;
virtual bool is_proxy_object() const override { return true; }
virtual bool is_function() const override { return m_target.is_function(); }
virtual bool is_function() const override { return m_target.is_function(); }
virtual bool is_array() const override { return m_target.is_array(); };
Object& m_target;