1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 17:57:35 +00:00

LibJS: Use OrdinaryCreateFromConstructor() in a bunch of constructors

Resolves various FIXMEs :^)
This commit is contained in:
Linus Groh 2021-06-20 01:09:39 +01:00 committed by Andreas Kling
parent e5753443ae
commit 8f6ac0db1c
16 changed files with 173 additions and 102 deletions

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/DataView.h>
#include <LibJS/Runtime/DataViewConstructor.h>
#include <LibJS/Runtime/Error.h>
@ -40,28 +41,30 @@ Value DataViewConstructor::call()
}
// 25.3.2.1 DataView ( buffer [ , byteOffset [ , byteLength ] ] ), https://tc39.es/ecma262/#sec-dataview-buffer-byteoffset-bytelength
Value DataViewConstructor::construct(Function&)
Value DataViewConstructor::construct(Function& new_target)
{
auto& vm = this->vm();
auto& global_object = this->global_object();
auto buffer = vm.argument(0);
if (!buffer.is_object() || !is<ArrayBuffer>(buffer.as_object())) {
vm.throw_exception<TypeError>(global_object(), ErrorType::IsNotAn, buffer.to_string_without_side_effects(), vm.names.ArrayBuffer);
vm.throw_exception<TypeError>(global_object, ErrorType::IsNotAn, buffer.to_string_without_side_effects(), vm.names.ArrayBuffer);
return {};
}
auto& array_buffer = static_cast<ArrayBuffer&>(buffer.as_object());
auto offset = vm.argument(1).to_index(global_object());
auto offset = vm.argument(1).to_index(global_object);
if (vm.exception())
return {};
if (array_buffer.is_detached()) {
vm.throw_exception<TypeError>(global_object(), ErrorType::DetachedArrayBuffer);
vm.throw_exception<TypeError>(global_object, ErrorType::DetachedArrayBuffer);
return {};
}
auto buffer_byte_length = array_buffer.byte_length();
if (offset > buffer_byte_length) {
vm.throw_exception<RangeError>(global_object(), ErrorType::DataViewOutOfRangeByteOffset, offset, buffer_byte_length);
vm.throw_exception<RangeError>(global_object, ErrorType::DataViewOutOfRangeByteOffset, offset, buffer_byte_length);
return {};
}
@ -69,22 +72,25 @@ Value DataViewConstructor::construct(Function&)
if (vm.argument(2).is_undefined()) {
view_byte_length = buffer_byte_length - offset;
} else {
view_byte_length = vm.argument(2).to_index(global_object());
view_byte_length = vm.argument(2).to_index(global_object);
if (vm.exception())
return {};
if (offset + view_byte_length > buffer_byte_length) {
vm.throw_exception<RangeError>(global_object(), ErrorType::InvalidLength, vm.names.DataView);
vm.throw_exception<RangeError>(global_object, ErrorType::InvalidLength, vm.names.DataView);
return {};
}
}
// FIXME: Use OrdinaryCreateFromConstructor(newTarget, "%DataView.prototype%")
auto* data_view = ordinary_create_from_constructor<DataView>(global_object, new_target, &GlobalObject::data_view_prototype, &array_buffer, view_byte_length, offset);
if (vm.exception())
return {};
if (array_buffer.is_detached()) {
vm.throw_exception<TypeError>(global_object(), ErrorType::DetachedArrayBuffer);
vm.throw_exception<TypeError>(global_object, ErrorType::DetachedArrayBuffer);
return {};
}
return DataView::create(global_object(), &array_buffer, view_byte_length, offset);
return data_view;
}
}