LibJS: Convert internal_define_own_property() to ThrowCompletionOr

This commit is contained in:
Linus Groh 2021-09-29 17:54:25 +01:00
parent 0e69a6e487
commit 5da210125e
Notes: sideshowbarker 2024-07-18 03:19:18 +09:00
16 changed files with 67 additions and 74 deletions

View file

@ -1417,7 +1417,7 @@ public:
generator.append(R"~~~(
virtual JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> internal_get_own_property(JS::PropertyName const&) const override;
virtual bool internal_set(JS::PropertyName const&, JS::Value, JS::Value) override;
virtual bool internal_define_own_property(JS::PropertyName const&, JS::PropertyDescriptor const&) override;
virtual JS::ThrowCompletionOr<bool> internal_define_own_property(JS::PropertyName const&, JS::PropertyDescriptor const&) override;
virtual bool internal_delete(JS::PropertyName const&) override;
virtual JS::ThrowCompletionOr<bool> internal_prevent_extensions() override;
virtual JS::MarkedValueList internal_own_property_keys() const override;
@ -2051,7 +2051,7 @@ bool @class_name@::internal_set(JS::PropertyName const& property_name, JS::Value
// 3.9.3. [[DefineOwnProperty]], https://heycam.github.io/webidl/#legacy-platform-object-defineownproperty
scoped_generator.append(R"~~~(
bool @class_name@::internal_define_own_property(JS::PropertyName const& property_name, JS::PropertyDescriptor const& property_descriptor)
JS::ThrowCompletionOr<bool> @class_name@::internal_define_own_property(JS::PropertyName const& property_name, JS::PropertyDescriptor const& property_descriptor)
{
[[maybe_unused]] auto& vm = this->vm();
auto& global_object = this->global_object();
@ -2076,8 +2076,8 @@ bool @class_name@::internal_define_own_property(JS::PropertyName const& property
scoped_generator.append(R"~~~(
// 3. Invoke the indexed property setter on O with P and Desc.[[Value]].
invoke_indexed_property_setter(global_object, impl(), property_name, *property_descriptor.value);
if (vm.exception())
return {};
if (auto* exception = vm.exception())
return JS::throw_completion(exception->value());
// 4. Return true.
return true;
@ -2110,7 +2110,7 @@ bool @class_name@::internal_define_own_property(JS::PropertyName const& property
if (!interface.extended_attributes.contains("LegacyOverrideBuiltIns")) {
scoped_generator.append(R"~~~(
// NOTE: This has to be done manually instead of using Object::has_own_property, as that would use the overrided internal_get_own_property.
auto own_property_named_p = TRY_OR_DISCARD(Object::internal_get_own_property(property_name));
auto own_property_named_p = TRY(Object::internal_get_own_property(property_name));
if (!own_property_named_p.has_value()))~~~");
}
@ -2135,8 +2135,8 @@ bool @class_name@::internal_define_own_property(JS::PropertyName const& property
// 2. Invoke the named property setter on O with P and Desc.[[Value]].
invoke_named_property_setter(global_object, impl(), property_name_as_string, *property_descriptor.value);
if (vm.exception())
return {};
if (auto* exception = vm.exception())
return JS::throw_completion(exception->value());
// 3. Return true.
return true;

View file

@ -123,7 +123,7 @@ ThrowCompletionOr<Optional<PropertyDescriptor>> ArgumentsObject::internal_get_ow
}
// 10.4.4.2 [[DefineOwnProperty]] ( P, Desc ), https://tc39.es/ecma262/#sec-arguments-exotic-objects-defineownproperty-p-desc
bool ArgumentsObject::internal_define_own_property(PropertyName const& property_name, PropertyDescriptor const& descriptor)
ThrowCompletionOr<bool> ArgumentsObject::internal_define_own_property(PropertyName const& property_name, PropertyDescriptor const& descriptor)
{
// 1. Let map be args.[[ParameterMap]].
auto& map = parameter_map();
@ -146,9 +146,7 @@ bool ArgumentsObject::internal_define_own_property(PropertyName const& property_
}
// 5. Let allowed be ? OrdinaryDefineOwnProperty(args, P, newArgDesc).
bool allowed = Object::internal_define_own_property(property_name, new_arg_desc);
if (vm().exception())
return false;
bool allowed = TRY(Object::internal_define_own_property(property_name, new_arg_desc));
// 6. If allowed is false, return false.
if (!allowed)

View file

@ -24,7 +24,7 @@ public:
Environment& environment() { return m_environment; }
virtual ThrowCompletionOr<Optional<PropertyDescriptor>> internal_get_own_property(PropertyName const&) const override;
virtual bool internal_define_own_property(PropertyName const&, PropertyDescriptor const&) override;
virtual ThrowCompletionOr<bool> internal_define_own_property(PropertyName const&, PropertyDescriptor const&) override;
virtual Value internal_get(PropertyName const&, Value receiver) const override;
virtual bool internal_set(PropertyName const&, Value value, Value receiver) override;
virtual bool internal_delete(PropertyName const&) override;

View file

@ -24,7 +24,7 @@ Array* Array::create(GlobalObject& global_object, size_t length, Object* prototy
if (!prototype)
prototype = global_object.array_prototype();
auto* array = global_object.heap().allocate<Array>(global_object, *prototype);
array->internal_define_own_property(vm.names.length, { .value = Value(length), .writable = true, .enumerable = false, .configurable = false });
(void)array->internal_define_own_property(vm.names.length, { .value = Value(length), .writable = true, .enumerable = false, .configurable = false });
return array;
}
@ -168,7 +168,7 @@ ThrowCompletionOr<Optional<PropertyDescriptor>> Array::internal_get_own_property
}
// 10.4.2.1 [[DefineOwnProperty]] ( P, Desc ), https://tc39.es/ecma262/#sec-array-exotic-objects-defineownproperty-p-desc
bool Array::internal_define_own_property(PropertyName const& property_name, PropertyDescriptor const& property_descriptor)
ThrowCompletionOr<bool> Array::internal_define_own_property(PropertyName const& property_name, PropertyDescriptor const& property_descriptor)
{
auto& vm = this->vm();
@ -178,7 +178,10 @@ bool Array::internal_define_own_property(PropertyName const& property_name, Prop
// 2. If P is "length", then
if (property_name.is_string() && property_name.as_string() == vm.names.length.as_string()) {
// a. Return ? ArraySetLength(A, Desc).
return set_length(property_descriptor);
auto success = set_length(property_descriptor);
if (auto* exception = vm.exception())
return throw_completion(exception->value());
return success;
}
// 3. Else if P is an array index, then
@ -195,7 +198,7 @@ bool Array::internal_define_own_property(PropertyName const& property_name, Prop
return false;
// h. Let succeeded be ! OrdinaryDefineOwnProperty(A, P, Desc).
auto succeeded = Object::internal_define_own_property(property_name, property_descriptor);
auto succeeded = Object::internal_define_own_property(property_name, property_descriptor).release_value();
// i. If succeeded is false, return false.
if (!succeeded)
return false;

View file

@ -39,7 +39,7 @@ public:
virtual ~Array() override;
virtual ThrowCompletionOr<Optional<PropertyDescriptor>> internal_get_own_property(PropertyName const&) const override;
virtual bool internal_define_own_property(PropertyName const&, PropertyDescriptor const&) override;
virtual ThrowCompletionOr<bool> internal_define_own_property(PropertyName const&, PropertyDescriptor const&) override;
virtual bool internal_delete(PropertyName const&) override;
virtual MarkedValueList internal_own_property_keys() const override;

View file

@ -137,7 +137,7 @@ bool Object::create_data_property(PropertyName const& property_name, Value value
};
// 4. Return ? O.[[DefineOwnProperty]](P, newDesc).
return internal_define_own_property(property_name, new_descriptor);
return TRY_OR_DISCARD(internal_define_own_property(property_name, new_descriptor));
}
// 7.3.6 CreateMethodProperty ( O, P, V ), https://tc39.es/ecma262/#sec-createmethodproperty
@ -159,7 +159,7 @@ bool Object::create_method_property(PropertyName const& property_name, Value val
};
// 4. Return ? O.[[DefineOwnProperty]](P, newDesc).
return internal_define_own_property(property_name, new_descriptor);
return TRY_OR_DISCARD(internal_define_own_property(property_name, new_descriptor));
}
// 7.3.7 CreateDataPropertyOrThrow ( O, P, V ), https://tc39.es/ecma262/#sec-createdatapropertyorthrow
@ -213,9 +213,7 @@ bool Object::define_property_or_throw(PropertyName const& property_name, Propert
VERIFY(property_name.is_valid());
// 3. Let success be ? O.[[DefineOwnProperty]](P, desc).
auto success = internal_define_own_property(property_name, property_descriptor);
if (vm.exception())
return {};
auto success = TRY_OR_DISCARD(internal_define_own_property(property_name, property_descriptor));
// 4. If success is false, throw a TypeError exception.
if (!success) {
@ -600,18 +598,18 @@ ThrowCompletionOr<Optional<PropertyDescriptor>> Object::internal_get_own_propert
}
// 10.1.6 [[DefineOwnProperty]] ( P, Desc ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-defineownproperty-p-desc
bool Object::internal_define_own_property(PropertyName const& property_name, PropertyDescriptor const& property_descriptor)
ThrowCompletionOr<bool> Object::internal_define_own_property(PropertyName const& property_name, PropertyDescriptor const& property_descriptor)
{
VERIFY(property_name.is_valid());
auto& vm = this->vm();
// 1. Let current be ? O.[[GetOwnProperty]](P).
auto current = TRY_OR_DISCARD(internal_get_own_property(property_name));
auto current = TRY(internal_get_own_property(property_name));
// 2. Let extensible be ? IsExtensible(O).
auto extensible = is_extensible();
if (vm.exception())
return {};
if (auto* exception = vm.exception())
return throw_completion(exception->value());
// 3. Return ValidateAndApplyPropertyDescriptor(O, P, extensible, Desc, current).
return validate_and_apply_property_descriptor(this, property_name, extensible, property_descriptor, current);
@ -759,7 +757,7 @@ bool Object::ordinary_set_with_own_descriptor(PropertyName const& property_name,
auto value_descriptor = PropertyDescriptor { .value = value };
// iv. Return ? Receiver.[[DefineOwnProperty]](P, valueDesc).
return receiver.as_object().internal_define_own_property(property_name, value_descriptor);
return TRY_OR_DISCARD(receiver.as_object().internal_define_own_property(property_name, value_descriptor));
}
// e. Else,
else {
@ -783,6 +781,8 @@ bool Object::ordinary_set_with_own_descriptor(PropertyName const& property_name,
// 7. Perform ? Call(setter, Receiver, « V »).
(void)vm.call(*setter, receiver, value);
if (vm.exception())
return {};
// 8. Return true.
return true;

View file

@ -96,7 +96,7 @@ public:
virtual ThrowCompletionOr<bool> internal_is_extensible() const;
virtual ThrowCompletionOr<bool> internal_prevent_extensions();
virtual ThrowCompletionOr<Optional<PropertyDescriptor>> internal_get_own_property(PropertyName const&) const;
virtual bool internal_define_own_property(PropertyName const&, PropertyDescriptor const&);
virtual ThrowCompletionOr<bool> internal_define_own_property(PropertyName const&, PropertyDescriptor const&);
virtual bool internal_has_property(PropertyName const&) const;
virtual Value internal_get(PropertyName const&, Value receiver) const;
virtual bool internal_set(PropertyName const&, Value value, Value receiver);

View file

@ -330,7 +330,7 @@ ThrowCompletionOr<Optional<PropertyDescriptor>> ProxyObject::internal_get_own_pr
}
// 10.5.6 [[DefineOwnProperty]] ( P, Desc ), https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-defineownproperty-p-desc
bool ProxyObject::internal_define_own_property(PropertyName const& property_name, PropertyDescriptor const& property_descriptor)
ThrowCompletionOr<bool> ProxyObject::internal_define_own_property(PropertyName const& property_name, PropertyDescriptor const& property_descriptor)
{
auto& vm = this->vm();
auto& global_object = this->global_object();
@ -341,16 +341,14 @@ bool ProxyObject::internal_define_own_property(PropertyName const& property_name
// 2. Let handler be O.[[ProxyHandler]].
// 3. If handler is null, throw a TypeError exception.
if (m_is_revoked) {
vm.throw_exception<TypeError>(global_object, ErrorType::ProxyRevoked);
return {};
}
if (m_is_revoked)
return vm.throw_completion<TypeError>(global_object, ErrorType::ProxyRevoked);
// 4. Assert: Type(handler) is Object.
// 5. Let target be O.[[ProxyTarget]].
// 6. Let trap be ? GetMethod(handler, "defineProperty").
auto trap = TRY_OR_DISCARD(Value(&m_handler).get_method(global_object, vm.names.defineProperty));
auto trap = TRY(Value(&m_handler).get_method(global_object, vm.names.defineProperty));
// 7. If trap is undefined, then
if (!trap) {
@ -362,19 +360,19 @@ bool ProxyObject::internal_define_own_property(PropertyName const& property_name
auto descriptor_object = from_property_descriptor(global_object, property_descriptor);
// 9. Let booleanTrapResult be ! ToBoolean(? Call(trap, handler, « target, P, descObj »)).
auto trap_result = TRY_OR_DISCARD(vm.call(*trap, &m_handler, &m_target, property_name_to_value(vm, property_name), descriptor_object)).to_boolean();
auto trap_result = TRY(vm.call(*trap, &m_handler, &m_target, property_name_to_value(vm, property_name), descriptor_object)).to_boolean();
// 10. If booleanTrapResult is false, return false.
if (!trap_result)
return false;
// 11. Let targetDesc be ? target.[[GetOwnProperty]](P).
auto target_descriptor = TRY_OR_DISCARD(m_target.internal_get_own_property(property_name));
auto target_descriptor = TRY(m_target.internal_get_own_property(property_name));
// 12. Let extensibleTarget be ? IsExtensible(target).
auto extensible_target = m_target.is_extensible();
if (vm.exception())
return {};
if (auto* exception = vm.exception())
return throw_completion(exception->value());
// 14. Else, let settingConfigFalse be false.
bool setting_config_false = false;
@ -388,35 +386,28 @@ bool ProxyObject::internal_define_own_property(PropertyName const& property_name
// 15. If targetDesc is undefined, then
if (!target_descriptor.has_value()) {
// a. If extensibleTarget is false, throw a TypeError exception.
if (!extensible_target) {
vm.throw_exception<TypeError>(global_object, ErrorType::ProxyDefinePropNonExtensible);
return {};
}
if (!extensible_target)
return vm.throw_completion<TypeError>(global_object, ErrorType::ProxyDefinePropNonExtensible);
// b. If settingConfigFalse is true, throw a TypeError exception.
if (setting_config_false) {
vm.throw_exception<TypeError>(global_object, ErrorType::ProxyDefinePropNonConfigurableNonExisting);
return {};
}
if (setting_config_false)
return vm.throw_completion<TypeError>(global_object, ErrorType::ProxyDefinePropNonConfigurableNonExisting);
}
// 16. Else,
else {
// a. If IsCompatiblePropertyDescriptor(extensibleTarget, Desc, targetDesc) is false, throw a TypeError exception.
if (!is_compatible_property_descriptor(extensible_target, property_descriptor, target_descriptor)) {
vm.throw_exception<TypeError>(global_object, ErrorType::ProxyDefinePropIncompatibleDescriptor);
return {};
}
if (!is_compatible_property_descriptor(extensible_target, property_descriptor, target_descriptor))
return vm.throw_completion<TypeError>(global_object, ErrorType::ProxyDefinePropIncompatibleDescriptor);
// b. If settingConfigFalse is true and targetDesc.[[Configurable]] is true, throw a TypeError exception.
if (setting_config_false && *target_descriptor->configurable) {
vm.throw_exception<TypeError>(global_object, ErrorType::ProxyDefinePropExistingConfigurable);
return {};
}
if (setting_config_false && *target_descriptor->configurable)
return vm.throw_completion<TypeError>(global_object, ErrorType::ProxyDefinePropExistingConfigurable);
// c. If IsDataDescriptor(targetDesc) is true, targetDesc.[[Configurable]] is false, and targetDesc.[[Writable]] is true, then
if (target_descriptor->is_data_descriptor() && !*target_descriptor->configurable && *target_descriptor->writable) {
// i. If Desc has a [[Writable]] field and Desc.[[Writable]] is false, throw a TypeError exception.
if (property_descriptor.writable.has_value() && !*property_descriptor.writable) {
vm.throw_exception<TypeError>(global_object, ErrorType::ProxyDefinePropNonWritable);
return {};
}
if (property_descriptor.writable.has_value() && !*property_descriptor.writable)
return vm.throw_completion<TypeError>(global_object, ErrorType::ProxyDefinePropNonWritable);
}
}

View file

@ -40,7 +40,7 @@ public:
virtual ThrowCompletionOr<bool> internal_is_extensible() const override;
virtual ThrowCompletionOr<bool> internal_prevent_extensions() override;
virtual ThrowCompletionOr<Optional<PropertyDescriptor>> internal_get_own_property(PropertyName const&) const override;
virtual bool internal_define_own_property(PropertyName const&, PropertyDescriptor const&) override;
virtual ThrowCompletionOr<bool> internal_define_own_property(PropertyName const&, PropertyDescriptor const&) override;
virtual bool internal_has_property(PropertyName const&) const override;
virtual Value internal_get(PropertyName const&, Value receiver) const override;
virtual bool internal_set(PropertyName const&, Value value, Value receiver) override;

View file

@ -122,7 +122,7 @@ JS_DEFINE_NATIVE_FUNCTION(ReflectObject::define_property)
return {};
// 4. Return ? target.[[DefineOwnProperty]](key, desc).
return Value(target.as_object().internal_define_own_property(key, descriptor));
return Value(TRY_OR_DISCARD(target.as_object().internal_define_own_property(key, descriptor)));
}
// 28.1.4 Reflect.deleteProperty ( target, propertyKey ), https://tc39.es/ecma262/#sec-reflect.deleteproperty

View file

@ -108,7 +108,7 @@ ThrowCompletionOr<Optional<PropertyDescriptor>> StringObject::internal_get_own_p
}
// 10.4.3.2 [[DefineOwnProperty]] ( P, Desc ), https://tc39.es/ecma262/#sec-string-exotic-objects-defineownproperty-p-desc
bool StringObject::internal_define_own_property(PropertyName const& property_name, PropertyDescriptor const& property_descriptor)
ThrowCompletionOr<bool> StringObject::internal_define_own_property(PropertyName const& property_name, PropertyDescriptor const& property_descriptor)
{
// 1. Assert: IsPropertyKey(P) is true.
VERIFY(property_name.is_valid());

View file

@ -28,7 +28,7 @@ public:
private:
virtual ThrowCompletionOr<Optional<PropertyDescriptor>> internal_get_own_property(PropertyName const&) const override;
virtual bool internal_define_own_property(PropertyName const&, PropertyDescriptor const&) override;
virtual ThrowCompletionOr<bool> internal_define_own_property(PropertyName const&, PropertyDescriptor const&) override;
virtual MarkedValueList internal_own_property_keys() const override;
virtual bool is_string_object() const final { return true; }

View file

@ -254,7 +254,7 @@ public:
}
// 10.4.5.3 [[DefineOwnProperty]] ( P, Desc ), https://tc39.es/ecma262/#sec-integer-indexed-exotic-objects-defineownproperty-p-desc
virtual bool internal_define_own_property(PropertyName const& property_name, PropertyDescriptor const& property_descriptor) override
virtual ThrowCompletionOr<bool> internal_define_own_property(PropertyName const& property_name, PropertyDescriptor const& property_descriptor) override
{
// 1. Assert: IsPropertyKey(P) is true.
VERIFY(property_name.is_valid());
@ -295,8 +295,8 @@ public:
// vi. If Desc has a [[Value]] field, perform ? IntegerIndexedElementSet(O, numericIndex, Desc.[[Value]]).
if (property_descriptor.value.has_value()) {
integer_indexed_element_set<T>(*this, numeric_index, *property_descriptor.value);
if (vm().exception())
return {};
if (auto* exception = vm().exception())
return throw_completion(exception->value());
}
// vii. Return true.

View file

@ -6,11 +6,12 @@
#pragma once
#define REPLACEABLE_PROPERTY_SETTER(ObjectType, property) \
auto this_value = vm.this_value(global_object); \
if (!this_value.is_object() || !is<ObjectType>(this_value.as_object())) { \
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, #ObjectType); \
return {}; \
} \
this_value.as_object().internal_define_own_property(#property, JS::PropertyDescriptor { .value = vm.argument(0), .writable = true }); \
#define REPLACEABLE_PROPERTY_SETTER(ObjectType, property) \
auto this_value = vm.this_value(global_object); \
if (!this_value.is_object() || !is<ObjectType>(this_value.as_object())) { \
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, #ObjectType); \
return {}; \
} \
TRY_OR_DISCARD(this_value.as_object().internal_define_own_property( \
#property, JS::PropertyDescriptor { .value = vm.argument(0), .writable = true })); \
return JS::js_undefined();

View file

@ -65,7 +65,7 @@ JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> ConsoleGlobalObject::int
return Base::internal_get_own_property(property_name);
}
bool ConsoleGlobalObject::internal_define_own_property(JS::PropertyName const& property_name, JS::PropertyDescriptor const& descriptor)
JS::ThrowCompletionOr<bool> ConsoleGlobalObject::internal_define_own_property(JS::PropertyName const& property_name, JS::PropertyDescriptor const& descriptor)
{
return m_window_object->internal_define_own_property(property_name, descriptor);
}

View file

@ -28,7 +28,7 @@ public:
virtual JS::ThrowCompletionOr<bool> internal_is_extensible() const override;
virtual JS::ThrowCompletionOr<bool> internal_prevent_extensions() override;
virtual JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> internal_get_own_property(JS::PropertyName const& name) const override;
virtual bool internal_define_own_property(JS::PropertyName const& name, JS::PropertyDescriptor const& descriptor) override;
virtual JS::ThrowCompletionOr<bool> internal_define_own_property(JS::PropertyName const& name, JS::PropertyDescriptor const& descriptor) override;
virtual bool internal_has_property(JS::PropertyName const& name) const override;
virtual JS::Value internal_get(JS::PropertyName const&, JS::Value) const override;
virtual bool internal_set(JS::PropertyName const&, JS::Value value, JS::Value receiver) override;