From 2fa9422b3b8bcb83b09f744e227efc0e958d0131 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Mon, 18 Oct 2021 20:07:29 +0100 Subject: [PATCH] LibJS: Convert PrototypeObject::typed_this_value() to ThrowCompletionOr --- .../LibJS/Runtime/ArrayBufferPrototype.cpp | 10 ++------- .../LibJS/Runtime/ArrayIteratorPrototype.cpp | 5 +---- .../LibJS/Runtime/DataViewPrototype.cpp | 22 +++++-------------- .../LibJS/Runtime/MapIteratorPrototype.cpp | 5 +---- .../Libraries/LibJS/Runtime/PrototypeObject.h | 11 +++------- .../Runtime/RegExpStringIteratorPrototype.cpp | 5 +---- .../LibJS/Runtime/SetIteratorPrototype.cpp | 5 +---- .../LibJS/Runtime/StringIteratorPrototype.cpp | 5 +---- 8 files changed, 15 insertions(+), 53 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/ArrayBufferPrototype.cpp b/Userland/Libraries/LibJS/Runtime/ArrayBufferPrototype.cpp index 8370667975..767b41b4cc 100644 --- a/Userland/Libraries/LibJS/Runtime/ArrayBufferPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/ArrayBufferPrototype.cpp @@ -37,10 +37,7 @@ ArrayBufferPrototype::~ArrayBufferPrototype() // 25.1.5.3 ArrayBuffer.prototype.slice ( start, end ), https://tc39.es/ecma262/#sec-arraybuffer.prototype.slice JS_DEFINE_NATIVE_FUNCTION(ArrayBufferPrototype::slice) { - auto array_buffer_object = typed_this_value(global_object); - if (!array_buffer_object) - return {}; - + auto* array_buffer_object = TRY_OR_DISCARD(typed_this_value(global_object)); // FIXME: Check for shared buffer if (array_buffer_object->is_detached()) { vm.throw_exception(global_object, ErrorType::DetachedArrayBuffer); @@ -108,10 +105,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayBufferPrototype::slice) // 25.1.5.1 get ArrayBuffer.prototype.byteLength, https://tc39.es/ecma262/#sec-get-arraybuffer.prototype.bytelength JS_DEFINE_NATIVE_GETTER(ArrayBufferPrototype::byte_length_getter) { - auto array_buffer_object = typed_this_value(global_object); - if (!array_buffer_object) - return {}; - + auto* array_buffer_object = TRY_OR_DISCARD(typed_this_value(global_object)); // FIXME: Check for shared buffer if (array_buffer_object->is_detached()) return Value(0); diff --git a/Userland/Libraries/LibJS/Runtime/ArrayIteratorPrototype.cpp b/Userland/Libraries/LibJS/Runtime/ArrayIteratorPrototype.cpp index 9988f438be..47b9df9271 100644 --- a/Userland/Libraries/LibJS/Runtime/ArrayIteratorPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/ArrayIteratorPrototype.cpp @@ -38,10 +38,7 @@ ArrayIteratorPrototype::~ArrayIteratorPrototype() // FIXME: This seems to be CreateArrayIterator (https://tc39.es/ecma262/#sec-createarrayiterator) instead of %ArrayIteratorPrototype%.next. JS_DEFINE_NATIVE_FUNCTION(ArrayIteratorPrototype::next) { - auto* iterator = typed_this_value(global_object); - if (vm.exception()) - return {}; - + auto* iterator = TRY_OR_DISCARD(typed_this_value(global_object)); auto target_array = iterator->array(); if (target_array.is_undefined()) return create_iterator_result_object(global_object, js_undefined(), true); diff --git a/Userland/Libraries/LibJS/Runtime/DataViewPrototype.cpp b/Userland/Libraries/LibJS/Runtime/DataViewPrototype.cpp index 8cbe575ce4..b812a9d35d 100644 --- a/Userland/Libraries/LibJS/Runtime/DataViewPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/DataViewPrototype.cpp @@ -59,10 +59,7 @@ template static Value get_view_value(GlobalObject& global_object, Value request_index, Value is_little_endian) { auto& vm = global_object.vm(); - auto* view = DataViewPrototype::typed_this_value(global_object); - if (!view) - return {}; - + auto* view = TRY_OR_DISCARD(DataViewPrototype::typed_this_value(global_object)); auto get_index = TRY_OR_DISCARD(request_index.to_index(global_object)); auto little_endian = is_little_endian.to_boolean(); @@ -96,10 +93,7 @@ template static Value set_view_value(GlobalObject& global_object, Value request_index, Value is_little_endian, Value value) { auto& vm = global_object.vm(); - auto* view = DataViewPrototype::typed_this_value(global_object); - if (!view) - return {}; - + auto* view = TRY_OR_DISCARD(DataViewPrototype::typed_this_value(global_object)); auto get_index = TRY_OR_DISCARD(request_index.to_index(global_object)); Value number_value; @@ -249,18 +243,14 @@ JS_DEFINE_NATIVE_FUNCTION(DataViewPrototype::set_uint_32) // 25.3.4.1 get DataView.prototype.buffer, https://tc39.es/ecma262/#sec-get-dataview.prototype.buffer JS_DEFINE_NATIVE_GETTER(DataViewPrototype::buffer_getter) { - auto* data_view = typed_this_value(global_object); - if (!data_view) - return {}; + auto* data_view = TRY_OR_DISCARD(typed_this_value(global_object)); return data_view->viewed_array_buffer(); } // 25.3.4.2 get DataView.prototype.byteLength, https://tc39.es/ecma262/#sec-get-dataview.prototype.bytelength JS_DEFINE_NATIVE_GETTER(DataViewPrototype::byte_length_getter) { - auto* data_view = typed_this_value(global_object); - if (!data_view) - return {}; + auto* data_view = TRY_OR_DISCARD(typed_this_value(global_object)); if (data_view->viewed_array_buffer()->is_detached()) { vm.throw_exception(global_object, ErrorType::DetachedArrayBuffer); return {}; @@ -271,9 +261,7 @@ JS_DEFINE_NATIVE_GETTER(DataViewPrototype::byte_length_getter) // 25.3.4.3 get DataView.prototype.byteOffset, https://tc39.es/ecma262/#sec-get-dataview.prototype.byteoffset JS_DEFINE_NATIVE_GETTER(DataViewPrototype::byte_offset_getter) { - auto* data_view = typed_this_value(global_object); - if (!data_view) - return {}; + auto* data_view = TRY_OR_DISCARD(typed_this_value(global_object)); if (data_view->viewed_array_buffer()->is_detached()) { vm.throw_exception(global_object, ErrorType::DetachedArrayBuffer); return {}; diff --git a/Userland/Libraries/LibJS/Runtime/MapIteratorPrototype.cpp b/Userland/Libraries/LibJS/Runtime/MapIteratorPrototype.cpp index 46623408ef..43f3e4e558 100644 --- a/Userland/Libraries/LibJS/Runtime/MapIteratorPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/MapIteratorPrototype.cpp @@ -34,10 +34,7 @@ MapIteratorPrototype::~MapIteratorPrototype() // 24.1.5.2.1 %MapIteratorPrototype%.next ( ), https://tc39.es/ecma262/#sec-%mapiteratorprototype%.next JS_DEFINE_NATIVE_FUNCTION(MapIteratorPrototype::next) { - auto* map_iterator = typed_this_value(global_object); - if (vm.exception()) - return {}; - + auto* map_iterator = TRY_OR_DISCARD(typed_this_value(global_object)); if (map_iterator->done()) return create_iterator_result_object(global_object, js_undefined(), true); diff --git a/Userland/Libraries/LibJS/Runtime/PrototypeObject.h b/Userland/Libraries/LibJS/Runtime/PrototypeObject.h index 0e8ee25abc..1eb44e32aa 100644 --- a/Userland/Libraries/LibJS/Runtime/PrototypeObject.h +++ b/Userland/Libraries/LibJS/Runtime/PrototypeObject.h @@ -46,17 +46,12 @@ public: } // Use typed_this_value() when the spec does not coerce |this| value to an object. - static ObjectType* typed_this_value(GlobalObject& global_object) + static ThrowCompletionOr typed_this_value(GlobalObject& global_object) { auto& vm = global_object.vm(); - auto this_value = vm.this_value(global_object); - - if (!this_value.is_object() || !is(this_value.as_object())) { - vm.throw_exception(global_object, ErrorType::NotAnObjectOfType, PrototypeType::display_name()); - return nullptr; - } - + if (!this_value.is_object() || !is(this_value.as_object())) + return vm.throw_completion(global_object, ErrorType::NotAnObjectOfType, PrototypeType::display_name()); return static_cast(&this_value.as_object()); } diff --git a/Userland/Libraries/LibJS/Runtime/RegExpStringIteratorPrototype.cpp b/Userland/Libraries/LibJS/Runtime/RegExpStringIteratorPrototype.cpp index 289720ac1f..67d006ee42 100644 --- a/Userland/Libraries/LibJS/Runtime/RegExpStringIteratorPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/RegExpStringIteratorPrototype.cpp @@ -33,10 +33,7 @@ void RegExpStringIteratorPrototype::initialize(GlobalObject& global_object) JS_DEFINE_NATIVE_FUNCTION(RegExpStringIteratorPrototype::next) { // For details, see the 'closure' of: https://tc39.es/ecma262/#sec-createregexpstringiterator - auto* iterator = typed_this_value(global_object); - if (vm.exception()) - return {}; - + auto* iterator = TRY_OR_DISCARD(typed_this_value(global_object)); if (iterator->done()) return create_iterator_result_object(global_object, js_undefined(), true); diff --git a/Userland/Libraries/LibJS/Runtime/SetIteratorPrototype.cpp b/Userland/Libraries/LibJS/Runtime/SetIteratorPrototype.cpp index 1e4d20db8f..7eea4224b3 100644 --- a/Userland/Libraries/LibJS/Runtime/SetIteratorPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/SetIteratorPrototype.cpp @@ -36,10 +36,7 @@ SetIteratorPrototype::~SetIteratorPrototype() // 24.2.5.2.1 %SetIteratorPrototype%.next ( ), https://tc39.es/ecma262/#sec-%setiteratorprototype%.next JS_DEFINE_NATIVE_FUNCTION(SetIteratorPrototype::next) { - auto* set_iterator = typed_this_value(global_object); - if (vm.exception()) - return {}; - + auto* set_iterator = TRY_OR_DISCARD(typed_this_value(global_object)); if (set_iterator->done()) return create_iterator_result_object(global_object, js_undefined(), true); diff --git a/Userland/Libraries/LibJS/Runtime/StringIteratorPrototype.cpp b/Userland/Libraries/LibJS/Runtime/StringIteratorPrototype.cpp index 277838e63c..54c44d93c0 100644 --- a/Userland/Libraries/LibJS/Runtime/StringIteratorPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/StringIteratorPrototype.cpp @@ -35,10 +35,7 @@ StringIteratorPrototype::~StringIteratorPrototype() // 22.1.5.1.1 %StringIteratorPrototype%.next ( ), https://tc39.es/ecma262/#sec-%stringiteratorprototype%.next JS_DEFINE_NATIVE_FUNCTION(StringIteratorPrototype::next) { - auto* iterator = typed_this_value(global_object); - if (vm.exception()) - return {}; - + auto* iterator = TRY_OR_DISCARD(typed_this_value(global_object)); if (iterator->done()) return create_iterator_result_object(global_object, js_undefined(), true);