LibJS: Move typed_array_create to TypedArray.{h,cpp}

This is to make it accessible from TypedArrayPrototype, for use with
typed_array_species_create.
This commit is contained in:
Luke 2021-07-09 06:18:56 +01:00 committed by Linus Groh
parent 9b4729dd40
commit 53bc3f8e3b
Notes: sideshowbarker 2024-07-18 10:01:13 +09:00
3 changed files with 29 additions and 27 deletions

View file

@ -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<TypeError>(global_object, ErrorType::NotA, "TypedArray");
return nullptr;
}
auto& typed_array = static_cast<TypedArrayBase&>(new_typed_array.as_object());
if (typed_array.viewed_array_buffer()->is_detached()) {
vm.throw_exception<TypeError>(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<TypeError>(global_object, ErrorType::InvalidLength, "typed array");
return nullptr;
}
return &typed_array;
}
void TypedArrayBase::visit_edges(Visitor& visitor)
{
Object::visit_edges(visitor);

View file

@ -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<Type> { \
JS_OBJECT(ClassName, TypedArray); \

View file

@ -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<TypeError>(global_object, ErrorType::NotA, "TypedArray");
return nullptr;
}
auto& typed_array = static_cast<TypedArrayBase&>(new_typed_array.as_object());
if (typed_array.viewed_array_buffer()->is_detached()) {
vm.throw_exception<TypeError>(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<TypeError>(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)
{