1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 01:48:11 +00:00

LibJS: Replace a bunch of vm() calls in ProxyObject with reference

This was probably a result of search & replace, it's quite ridiculous in
some places. Let use the existing pattern of getting a reference to the
VM once at each function start consistently.
This commit is contained in:
Linus Groh 2020-11-04 21:19:25 +00:00 committed by Andreas Kling
parent 2645dfafcf
commit 44e38b8457

View file

@ -75,39 +75,40 @@ ProxyObject::~ProxyObject()
Object* ProxyObject::prototype() Object* ProxyObject::prototype()
{ {
auto& vm = this->vm();
if (m_is_revoked) { if (m_is_revoked) {
vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyRevoked); vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyRevoked);
return nullptr; return nullptr;
} }
auto trap = m_handler.get(vm().names.getPrototypeOf); auto trap = m_handler.get(vm.names.getPrototypeOf);
if (vm().exception()) if (vm.exception())
return nullptr; return nullptr;
if (trap.is_empty() || trap.is_nullish()) if (trap.is_empty() || trap.is_nullish())
return m_target.prototype(); return m_target.prototype();
if (!trap.is_function()) { if (!trap.is_function()) {
vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyInvalidTrap, "getPrototypeOf"); vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyInvalidTrap, "getPrototypeOf");
return nullptr; return nullptr;
} }
auto trap_result = vm().call(trap.as_function(), Value(&m_handler), Value(&m_target)); auto trap_result = vm.call(trap.as_function(), Value(&m_handler), Value(&m_target));
if (vm().exception()) if (vm.exception())
return nullptr; return nullptr;
if (!trap_result.is_object() && !trap_result.is_null()) { if (!trap_result.is_object() && !trap_result.is_null()) {
vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyGetPrototypeOfReturn); vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyGetPrototypeOfReturn);
return nullptr; return nullptr;
} }
if (m_target.is_extensible()) { if (m_target.is_extensible()) {
if (vm().exception()) if (vm.exception())
return nullptr; return nullptr;
if (trap_result.is_null()) if (trap_result.is_null())
return nullptr; return nullptr;
return &trap_result.as_object(); return &trap_result.as_object();
} }
auto target_proto = m_target.prototype(); auto target_proto = m_target.prototype();
if (vm().exception()) if (vm.exception())
return nullptr; return nullptr;
if (!same_value(trap_result, Value(target_proto))) { if (!same_value(trap_result, Value(target_proto))) {
vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyGetPrototypeOfNonExtensible); vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyGetPrototypeOfNonExtensible);
return nullptr; return nullptr;
} }
return &trap_result.as_object(); return &trap_result.as_object();
@ -115,8 +116,9 @@ Object* ProxyObject::prototype()
const Object* ProxyObject::prototype() const const Object* ProxyObject::prototype() const
{ {
auto& vm = this->vm();
if (m_is_revoked) { if (m_is_revoked) {
vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyRevoked); vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyRevoked);
return nullptr; return nullptr;
} }
return const_cast<const Object*>(const_cast<ProxyObject*>(this)->prototype()); return const_cast<const Object*>(const_cast<ProxyObject*>(this)->prototype());
@ -124,30 +126,31 @@ const Object* ProxyObject::prototype() const
bool ProxyObject::set_prototype(Object* object) bool ProxyObject::set_prototype(Object* object)
{ {
auto& vm = this->vm();
if (m_is_revoked) { if (m_is_revoked) {
vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyRevoked); vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyRevoked);
return false; return false;
} }
auto trap = m_handler.get(vm().names.setPrototypeOf); auto trap = m_handler.get(vm.names.setPrototypeOf);
if (vm().exception()) if (vm.exception())
return false; return false;
if (trap.is_empty() || trap.is_nullish()) if (trap.is_empty() || trap.is_nullish())
return m_target.set_prototype(object); return m_target.set_prototype(object);
if (!trap.is_function()) { if (!trap.is_function()) {
vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyInvalidTrap, "setPrototypeOf"); vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyInvalidTrap, "setPrototypeOf");
return false; return false;
} }
auto trap_result = vm().call(trap.as_function(), Value(&m_handler), Value(&m_target), Value(object)); auto trap_result = vm.call(trap.as_function(), Value(&m_handler), Value(&m_target), Value(object));
if (vm().exception() || !trap_result.to_boolean()) if (vm.exception() || !trap_result.to_boolean())
return false; return false;
if (m_target.is_extensible()) if (m_target.is_extensible())
return true; return true;
auto* target_proto = m_target.prototype(); auto* target_proto = m_target.prototype();
if (vm().exception()) if (vm.exception())
return false; return false;
if (!same_value(Value(object), Value(target_proto))) { if (!same_value(Value(object), Value(target_proto))) {
vm().throw_exception<TypeError>(global_object(), ErrorType::ProxySetPrototypeOfNonExtensible); vm.throw_exception<TypeError>(global_object(), ErrorType::ProxySetPrototypeOfNonExtensible);
return false; return false;
} }
return true; return true;
@ -155,26 +158,27 @@ bool ProxyObject::set_prototype(Object* object)
bool ProxyObject::is_extensible() const bool ProxyObject::is_extensible() const
{ {
auto& vm = this->vm();
if (m_is_revoked) { if (m_is_revoked) {
vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyRevoked); vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyRevoked);
return false; return false;
} }
auto trap = m_handler.get(vm().names.isExtensible); auto trap = m_handler.get(vm.names.isExtensible);
if (vm().exception()) if (vm.exception())
return false; return false;
if (trap.is_empty() || trap.is_nullish()) if (trap.is_empty() || trap.is_nullish())
return m_target.is_extensible(); return m_target.is_extensible();
if (!trap.is_function()) { if (!trap.is_function()) {
vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyInvalidTrap, "isExtensible"); vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyInvalidTrap, "isExtensible");
return {}; return {};
} }
auto trap_result = vm().call(trap.as_function(), Value(&m_handler), Value(&m_target)); auto trap_result = vm.call(trap.as_function(), Value(&m_handler), Value(&m_target));
if (vm().exception()) if (vm.exception())
return false; return false;
if (trap_result.to_boolean() != m_target.is_extensible()) { if (trap_result.to_boolean() != m_target.is_extensible()) {
if (!vm().exception()) if (!vm.exception())
vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyIsExtensibleReturn); vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyIsExtensibleReturn);
return false; return false;
} }
return trap_result.to_boolean(); return trap_result.to_boolean();
@ -182,26 +186,27 @@ bool ProxyObject::is_extensible() const
bool ProxyObject::prevent_extensions() bool ProxyObject::prevent_extensions()
{ {
auto& vm = this->vm();
if (m_is_revoked) { if (m_is_revoked) {
vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyRevoked); vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyRevoked);
return false; return false;
} }
auto trap = m_handler.get(vm().names.preventExtensions); auto trap = m_handler.get(vm.names.preventExtensions);
if (vm().exception()) if (vm.exception())
return false; return false;
if (trap.is_empty() || trap.is_nullish()) if (trap.is_empty() || trap.is_nullish())
return m_target.prevent_extensions(); return m_target.prevent_extensions();
if (!trap.is_function()) { if (!trap.is_function()) {
vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyInvalidTrap, "preventExtensions"); vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyInvalidTrap, "preventExtensions");
return {}; return {};
} }
auto trap_result = vm().call(trap.as_function(), Value(&m_handler), Value(&m_target)); auto trap_result = vm.call(trap.as_function(), Value(&m_handler), Value(&m_target));
if (vm().exception()) if (vm.exception())
return false; return false;
if (trap_result.to_boolean() && m_target.is_extensible()) { if (trap_result.to_boolean() && m_target.is_extensible()) {
if (!vm().exception()) if (!vm.exception())
vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyPreventExtensionsReturn); vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyPreventExtensionsReturn);
return false; return false;
} }
return trap_result.to_boolean(); return trap_result.to_boolean();
@ -209,54 +214,55 @@ bool ProxyObject::prevent_extensions()
Optional<PropertyDescriptor> ProxyObject::get_own_property_descriptor(const PropertyName& name) const Optional<PropertyDescriptor> ProxyObject::get_own_property_descriptor(const PropertyName& name) const
{ {
auto& vm = this->vm();
if (m_is_revoked) { if (m_is_revoked) {
vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyRevoked); vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyRevoked);
return {}; return {};
} }
auto trap = m_handler.get(vm().names.getOwnPropertyDescriptor); auto trap = m_handler.get(vm.names.getOwnPropertyDescriptor);
if (vm().exception()) if (vm.exception())
return {}; return {};
if (trap.is_empty() || trap.is_nullish()) if (trap.is_empty() || trap.is_nullish())
return m_target.get_own_property_descriptor(name); return m_target.get_own_property_descriptor(name);
if (!trap.is_function()) { if (!trap.is_function()) {
vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyInvalidTrap, "getOwnPropertyDescriptor"); vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyInvalidTrap, "getOwnPropertyDescriptor");
return {}; return {};
} }
auto trap_result = vm().call(trap.as_function(), Value(&m_handler), Value(&m_target), js_string(vm(), name.to_string())); auto trap_result = vm.call(trap.as_function(), Value(&m_handler), Value(&m_target), js_string(vm, name.to_string()));
if (vm().exception()) if (vm.exception())
return {}; return {};
if (!trap_result.is_object() && !trap_result.is_undefined()) { if (!trap_result.is_object() && !trap_result.is_undefined()) {
vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyGetOwnDescriptorReturn); vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyGetOwnDescriptorReturn);
return {}; return {};
} }
auto target_desc = m_target.get_own_property_descriptor(name); auto target_desc = m_target.get_own_property_descriptor(name);
if (vm().exception()) if (vm.exception())
return {}; return {};
if (trap_result.is_undefined()) { if (trap_result.is_undefined()) {
if (!target_desc.has_value()) if (!target_desc.has_value())
return {}; return {};
if (!target_desc.value().attributes.is_configurable()) { if (!target_desc.value().attributes.is_configurable()) {
vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyGetOwnDescriptorNonConfigurable); vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyGetOwnDescriptorNonConfigurable);
return {}; return {};
} }
if (!m_target.is_extensible()) { if (!m_target.is_extensible()) {
if (!vm().exception()) if (!vm.exception())
vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyGetOwnDescriptorUndefReturn); vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyGetOwnDescriptorUndefReturn);
return {}; return {};
} }
return {}; return {};
} }
auto result_desc = PropertyDescriptor::from_dictionary(vm(), trap_result.as_object()); auto result_desc = PropertyDescriptor::from_dictionary(vm, trap_result.as_object());
if (vm().exception()) if (vm.exception())
return {}; return {};
if (!is_compatible_property_descriptor(m_target.is_extensible(), result_desc, target_desc)) { if (!is_compatible_property_descriptor(m_target.is_extensible(), result_desc, target_desc)) {
if (!vm().exception()) if (!vm.exception())
vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyGetOwnDescriptorInvalidDescriptor); vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyGetOwnDescriptorInvalidDescriptor);
return {}; return {};
} }
if (!result_desc.attributes.is_configurable() && (!target_desc.has_value() || target_desc.value().attributes.is_configurable())) { if (!result_desc.attributes.is_configurable() && (!target_desc.has_value() || target_desc.value().attributes.is_configurable())) {
vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyGetOwnDescriptorInvalidNonConfig); vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyGetOwnDescriptorInvalidNonConfig);
return {}; return {};
} }
return result_desc; return result_desc;
@ -264,49 +270,50 @@ Optional<PropertyDescriptor> ProxyObject::get_own_property_descriptor(const Prop
bool ProxyObject::define_property(const StringOrSymbol& property_name, const Object& descriptor, bool throw_exceptions) bool ProxyObject::define_property(const StringOrSymbol& property_name, const Object& descriptor, bool throw_exceptions)
{ {
auto& vm = this->vm();
if (m_is_revoked) { if (m_is_revoked) {
vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyRevoked); vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyRevoked);
return false; return false;
} }
auto trap = m_handler.get(vm().names.defineProperty); auto trap = m_handler.get(vm.names.defineProperty);
if (vm().exception()) if (vm.exception())
return false; return false;
if (trap.is_empty() || trap.is_nullish()) if (trap.is_empty() || trap.is_nullish())
return m_target.define_property(property_name, descriptor, throw_exceptions); return m_target.define_property(property_name, descriptor, throw_exceptions);
if (!trap.is_function()) { if (!trap.is_function()) {
vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyInvalidTrap, "defineProperty"); vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyInvalidTrap, "defineProperty");
return false; return false;
} }
auto trap_result = vm().call(trap.as_function(), Value(&m_handler), Value(&m_target), property_name.to_value(vm()), Value(const_cast<Object*>(&descriptor))); auto trap_result = vm.call(trap.as_function(), Value(&m_handler), Value(&m_target), property_name.to_value(vm), Value(const_cast<Object*>(&descriptor)));
if (vm().exception() || !trap_result.to_boolean()) if (vm.exception() || !trap_result.to_boolean())
return false; return false;
auto target_desc = m_target.get_own_property_descriptor(property_name); auto target_desc = m_target.get_own_property_descriptor(property_name);
if (vm().exception()) if (vm.exception())
return false; return false;
bool setting_config_false = false; bool setting_config_false = false;
if (descriptor.has_property(vm().names.configurable) && !descriptor.get(vm().names.configurable).to_boolean()) if (descriptor.has_property(vm.names.configurable) && !descriptor.get(vm.names.configurable).to_boolean())
setting_config_false = true; setting_config_false = true;
if (vm().exception()) if (vm.exception())
return false; return false;
if (!target_desc.has_value()) { if (!target_desc.has_value()) {
if (!m_target.is_extensible()) { if (!m_target.is_extensible()) {
if (!vm().exception()) if (!vm.exception())
vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyDefinePropNonExtensible); vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyDefinePropNonExtensible);
return false; return false;
} }
if (setting_config_false) { if (setting_config_false) {
vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyDefinePropNonConfigurableNonExisting); vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyDefinePropNonConfigurableNonExisting);
return false; return false;
} }
} else { } else {
if (!is_compatible_property_descriptor(m_target.is_extensible(), PropertyDescriptor::from_dictionary(vm(), descriptor), target_desc)) { if (!is_compatible_property_descriptor(m_target.is_extensible(), PropertyDescriptor::from_dictionary(vm, descriptor), target_desc)) {
if (!vm().exception()) if (!vm.exception())
vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyDefinePropIncompatibleDescriptor); vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyDefinePropIncompatibleDescriptor);
return false; return false;
} }
if (setting_config_false && target_desc.value().attributes.is_configurable()) { if (setting_config_false && target_desc.value().attributes.is_configurable()) {
vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyDefinePropExistingConfigurable); vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyDefinePropExistingConfigurable);
return false; return false;
} }
} }
@ -315,35 +322,36 @@ bool ProxyObject::define_property(const StringOrSymbol& property_name, const Obj
bool ProxyObject::has_property(const PropertyName& name) const bool ProxyObject::has_property(const PropertyName& name) const
{ {
auto& vm = this->vm();
if (m_is_revoked) { if (m_is_revoked) {
vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyRevoked); vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyRevoked);
return false; return false;
} }
auto trap = m_handler.get(vm().names.has); auto trap = m_handler.get(vm.names.has);
if (vm().exception()) if (vm.exception())
return false; return false;
if (trap.is_empty() || trap.is_nullish()) if (trap.is_empty() || trap.is_nullish())
return m_target.has_property(name); return m_target.has_property(name);
if (!trap.is_function()) { if (!trap.is_function()) {
vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyInvalidTrap, "has"); vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyInvalidTrap, "has");
return false; return false;
} }
auto trap_result = vm().call(trap.as_function(), Value(&m_handler), Value(&m_target), js_string(vm(), name.to_string())); auto trap_result = vm.call(trap.as_function(), Value(&m_handler), Value(&m_target), js_string(vm, name.to_string()));
if (vm().exception()) if (vm.exception())
return false; return false;
if (!trap_result.to_boolean()) { if (!trap_result.to_boolean()) {
auto target_desc = m_target.get_own_property_descriptor(name); auto target_desc = m_target.get_own_property_descriptor(name);
if (vm().exception()) if (vm.exception())
return false; return false;
if (target_desc.has_value()) { if (target_desc.has_value()) {
if (!target_desc.value().attributes.is_configurable()) { if (!target_desc.value().attributes.is_configurable()) {
vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyHasExistingNonConfigurable); vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyHasExistingNonConfigurable);
return false; return false;
} }
if (!m_target.is_extensible()) { if (!m_target.is_extensible()) {
if (!vm().exception()) if (!vm.exception())
vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyHasExistingNonExtensible); vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyHasExistingNonExtensible);
return false; return false;
} }
} }
@ -353,33 +361,34 @@ bool ProxyObject::has_property(const PropertyName& name) const
Value ProxyObject::get(const PropertyName& name, Value) const Value ProxyObject::get(const PropertyName& name, Value) const
{ {
auto& vm = this->vm();
if (m_is_revoked) { if (m_is_revoked) {
vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyRevoked); vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyRevoked);
return {}; return {};
} }
auto trap = m_handler.get(vm().names.get); auto trap = m_handler.get(vm.names.get);
if (vm().exception()) if (vm.exception())
return {}; return {};
if (trap.is_empty() || trap.is_nullish()) if (trap.is_empty() || trap.is_nullish())
return m_target.get(name); return m_target.get(name);
if (!trap.is_function()) { if (!trap.is_function()) {
vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyInvalidTrap, "get"); vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyInvalidTrap, "get");
return {}; return {};
} }
auto trap_result = vm().call(trap.as_function(), Value(&m_handler), Value(&m_target), js_string(vm(), name.to_string()), Value(const_cast<ProxyObject*>(this))); auto trap_result = vm.call(trap.as_function(), Value(&m_handler), Value(&m_target), js_string(vm, name.to_string()), Value(const_cast<ProxyObject*>(this)));
if (vm().exception()) if (vm.exception())
return {}; return {};
auto target_desc = m_target.get_own_property_descriptor(name); auto target_desc = m_target.get_own_property_descriptor(name);
if (target_desc.has_value()) { if (target_desc.has_value()) {
if (vm().exception()) if (vm.exception())
return {}; return {};
if (target_desc.value().is_data_descriptor() && !target_desc.value().attributes.is_writable() && !same_value(trap_result, target_desc.value().value)) { if (target_desc.value().is_data_descriptor() && !target_desc.value().attributes.is_writable() && !same_value(trap_result, target_desc.value().value)) {
vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyGetImmutableDataProperty); vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyGetImmutableDataProperty);
return {}; return {};
} }
if (target_desc.value().is_accessor_descriptor() && target_desc.value().getter == nullptr && !trap_result.is_undefined()) { if (target_desc.value().is_accessor_descriptor() && target_desc.value().getter == nullptr && !trap_result.is_undefined()) {
vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyGetNonConfigurableAccessor); vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyGetNonConfigurableAccessor);
return {}; return {};
} }
} }