Browse Source

LibJS: Convert internal_set_prototype_of() to ThrowCompletionOr

Linus Groh 3 years ago
parent
commit
8c81c84c18

+ 3 - 3
Meta/Lagom/Tools/CodeGenerators/LibWeb/WrapperGenerator.cpp

@@ -1553,7 +1553,7 @@ namespace Web::Bindings {
 @wrapper_class@::@wrapper_class@(JS::GlobalObject& global_object, @fully_qualified_name@& impl)
     : @wrapper_base_class@(global_object, impl)
 {
-    auto success = internal_set_prototype_of(&static_cast<WindowObject&>(global_object).ensure_web_prototype<@prototype_class@>("@name@"));
+    auto success = internal_set_prototype_of(&static_cast<WindowObject&>(global_object).ensure_web_prototype<@prototype_class@>("@name@")).release_value();
     VERIFY(success);
 }
 )~~~");
@@ -2808,12 +2808,12 @@ namespace Web::Bindings {
         // https://heycam.github.io/webidl/#es-DOMException-specialness
         // Object.getPrototypeOf(DOMException.prototype) === Error.prototype
         generator.append(R"~~~(
-    auto success = internal_set_prototype_of(global_object.error_prototype());
+    auto success = internal_set_prototype_of(global_object.error_prototype()).release_value();
     VERIFY(success);
 )~~~");
     } else if (!interface.parent_name.is_empty()) {
         generator.append(R"~~~(
-    auto success = internal_set_prototype_of(&static_cast<WindowObject&>(global_object).ensure_web_prototype<@prototype_base_class@>("@parent_name@"));
+    auto success = internal_set_prototype_of(&static_cast<WindowObject&>(global_object).ensure_web_prototype<@prototype_base_class@>("@parent_name@")).release_value();
     VERIFY(success);
 )~~~");
     }

+ 1 - 1
Userland/Libraries/LibJS/AST.cpp

@@ -906,7 +906,7 @@ Value ClassExpression::execute(Interpreter& interpreter, GlobalObject& global_ob
         class_constructor->define_direct_property(vm.names.prototype, prototype, Attribute::Writable);
         if (interpreter.exception())
             return {};
-        class_constructor->internal_set_prototype_of(super_constructor.is_null() ? global_object.function_prototype() : &super_constructor.as_object());
+        TRY_OR_DISCARD(class_constructor->internal_set_prototype_of(super_constructor.is_null() ? global_object.function_prototype() : &super_constructor.as_object()));
     }
 
     auto class_prototype = class_constructor->get(vm.names.prototype);

+ 2 - 5
Userland/Libraries/LibJS/Runtime/BoundFunction.cpp

@@ -38,11 +38,8 @@ Value BoundFunction::call()
 
 Value BoundFunction::construct(FunctionObject& new_target)
 {
-    if (auto this_value = vm().this_value(global_object()); m_constructor_prototype && this_value.is_object()) {
-        this_value.as_object().internal_set_prototype_of(m_constructor_prototype);
-        if (vm().exception())
-            return {};
-    }
+    if (auto this_value = vm().this_value(global_object()); m_constructor_prototype && this_value.is_object())
+        TRY_OR_DISCARD(this_value.as_object().internal_set_prototype_of(m_constructor_prototype));
     return m_bound_target_function->construct(new_target);
 }
 

+ 1 - 1
Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.cpp

@@ -81,7 +81,7 @@ void ECMAScriptFunctionObject::initialize(GlobalObject& global_object)
             break;
         case FunctionKind::Generator:
             // prototype is "g1.prototype" in figure-2 (https://tc39.es/ecma262/img/figure-2.png)
-            prototype->internal_set_prototype_of(global_object.generator_object_prototype());
+            (void)prototype->internal_set_prototype_of(global_object.generator_object_prototype());
             break;
         }
         define_direct_property(vm.names.prototype, prototype, Attribute::Writable);

+ 1 - 1
Userland/Libraries/LibJS/Runtime/GlobalObject.cpp

@@ -144,7 +144,7 @@ void GlobalObject::initialize_global_object()
     static_cast<ObjectPrototype*>(m_object_prototype)->initialize(*this);
     m_object_prototype->set_initialized(Badge<GlobalObject> {});
 
-    auto success = Object::internal_set_prototype_of(m_object_prototype);
+    auto success = Object::internal_set_prototype_of(m_object_prototype).release_value();
     VERIFY(success);
 
     // This must be initialized before allocating AggregateErrorPrototype, which uses ErrorPrototype as its prototype.

+ 3 - 2
Userland/Libraries/LibJS/Runtime/Object.cpp

