1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 09:47:35 +00:00

LibJS: Convert ArrayBufferPrototype functions to ThrowCompletionOr

This commit is contained in:
Idan Horowitz 2021-10-29 00:55:10 +03:00
parent ffa58184d2
commit 720bb21ee2
2 changed files with 24 additions and 36 deletions

View file

@ -23,8 +23,8 @@ void ArrayBufferPrototype::initialize(GlobalObject& global_object)
auto& vm = this->vm(); auto& vm = this->vm();
Object::initialize(global_object); Object::initialize(global_object);
u8 attr = Attribute::Writable | Attribute::Configurable; u8 attr = Attribute::Writable | Attribute::Configurable;
define_old_native_function(vm.names.slice, slice, 2, attr); define_native_function(vm.names.slice, slice, 2, attr);
define_old_native_accessor(vm.names.byteLength, byte_length_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.byteLength, byte_length_getter, {}, Attribute::Configurable);
// 25.1.5.4 ArrayBuffer.prototype [ @@toStringTag ], https://tc39.es/ecma262/#sec-arraybuffer.prototype-@@tostringtag // 25.1.5.4 ArrayBuffer.prototype [ @@toStringTag ], https://tc39.es/ecma262/#sec-arraybuffer.prototype-@@tostringtag
define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm, vm.names.ArrayBuffer.as_string()), Attribute::Configurable); define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm, vm.names.ArrayBuffer.as_string()), Attribute::Configurable);
@ -35,18 +35,16 @@ ArrayBufferPrototype::~ArrayBufferPrototype()
} }
// 25.1.5.3 ArrayBuffer.prototype.slice ( start, end ), https://tc39.es/ecma262/#sec-arraybuffer.prototype.slice // 25.1.5.3 ArrayBuffer.prototype.slice ( start, end ), https://tc39.es/ecma262/#sec-arraybuffer.prototype.slice
JS_DEFINE_OLD_NATIVE_FUNCTION(ArrayBufferPrototype::slice) JS_DEFINE_NATIVE_FUNCTION(ArrayBufferPrototype::slice)
{ {
auto* array_buffer_object = TRY_OR_DISCARD(typed_this_value(global_object)); auto* array_buffer_object = TRY(typed_this_value(global_object));
// FIXME: Check for shared buffer // FIXME: Check for shared buffer
if (array_buffer_object->is_detached()) { if (array_buffer_object->is_detached())
vm.throw_exception<TypeError>(global_object, ErrorType::DetachedArrayBuffer); return vm.throw_completion<TypeError>(global_object, ErrorType::DetachedArrayBuffer);
return {};
}
auto length = array_buffer_object->byte_length(); auto length = array_buffer_object->byte_length();
auto relative_start = TRY_OR_DISCARD(vm.argument(0).to_integer_or_infinity(global_object)); auto relative_start = TRY(vm.argument(0).to_integer_or_infinity(global_object));
double first; double first;
if (relative_start < 0) if (relative_start < 0)
@ -54,7 +52,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(ArrayBufferPrototype::slice)
else else
first = min(relative_start, (double)length); first = min(relative_start, (double)length);
auto relative_end = vm.argument(1).is_undefined() ? length : TRY_OR_DISCARD(vm.argument(1).to_integer_or_infinity(global_object)); auto relative_end = vm.argument(1).is_undefined() ? length : TRY(vm.argument(1).to_integer_or_infinity(global_object));
double final; double final;
if (relative_end < 0) if (relative_end < 0)
@ -64,36 +62,26 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(ArrayBufferPrototype::slice)
auto new_length = max(final - first, 0.0); auto new_length = max(final - first, 0.0);
auto* constructor = TRY_OR_DISCARD(species_constructor(global_object, *array_buffer_object, *global_object.array_buffer_constructor())); auto* constructor = TRY(species_constructor(global_object, *array_buffer_object, *global_object.array_buffer_constructor()));
MarkedValueList arguments(vm.heap()); MarkedValueList arguments(vm.heap());
arguments.append(Value(new_length)); arguments.append(Value(new_length));
auto* new_array_buffer = TRY_OR_DISCARD(construct(global_object, *constructor, move(arguments))); auto* new_array_buffer = TRY(construct(global_object, *constructor, move(arguments)));
if (!is<ArrayBuffer>(new_array_buffer)) { if (!is<ArrayBuffer>(new_array_buffer))
vm.throw_exception<TypeError>(global_object, ErrorType::SpeciesConstructorDidNotCreate, "an ArrayBuffer"); return vm.throw_completion<TypeError>(global_object, ErrorType::SpeciesConstructorDidNotCreate, "an ArrayBuffer");
return {};
}
auto* new_array_buffer_object = static_cast<ArrayBuffer*>(new_array_buffer); auto* new_array_buffer_object = static_cast<ArrayBuffer*>(new_array_buffer);
// FIXME: Check for shared buffer // FIXME: Check for shared buffer
if (new_array_buffer_object->is_detached()) { if (new_array_buffer_object->is_detached())
vm.throw_exception<TypeError>(global_object, ErrorType::SpeciesConstructorReturned, "a detached ArrayBuffer"); return vm.throw_completion<TypeError>(global_object, ErrorType::SpeciesConstructorReturned, "a detached ArrayBuffer");
return {}; if (same_value(new_array_buffer_object, array_buffer_object))
} return vm.throw_completion<TypeError>(global_object, ErrorType::SpeciesConstructorReturned, "same ArrayBuffer instance");
if (same_value(new_array_buffer_object, array_buffer_object)) { if (new_array_buffer_object->byte_length() < new_length)
vm.throw_exception<TypeError>(global_object, ErrorType::SpeciesConstructorReturned, "same ArrayBuffer instance"); return vm.throw_completion<TypeError>(global_object, ErrorType::SpeciesConstructorReturned, "an ArrayBuffer smaller than requested");
return {};
}
if (new_array_buffer_object->byte_length() < new_length) {
vm.throw_exception<TypeError>(global_object, ErrorType::SpeciesConstructorReturned, "an ArrayBuffer smaller than requested");
return {};
}
if (array_buffer_object->is_detached()) { if (array_buffer_object->is_detached())
vm.throw_exception<TypeError>(global_object, ErrorType::DetachedArrayBuffer); return vm.throw_completion<TypeError>(global_object, ErrorType::DetachedArrayBuffer);
return {};
}
// This is ugly, is there a better way to do this? // This is ugly, is there a better way to do this?
array_buffer_object->buffer().span().slice(first, new_length).copy_to(new_array_buffer_object->buffer().span()); array_buffer_object->buffer().span().slice(first, new_length).copy_to(new_array_buffer_object->buffer().span());
@ -101,9 +89,9 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(ArrayBufferPrototype::slice)
} }
// 25.1.5.1 get ArrayBuffer.prototype.byteLength, https://tc39.es/ecma262/#sec-get-arraybuffer.prototype.bytelength // 25.1.5.1 get ArrayBuffer.prototype.byteLength, https://tc39.es/ecma262/#sec-get-arraybuffer.prototype.bytelength
JS_DEFINE_OLD_NATIVE_FUNCTION(ArrayBufferPrototype::byte_length_getter) JS_DEFINE_NATIVE_FUNCTION(ArrayBufferPrototype::byte_length_getter)
{ {
auto* array_buffer_object = TRY_OR_DISCARD(typed_this_value(global_object)); auto* array_buffer_object = TRY(typed_this_value(global_object));
// FIXME: Check for shared buffer // FIXME: Check for shared buffer
if (array_buffer_object->is_detached()) if (array_buffer_object->is_detached())
return Value(0); return Value(0);

View file

@ -20,8 +20,8 @@ public:
virtual ~ArrayBufferPrototype() override; virtual ~ArrayBufferPrototype() override;
private: private:
JS_DECLARE_OLD_NATIVE_FUNCTION(slice); JS_DECLARE_NATIVE_FUNCTION(slice);
JS_DECLARE_OLD_NATIVE_FUNCTION(byte_length_getter); JS_DECLARE_NATIVE_FUNCTION(byte_length_getter);
}; };
} }