From 53bc3f8e3b5944438ef058fc84d32adf9ab2273d Mon Sep 17 00:00:00 2001 From: Luke Date: Fri, 9 Jul 2021 06:18:56 +0100 Subject: [PATCH] LibJS: Move typed_array_create to TypedArray.{h,cpp} This is to make it accessible from TypedArrayPrototype, for use with typed_array_species_create. --- .../Libraries/LibJS/Runtime/TypedArray.cpp | 27 +++++++++++++++++++ Userland/Libraries/LibJS/Runtime/TypedArray.h | 2 ++ .../LibJS/Runtime/TypedArrayConstructor.cpp | 27 ------------------- 3 files changed, 29 insertions(+), 27 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/TypedArray.cpp b/Userland/Libraries/LibJS/Runtime/TypedArray.cpp index 7248ac978cd..ed8696fd15b 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArray.cpp +++ b/Userland/Libraries/LibJS/Runtime/TypedArray.cpp @@ -202,6 +202,33 @@ static void initialize_typed_array_from_list(GlobalObject& global_object, TypedA } } +// 23.2.4.2 TypedArrayCreate ( constructor, argumentList ), https://tc39.es/ecma262/#typedarray-create +TypedArrayBase* typed_array_create(GlobalObject& global_object, FunctionObject& constructor, MarkedValueList arguments) +{ + auto& vm = global_object.vm(); + + auto argument_count = arguments.size(); + auto first_argument = argument_count > 0 ? arguments[0] : js_undefined(); + + auto new_typed_array = vm.construct(constructor, constructor, move(arguments)); + if (vm.exception()) + return nullptr; + if (!new_typed_array.is_object() || !new_typed_array.as_object().is_typed_array()) { + vm.throw_exception(global_object, ErrorType::NotA, "TypedArray"); + return nullptr; + } + auto& typed_array = static_cast(new_typed_array.as_object()); + if (typed_array.viewed_array_buffer()->is_detached()) { + vm.throw_exception(global_object, ErrorType::DetachedArrayBuffer); + return nullptr; + } + if (argument_count == 1 && first_argument.is_number() && typed_array.array_length() < first_argument.as_double()) { + vm.throw_exception(global_object, ErrorType::InvalidLength, "typed array"); + return nullptr; + } + return &typed_array; +} + void TypedArrayBase::visit_edges(Visitor& visitor) { Object::visit_edges(visitor); diff --git a/Userland/Libraries/LibJS/Runtime/TypedArray.h b/Userland/Libraries/LibJS/Runtime/TypedArray.h index 43a3411ef64..83d40f6cc7c 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArray.h +++ b/Userland/Libraries/LibJS/Runtime/TypedArray.h @@ -455,6 +455,8 @@ private: virtual bool is_typed_array() const final { return true; } }; +TypedArrayBase* typed_array_create(GlobalObject& global_object, FunctionObject& constructor, MarkedValueList arguments); + #define JS_DECLARE_TYPED_ARRAY(ClassName, snake_name, PrototypeName, ConstructorName, Type) \ class ClassName : public TypedArray { \ JS_OBJECT(ClassName, TypedArray); \ diff --git a/Userland/Libraries/LibJS/Runtime/TypedArrayConstructor.cpp b/Userland/Libraries/LibJS/Runtime/TypedArrayConstructor.cpp index 98e703d5860..db7061086f1 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArrayConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/TypedArrayConstructor.cpp @@ -55,33 +55,6 @@ Value TypedArrayConstructor::construct(FunctionObject&) return {}; } -// 23.2.4.2 TypedArrayCreate ( constructor, argumentList ), https://tc39.es/ecma262/#typedarray-create -static TypedArrayBase* typed_array_create(GlobalObject& global_object, FunctionObject& constructor, MarkedValueList arguments) -{ - auto& vm = global_object.vm(); - - auto argument_count = arguments.size(); - auto first_argument = argument_count > 0 ? arguments[0] : js_undefined(); - - auto new_typed_array = vm.construct(constructor, constructor, move(arguments)); - if (vm.exception()) - return nullptr; - if (!new_typed_array.is_object() || !new_typed_array.as_object().is_typed_array()) { - vm.throw_exception(global_object, ErrorType::NotA, "TypedArray"); - return nullptr; - } - auto& typed_array = static_cast(new_typed_array.as_object()); - if (typed_array.viewed_array_buffer()->is_detached()) { - vm.throw_exception(global_object, ErrorType::DetachedArrayBuffer); - return nullptr; - } - if (argument_count == 1 && first_argument.is_number() && typed_array.array_length() < first_argument.as_double()) { - vm.throw_exception(global_object, ErrorType::InvalidLength, "typed array"); - return nullptr; - } - return &typed_array; -} - // 23.2.2.1 %TypedArray%.from ( source [ , mapfn [ , thisArg ] ] ), https://tc39.es/ecma262/#sec-%typedarray%.from JS_DEFINE_NATIVE_FUNCTION(TypedArrayConstructor::from) {