LibJS: Convert internal_set() to ThrowCompletionOr

This commit is contained in:
Linus Groh 2021-09-29 18:31:37 +01:00
parent 6c2b974db2
commit e5409c6ead
Notes: sideshowbarker 2024-07-18 03:19:06 +09:00
19 changed files with 66 additions and 69 deletions

View file

@ -1397,7 +1397,7 @@ public:
}
if (interface.extended_attributes.contains("CustomSet")) {
generator.append(R"~~~(
virtual bool internal_set(JS::PropertyName const&, JS::Value, JS::Value receiver) override;
virtual JS::ThrowCompletionOr<bool> internal_set(JS::PropertyName const&, JS::Value, JS::Value receiver) override;
)~~~");
}
@ -1416,7 +1416,7 @@ public:
if (interface.is_legacy_platform_object()) {
generator.append(R"~~~(
virtual JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> internal_get_own_property(JS::PropertyName const&) const override;
virtual bool internal_set(JS::PropertyName const&, JS::Value, JS::Value) override;
virtual JS::ThrowCompletionOr<bool> internal_set(JS::PropertyName const&, JS::Value, JS::Value) override;
virtual JS::ThrowCompletionOr<bool> internal_define_own_property(JS::PropertyName const&, JS::PropertyDescriptor const&) override;
virtual bool internal_delete(JS::PropertyName const&) override;
virtual JS::ThrowCompletionOr<bool> internal_prevent_extensions() override;
@ -1986,7 +1986,7 @@ JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> @class_name@::internal_g
// 3.9.2. [[Set]], https://heycam.github.io/webidl/#legacy-platform-object-set
scoped_generator.append(R"~~~(
bool @class_name@::internal_set(JS::PropertyName const& property_name, JS::Value value, JS::Value receiver)
JS::ThrowCompletionOr<bool> @class_name@::internal_set(JS::PropertyName const& property_name, JS::Value value, JS::Value receiver)
{
auto& vm = this->vm();
[[maybe_unused]] auto& global_object = this->global_object();
@ -2006,8 +2006,8 @@ bool @class_name@::internal_set(JS::PropertyName const& property_name, JS::Value
if (IDL::is_an_array_index(global_object, property_name)) {
// 1. Invoke the indexed property setter on O with P and V.
invoke_indexed_property_setter(global_object, impl(), property_name, value);
if (vm.exception())
return {};
if (auto* exception = vm.exception())
return JS::throw_completion(exception->value());
// 2. Return true.
return true;
@ -2022,8 +2022,8 @@ bool @class_name@::internal_set(JS::PropertyName const& property_name, JS::Value
if (property_name.is_string()) {
// 1. Invoke the named property setter on O with P and V.
invoke_named_property_setter(global_object, impl(), property_name.as_string(), value);
if (vm.exception())
return {};
if (auto* exception = vm.exception())
return JS::throw_completion(exception->value());
// 2. Return true.
return true;
@ -2039,13 +2039,16 @@ bool @class_name@::internal_set(JS::PropertyName const& property_name, JS::Value
scoped_generator.append(R"~~~(
// 2. Let ownDesc be LegacyPlatformObjectGetOwnProperty(O, P, true).
auto own_descriptor = legacy_platform_object_get_own_property_for_set_slot(property_name);
if (vm.exception())
return {};
if (auto* exception = vm.exception())
return JS::throw_completion(exception->value());
// 3. Perform ? OrdinarySetWithOwnDescriptor(O, P, V, Receiver, ownDesc).
// NOTE: The spec says "perform" instead of "return", meaning nothing will be returned on this path according to the spec, which isn't possible to do.
// Let's treat it as though it says "return" instead of "perform".
return ordinary_set_with_own_descriptor(property_name, value, receiver, own_descriptor);
auto result = ordinary_set_with_own_descriptor(property_name, value, receiver, own_descriptor);
if (auto* exception = vm.exception())
return JS::throw_completion(exception->value());
return result;
}
)~~~");

View file

@ -121,7 +121,7 @@ JS::ThrowCompletionOr<JS::Value> SheetGlobalObject::internal_get(const JS::Prope
return Base::internal_get(property_name, receiver);
}
bool SheetGlobalObject::internal_set(const JS::PropertyName& property_name, JS::Value value, JS::Value receiver)
JS::ThrowCompletionOr<bool> SheetGlobalObject::internal_set(const JS::PropertyName& property_name, JS::Value value, JS::Value receiver)
{
if (property_name.is_string()) {
if (auto pos = m_sheet.parse_cell_name(property_name.as_string()); pos.has_value()) {

View file

@ -28,7 +28,7 @@ public:
virtual ~SheetGlobalObject() override;
virtual JS::ThrowCompletionOr<JS::Value> internal_get(JS::PropertyName const&, JS::Value receiver) const override;
virtual bool internal_set(JS::PropertyName const&, JS::Value value, JS::Value receiver) override;
virtual JS::ThrowCompletionOr<bool> internal_set(JS::PropertyName const&, JS::Value value, JS::Value receiver) override;
virtual void initialize_global_object() override;
JS_DECLARE_NATIVE_FUNCTION(get_real_cell_contents);

View file

@ -40,7 +40,7 @@ JS::ThrowCompletionOr<JS::Value> DebuggerGlobalJSObject::internal_get(JS::Proper
return vm().throw_completion<JS::TypeError>(const_cast<DebuggerGlobalJSObject&>(*this), move(error_string));
}
bool DebuggerGlobalJSObject::internal_set(JS::PropertyName const& property_name, JS::Value value, JS::Value receiver)
JS::ThrowCompletionOr<bool> DebuggerGlobalJSObject::internal_set(JS::PropertyName const& property_name, JS::Value value, JS::Value receiver)
{
if (m_variables.is_empty() || !property_name.is_string())
return Base::internal_set(property_name, value, receiver);
@ -55,8 +55,7 @@ bool DebuggerGlobalJSObject::internal_set(JS::PropertyName const& property_name,
if (debugger_value.has_value())
return Debugger::the().session()->poke((u32*)target_variable.location_data.address, debugger_value.value());
auto error_string = String::formatted("Cannot convert JS value {} to variable {} of type {}", value.to_string_without_side_effects(), property_name.as_string(), target_variable.type_name);
vm().throw_exception<JS::TypeError>(const_cast<DebuggerGlobalJSObject&>(*this), error_string);
return {};
return vm().throw_completion<JS::TypeError>(const_cast<DebuggerGlobalJSObject&>(*this), move(error_string));
}
Optional<JS::Value> DebuggerGlobalJSObject::debugger_to_js(const Debug::DebugInfo::VariableInfo& variable) const

View file

@ -22,7 +22,7 @@ public:
DebuggerGlobalJSObject();
virtual JS::ThrowCompletionOr<JS::Value> internal_get(JS::PropertyName const&, JS::Value receiver) const override;
virtual bool internal_set(JS::PropertyName const&, JS::Value value, JS::Value receiver) override;
virtual JS::ThrowCompletionOr<bool> internal_set(JS::PropertyName const&, JS::Value value, JS::Value receiver) override;
Optional<JS::Value> debugger_to_js(const Debug::DebugInfo::VariableInfo&) const;
Optional<u32> js_to_debugger(JS::Value value, const Debug::DebugInfo::VariableInfo&) const;

View file

@ -7,6 +7,7 @@
#include "DebuggerVariableJSObject.h"
#include "Debugger.h"
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/PrimitiveString.h>
#include <LibJS/Runtime/PropertyName.h>
@ -28,30 +29,26 @@ DebuggerVariableJSObject::~DebuggerVariableJSObject()
{
}
bool DebuggerVariableJSObject::internal_set(const JS::PropertyName& property_name, JS::Value value, JS::Value)
JS::ThrowCompletionOr<bool> DebuggerVariableJSObject::internal_set(const JS::PropertyName& property_name, JS::Value value, JS::Value)
{
if (!property_name.is_string()) {
vm().throw_exception<JS::TypeError>(global_object(), String::formatted("Invalid variable name {}", property_name.to_string()));
return false;
}
auto& vm = this->vm();
if (!property_name.is_string())
return vm.throw_completion<JS::TypeError>(global_object(), String::formatted("Invalid variable name {}", property_name.to_string()));
auto name = property_name.as_string();
auto it = m_variable_info.members.find_if([&](auto& variable) {
return variable->name == name;
});
if (it.is_end()) {
vm().throw_exception<JS::TypeError>(global_object(), String::formatted("Variable of type {} has no property {}", m_variable_info.type_name, property_name));
return false;
}
if (it.is_end())
return vm.throw_completion<JS::TypeError>(global_object(), String::formatted("Variable of type {} has no property {}", m_variable_info.type_name, property_name));
auto& member = **it;
auto new_value = debugger_object().js_to_debugger(value, member);
if (!new_value.has_value()) {
auto string_error = String::formatted("Cannot convert JS value {} to variable {} of type {}", value.to_string_without_side_effects(), name, member.type_name);
vm().throw_exception<JS::TypeError>(global_object(), string_error);
return false;
}
if (!new_value.has_value())
return vm.throw_completion<JS::TypeError>(global_object(), String::formatted("Cannot convert JS value {} to variable {} of type {}", value.to_string_without_side_effects(), name, member.type_name));
Debugger::the().session()->poke((u32*)member.location_data.address, new_value.value());
return true;
}

View file

@ -9,6 +9,7 @@
#include "DebuggerGlobalJSObject.h"
#include <LibDebug/DebugInfo.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/Object.h>
namespace HackStudio {
@ -24,7 +25,7 @@ public:
virtual const char* class_name() const override { return m_variable_info.type_name.characters(); }
bool internal_set(JS::PropertyName const&, JS::Value value, JS::Value receiver) override;
JS::ThrowCompletionOr<bool> internal_set(JS::PropertyName const&, JS::Value value, JS::Value receiver) override;
private:
DebuggerGlobalJSObject& debugger_object() const;

View file

@ -54,7 +54,7 @@ ThrowCompletionOr<Value> ArgumentsObject::internal_get(PropertyName const& prope
}
// 10.4.4.4 [[Set]] ( P, V, Receiver ), https://tc39.es/ecma262/#sec-arguments-exotic-objects-set-p-v-receiver
bool ArgumentsObject::internal_set(PropertyName const& property_name, Value value, Value receiver)
ThrowCompletionOr<bool> ArgumentsObject::internal_set(PropertyName const& property_name, Value value, Value receiver)
{
bool is_mapped = false;

View file

@ -26,7 +26,7 @@ public:
virtual ThrowCompletionOr<Optional<PropertyDescriptor>> internal_get_own_property(PropertyName const&) const override;
virtual ThrowCompletionOr<bool> internal_define_own_property(PropertyName const&, PropertyDescriptor const&) override;
virtual ThrowCompletionOr<Value> internal_get(PropertyName const&, Value receiver) const override;
virtual bool internal_set(PropertyName const&, Value value, Value receiver) override;
virtual ThrowCompletionOr<bool> internal_set(PropertyName const&, Value value, Value receiver) override;
virtual bool internal_delete(PropertyName const&) override;
// [[ParameterMap]]

View file

@ -105,9 +105,7 @@ bool Object::set(PropertyName const& property_name, Value value, ShouldThrowExce
// 3. Assert: Type(Throw) is Boolean.
// 4. Let success be ? O.[[Set]](P, V, O).
auto success = internal_set(property_name, value, this);
if (vm.exception())
return {};
auto success = TRY_OR_DISCARD(internal_set(property_name, value, this));
// 5. If success is false and Throw is true, throw a TypeError exception.
if (!success && throw_exceptions == ShouldThrowExceptions::Yes) {
@ -690,19 +688,23 @@ ThrowCompletionOr<Value> Object::internal_get(PropertyName const& property_name,
}
// 10.1.9 [[Set]] ( P, V, Receiver ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-set-p-v-receiver
bool Object::internal_set(PropertyName const& property_name, Value value, Value receiver)
ThrowCompletionOr<bool> Object::internal_set(PropertyName const& property_name, Value value, Value receiver)
{
VERIFY(!value.is_empty());
VERIFY(!receiver.is_empty());
auto& vm = this->vm();
// 1. Assert: IsPropertyKey(P) is true.
VERIFY(property_name.is_valid());
// 2. Let ownDesc be ? O.[[GetOwnProperty]](P).
auto own_descriptor = TRY_OR_DISCARD(internal_get_own_property(property_name));
auto own_descriptor = TRY(internal_get_own_property(property_name));
// 3. Return OrdinarySetWithOwnDescriptor(O, P, V, Receiver, ownDesc).
return ordinary_set_with_own_descriptor(property_name, value, receiver, own_descriptor);
auto success = ordinary_set_with_own_descriptor(property_name, value, receiver, own_descriptor);
if (auto* exception = vm.exception())
return throw_completion(exception->value());
return success;
}
// 10.1.9.2 OrdinarySetWithOwnDescriptor ( O, P, V, Receiver, ownDesc ), https://tc39.es/ecma262/#sec-ordinarysetwithowndescriptor
@ -721,7 +723,7 @@ bool Object::ordinary_set_with_own_descriptor(PropertyName const& property_name,
// b. If parent is not null, then
if (parent) {
// i. Return ? parent.[[Set]](P, V, Receiver).
return parent->internal_set(property_name, value, receiver);
return TRY_OR_DISCARD(parent->internal_set(property_name, value, receiver));
}
// c. Else,
else {

View file

@ -99,7 +99,7 @@ public:
virtual ThrowCompletionOr<bool> internal_define_own_property(PropertyName const&, PropertyDescriptor const&);
virtual ThrowCompletionOr<bool> internal_has_property(PropertyName const&) const;
virtual ThrowCompletionOr<Value> internal_get(PropertyName const&, Value receiver) const;
virtual bool internal_set(PropertyName const&, Value value, Value receiver);
virtual ThrowCompletionOr<bool> internal_set(PropertyName const&, Value value, Value receiver);
virtual bool internal_delete(PropertyName const&);
virtual MarkedValueList internal_own_property_keys() const;

View file

@ -543,7 +543,7 @@ ThrowCompletionOr<Value> ProxyObject::internal_get(PropertyName const& property_
}
// 10.5.9 [[Set]] ( P, V, Receiver ), https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-set-p-v-receiver
bool ProxyObject::internal_set(PropertyName const& property_name, Value value, Value receiver)
ThrowCompletionOr<bool> ProxyObject::internal_set(PropertyName const& property_name, Value value, Value receiver)
{
VERIFY(!value.is_empty());
VERIFY(!receiver.is_empty());
@ -557,16 +557,14 @@ bool ProxyObject::internal_set(PropertyName const& property_name, Value value, V
// 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, "set").
auto trap = TRY_OR_DISCARD(Value(&m_handler).get_method(global_object, vm.names.set));
auto trap = TRY(Value(&m_handler).get_method(global_object, vm.names.set));
// 7. If trap is undefined, then
if (!trap) {
@ -575,32 +573,28 @@ bool ProxyObject::internal_set(PropertyName const& property_name, Value value, V
}
// 8. Let booleanTrapResult be ! ToBoolean(? Call(trap, handler, « target, P, V, Receiver »)).
auto trap_result = TRY_OR_DISCARD(vm.call(*trap, &m_handler, &m_target, property_name_to_value(vm, property_name), value, receiver)).to_boolean();
auto trap_result = TRY(vm.call(*trap, &m_handler, &m_target, property_name_to_value(vm, property_name), value, receiver)).to_boolean();
// 9. If booleanTrapResult is false, return false.
if (!trap_result)
return false;
// 10. Let targetDesc be ? target.[[GetOwnProperty]](P).
auto target_descriptor = TRY_OR_DISCARD(m_target.internal_get_own_property(property_name));
auto target_descriptor = TRY(m_target.internal_get_own_property(property_name));
// 11. If targetDesc is not undefined and targetDesc.[[Configurable]] is false, then
if (target_descriptor.has_value() && !*target_descriptor->configurable) {
// a. If IsDataDescriptor(targetDesc) is true and targetDesc.[[Writable]] is false, then
if (target_descriptor->is_data_descriptor() && !*target_descriptor->writable) {
// i. If SameValue(V, targetDesc.[[Value]]) is false, throw a TypeError exception.
if (!same_value(value, *target_descriptor->value)) {
vm.throw_exception<TypeError>(global_object, ErrorType::ProxySetImmutableDataProperty);
return {};
}
if (!same_value(value, *target_descriptor->value))
return vm.throw_completion<TypeError>(global_object, ErrorType::ProxySetImmutableDataProperty);
}
// b. If IsAccessorDescriptor(targetDesc) is true, then
if (target_descriptor->is_accessor_descriptor()) {
// i. If targetDesc.[[Set]] is undefined, throw a TypeError exception.
if (!*target_descriptor->set) {
vm.throw_exception<TypeError>(global_object, ErrorType::ProxySetNonConfigurableAccessor);
return {};
}
if (!*target_descriptor->set)
return vm.throw_completion<TypeError>(global_object, ErrorType::ProxySetNonConfigurableAccessor);
}
}

View file

@ -43,7 +43,7 @@ public:
virtual ThrowCompletionOr<bool> internal_define_own_property(PropertyName const&, PropertyDescriptor const&) override;
virtual ThrowCompletionOr<bool> internal_has_property(PropertyName const&) const override;
virtual ThrowCompletionOr<Value> internal_get(PropertyName const&, Value receiver) const override;
virtual bool internal_set(PropertyName const&, Value value, Value receiver) override;
virtual ThrowCompletionOr<bool> internal_set(PropertyName const&, Value value, Value receiver) override;
virtual bool internal_delete(PropertyName const&) override;
virtual MarkedValueList internal_own_property_keys() const override;

View file

@ -39,9 +39,10 @@ void Reference::put_value(GlobalObject& global_object, Value value)
if (!base_obj)
return;
bool succeeded = base_obj->internal_set(m_name, value, get_this_value());
if (vm.exception())
auto succeeded_or_error = base_obj->internal_set(m_name, value, get_this_value());
if (succeeded_or_error.is_error())
return;
auto succeeded = succeeded_or_error.release_value();
if (!succeeded && m_strict) {
vm.throw_exception<TypeError>(global_object, ErrorType::ReferenceNullishSetProperty, m_name, m_base_value.to_string_without_side_effects());
return;

View file

@ -310,7 +310,7 @@ JS_DEFINE_NATIVE_FUNCTION(ReflectObject::set)
}
// 4. Return ? target.[[Set]](key, V, receiver).
return Value(target.as_object().internal_set(key, value, receiver));
return Value(TRY_OR_DISCARD(target.as_object().internal_set(key, value, receiver)));
}
// 28.1.13 Reflect.setPrototypeOf ( target, proto ), https://tc39.es/ecma262/#sec-reflect.setprototypeof

View file

@ -336,7 +336,7 @@ public:
}
// 10.4.5.5 [[Set]] ( P, V, Receiver ), https://tc39.es/ecma262/#sec-integer-indexed-exotic-objects-set-p-v-receiver
virtual bool internal_set(PropertyName const& property_name, Value value, Value receiver) override
virtual ThrowCompletionOr<bool> internal_set(PropertyName const& property_name, Value value, Value receiver) override
{
VERIFY(!value.is_empty());
VERIFY(!receiver.is_empty());
@ -356,8 +356,8 @@ public:
if (!numeric_index.is_undefined()) {
// i. Perform ? IntegerIndexedElementSet(O, numericIndex, V).
integer_indexed_element_set<T>(*this, numeric_index, value);
if (vm().exception())
return {};
if (auto* exception = vm().exception())
return throw_completion(exception->value());
// ii. Return true.
return true;

View file

@ -30,7 +30,7 @@ JS::ThrowCompletionOr<JS::Value> CSSStyleDeclarationWrapper::internal_get(JS::Pr
return { js_string(vm(), String::empty()) };
}
bool CSSStyleDeclarationWrapper::internal_set(JS::PropertyName const& name, JS::Value value, JS::Value receiver)
JS::ThrowCompletionOr<bool> CSSStyleDeclarationWrapper::internal_set(JS::PropertyName const& name, JS::Value value, JS::Value receiver)
{
if (!name.is_string())
return Base::internal_set(name, value, receiver);
@ -39,8 +39,8 @@ bool CSSStyleDeclarationWrapper::internal_set(JS::PropertyName const& name, JS::
return Base::internal_set(name, value, receiver);
auto css_text = value.to_string(global_object());
if (vm().exception())
return false;
if (auto* exception = vm().exception())
return JS::throw_completion(exception->value());
impl().set_property(property_id, css_text);
return true;

View file

@ -83,7 +83,7 @@ JS::ThrowCompletionOr<JS::Value> ConsoleGlobalObject::internal_get(JS::PropertyN
return Base::internal_get(property_name, receiver);
}
bool ConsoleGlobalObject::internal_set(JS::PropertyName const& property_name, JS::Value value, JS::Value receiver)
JS::ThrowCompletionOr<bool> ConsoleGlobalObject::internal_set(JS::PropertyName const& property_name, JS::Value value, JS::Value receiver)
{
return m_window_object->internal_set(property_name, value, (receiver == this) ? m_window_object : receiver);
}

View file

@ -31,7 +31,7 @@ public:
virtual JS::ThrowCompletionOr<bool> internal_define_own_property(JS::PropertyName const& name, JS::PropertyDescriptor const& descriptor) override;
virtual JS::ThrowCompletionOr<bool> internal_has_property(JS::PropertyName const& name) const override;
virtual JS::ThrowCompletionOr<JS::Value> internal_get(JS::PropertyName const&, JS::Value) const override;
virtual bool internal_set(JS::PropertyName const&, JS::Value value, JS::Value receiver) override;
virtual JS::ThrowCompletionOr<bool> internal_set(JS::PropertyName const&, JS::Value value, JS::Value receiver) override;
virtual bool internal_delete(JS::PropertyName const& name) override;
virtual JS::MarkedValueList internal_own_property_keys() const override;