1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 02:07:36 +00:00

LibJS: Convert internal_prevent_extensions() to ThrowCompletionOr

This commit is contained in:
Linus Groh 2021-09-29 00:13:41 +01:00
parent 9b4362f10a
commit 73bae7d779
12 changed files with 22 additions and 30 deletions

View file

@ -206,7 +206,7 @@ void GlobalObject::initialize_global_object()
});
m_throw_type_error_function->define_direct_property_without_transition(vm.names.length, Value(0), 0);
m_throw_type_error_function->define_direct_property_without_transition(vm.names.name, js_string(vm, ""), 0);
m_throw_type_error_function->internal_prevent_extensions();
(void)m_throw_type_error_function->internal_prevent_extensions();
// 10.2.4 AddRestrictedFunctionProperties ( F, realm ), https://tc39.es/ecma262/#sec-addrestrictedfunctionproperties
m_function_prototype->define_direct_accessor_without_transition(vm.names.caller, m_throw_type_error_function, m_throw_type_error_function, Attribute::Configurable);

View file

@ -301,9 +301,7 @@ bool Object::set_integrity_level(IntegrityLevel level)
VERIFY(level == IntegrityLevel::Sealed || level == IntegrityLevel::Frozen);
// 3. Let status be ? O.[[PreventExtensions]]().
auto status = internal_prevent_extensions();
if (vm.exception())
return {};
auto status = TRY_OR_DISCARD(internal_prevent_extensions());
// 4. If status is false, return false.
if (!status)
@ -554,7 +552,7 @@ ThrowCompletionOr<bool> Object::internal_is_extensible() const
}
// 10.1.4 [[PreventExtensions]] ( ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-preventextensions
bool Object::internal_prevent_extensions()
ThrowCompletionOr<bool> Object::internal_prevent_extensions()
{
// 1. Set O.[[Extensible]] to false.
m_is_extensible = false;

View file

@ -94,7 +94,7 @@ public:
virtual ThrowCompletionOr<Object*> internal_get_prototype_of() const;
virtual ThrowCompletionOr<bool> internal_set_prototype_of(Object* prototype);
virtual ThrowCompletionOr<bool> internal_is_extensible() const;
virtual bool internal_prevent_extensions();
virtual ThrowCompletionOr<bool> internal_prevent_extensions();
virtual Optional<PropertyDescriptor> internal_get_own_property(PropertyName const&) const;
virtual bool internal_define_own_property(PropertyName const&, PropertyDescriptor const&);
virtual bool internal_has_property(PropertyName const&) const;

View file

@ -208,9 +208,7 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::prevent_extensions)
auto argument = vm.argument(0);
if (!argument.is_object())
return argument;
auto status = argument.as_object().internal_prevent_extensions();
if (vm.exception())
return {};
auto status = TRY_OR_DISCARD(argument.as_object().internal_prevent_extensions());
if (!status) {
// FIXME: Improve/contextualize error message
vm.throw_exception<TypeError>(global_object, ErrorType::ObjectPreventExtensionsReturnedFalse);

View file

@ -188,7 +188,7 @@ ThrowCompletionOr<bool> ProxyObject::internal_is_extensible() const
}
// 10.5.4 [[PreventExtensions]] ( ), https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-preventextensions
bool ProxyObject::internal_prevent_extensions()
ThrowCompletionOr<bool> ProxyObject::internal_prevent_extensions()
{
auto& vm = this->vm();
auto& global_object = this->global_object();
@ -196,16 +196,14 @@ bool ProxyObject::internal_prevent_extensions()
// 1. Let handler be O.[[ProxyHandler]].
// 2. 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);
// 3. Assert: Type(handler) is Object.
// 4. Let target be O.[[ProxyTarget]].
// 5. Let trap be ? GetMethod(handler, "preventExtensions").
auto trap = TRY_OR_DISCARD(Value(&m_handler).get_method(global_object, vm.names.preventExtensions));
auto trap = TRY(Value(&m_handler).get_method(global_object, vm.names.preventExtensions));
// 6. If trap is undefined, then
if (!trap) {
@ -214,20 +212,18 @@ bool ProxyObject::internal_prevent_extensions()
}
// 7. Let booleanTrapResult be ! ToBoolean(? Call(trap, handler, « target »)).
auto trap_result = TRY_OR_DISCARD(vm.call(*trap, &m_handler, &m_target)).to_boolean();
auto trap_result = TRY(vm.call(*trap, &m_handler, &m_target)).to_boolean();
// 8. If booleanTrapResult is true, then
if (trap_result) {
// a. 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());
// b. If extensibleTarget is true, throw a TypeError exception.
if (extensible_target) {
vm.throw_exception<TypeError>(global_object, ErrorType::ProxyPreventExtensionsReturn);
return {};
}
if (extensible_target)
return vm.throw_completion<TypeError>(global_object, ErrorType::ProxyPreventExtensionsReturn);
}
// 9. Return booleanTrapResult.

View file

@ -38,7 +38,7 @@ public:
virtual ThrowCompletionOr<Object*> internal_get_prototype_of() const override;
virtual ThrowCompletionOr<bool> internal_set_prototype_of(Object* prototype) override;
virtual ThrowCompletionOr<bool> internal_is_extensible() const override;
virtual bool internal_prevent_extensions() override;
virtual ThrowCompletionOr<bool> internal_prevent_extensions() override;
virtual Optional<PropertyDescriptor> internal_get_own_property(PropertyName const&) const override;
virtual bool internal_define_own_property(PropertyName const&, PropertyDescriptor const&) override;
virtual bool internal_has_property(PropertyName const&) const override;

View file

@ -283,7 +283,7 @@ JS_DEFINE_NATIVE_FUNCTION(ReflectObject::prevent_extensions)
}
// 2. Return ? target.[[PreventExtensions]]().
return Value(target.as_object().internal_prevent_extensions());
return Value(TRY_OR_DISCARD(target.as_object().internal_prevent_extensions()));
}
// 28.1.12 Reflect.set ( target, propertyKey, V [ , receiver ] ), https://tc39.es/ecma262/#sec-reflect.set