@@ -48,7 +48,8 @@ Object::Object(ConstructWithoutPrototypeTag, GlobalObject& global_object)
 Object::Object(Object& prototype)
 {
     m_shape = prototype.global_object().empty_object_shape();
-    auto success = internal_set_prototype_of(&prototype);
+    // FIXME: Factor out step 9 into a simple prototype setter and use that
+    auto success = internal_set_prototype_of(&prototype).release_value();
     VERIFY(success);
 }
 
@@ -496,7 +497,7 @@ ThrowCompletionOr<Object*> Object::internal_get_prototype_of() const
 }
 
 // 10.1.2 [[SetPrototypeOf]] ( V ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-setprototypeof-v
-bool Object::internal_set_prototype_of(Object* new_prototype)
+ThrowCompletionOr<bool> Object::internal_set_prototype_of(Object* new_prototype)
 {
     // 1. Assert: Either Type(V) is Object or Type(V) is Null.
 

+ 1 - 1
Userland/Libraries/LibJS/Runtime/Object.h

@@ -92,7 +92,7 @@ public:
     // 10.1 Ordinary Object Internal Methods and Internal Slots, https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots
 
     virtual ThrowCompletionOr<Object*> internal_get_prototype_of() const;
-    virtual bool internal_set_prototype_of(Object* prototype);
+    virtual ThrowCompletionOr<bool> internal_set_prototype_of(Object* prototype);
     virtual bool internal_is_extensible() const;
     virtual bool internal_prevent_extensions();
     virtual Optional<PropertyDescriptor> internal_get_own_property(PropertyName const&) const;

+ 1 - 3
Userland/Libraries/LibJS/Runtime/ObjectConstructor.cpp

@@ -162,9 +162,7 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::set_prototype_of)
         return object;
 
     // 4. Let status be ? O.[[SetPrototypeOf]](proto).
-    auto status = object.as_object().internal_set_prototype_of(proto.is_null() ? nullptr : &proto.as_object());
-    if (vm.exception())
-        return {};
+    auto status = TRY_OR_DISCARD(object.as_object().internal_set_prototype_of(proto.is_null() ? nullptr : &proto.as_object()));
 
     // 5. If status is false, throw a TypeError exception.
     if (!status) {

+ 3 - 4
Userland/Libraries/LibJS/Runtime/ObjectPrototype.cpp

@@ -10,6 +10,7 @@
 #include <LibJS/Runtime/AbstractOperations.h>
 #include <LibJS/Runtime/Accessor.h>
 #include <LibJS/Runtime/BooleanObject.h>
+#include <LibJS/Runtime/Completion.h>
 #include <LibJS/Runtime/Date.h>
 #include <LibJS/Runtime/GlobalObject.h>
 #include <LibJS/Runtime/NumberObject.h>
@@ -52,7 +53,7 @@ ObjectPrototype::~ObjectPrototype()
 }
 
 // 10.4.7.1 [[SetPrototypeOf]] ( V ), https://tc39.es/ecma262/#sec-immutable-prototype-exotic-objects-setprototypeof-v
-bool ObjectPrototype::internal_set_prototype_of(Object* prototype)
+ThrowCompletionOr<bool> ObjectPrototype::internal_set_prototype_of(Object* prototype)
 {
     return set_immutable_prototype(prototype);
 }
@@ -320,9 +321,7 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::proto_setter)
     if (!object.is_object())
         return js_undefined();
 
