From 73bae7d779ebbf14eec603d8619f4d5247a6e69a Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Wed, 29 Sep 2021 00:13:41 +0100 Subject: [PATCH] LibJS: Convert internal_prevent_extensions() to ThrowCompletionOr --- .../LibWeb/WrapperGenerator.cpp | 4 ++-- .../Libraries/LibJS/Runtime/GlobalObject.cpp | 2 +- Userland/Libraries/LibJS/Runtime/Object.cpp | 6 ++--- Userland/Libraries/LibJS/Runtime/Object.h | 2 +- .../LibJS/Runtime/ObjectConstructor.cpp | 4 +--- .../Libraries/LibJS/Runtime/ProxyObject.cpp | 22 ++++++++----------- .../Libraries/LibJS/Runtime/ProxyObject.h | 2 +- .../Libraries/LibJS/Runtime/ReflectObject.cpp | 2 +- .../LibWeb/Bindings/LocationObject.cpp | 2 +- .../LibWeb/Bindings/LocationObject.h | 2 +- .../WebContent/ConsoleGlobalObject.cpp | 2 +- .../Services/WebContent/ConsoleGlobalObject.h | 2 +- 12 files changed, 22 insertions(+), 30 deletions(-) diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/WrapperGenerator.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/WrapperGenerator.cpp index 25195cda90..4c7d96129b 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/WrapperGenerator.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/WrapperGenerator.cpp @@ -1419,7 +1419,7 @@ public: 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 bool internal_delete(JS::PropertyName const&) override; - virtual bool internal_prevent_extensions() override; + virtual JS::ThrowCompletionOr internal_prevent_extensions() override; virtual JS::MarkedValueList internal_own_property_keys() const override; )~~~"); } @@ -2293,7 +2293,7 @@ bool @class_name@::internal_delete(JS::PropertyName const& property_name) // 3.9.5. [[PreventExtensions]], https://heycam.github.io/webidl/#legacy-platform-object-preventextensions scoped_generator.append(R"~~~( -bool @class_name@::internal_prevent_extensions() +JS::ThrowCompletionOr @class_name@::internal_prevent_extensions() { // 1. Return false. return false; diff --git a/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp b/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp index 6f4ab44266..e93430100a 100644 --- a/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp @@ -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); diff --git a/Userland/Libraries/LibJS/Runtime/Object.cpp b/Userland/Libraries/LibJS/Runtime/Object.cpp index 61a2ee4bb7..ce0a3b2836 100644 --- a/Userland/Libraries/LibJS/Runtime/Object.cpp +++ b/Userland/Libraries/LibJS/Runtime/Object.cpp @@ -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 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 Object::internal_prevent_extensions() { // 1. Set O.[[Extensible]] to false. m_is_extensible = false; diff --git a/Userland/Libraries/LibJS/Runtime/Object.h b/Userland/Libraries/LibJS/Runtime/Object.h index eb9a1f0c03..fe2e63cca0 100644 --- a/Userland/Libraries/LibJS/Runtime/Object.h +++ b/Userland/Libraries/LibJS/Runtime/Object.h @@ -94,7 +94,7 @@ public: virtual ThrowCompletionOr internal_get_prototype_of() const; virtual ThrowCompletionOr internal_set_prototype_of(Object* prototype); virtual ThrowCompletionOr internal_is_extensible() const; - virtual bool internal_prevent_extensions(); + virtual ThrowCompletionOr internal_prevent_extensions(); virtual Optional 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; diff --git a/Userland/Libraries/LibJS/Runtime/ObjectConstructor.cpp b/Userland/Libraries/LibJS/Runtime/ObjectConstructor.cpp index a93b509610..34b7d1c923 100644 --- a/Userland/Libraries/LibJS/Runtime/ObjectConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/ObjectConstructor.cpp @@ -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(global_object, ErrorType::ObjectPreventExtensionsReturnedFalse); diff --git a/Userland/Libraries/LibJS/Runtime/ProxyObject.cpp b/Userland/Libraries/LibJS/Runtime/ProxyObject.cpp index a3aa946465..64c959185c 100644 --- a/Userland/Libraries/LibJS/Runtime/ProxyObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/ProxyObject.cpp @@ -188,7 +188,7 @@ ThrowCompletionOr 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 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(global_object, ErrorType::ProxyRevoked); - return {}; - } + if (m_is_revoked) + return vm.throw_completion(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(global_object, ErrorType::ProxyPreventExtensionsReturn); - return {}; - } + if (extensible_target) + return vm.throw_completion(global_object, ErrorType::ProxyPreventExtensionsReturn); } // 9. Return booleanTrapResult. diff --git a/Userland/Libraries/LibJS/Runtime/ProxyObject.h b/Userland/Libraries/LibJS/Runtime/ProxyObject.h index faec553e70..aab0000d04 100644 --- a/Userland/Libraries/LibJS/Runtime/ProxyObject.h +++ b/Userland/Libraries/LibJS/Runtime/ProxyObject.h @@ -38,7 +38,7 @@ public: virtual ThrowCompletionOr internal_get_prototype_of() const override; virtual ThrowCompletionOr internal_set_prototype_of(Object* prototype) override; virtual ThrowCompletionOr internal_is_extensible() const override; - virtual bool internal_prevent_extensions() override; + virtual ThrowCompletionOr internal_prevent_extensions() override; virtual Optional 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; diff --git a/Userland/Libraries/LibJS/Runtime/ReflectObject.cpp b/Userland/Libraries/LibJS/Runtime/ReflectObject.cpp index e4b32240a2..ac2d057d17 100644 --- a/Userland/Libraries/LibJS/Runtime/ReflectObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/ReflectObject.cpp @@ -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 diff --git a/Userland/Libraries/LibWeb/Bindings/LocationObject.cpp b/Userland/Libraries/LibWeb/Bindings/LocationObject.cpp index df1b069154..a5946d2d90 100644 --- a/Userland/Libraries/LibWeb/Bindings/LocationObject.cpp +++ b/Userland/Libraries/LibWeb/Bindings/LocationObject.cpp @@ -140,7 +140,7 @@ JS::ThrowCompletionOr LocationObject::internal_is_extensible() const } // https://html.spec.whatwg.org/multipage/history.html#location-preventextensions -bool LocationObject::internal_prevent_extensions() +JS::ThrowCompletionOr LocationObject::internal_prevent_extensions() { // 1. Return false. return false; diff --git a/Userland/Libraries/LibWeb/Bindings/LocationObject.h b/Userland/Libraries/LibWeb/Bindings/LocationObject.h index 39af96818c..7062c4ae25 100644 --- a/Userland/Libraries/LibWeb/Bindings/LocationObject.h +++ b/Userland/Libraries/LibWeb/Bindings/LocationObject.h @@ -23,7 +23,7 @@ public: virtual JS::ThrowCompletionOr internal_set_prototype_of(Object* prototype) override; virtual JS::ThrowCompletionOr internal_is_extensible() const override; - virtual bool internal_prevent_extensions() override; + virtual JS::ThrowCompletionOr internal_prevent_extensions() override; // FIXME: There should also be a custom [[GetPrototypeOf]], [[GetOwnProperty]], [[DefineOwnProperty]], [[Get]], [[Set]], [[Delete]] and [[OwnPropertyKeys]], // but we don't have the infrastructure in place to implement them yet. diff --git a/Userland/Services/WebContent/ConsoleGlobalObject.cpp b/Userland/Services/WebContent/ConsoleGlobalObject.cpp index 9b9ae85335..2fdfc27366 100644 --- a/Userland/Services/WebContent/ConsoleGlobalObject.cpp +++ b/Userland/Services/WebContent/ConsoleGlobalObject.cpp @@ -52,7 +52,7 @@ JS::ThrowCompletionOr ConsoleGlobalObject::internal_is_extensible() const return m_window_object->internal_is_extensible(); } -bool ConsoleGlobalObject::internal_prevent_extensions() +JS::ThrowCompletionOr ConsoleGlobalObject::internal_prevent_extensions() { return m_window_object->internal_prevent_extensions(); } diff --git a/Userland/Services/WebContent/ConsoleGlobalObject.h b/Userland/Services/WebContent/ConsoleGlobalObject.h index c0ded11f0e..427dbaa0a9 100644 --- a/Userland/Services/WebContent/ConsoleGlobalObject.h +++ b/Userland/Services/WebContent/ConsoleGlobalObject.h @@ -26,7 +26,7 @@ public: virtual JS::ThrowCompletionOr internal_get_prototype_of() const override; virtual JS::ThrowCompletionOr internal_set_prototype_of(Object* prototype) override; virtual JS::ThrowCompletionOr internal_is_extensible() const override; - virtual bool internal_prevent_extensions() override; + virtual JS::ThrowCompletionOr internal_prevent_extensions() override; virtual Optional 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 bool internal_has_property(JS::PropertyName const& name) const override;