summaryrefslogtreecommitdiff
path: root/Libraries/LibJS
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2020-04-29 16:29:26 +0100
committerAndreas Kling <kling@serenityos.org>2020-04-29 18:53:21 +0200
commit2c6e7dbd07f59f687a08c9d7460a2573ae20cbcd (patch)
tree4aef0d77469326eb6bac7394b55dcd2d2c18b454 /Libraries/LibJS
parent95abcc3722af0ec0115d519504e19d05c9cb79a5 (diff)
downloadserenity-2c6e7dbd07f59f687a08c9d7460a2573ae20cbcd.zip
LibJS: Throw error in Object::to_string() if string conversion fails
Diffstat (limited to 'Libraries/LibJS')
-rw-r--r--Libraries/LibJS/Runtime/Object.cpp8
-rw-r--r--Libraries/LibJS/Runtime/Value.cpp9
2 files changed, 14 insertions, 3 deletions
diff --git a/Libraries/LibJS/Runtime/Object.cpp b/Libraries/LibJS/Runtime/Object.cpp
index 403c99f066..29f591e4cf 100644
--- a/Libraries/LibJS/Runtime/Object.cpp
+++ b/Libraries/LibJS/Runtime/Object.cpp
@@ -360,7 +360,13 @@ Value Object::to_string() const
&& to_string_property.is_object()
&& to_string_property.as_object().is_function()) {
auto& to_string_function = static_cast<Function&>(to_string_property.as_object());
- return const_cast<Object*>(this)->interpreter().call(to_string_function, const_cast<Object*>(this));
+ auto& interpreter = const_cast<Object*>(this)->interpreter();
+ auto string_value = interpreter.call(to_string_function, const_cast<Object*>(this));
+ if (!string_value.is_string())
+ interpreter.throw_exception<TypeError>("Cannot convert object to string");
+ if (interpreter.exception())
+ return {};
+ return string_value;
}
return js_string(heap(), String::format("[object %s]", class_name()));
}
diff --git a/Libraries/LibJS/Runtime/Value.cpp b/Libraries/LibJS/Runtime/Value.cpp
index 7a66ecdcd9..7faabd419e 100644
--- a/Libraries/LibJS/Runtime/Value.cpp
+++ b/Libraries/LibJS/Runtime/Value.cpp
@@ -69,8 +69,13 @@ String Value::to_string() const
return String::format("%.4f", as_double());
}
- if (is_object())
- return as_object().to_primitive(Object::PreferredType::String).to_string();
+ if (is_object()) {
+ auto primitive_value = as_object().to_primitive(Object::PreferredType::String);
+ // FIXME: Maybe we should pass in the Interpreter& and call interpreter.exception() instead?
+ if (primitive_value.is_empty())
+ return {};
+ return primitive_value.to_string();
+ }
if (is_string())
return m_value.as_string->string();