-    auto status = object.as_object().internal_set_prototype_of(proto.is_object() ? &proto.as_object() : nullptr);
-    if (vm.exception())
-        return {};
+    auto status = TRY_OR_DISCARD(object.as_object().internal_set_prototype_of(proto.is_object() ? &proto.as_object() : nullptr));
     if (!status) {
         // FIXME: Improve/contextualize error message
         vm.throw_exception<TypeError>(global_object, ErrorType::ObjectSetPrototypeOfReturnedFalse);

+ 2 - 1
Userland/Libraries/LibJS/Runtime/ObjectPrototype.h

@@ -6,6 +6,7 @@
 
 #pragma once
 
+#include <LibJS/Runtime/Completion.h>
 #include <LibJS/Runtime/Object.h>
 
 namespace JS {
@@ -20,7 +21,7 @@ public:
 
     // 10.4.7 Immutable Prototype Exotic Objects, https://tc39.es/ecma262/#sec-immutable-prototype-exotic-objects
 
-    virtual bool internal_set_prototype_of(Object* prototype) override;
+    virtual ThrowCompletionOr<bool> internal_set_prototype_of(Object* prototype) override;
 
     // public to serve as intrinsic function %Object.prototype.toString%
     JS_DECLARE_NATIVE_FUNCTION(to_string);

+ 10 - 14
Userland/Libraries/LibJS/Runtime/ProxyObject.cpp

@@ -96,7 +96,7 @@ ThrowCompletionOr<Object*> ProxyObject::internal_get_prototype_of() const
 }
 
 // 10.5.2 [[SetPrototypeOf]] ( V ), https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-setprototypeof-v
-bool ProxyObject::internal_set_prototype_of(Object* prototype)
+ThrowCompletionOr<bool> ProxyObject::internal_set_prototype_of(Object* prototype)
 {
     auto& vm = this->vm();
     auto& global_object = this->global_object();
@@ -105,16 +105,14 @@ bool ProxyObject::internal_set_prototype_of(Object* prototype)
     // 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, "setPrototypeOf").
-    auto trap = TRY_OR_DISCARD(Value(&m_handler).get_method(global_object, vm.names.setPrototypeOf));
+    auto trap = TRY(Value(&m_handler).get_method(global_object, vm.names.setPrototypeOf));
 
     // 7. If trap is undefined, then
     if (!trap) {
@@ -123,7 +121,7 @@ bool ProxyObject::internal_set_prototype_of(Object* prototype)
     }
 
     // 8. Let booleanTrapResult be ! ToBoolean(? Call(trap, handler, « target, V »)).
-    auto trap_result = TRY_OR_DISCARD(vm.call(*trap, &m_handler, &m_target, prototype)).to_boolean();
+    auto trap_result = TRY(vm.call(*trap, &m_handler, &m_target, prototype)).to_boolean();
 
     // 9. If booleanTrapResult is false, return false.
     if (!trap_result)
@@ -131,21 +129,19 @@ bool ProxyObject::internal_set_prototype_of(Object* prototype)
 
     // 10. 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());
 
     // 11. If extensibleTarget is true, return true.
     if (extensible_target)
         return true;
 
     // 12. Let targetProto be ? target.[[GetPrototypeOf]]().
-    auto* target_proto = TRY_OR_DISCARD(m_target.internal_get_prototype_of());
+    auto* target_proto = TRY(m_target.internal_get_prototype_of());
 
     // 13. If SameValue(V, targetProto) is false, throw a TypeError exception.
-    if (!same_value(prototype, target_proto)) {
-        vm.throw_exception<TypeError>(global_object, ErrorType::ProxySetPrototypeOfNonExtensible);
-        return {};
-    }
+    if (!same_value(prototype, target_proto))
+        return vm.throw_completion<TypeError>(global_object, ErrorType::ProxySetPrototypeOfNonExtensible);
 
     // 14. Return true.
     return true;

+ 1 - 1
Userland/Libraries/LibJS/Runtime/ProxyObject.h

@@ -36,7 +36,7 @@ public:
     // 10.5 Proxy Object Internal Methods and Internal Slots, https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots
 
     virtual ThrowCompletionOr<Object*> internal_get_prototype_of() const override;
-    virtual bool internal_set_prototype_of(Object* prototype) override;
+    virtual ThrowCompletionOr<bool> internal_set_prototype_of(Object* prototype) override;
     virtual bool internal_is_extensible() const override;
     virtual bool internal_prevent_extensions() override;
     virtual Optional<PropertyDescriptor> internal_get_own_property(PropertyName const&) const override;

+ 1 - 1
Userland/Libraries/LibJS/Runtime/ReflectObject.cpp

@@ -334,7 +334,7 @@ JS_DEFINE_NATIVE_FUNCTION(ReflectObject::set_prototype_of)
     }
 
     // 3. Return ? target.[[SetPrototypeOf]](proto).
-    return Value(target.as_object().internal_set_prototype_of(proto.is_null() ? nullptr : &proto.as_object()));
+    return Value(TRY_OR_DISCARD(target.as_object().internal_set_prototype_of(proto.is_null() ? nullptr : &proto.as_object())));
 }
 
 }

+ 2 - 5
Userland/Libraries/LibJS/Runtime/VM.cpp

@@ -537,11 +537,8 @@ Value VM::construct(FunctionObject& function, FunctionObject& new_target, Option
         auto prototype = new_target.get(names.prototype);
         if (exception())
             return {};
-        if (prototype.is_object()) {
-            result.as_object().internal_set_prototype_of(&prototype.as_object());
-            if (exception())
-                return {};
-        }
+        if (prototype.is_object())
+            TRY_OR_DISCARD(result.as_object().internal_set_prototype_of(&prototype.as_object()));
         return result;
     }
 

+ 2 - 1
Userland/Libraries/LibWeb/Bindings/LocationObject.cpp

@@ -6,6 +6,7 @@
 
 #include <AK/FlyString.h>
 #include <AK/StringBuilder.h>
+#include <LibJS/Runtime/Completion.h>
 #include <LibWeb/Bindings/LocationObject.h>
 #include <LibWeb/Bindings/WindowObject.h>
 #include <LibWeb/DOM/Document.h>
