1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 17:08:12 +00:00

LibJS: Convert internal_set_prototype_of() to ThrowCompletionOr

This commit is contained in:
Linus Groh 2021-09-28 23:54:42 +01:00
parent 5148150e1c
commit 8c81c84c18
20 changed files with 43 additions and 50 deletions

View file

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

View file

@ -906,7 +906,7 @@ Value ClassExpression::execute(Interpreter& interpreter, GlobalObject& global_ob
class_constructor->define_direct_property(vm.names.prototype, prototype, Attribute::Writable); class_constructor->define_direct_property(vm.names.prototype, prototype, Attribute::Writable);
if (interpreter.exception()) if (interpreter.exception())
return {}; 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); auto class_prototype = class_constructor->get(vm.names.prototype);

View file

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

View file

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

View file

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

View file

@ -48,7 +48,8 @@ Object::Object(ConstructWithoutPrototypeTag, GlobalObject& global_object)
Object::Object(Object& prototype) Object::Object(Object& prototype)
{ {
m_shape = prototype.global_object().empty_object_shape(); 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); 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 // 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. Assert: Either Type(V) is Object or Type(V) is Null.

View file

@ -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 // 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 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_is_extensible() const;
virtual bool internal_prevent_extensions(); virtual bool internal_prevent_extensions();
virtual Optional<PropertyDescriptor> internal_get_own_property(PropertyName const&) const; virtual Optional<PropertyDescriptor> internal_get_own_property(PropertyName const&) const;

View file

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

View file

@ -10,6 +10,7 @@
#include <LibJS/Runtime/AbstractOperations.h> #include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Accessor.h> #include <LibJS/Runtime/Accessor.h>
#include <LibJS/Runtime/BooleanObject.h> #include <LibJS/Runtime/BooleanObject.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/Date.h> #include <LibJS/Runtime/Date.h>
#include <LibJS/Runtime/GlobalObject.h> #include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/NumberObject.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 // 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); return set_immutable_prototype(prototype);
} }
@ -320,9 +321,7 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::proto_setter)
if (!object.is_object()) if (!object.is_object())
return js_undefined(); return js_undefined();
auto status = object.as_object().internal_set_prototype_of(proto.is_object() ? &proto.as_object() : nullptr); auto status = TRY_OR_DISCARD(object.as_object().internal_set_prototype_of(proto.is_object() ? &proto.as_object() : nullptr));
if (vm.exception())
return {};
if (!status) { if (!status) {
// FIXME: Improve/contextualize error message // FIXME: Improve/contextualize error message
vm.throw_exception<TypeError>(global_object, ErrorType::ObjectSetPrototypeOfReturnedFalse); vm.throw_exception<TypeError>(global_object, ErrorType::ObjectSetPrototypeOfReturnedFalse);

View file

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

View file

@ -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 // 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& vm = this->vm();
auto& global_object = this->global_object(); auto& global_object = this->global_object();
@ -105,16 +105,14 @@ bool ProxyObject::internal_set_prototype_of(Object* prototype)
// 2. Let handler be O.[[ProxyHandler]]. // 2. Let handler be O.[[ProxyHandler]].
// 3. If handler is null, throw a TypeError exception. // 3. If handler is null, throw a TypeError exception.
if (m_is_revoked) { if (m_is_revoked)
vm.throw_exception<TypeError>(global_object, ErrorType::ProxyRevoked); return vm.throw_completion<TypeError>(global_object, ErrorType::ProxyRevoked);
return {};
}
// 4. Assert: Type(handler) is Object. // 4. Assert: Type(handler) is Object.
// 5. Let target be O.[[ProxyTarget]]. // 5. Let target be O.[[ProxyTarget]].
// 6. Let trap be ? GetMethod(handler, "setPrototypeOf"). // 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 // 7. If trap is undefined, then
if (!trap) { if (!trap) {
@ -123,7 +121,7 @@ bool ProxyObject::internal_set_prototype_of(Object* prototype)
} }
// 8. Let booleanTrapResult be ! ToBoolean(? Call(trap, handler, « target, V »)). // 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. // 9. If booleanTrapResult is false, return false.
if (!trap_result) if (!trap_result)
@ -131,21 +129,19 @@ bool ProxyObject::internal_set_prototype_of(Object* prototype)
// 10. Let extensibleTarget be ? IsExtensible(target). // 10. Let extensibleTarget be ? IsExtensible(target).
auto extensible_target = m_target.is_extensible(); auto extensible_target = m_target.is_extensible();
if (vm.exception()) if (auto* exception = vm.exception())
return {}; return throw_completion(exception->value());
// 11. If extensibleTarget is true, return true. // 11. If extensibleTarget is true, return true.
if (extensible_target) if (extensible_target)
return true; return true;
// 12. Let targetProto be ? target.[[GetPrototypeOf]](). // 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. // 13. If SameValue(V, targetProto) is false, throw a TypeError exception.
if (!same_value(prototype, target_proto)) { if (!same_value(prototype, target_proto))
vm.throw_exception<TypeError>(global_object, ErrorType::ProxySetPrototypeOfNonExtensible); return vm.throw_completion<TypeError>(global_object, ErrorType::ProxySetPrototypeOfNonExtensible);
return {};
}
// 14. Return true. // 14. Return true.
return true; return true;

View file

@ -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 // 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 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_is_extensible() const override;
virtual bool internal_prevent_extensions() override; virtual bool internal_prevent_extensions() override;
virtual Optional<PropertyDescriptor> internal_get_own_property(PropertyName const&) const override; virtual Optional<PropertyDescriptor> internal_get_own_property(PropertyName const&) const override;

View file

@ -334,7 +334,7 @@ JS_DEFINE_NATIVE_FUNCTION(ReflectObject::set_prototype_of)
} }
// 3. Return ? target.[[SetPrototypeOf]](proto). // 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())));
} }
} }

