mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 05:07:35 +00:00
LibJS: Convert internal_set_prototype_of() to ThrowCompletionOr
This commit is contained in:
parent
5148150e1c
commit
8c81c84c18
20 changed files with 43 additions and 50 deletions
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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.
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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())));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue