LibJS: Convert internal_get() to ThrowCompletionOr

This commit is contained in:
Linus Groh 2021-09-29 18:20:10 +01:00
parent d9895ec12d
commit 6c2b974db2
Notes: sideshowbarker 2024-07-18 03:19:10 +09:00
17 changed files with 39 additions and 44 deletions

View file

@ -1392,7 +1392,7 @@ public:
if (interface.extended_attributes.contains("CustomGet")) {
generator.append(R"~~~(
virtual JS::Value internal_get(JS::PropertyName const&, JS::Value receiver) const override;
virtual JS::ThrowCompletionOr<JS::Value> internal_get(JS::PropertyName const&, JS::Value receiver) const override;
)~~~");
}
if (interface.extended_attributes.contains("CustomSet")) {

View file

@ -8,6 +8,7 @@
#include "Spreadsheet.h"
#include "Workbook.h"
#include <LibJS/Lexer.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Object.h>
@ -101,7 +102,7 @@ SheetGlobalObject::~SheetGlobalObject()
{
}
JS::Value SheetGlobalObject::internal_get(const JS::PropertyName& property_name, JS::Value receiver) const
JS::ThrowCompletionOr<JS::Value> SheetGlobalObject::internal_get(const JS::PropertyName& property_name, JS::Value receiver) const
{
if (property_name.is_string()) {
if (property_name.as_string() == "value") {

View file

@ -8,6 +8,7 @@
#include "Forward.h"
#include <LibJS/Forward.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/GlobalObject.h>
namespace Spreadsheet {
@ -26,7 +27,7 @@ public:
virtual ~SheetGlobalObject() override;
virtual JS::Value internal_get(JS::PropertyName const&, JS::Value receiver) const 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 void initialize_global_object() override;

View file

@ -7,6 +7,7 @@
#include "DebuggerGlobalJSObject.h"
#include "Debugger.h"
#include "DebuggerVariableJSObject.h"
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/ProxyObject.h>
@ -21,7 +22,7 @@ DebuggerGlobalJSObject::DebuggerGlobalJSObject()
m_variables = lib->debug_info->get_variables_in_current_scope(regs);
}
JS::Value DebuggerGlobalJSObject::internal_get(JS::PropertyName const& property_name, JS::Value receiver) const
JS::ThrowCompletionOr<JS::Value> DebuggerGlobalJSObject::internal_get(JS::PropertyName const& property_name, JS::Value receiver) const
{
if (m_variables.is_empty() || !property_name.is_string())
return Base::internal_get(property_name, receiver);
@ -36,8 +37,7 @@ JS::Value DebuggerGlobalJSObject::internal_get(JS::PropertyName const& property_
if (js_value.has_value())
return js_value.value();
auto error_string = String::formatted("Variable {} of type {} is not convertible to a JS Value", 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));
}
bool DebuggerGlobalJSObject::internal_set(JS::PropertyName const& property_name, JS::Value value, JS::Value receiver)

View file

@ -8,6 +8,7 @@
#include <AK/Weakable.h>
#include <LibDebug/DebugInfo.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/GlobalObject.h>
namespace HackStudio {
@ -20,7 +21,7 @@ class DebuggerGlobalJSObject final
public:
DebuggerGlobalJSObject();
virtual JS::Value internal_get(JS::PropertyName const&, JS::Value receiver) const 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;
Optional<JS::Value> debugger_to_js(const Debug::DebugInfo::VariableInfo&) const;

View file

@ -35,7 +35,7 @@ void ArgumentsObject::visit_edges(Cell::Visitor& visitor)
}
// 10.4.4.3 [[Get]] ( P, Receiver ), https://tc39.es/ecma262/#sec-arguments-exotic-objects-get-p-receiver
Value ArgumentsObject::internal_get(PropertyName const& property_name, Value receiver) const
ThrowCompletionOr<Value> ArgumentsObject::internal_get(PropertyName const& property_name, Value receiver) const
{
// 1. Let map be args.[[ParameterMap]].
auto& map = *m_parameter_map;

View file

@ -25,7 +25,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 Value internal_get(PropertyName const&, Value receiver) 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 bool internal_delete(PropertyName const&) override;

View file

@ -86,7 +86,7 @@ Value Object::get(PropertyName const& property_name) const
VERIFY(property_name.is_valid());
// 3. Return ? O.[[Get]](P, O).
return internal_get(property_name, this);
return TRY_OR_DISCARD(internal_get(property_name, this));
}
// 7.3.3 GetV ( V, P ) is defined as Value::get().
@ -647,7 +647,7 @@ ThrowCompletionOr<bool> Object::internal_has_property(PropertyName const& proper
}
// 10.1.8 [[Get]] ( P, Receiver ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-get-p-receiver
Value Object::internal_get(PropertyName const& property_name, Value receiver) const
ThrowCompletionOr<Value> Object::internal_get(PropertyName const& property_name, Value receiver) const
{
VERIFY(!receiver.is_empty());
auto& vm = this->vm();
@ -656,12 +656,12 @@ Value Object::internal_get(PropertyName const& property_name, Value receiver) co
VERIFY(property_name.is_valid());
// 2. Let desc be ? O.[[GetOwnProperty]](P).
auto descriptor = TRY_OR_DISCARD(internal_get_own_property(property_name));
auto descriptor = TRY(internal_get_own_property(property_name));
// 3. If desc is undefined, then
if (!descriptor.has_value()) {
// a. Let parent be ? O.[[GetPrototypeOf]]().
auto* parent = TRY_OR_DISCARD(internal_get_prototype_of());
auto* parent = TRY(internal_get_prototype_of());
// b. If parent is null, return undefined.
if (!parent)
@ -686,7 +686,7 @@ Value Object::internal_get(PropertyName const& property_name, Value receiver) co
return js_undefined();
// 8. Return ? Call(getter, Receiver).
return TRY_OR_DISCARD(vm.call(*getter, receiver));
return TRY(vm.call(*getter, receiver));
}
// 10.1.9 [[Set]] ( P, V, Receiver ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-set-p-v-receiver

View file

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

View file

@ -472,7 +472,7 @@ ThrowCompletionOr<bool> ProxyObject::internal_has_property(PropertyName const& p
}
// 10.5.8 [[Get]] ( P, Receiver ), https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-get-p-receiver
Value ProxyObject::internal_get(PropertyName const& property_name, Value receiver) const
ThrowCompletionOr<Value> ProxyObject::internal_get(PropertyName const& property_name, Value receiver) const
{
VERIFY(!receiver.is_empty());
@ -485,10 +485,8 @@ Value ProxyObject::internal_get(PropertyName const& property_name, Value receive
// 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]].
@ -506,13 +504,11 @@ Value ProxyObject::internal_get(PropertyName const& property_name, Value receive
// 6. goto 1
//
// In JS code: `h = {}; p = new Proxy({}, h); h.__proto__ = p; p.foo // or h.foo`
if (vm.did_reach_stack_space_limit()) {
vm.throw_exception<Error>(global_object, ErrorType::CallStackSizeExceeded);
return {};
}
if (vm.did_reach_stack_space_limit())
return vm.throw_completion<Error>(global_object, ErrorType::CallStackSizeExceeded);
// 6. Let trap be ? GetMethod(handler, "get").
auto trap = TRY_OR_DISCARD(Value(&m_handler).get_method(global_object, vm.names.get));
auto trap = TRY(Value(&m_handler).get_method(global_object, vm.names.get));
// 7. If trap is undefined, then
if (!trap) {
@ -521,28 +517,24 @@ Value ProxyObject::internal_get(PropertyName const& property_name, Value receive
}
// 8. Let trapResult be ? Call(trap, handler, « target, P, Receiver »).
auto trap_result = TRY_OR_DISCARD(vm.call(*trap, &m_handler, &m_target, property_name_to_value(vm, property_name), receiver));
auto trap_result = TRY(vm.call(*trap, &m_handler, &m_target, property_name_to_value(vm, property_name), receiver));
// 9. 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));
// 10. 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(trapResult, targetDesc.[[Value]]) is false, throw a TypeError exception.
if (!same_value(trap_result, *target_descriptor->value)) {
vm.throw_exception<TypeError>(global_object, ErrorType::ProxyGetImmutableDataProperty);
return {};
}
if (!same_value(trap_result, *target_descriptor->value))
return vm.throw_completion<TypeError>(global_object, ErrorType::ProxyGetImmutableDataProperty);
}
// b. If IsAccessorDescriptor(targetDesc) is true and targetDesc.[[Get]] is undefined, then
if (target_descriptor->is_accessor_descriptor() && !*target_descriptor->get) {
// i. If trapResult is not undefined, throw a TypeError exception.
if (!trap_result.is_undefined()) {
vm.throw_exception<TypeError>(global_object, ErrorType::ProxyGetNonConfigurableAccessor);
return {};
}
if (!trap_result.is_undefined())
return vm.throw_completion<TypeError>(global_object, ErrorType::ProxyGetNonConfigurableAccessor);
}
}

View file

@ -42,7 +42,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<bool> internal_has_property(PropertyName const&) const override;
virtual Value internal_get(PropertyName const&, Value receiver) 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 bool internal_delete(PropertyName const&) override;
virtual MarkedValueList internal_own_property_keys() const override;

View file

@ -171,7 +171,7 @@ JS_DEFINE_NATIVE_FUNCTION(ReflectObject::get)
}
// 4. Return ? target.[[Get]](key, receiver).
return target.as_object().internal_get(key, receiver);
return TRY_OR_DISCARD(target.as_object().internal_get(key, receiver));
}
// 28.1.6 Reflect.getOwnPropertyDescriptor ( target, propertyKey ), https://tc39.es/ecma262/#sec-reflect.getownpropertydescriptor

View file

@ -309,7 +309,7 @@ public:
}
// 10.4.5.4 [[Get]] ( P, Receiver ), 10.4.5.4 [[Get]] ( P, Receiver )
virtual Value internal_get(PropertyName const& property_name, Value receiver) const override
virtual ThrowCompletionOr<Value> internal_get(PropertyName const& property_name, Value receiver) const override
{
VERIFY(!receiver.is_empty());

View file

@ -820,7 +820,7 @@ Value Value::get(GlobalObject& global_object, PropertyName const& property_name)
return {};
// 3. Return ? O.[[Get]](P, V).
return object->internal_get(property_name, *this);
return TRY_OR_DISCARD(object->internal_get(property_name, *this));
}
// 7.3.10 GetMethod ( V, P ), https://tc39.es/ecma262/#sec-getmethod

View file

@ -18,7 +18,7 @@ JS::ThrowCompletionOr<bool> CSSStyleDeclarationWrapper::internal_has_property(JS
return property_id != CSS::PropertyID::Invalid;
}
JS::Value CSSStyleDeclarationWrapper::internal_get(JS::PropertyName const& name, JS::Value receiver) const
JS::ThrowCompletionOr<JS::Value> CSSStyleDeclarationWrapper::internal_get(JS::PropertyName const& name, JS::Value receiver) const
{
if (!name.is_string())
return Base::internal_get(name, receiver);
@ -26,8 +26,8 @@ JS::Value CSSStyleDeclarationWrapper::internal_get(JS::PropertyName const& name,
if (property_id == CSS::PropertyID::Invalid)
return Base::internal_get(name, receiver);
if (auto maybe_property = impl().property(property_id); maybe_property.has_value())
return js_string(vm(), maybe_property->value->to_string());
return js_string(vm(), String::empty());
return { js_string(vm(), maybe_property->value->to_string()) };
return { js_string(vm(), String::empty()) };
}
bool CSSStyleDeclarationWrapper::internal_set(JS::PropertyName const& name, JS::Value value, JS::Value receiver)

View file

@ -75,7 +75,7 @@ JS::ThrowCompletionOr<bool> ConsoleGlobalObject::internal_has_property(JS::Prope
return TRY(Object::internal_has_property(property_name)) || TRY(m_window_object->internal_has_property(property_name));
}
JS::Value ConsoleGlobalObject::internal_get(JS::PropertyName const& property_name, JS::Value receiver) const
JS::ThrowCompletionOr<JS::Value> ConsoleGlobalObject::internal_get(JS::PropertyName const& property_name, JS::Value receiver) const
{
if (m_window_object->has_own_property(property_name))
return m_window_object->internal_get(property_name, (receiver == this) ? m_window_object : receiver);

View file

@ -30,7 +30,7 @@ public:
virtual JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> internal_get_own_property(JS::PropertyName const& name) const override;
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::Value internal_get(JS::PropertyName const&, JS::Value) 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 bool internal_delete(JS::PropertyName const& name) override;
virtual JS::MarkedValueList internal_own_property_keys() const override;