Sfoglia il codice sorgente

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

This is to make it accessible from TypedArrayPrototype, for use with
typed_array_species_create.
Luke 4 anni fa
parent
commit
53bc3f8e3b

+ 27 - 0
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<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);

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

+ 0 - 27
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<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)
 {