1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 21:47:46 +00:00

LibJS: Convert PrototypeObject::typed_this_value() to ThrowCompletionOr

This commit is contained in:
Linus Groh 2021-10-18 20:07:29 +01:00
parent 4b7c1f703e
commit 2fa9422b3b
8 changed files with 15 additions and 53 deletions

View file

@ -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<TypeError>(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);

View file

@ -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);

View file

@ -59,10 +59,7 @@ template<typename T>
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<typename T>
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<TypeError>(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<TypeError>(global_object, ErrorType::DetachedArrayBuffer);
return {};

View file

@ -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);

View file

@ -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<ObjectType*> 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<ObjectType>(this_value.as_object())) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObjectOfType, PrototypeType::display_name());
return nullptr;
}
if (!this_value.is_object() || !is<ObjectType>(this_value.as_object()))
return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObjectOfType, PrototypeType::display_name());
return static_cast<ObjectType*>(&this_value.as_object());
}

View file

@ -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);

View file

@ -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);

View file

@ -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);