From 6c2b974db28a0720a8788676f3a01f7cbc668afa Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Wed, 29 Sep 2021 18:20:10 +0100 Subject: LibJS: Convert internal_get() to ThrowCompletionOr --- .../Applications/Spreadsheet/JSIntegration.cpp | 3 +- Userland/Applications/Spreadsheet/JSIntegration.h | 3 +- .../HackStudio/Debugger/DebuggerGlobalJSObject.cpp | 6 ++-- .../HackStudio/Debugger/DebuggerGlobalJSObject.h | 3 +- .../Libraries/LibJS/Runtime/ArgumentsObject.cpp | 2 +- Userland/Libraries/LibJS/Runtime/ArgumentsObject.h | 2 +- Userland/Libraries/LibJS/Runtime/Object.cpp | 10 +++---- Userland/Libraries/LibJS/Runtime/Object.h | 2 +- Userland/Libraries/LibJS/Runtime/ProxyObject.cpp | 32 ++++++++-------------- Userland/Libraries/LibJS/Runtime/ProxyObject.h | 2 +- Userland/Libraries/LibJS/Runtime/ReflectObject.cpp | 2 +- Userland/Libraries/LibJS/Runtime/TypedArray.h | 2 +- Userland/Libraries/LibJS/Runtime/Value.cpp | 2 +- .../Bindings/CSSStyleDeclarationWrapperCustom.cpp | 6 ++-- .../Services/WebContent/ConsoleGlobalObject.cpp | 2 +- Userland/Services/WebContent/ConsoleGlobalObject.h | 2 +- 16 files changed, 38 insertions(+), 43 deletions(-) (limited to 'Userland') diff --git a/Userland/Applications/Spreadsheet/JSIntegration.cpp b/Userland/Applications/Spreadsheet/JSIntegration.cpp index a1dd89fa26..7feed88a3f 100644 --- a/Userland/Applications/Spreadsheet/JSIntegration.cpp +++ b/Userland/Applications/Spreadsheet/JSIntegration.cpp @@ -8,6 +8,7 @@ #include "Spreadsheet.h" #include "Workbook.h" #include +#include #include #include #include @@ -101,7 +102,7 @@ SheetGlobalObject::~SheetGlobalObject() { } -JS::Value SheetGlobalObject::internal_get(const JS::PropertyName& property_name, JS::Value receiver) const +JS::ThrowCompletionOr SheetGlobalObject::internal_get(const JS::PropertyName& property_name, JS::Value receiver) const { if (property_name.is_string()) { if (property_name.as_string() == "value") { diff --git a/Userland/Applications/Spreadsheet/JSIntegration.h b/Userland/Applications/Spreadsheet/JSIntegration.h index 94c5e86049..7d45c46af5 100644 --- a/Userland/Applications/Spreadsheet/JSIntegration.h +++ b/Userland/Applications/Spreadsheet/JSIntegration.h @@ -8,6 +8,7 @@ #include "Forward.h" #include +#include #include 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 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; diff --git a/Userland/DevTools/HackStudio/Debugger/DebuggerGlobalJSObject.cpp b/Userland/DevTools/HackStudio/Debugger/DebuggerGlobalJSObject.cpp index 182e85befd..50d6ced836 100644 --- a/Userland/DevTools/HackStudio/Debugger/DebuggerGlobalJSObject.cpp +++ b/Userland/DevTools/HackStudio/Debugger/DebuggerGlobalJSObject.cpp @@ -7,6 +7,7 @@ #include "DebuggerGlobalJSObject.h" #include "Debugger.h" #include "DebuggerVariableJSObject.h" +#include #include #include @@ -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 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(const_cast(*this), error_string); - return {}; + return vm().throw_completion(const_cast(*this), move(error_string)); } bool DebuggerGlobalJSObject::internal_set(JS::PropertyName const& property_name, JS::Value value, JS::Value receiver) diff --git a/Userland/DevTools/HackStudio/Debugger/DebuggerGlobalJSObject.h b/Userland/DevTools/HackStudio/Debugger/DebuggerGlobalJSObject.h index 255fb4fca6..348974de55 100644 --- a/Userland/DevTools/HackStudio/Debugger/DebuggerGlobalJSObject.h +++ b/Userland/DevTools/HackStudio/Debugger/DebuggerGlobalJSObject.h @@ -8,6 +8,7 @@ #include #include +#include #include 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 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 debugger_to_js(const Debug::DebugInfo::VariableInfo&) const; diff --git a/Userland/Libraries/LibJS/Runtime/ArgumentsObject.cpp b/Userland/Libraries/LibJS/Runtime/ArgumentsObject.cpp index 9edd408f31..de642d2b93 100644 --- a/Userland/Libraries/LibJS/Runtime/ArgumentsObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/ArgumentsObject.cpp @@ -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 ArgumentsObject::internal_get(PropertyName const& property_name, Value receiver) const { // 1. Let map be args.[[ParameterMap]]. auto& map = *m_parameter_map; diff --git a/Userland/Libraries/LibJS/Runtime/ArgumentsObject.h b/Userland/Libraries/LibJS/Runtime/ArgumentsObject.h index 947b374091..a08e60cc7d 100644 --- a/Userland/Libraries/LibJS/Runtime/ArgumentsObject.h +++ b/Userland/Libraries/LibJS/Runtime/ArgumentsObject.h @@ -25,7 +25,7 @@ public: virtual ThrowCompletionOr> internal_get_own_property(PropertyName const&) const override; virtual ThrowCompletionOr internal_define_own_property(PropertyName const&, PropertyDescriptor const&) override; - virtual Value internal_get(PropertyName const&, Value receiver) const override; + virtual ThrowCompletionOr 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; diff --git a/Userland/Libraries/LibJS/Runtime/Object.cpp b/Userland/Libraries/LibJS/Runtime/Object.cpp index cee28b6580..fc487f8080 100644 --- a/Userland/Libraries/LibJS/Runtime/Object.cpp +++ b/Userland/Libraries/LibJS/Runtime/Object.cpp @@ -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 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 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 diff --git a/Userland/Libraries/LibJS/Runtime/Object.h b/Userland/Libraries/LibJS/Runtime/Object.h index a02b7395ef..233628741d 100644 --- a/Userland/Libraries/LibJS/Runtime/Object.h +++ b/Userland/Libraries/LibJS/Runtime/Object.h @@ -98,7 +98,7 @@ public: virtual ThrowCompletionOr> internal_get_own_property(PropertyName const&) const; virtual ThrowCompletionOr internal_define_own_property(PropertyName const&, PropertyDescriptor const&); virtual ThrowCompletionOr internal_has_property(PropertyName const&) const; - virtual Value internal_get(PropertyName const&, Value receiver) const; + virtual ThrowCompletionOr 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; diff --git a/Userland/Libraries/LibJS/Runtime/ProxyObject.cpp b/Userland/Libraries/LibJS/Runtime/ProxyObject.cpp index 398e3d46c4..62629f9117 100644 --- a/Userland/Libraries/LibJS/Runtime/ProxyObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/ProxyObject.cpp @@ -472,7 +472,7 @@ ThrowCompletionOr 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 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(global_object, ErrorType::ProxyRevoked); - return {}; - } + if (m_is_revoked) + return vm.throw_completion(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(global_object, ErrorType::CallStackSizeExceeded); - return {}; - } + if (vm.did_reach_stack_space_limit()) + return vm.throw_completion(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(global_object, ErrorType::ProxyGetImmutableDataProperty); - return {}; - } + if (!same_value(trap_result, *target_descriptor->value)) + return vm.throw_completion(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(global_object, ErrorType::ProxyGetNonConfigurableAccessor); - return {}; - } + if (!trap_result.is_undefined()) + return vm.throw_completion(global_object, ErrorType::ProxyGetNonConfigurableAccessor); } } diff --git a/Userland/Libraries/LibJS/Runtime/ProxyObject.h b/Userland/Libraries/LibJS/Runtime/ProxyObject.h index 1b9bfec70e..3eb1a1d6bb 100644 --- a/Userland/Libraries/LibJS/Runtime/ProxyObject.h +++ b/Userland/Libraries/LibJS/Runtime/ProxyObject.h @@ -42,7 +42,7 @@ public: virtual ThrowCompletionOr> internal_get_own_property(PropertyName const&) const override; virtual ThrowCompletionOr internal_define_own_property(PropertyName const&, PropertyDescriptor const&) override; virtual ThrowCompletionOr internal_has_property(PropertyName const&) const override; - virtual Value internal_get(PropertyName const&, Value receiver) const override; + virtual ThrowCompletionOr 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; diff --git a/Userland/Libraries/LibJS/Runtime/ReflectObject.cpp b/Userland/Libraries/LibJS/Runtime/ReflectObject.cpp index 3ac12b4236..e8358236e8 100644 --- a/Userland/Libraries/LibJS/Runtime/ReflectObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/ReflectObject.cpp @@ -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 diff --git a/Userland/Libraries/LibJS/Runtime/TypedArray.h b/Userland/Libraries/LibJS/Runtime/TypedArray.h index e86b59048b..0ee7aff0ab 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArray.h +++ b/Userland/Libraries/LibJS/Runtime/TypedArray.h @@ -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 internal_get(PropertyName const& property_name, Value receiver) const override { VERIFY(!receiver.is_empty()); diff --git a/Userland/Libraries/LibJS/Runtime/Value.cpp b/Userland/Libraries/LibJS/Runtime/Value.cpp index 138513a3f6..e87ec1f189 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.cpp +++ b/Userland/Libraries/LibJS/Runtime/Value.cpp @@ -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 diff --git a/Userland/Libraries/LibWeb/Bindings/CSSStyleDeclarationWrapperCustom.cpp b/Userland/Libraries/LibWeb/Bindings/CSSStyleDeclarationWrapperCustom.cpp index 9707b93587..e8690c2d16 100644 --- a/Userland/Libraries/LibWeb/Bindings/CSSStyleDeclarationWrapperCustom.cpp +++ b/Userland/Libraries/LibWeb/Bindings/CSSStyleDeclarationWrapperCustom.cpp @@ -18,7 +18,7 @@ JS::ThrowCompletionOr 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 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) diff --git a/Userland/Services/WebContent/ConsoleGlobalObject.cpp b/Userland/Services/WebContent/ConsoleGlobalObject.cpp index 66f26b921d..d4d79c6468 100644 --- a/Userland/Services/WebContent/ConsoleGlobalObject.cpp +++ b/Userland/Services/WebContent/ConsoleGlobalObject.cpp @@ -75,7 +75,7 @@ JS::ThrowCompletionOr 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 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); diff --git a/Userland/Services/WebContent/ConsoleGlobalObject.h b/Userland/Services/WebContent/ConsoleGlobalObject.h index 1ff4832ff8..ca79a34579 100644 --- a/Userland/Services/WebContent/ConsoleGlobalObject.h +++ b/Userland/Services/WebContent/ConsoleGlobalObject.h @@ -30,7 +30,7 @@ public: virtual JS::ThrowCompletionOr> internal_get_own_property(JS::PropertyName const& name) const override; virtual JS::ThrowCompletionOr internal_define_own_property(JS::PropertyName const& name, JS::PropertyDescriptor const& descriptor) override; virtual JS::ThrowCompletionOr internal_has_property(JS::PropertyName const& name) const override; - virtual JS::Value internal_get(JS::PropertyName const&, JS::Value) const override; + virtual JS::ThrowCompletionOr 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; -- cgit v1.2.3