View file

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

View file

@ -6,6 +6,7 @@
#include <AK/FlyString.h> #include <AK/FlyString.h>
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>
#include <LibJS/Runtime/Completion.h>
#include <LibWeb/Bindings/LocationObject.h> #include <LibWeb/Bindings/LocationObject.h>
#include <LibWeb/Bindings/WindowObject.h> #include <LibWeb/Bindings/WindowObject.h>
#include <LibWeb/DOM/Document.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 // 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). // 1. Return ! SetImmutablePrototype(this, V).
return set_immutable_prototype(prototype); return set_immutable_prototype(prototype);

View file

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

View file

@ -8,6 +8,7 @@
#include <AK/Base64.h> #include <AK/Base64.h>
#include <AK/String.h> #include <AK/String.h>
#include <AK/Utf8View.h> #include <AK/Utf8View.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/Error.h> #include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/FunctionObject.h> #include <LibJS/Runtime/FunctionObject.h>
#include <LibJS/Runtime/Shape.h> #include <LibJS/Runtime/Shape.h>
@ -52,7 +53,7 @@ void WindowObject::initialize_global_object()
{ {
Base::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); VERIFY(success);
// FIXME: These should be native accessors, not properties // FIXME: These should be native accessors, not properties
@ -143,7 +144,7 @@ Origin WindowObject::origin() const
} }
// https://heycam.github.io/webidl/#platform-object-setprototypeof // 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). // 1. Return ? SetImmutablePrototype(O, V).
return set_immutable_prototype(prototype); return set_immutable_prototype(prototype);

View file

@ -9,6 +9,7 @@
#include <AK/TypeCasts.h> #include <AK/TypeCasts.h>
#include <AK/Weakable.h> #include <AK/Weakable.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/GlobalObject.h> #include <LibJS/Runtime/GlobalObject.h>
#include <LibWeb/Forward.h> #include <LibWeb/Forward.h>
#include <LibWeb/HTML/GlobalEventHandlers.h> #include <LibWeb/HTML/GlobalEventHandlers.h>
@ -60,7 +61,7 @@ public:
return *constructor; 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: private:
virtual void visit_edges(Visitor&) override; virtual void visit_edges(Visitor&) override;

View file

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

View file

@ -24,7 +24,7 @@ public:
virtual ~ConsoleGlobalObject() override; virtual ~ConsoleGlobalObject() override;
virtual JS::ThrowCompletionOr<Object*> internal_get_prototype_of() const 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_is_extensible() const override;
virtual bool internal_prevent_extensions() override; virtual bool internal_prevent_extensions() override;
virtual Optional<JS::PropertyDescriptor> internal_get_own_property(JS::PropertyName const& name) const override; virtual Optional<JS::PropertyDescriptor> internal_get_own_property(JS::PropertyName const& name) const override;