1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 21:44:59 +00:00

LibJS: Consolidate error messages into ErrorTypes.h

Now, exceptions can be thrown with
interpreter.throw_exception<T>(ErrorType:TYPE, "format", "args",
"here").
This commit is contained in:
Matthew Olsson 2020-06-09 22:48:01 -07:00 committed by Andreas Kling
parent 9940a7f6de
commit 78155a6668
63 changed files with 439 additions and 223 deletions

View file

@ -76,7 +76,7 @@ ProxyObject::~ProxyObject()
Object* ProxyObject::prototype()
{
if (m_is_revoked) {
interpreter().throw_exception<TypeError>("An operation was performed on a revoked Proxy object");
interpreter().throw_exception<TypeError>(ErrorType::ProxyRevoked);
return nullptr;
}
auto trap = m_handler.get("getPrototypeOf");
@ -85,7 +85,7 @@ Object* ProxyObject::prototype()
if (trap.is_empty() || trap.is_undefined() || trap.is_null())
return m_target.prototype();
if (!trap.is_function()) {
interpreter().throw_exception<TypeError>("Proxy handler's getPrototypeOf trap wasn't undefined, null, or callable");
interpreter().throw_exception<TypeError>(ErrorType::ProxyInvalidTrap, "getPrototypeOf");
return nullptr;
}
MarkedValueList arguments(interpreter().heap());
@ -94,7 +94,7 @@ Object* ProxyObject::prototype()
if (interpreter().exception())
return nullptr;
if (!trap_result.is_object() && !trap_result.is_null()) {
interpreter().throw_exception<TypeError>("Proxy handler's getPrototypeOf trap violates invariant: must return an object or null");
interpreter().throw_exception<TypeError>(ErrorType::ProxyGetPrototypeOfReturn);
return nullptr;
}
if (m_target.is_extensible()) {
@ -108,7 +108,7 @@ Object* ProxyObject::prototype()
if (interpreter().exception())
return nullptr;
if (!same_value(interpreter(), trap_result, Value(target_proto))) {
interpreter().throw_exception<TypeError>("Proxy handler's getPrototypeOf trap violates invariant: cannot return a different prototype object for a non-extensible target");
interpreter().throw_exception<TypeError>(ErrorType::ProxyGetPrototypeOfNonExtensible);
return nullptr;
}
return &trap_result.as_object();
@ -117,7 +117,7 @@ Object* ProxyObject::prototype()
const Object* ProxyObject::prototype() const
{
if (m_is_revoked) {
interpreter().throw_exception<TypeError>("An operation was performed on a revoked Proxy object");
interpreter().throw_exception<TypeError>(ErrorType::ProxyRevoked);
return nullptr;
}
return const_cast<const Object*>(const_cast<ProxyObject*>(this)->prototype());
@ -126,7 +126,7 @@ const Object* ProxyObject::prototype() const
bool ProxyObject::set_prototype(Object* object)
{
if (m_is_revoked) {
interpreter().throw_exception<TypeError>("An operation was performed on a revoked Proxy object");
interpreter().throw_exception<TypeError>(ErrorType::ProxyRevoked);
return false;
}
auto trap = m_handler.get("setPrototypeOf");
@ -135,7 +135,7 @@ bool ProxyObject::set_prototype(Object* object)
if (trap.is_empty() || trap.is_undefined() || trap.is_null())
return m_target.set_prototype(object);
if (!trap.is_function()) {
interpreter().throw_exception<TypeError>("Proxy handler's setPrototypeOf trap wasn't undefined, null, or callable");
interpreter().throw_exception<TypeError>(ErrorType::ProxyInvalidTrap, "setPrototypeOf");
return false;
}
MarkedValueList arguments(interpreter().heap());
@ -150,7 +150,7 @@ bool ProxyObject::set_prototype(Object* object)
if (interpreter().exception())
return false;
if (!same_value(interpreter(), Value(object), Value(target_proto))) {
interpreter().throw_exception<TypeError>("Proxy handler's setPrototypeOf trap violates invariant: the argument must match the prototype of the target if the target is non-extensible");
interpreter().throw_exception<TypeError>(ErrorType::ProxySetPrototypeOfNonExtensible);
return false;
}
return true;
@ -159,7 +159,7 @@ bool ProxyObject::set_prototype(Object* object)
bool ProxyObject::is_extensible() const
{
if (m_is_revoked) {
interpreter().throw_exception<TypeError>("An operation was performed on a revoked Proxy object");
interpreter().throw_exception<TypeError>(ErrorType::ProxyRevoked);
return false;
}
auto trap = m_handler.get("isExtensible");
@ -168,7 +168,7 @@ bool ProxyObject::is_extensible() const
if (trap.is_empty() || trap.is_undefined() || trap.is_null())
return m_target.is_extensible();
if (!trap.is_function()) {
interpreter().throw_exception<TypeError>("Proxy handler's isExtensible trap wasn't undefined, null, or callable");
interpreter().throw_exception<TypeError>(ErrorType::ProxyInvalidTrap, "isExtensible");
return {};
}
MarkedValueList arguments(interpreter().heap());
@ -178,7 +178,7 @@ bool ProxyObject::is_extensible() const
return false;
if (trap_result != m_target.is_extensible()) {
if (!interpreter().exception())
interpreter().throw_exception<TypeError>("Proxy handler's isExtensible trap violates invariant: return value must match the target's extensibility");
interpreter().throw_exception<TypeError>(ErrorType::ProxyIsExtensibleReturn);
return false;
}
return trap_result;
@ -187,7 +187,7 @@ bool ProxyObject::is_extensible() const
bool ProxyObject::prevent_extensions()
{
if (m_is_revoked) {
interpreter().throw_exception<TypeError>("An operation was performed on a revoked Proxy object");
interpreter().throw_exception<TypeError>(ErrorType::ProxyRevoked);
return false;
}
auto trap = m_handler.get("preventExtensions");
@ -196,7 +196,7 @@ bool ProxyObject::prevent_extensions()
if (trap.is_empty() || trap.is_undefined() || trap.is_null())
return m_target.prevent_extensions();
if (!trap.is_function()) {
interpreter().throw_exception<TypeError>("Proxy handler's preventExtensions trap wasn't undefined, null, or callable");
interpreter().throw_exception<TypeError>(ErrorType::ProxyInvalidTrap, "preventExtensions");
return {};
}
MarkedValueList arguments(interpreter().heap());
@ -206,7 +206,7 @@ bool ProxyObject::prevent_extensions()
return false;
if (trap_result && m_target.is_extensible()) {
if (!interpreter().exception())
interpreter().throw_exception<TypeError>("Proxy handler's preventExtensions trap violates invariant: cannot return true if the target object is extensible");
interpreter().throw_exception<TypeError>(ErrorType::ProxyPreventExtensionsReturn);
return false;
}
return trap_result;
@ -215,7 +215,7 @@ bool ProxyObject::prevent_extensions()
Optional<PropertyDescriptor> ProxyObject::get_own_property_descriptor(PropertyName name) const
{
if (m_is_revoked) {
interpreter().throw_exception<TypeError>("An operation was performed on a revoked Proxy object");
interpreter().throw_exception<TypeError>(ErrorType::ProxyRevoked);
return {};
}
auto trap = m_handler.get("getOwnPropertyDescriptor");
@ -224,7 +224,7 @@ Optional<PropertyDescriptor> ProxyObject::get_own_property_descriptor(PropertyNa
if (trap.is_empty() || trap.is_undefined() || trap.is_null())
return m_target.get_own_property_descriptor(name);
if (!trap.is_function()) {
interpreter().throw_exception<TypeError>("Proxy handler's getOwnPropertyDescriptor trap wasn't undefined, null, or callable");
interpreter().throw_exception<TypeError>(ErrorType::ProxyInvalidTrap, "getOwnPropertyDescriptor");
return {};
}
MarkedValueList arguments(interpreter().heap());
@ -234,7 +234,7 @@ Optional<PropertyDescriptor> ProxyObject::get_own_property_descriptor(PropertyNa
if (interpreter().exception())
return {};
if (!trap_result.is_object() && !trap_result.is_undefined()) {
interpreter().throw_exception<TypeError>("Proxy handler's getOwnPropertyDescriptor trap violates invariant: must return an object or undefined");
interpreter().throw_exception<TypeError>(ErrorType::ProxyGetOwnDescriptorReturn);
return {};
}
auto target_desc = m_target.get_own_property_descriptor(name);
@ -244,12 +244,12 @@ Optional<PropertyDescriptor> ProxyObject::get_own_property_descriptor(PropertyNa
if (!target_desc.has_value())
return {};
if (!target_desc.value().attributes.is_configurable()) {
interpreter().throw_exception<TypeError>("Proxy handler's getOwnPropertyDescriptor trap violates invariant: cannot return undefined for a property on the target which is a non-configurable property");
interpreter().throw_exception<TypeError>(ErrorType::ProxyGetOwnDescriptorNonConfigurable);
return {};
}
if (!m_target.is_extensible()) {
if (!interpreter().exception())
interpreter().throw_exception<TypeError>("Proxy handler's getOwnPropertyDescriptor trap violates invariant: cannot report a property as being undefined if it exists as an own property of the target and the target is non-extensible");
interpreter().throw_exception<TypeError>(ErrorType::ProxyGetOwnDescriptorUndefReturn);
return {};
}
return {};
@ -259,11 +259,11 @@ Optional<PropertyDescriptor> ProxyObject::get_own_property_descriptor(PropertyNa
return {};
if (!is_compatible_property_descriptor(interpreter(), m_target.is_extensible(), result_desc, target_desc)) {
if (!interpreter().exception())
interpreter().throw_exception<TypeError>("Proxy handler's getOwnPropertyDescriptor trap violates invariant: invalid property descriptor for existing property on the target");
interpreter().throw_exception<TypeError>(ErrorType::ProxyGetOwnDescriptorInvalidDescriptor);
return {};
}
if (!result_desc.attributes.is_configurable() && (!target_desc.has_value() || target_desc.value().attributes.is_configurable())) {
interpreter().throw_exception<TypeError>("Proxy handler's getOwnPropertyDescriptor trap violates invariant: cannot report target's property as non-configurable if the property does not exist, or if it is configurable");
interpreter().throw_exception<TypeError>(ErrorType::ProxyGetOwnDescriptorInvalidNonConfig);
return {};
}
return result_desc;
@ -272,7 +272,7 @@ Optional<PropertyDescriptor> ProxyObject::get_own_property_descriptor(PropertyNa
bool ProxyObject::define_property(const FlyString& property_name, const Object& descriptor, bool throw_exceptions)
{
if (m_is_revoked) {
interpreter().throw_exception<TypeError>("An operation was performed on a revoked Proxy object");
interpreter().throw_exception<TypeError>(ErrorType::ProxyRevoked);
return false;
}
auto trap = m_handler.get("defineProperty");
@ -281,7 +281,7 @@ bool ProxyObject::define_property(const FlyString& property_name, const Object&
if (trap.is_empty() || trap.is_undefined() || trap.is_null())
return m_target.define_property(property_name, descriptor, throw_exceptions);
if (!trap.is_function()) {
interpreter().throw_exception<TypeError>("Proxy handler's defineProperty trap wasn't undefined, null, or callable");
interpreter().throw_exception<TypeError>(ErrorType::ProxyInvalidTrap, "defineProperty");
return false;
}
MarkedValueList arguments(interpreter().heap());
@ -302,21 +302,21 @@ bool ProxyObject::define_property(const FlyString& property_name, const Object&
if (!target_desc.has_value()) {
if (!m_target.is_extensible()) {
if (!interpreter().exception())
interpreter().throw_exception<TypeError>("Proxy handler's defineProperty trap violates invariant: a property cannot be reported as being defined if the property does not exist on the target and the target is non-extensible");
interpreter().throw_exception<TypeError>(ErrorType::ProxyDefinePropNonExtensible);
return false;
}
if (setting_config_false) {
interpreter().throw_exception<TypeError>("Proxy handler's defineProperty trap violates invariant: a property cannot be defined as non-configurable if it does not already exist on the target object");
interpreter().throw_exception<TypeError>(ErrorType::ProxyDefinePropNonConfigurableNonExisting);
return false;
}
} else {
if (!is_compatible_property_descriptor(interpreter(), m_target.is_extensible(), PropertyDescriptor::from_dictionary(interpreter(), descriptor), target_desc)) {
if (!interpreter().exception())
interpreter().throw_exception<TypeError>("Proxy handler's defineProperty trap violates invariant: the new descriptor is not compatible with the existing descriptor of the property on the target");
interpreter().throw_exception<TypeError>(ErrorType::ProxyDefinePropIncompatibleDescriptor);
return false;
}
if (setting_config_false && target_desc.value().attributes.is_configurable()) {
interpreter().throw_exception<TypeError>("Proxy handler's defineProperty trap violates invariant: a property cannot be defined as non-configurable if it already exists on the target object as a configurable property");
interpreter().throw_exception<TypeError>(ErrorType::ProxyDefinePropExistingConfigurable);
return false;
}
}
@ -326,7 +326,7 @@ bool ProxyObject::define_property(const FlyString& property_name, const Object&
bool ProxyObject::has_property(PropertyName name) const
{
if (m_is_revoked) {
interpreter().throw_exception<TypeError>("An operation was performed on a revoked Proxy object");
interpreter().throw_exception<TypeError>(ErrorType::ProxyRevoked);
return false;
}
auto trap = m_handler.get("has");
@ -335,7 +335,7 @@ bool ProxyObject::has_property(PropertyName name) const
if (trap.is_empty() || trap.is_undefined() || trap.is_null())
return m_target.has_property(name);
if (!trap.is_function()) {
interpreter().throw_exception<TypeError>("Proxy handler's has trap wasn't undefined, null, or callable");
interpreter().throw_exception<TypeError>(ErrorType::ProxyInvalidTrap, "has");
return false;
}
MarkedValueList arguments(interpreter().heap());
@ -350,12 +350,12 @@ bool ProxyObject::has_property(PropertyName name) const
return false;
if (target_desc.has_value()) {
if (!target_desc.value().attributes.is_configurable()) {
interpreter().throw_exception<TypeError>("Proxy handler's has trap violates invariant: a property cannot be reported as non-existent if it exists on the target as a non-configurable property");
interpreter().throw_exception<TypeError>(ErrorType::ProxyHasExistingNonConfigurable);
return false;
}
if (!m_target.is_extensible()) {
if (!interpreter().exception())
interpreter().throw_exception<TypeError>("Proxy handler's has trap violates invariant: a property cannot be reported as non-existent if it exist on the target and the target is non-extensible");
interpreter().throw_exception<TypeError>(ErrorType::ProxyHasExistingNonExtensible);
return false;
}
}
@ -366,7 +366,7 @@ bool ProxyObject::has_property(PropertyName name) const
Value ProxyObject::get(PropertyName name) const
{
if (m_is_revoked) {
interpreter().throw_exception<TypeError>("An operation was performed on a revoked Proxy object");
interpreter().throw_exception<TypeError>(ErrorType::ProxyRevoked);
return {};
}
auto trap = m_handler.get("get");
@ -375,7 +375,7 @@ Value ProxyObject::get(PropertyName name) const
if (trap.is_empty() || trap.is_undefined() || trap.is_null())
return m_target.get(name);
if (!trap.is_function())
return interpreter().throw_exception<TypeError>("Proxy handler's get trap wasn't undefined, null, or callable");
return interpreter().throw_exception<TypeError>(ErrorType::ProxyInvalidTrap, "get");
MarkedValueList arguments(interpreter().heap());
arguments.values().append(Value(&m_target));
arguments.values().append(js_string(interpreter(), name.to_string()));
@ -388,9 +388,9 @@ Value ProxyObject::get(PropertyName name) const
if (interpreter().exception())
return {};
if (target_desc.value().is_data_descriptor() && !target_desc.value().attributes.is_writable() && !same_value(interpreter(), trap_result, target_desc.value().value))
return interpreter().throw_exception<TypeError>("Proxy handler's get trap violates invariant: the returned value must match the value on the target if the property exists on the target as a non-writable, non-configurable own data property");
return interpreter().throw_exception<TypeError>(ErrorType::ProxyGetImmutableDataProperty);
if (target_desc.value().is_accessor_descriptor() && target_desc.value().getter == nullptr && !trap_result.is_undefined())
return interpreter().throw_exception<TypeError>("Proxy handler's get trap violates invariant: the returned value must be undefined if the property exists on the target as a non-configurable accessor property with an undefined get attribute");
return interpreter().throw_exception<TypeError>(ErrorType::ProxyGetNonConfigurableAccessor);
}
return trap_result;
}
@ -398,7 +398,7 @@ Value ProxyObject::get(PropertyName name) const
bool ProxyObject::put(PropertyName name, Value value)
{
if (m_is_revoked) {
interpreter().throw_exception<TypeError>("An operation was performed on a revoked Proxy object");
interpreter().throw_exception<TypeError>(ErrorType::ProxyRevoked);
return false;
}
auto trap = m_handler.get("set");
@ -407,7 +407,7 @@ bool ProxyObject::put(PropertyName name, Value value)
if (trap.is_empty() || trap.is_undefined() || trap.is_null())
return m_target.put(name, value);
if (!trap.is_function()) {
interpreter().throw_exception<TypeError>("Proxy handler's set trap wasn't undefined, null, or callable");
interpreter().throw_exception<TypeError>(ErrorType::ProxyInvalidTrap, "set");
return false;
}
MarkedValueList arguments(interpreter().heap());
@ -423,11 +423,11 @@ bool ProxyObject::put(PropertyName name, Value value)
return false;
if (target_desc.has_value() && !target_desc.value().attributes.is_configurable()) {
if (target_desc.value().is_data_descriptor() && !target_desc.value().attributes.is_writable() && !same_value(interpreter(), value, target_desc.value().value)) {
interpreter().throw_exception<TypeError>("Proxy handler's set trap violates invariant: cannot return true for a property on the target which is a non-configurable, non-writable own data property");
interpreter().throw_exception<TypeError>(ErrorType::ProxySetImmutableDataProperty);
return false;
}
if (target_desc.value().is_accessor_descriptor() && !target_desc.value().setter) {
interpreter().throw_exception<TypeError>("Proxy handler's set trap violates invariant: cannot return true for a property on the target which is a non-configurable own accessor property with an undefined set attribute");
interpreter().throw_exception<TypeError>(ErrorType::ProxySetNonConfigurableAccessor);
}
}
return true;
@ -436,7 +436,7 @@ bool ProxyObject::put(PropertyName name, Value value)
Value ProxyObject::delete_property(PropertyName name)
{
if (m_is_revoked) {
interpreter().throw_exception<TypeError>("An operation was performed on a revoked Proxy object");
interpreter().throw_exception<TypeError>(ErrorType::ProxyRevoked);
return {};
}
auto trap = m_handler.get("deleteProperty");
@ -445,7 +445,7 @@ Value ProxyObject::delete_property(PropertyName name)
if (trap.is_empty() || trap.is_undefined() || trap.is_null())
return m_target.delete_property(name);
if (!trap.is_function())
return interpreter().throw_exception<TypeError>("Proxy handler's delete trap wasn't undefined, null, or callable");
return interpreter().throw_exception<TypeError>(ErrorType::ProxyInvalidTrap, "deleteProperty");
MarkedValueList arguments(interpreter().heap());
arguments.values().append(Value(&m_target));
arguments.values().append(js_string(interpreter(), name.to_string()));
@ -460,7 +460,7 @@ Value ProxyObject::delete_property(PropertyName name)
if (!target_desc.has_value())
return Value(true);
if (!target_desc.value().attributes.is_configurable())
return interpreter().throw_exception<TypeError>("Proxy handler's delete trap violates invariant: cannot report a non-configurable own property of the target as deleted");
return interpreter().throw_exception<TypeError>(ErrorType::ProxyDeleteNonConfigurable);
return Value(true);
}