@@ -125,7 +126,7 @@ JS_DEFINE_NATIVE_FUNCTION(LocationObject::reload)
 }
 
 // https://html.spec.whatwg.org/multipage/history.html#location-setprototypeof
-bool LocationObject::internal_set_prototype_of(Object* prototype)
+JS::ThrowCompletionOr<bool> LocationObject::internal_set_prototype_of(Object* prototype)
 {
     // 1. Return ! SetImmutablePrototype(this, V).
     return set_immutable_prototype(prototype);

+ 2 - 1
Userland/Libraries/LibWeb/Bindings/LocationObject.h

@@ -6,6 +6,7 @@
 
 #pragma once
 
+#include <LibJS/Runtime/Completion.h>
 #include <LibJS/Runtime/Object.h>
 #include <LibWeb/Forward.h>
 
@@ -20,7 +21,7 @@ public:
     virtual void initialize(JS::GlobalObject&) override;
     virtual ~LocationObject() override;
 
-    virtual bool internal_set_prototype_of(Object* prototype) override;
+    virtual JS::ThrowCompletionOr<bool> internal_set_prototype_of(Object* prototype) override;
     virtual bool internal_is_extensible() const override;
     virtual bool internal_prevent_extensions() override;
 

+ 3 - 2
Userland/Libraries/LibWeb/Bindings/WindowObject.cpp

@@ -8,6 +8,7 @@
 #include <AK/Base64.h>
 #include <AK/String.h>
 #include <AK/Utf8View.h>
+#include <LibJS/Runtime/Completion.h>
 #include <LibJS/Runtime/Error.h>
 #include <LibJS/Runtime/FunctionObject.h>
 #include <LibJS/Runtime/Shape.h>
@@ -52,7 +53,7 @@ void WindowObject::initialize_global_object()
 {
     Base::initialize_global_object();
 
-    auto success = Object::internal_set_prototype_of(&ensure_web_prototype<EventTargetPrototype>("EventTarget"));
+    auto success = Object::internal_set_prototype_of(&ensure_web_prototype<EventTargetPrototype>("EventTarget")).release_value();
     VERIFY(success);
 
     // FIXME: These should be native accessors, not properties
@@ -143,7 +144,7 @@ Origin WindowObject::origin() const
 }
 
 // https://heycam.github.io/webidl/#platform-object-setprototypeof
-bool WindowObject::internal_set_prototype_of(JS::Object* prototype)
+JS::ThrowCompletionOr<bool> WindowObject::internal_set_prototype_of(JS::Object* prototype)
 {
     // 1. Return ? SetImmutablePrototype(O, V).
     return set_immutable_prototype(prototype);

+ 2 - 1
Userland/Libraries/LibWeb/Bindings/WindowObject.h

@@ -9,6 +9,7 @@
 
 #include <AK/TypeCasts.h>
 #include <AK/Weakable.h>
+#include <LibJS/Runtime/Completion.h>
 #include <LibJS/Runtime/GlobalObject.h>
 #include <LibWeb/Forward.h>
 #include <LibWeb/HTML/GlobalEventHandlers.h>
@@ -60,7 +61,7 @@ public:
         return *constructor;
     }
 
-    virtual bool internal_set_prototype_of(JS::Object* prototype) override;
+    virtual JS::ThrowCompletionOr<bool> internal_set_prototype_of(JS::Object* prototype) override;
 
 private:
     virtual void visit_edges(Visitor&) override;

+ 1 - 1
Userland/Services/WebContent/ConsoleGlobalObject.cpp

@@ -42,7 +42,7 @@ JS::ThrowCompletionOr<JS::Object*> ConsoleGlobalObject::internal_get_prototype_o
     return m_window_object->internal_get_prototype_of();
 }
 
-bool ConsoleGlobalObject::internal_set_prototype_of(JS::Object* prototype)
+JS::ThrowCompletionOr<bool> ConsoleGlobalObject::internal_set_prototype_of(JS::Object* prototype)
 {
     return m_window_object->internal_set_prototype_of(prototype);
 }

+ 1 - 1
Userland/Services/WebContent/ConsoleGlobalObject.h

@@ -24,7 +24,7 @@ public:
     virtual ~ConsoleGlobalObject() override;
 
     virtual JS::ThrowCompletionOr<Object*> internal_get_prototype_of() const override;
-    virtual bool internal_set_prototype_of(Object* prototype) override;
+    virtual JS::ThrowCompletionOr<bool> internal_set_prototype_of(Object* prototype) override;
     virtual bool internal_is_extensible() const override;
     virtual bool internal_prevent_extensions() override;
     virtual Optional<JS::PropertyDescriptor> internal_get_own_property(JS::PropertyName const& name) const override;