Browse Source

LibJS: Convert TypedArray::create() to NonnullGCPtr

Linus Groh 2 năm trước cách đây
mục cha
commit
37c85fa07e

+ 1 - 1
Tests/LibWasm/test-wasm.cpp

@@ -24,7 +24,7 @@ TESTJS_GLOBAL_FUNCTION(read_binary_wasm_file, readBinaryWasmFile)
     if (file_size.is_error())
     if (file_size.is_error())
         return vm.throw_completion<JS::TypeError>(strerror(file_size.error().code()));
         return vm.throw_completion<JS::TypeError>(strerror(file_size.error().code()));
 
 
-    auto* array = TRY(JS::Uint8Array::create(realm, file_size.value()));
+    auto array = TRY(JS::Uint8Array::create(realm, file_size.value()));
 
 
     auto read = file.value()->read(array->data());
     auto read = file.value()->read(array->data());
     if (read.is_error())
     if (read.is_error())

+ 9 - 9
Userland/Libraries/LibJS/Runtime/TypedArray.cpp

@@ -421,22 +421,22 @@ void TypedArrayBase::visit_edges(Visitor& visitor)
 }
 }
 
 
 #define JS_DEFINE_TYPED_ARRAY(ClassName, snake_name, PrototypeName, ConstructorName, Type)                                       \
 #define JS_DEFINE_TYPED_ARRAY(ClassName, snake_name, PrototypeName, ConstructorName, Type)                                       \
-    ThrowCompletionOr<ClassName*> ClassName::create(Realm& realm, u32 length, FunctionObject& new_target)                        \
+    ThrowCompletionOr<NonnullGCPtr<ClassName>> ClassName::create(Realm& realm, u32 length, FunctionObject& new_target)           \
     {                                                                                                                            \
     {                                                                                                                            \
         auto* prototype = TRY(get_prototype_from_constructor(realm.vm(), new_target, &Intrinsics::snake_name##_prototype));      \
         auto* prototype = TRY(get_prototype_from_constructor(realm.vm(), new_target, &Intrinsics::snake_name##_prototype));      \
         auto array_buffer = TRY(ArrayBuffer::create(realm, length * sizeof(UnderlyingBufferDataType)));                          \
         auto array_buffer = TRY(ArrayBuffer::create(realm, length * sizeof(UnderlyingBufferDataType)));                          \
-        return realm.heap().allocate<ClassName>(realm, *prototype, length, *array_buffer);                                       \
+        return NonnullGCPtr { *realm.heap().allocate<ClassName>(realm, *prototype, length, *array_buffer) };                     \
     }                                                                                                                            \
     }                                                                                                                            \
                                                                                                                                  \
                                                                                                                                  \
-    ThrowCompletionOr<ClassName*> ClassName::create(Realm& realm, u32 length)                                                    \
+    ThrowCompletionOr<NonnullGCPtr<ClassName>> ClassName::create(Realm& realm, u32 length)                                       \
     {                                                                                                                            \
     {                                                                                                                            \
         auto array_buffer = TRY(ArrayBuffer::create(realm, length * sizeof(UnderlyingBufferDataType)));                          \
         auto array_buffer = TRY(ArrayBuffer::create(realm, length * sizeof(UnderlyingBufferDataType)));                          \
         return create(realm, length, *array_buffer);                                                                             \
         return create(realm, length, *array_buffer);                                                                             \
     }                                                                                                                            \
     }                                                                                                                            \
                                                                                                                                  \
                                                                                                                                  \
-    ClassName* ClassName::create(Realm& realm, u32 length, ArrayBuffer& array_buffer)                                            \
+    NonnullGCPtr<ClassName> ClassName::create(Realm& realm, u32 length, ArrayBuffer& array_buffer)                               \
     {                                                                                                                            \
     {                                                                                                                            \
-        return realm.heap().allocate<ClassName>(realm, *realm.intrinsics().snake_name##_prototype(), length, array_buffer);      \
+        return *realm.heap().allocate<ClassName>(realm, *realm.intrinsics().snake_name##_prototype(), length, array_buffer);     \
     }                                                                                                                            \
     }                                                                                                                            \
                                                                                                                                  \
                                                                                                                                  \
     ClassName::ClassName(Object& prototype, u32 length, ArrayBuffer& array_buffer)                                               \
     ClassName::ClassName(Object& prototype, u32 length, ArrayBuffer& array_buffer)                                               \
@@ -511,11 +511,11 @@ void TypedArrayBase::visit_edges(Visitor& visitor)
         auto& realm = *vm.current_realm();                                                                                       \
         auto& realm = *vm.current_realm();                                                                                       \
                                                                                                                                  \
                                                                                                                                  \
         if (vm.argument_count() == 0)                                                                                            \
         if (vm.argument_count() == 0)                                                                                            \
-            return TRY(ClassName::create(realm, 0, new_target));                                                                 \
+            return TRY(ClassName::create(realm, 0, new_target)).ptr();                                                           \
                                                                                                                                  \
                                                                                                                                  \
         auto first_argument = vm.argument(0);                                                                                    \
         auto first_argument = vm.argument(0);                                                                                    \
         if (first_argument.is_object()) {                                                                                        \
         if (first_argument.is_object()) {                                                                                        \
-            auto* typed_array = TRY(ClassName::create(realm, 0, new_target));                                                    \
+            auto typed_array = TRY(ClassName::create(realm, 0, new_target));                                                     \
             if (first_argument.as_object().is_typed_array()) {                                                                   \
             if (first_argument.as_object().is_typed_array()) {                                                                   \
                 auto& arg_typed_array = static_cast<TypedArrayBase&>(first_argument.as_object());                                \
                 auto& arg_typed_array = static_cast<TypedArrayBase&>(first_argument.as_object());                                \
                 TRY(initialize_typed_array_from_typed_array(vm, *typed_array, arg_typed_array));                                 \
                 TRY(initialize_typed_array_from_typed_array(vm, *typed_array, arg_typed_array));                                 \
@@ -532,7 +532,7 @@ void TypedArrayBase::visit_edges(Visitor& visitor)
                     TRY(initialize_typed_array_from_array_like(vm, *typed_array, first_argument.as_object()));                   \
                     TRY(initialize_typed_array_from_array_like(vm, *typed_array, first_argument.as_object()));                   \
                 }                                                                                                                \
                 }                                                                                                                \
             }                                                                                                                    \
             }                                                                                                                    \
-            return typed_array;                                                                                                  \
+            return typed_array.ptr();                                                                                            \
         }                                                                                                                        \
         }                                                                                                                        \
                                                                                                                                  \
                                                                                                                                  \
         auto array_length_or_error = first_argument.to_index(vm);                                                                \
         auto array_length_or_error = first_argument.to_index(vm);                                                                \
@@ -550,7 +550,7 @@ void TypedArrayBase::visit_edges(Visitor& visitor)
         /* FIXME: What is the best/correct behavior here? */                                                                     \
         /* FIXME: What is the best/correct behavior here? */                                                                     \
         if (Checked<u32>::multiplication_would_overflow(array_length, sizeof(Type)))                                             \
         if (Checked<u32>::multiplication_would_overflow(array_length, sizeof(Type)))                                             \
             return vm.throw_completion<RangeError>(ErrorType::InvalidLength, "typed array");                                     \
             return vm.throw_completion<RangeError>(ErrorType::InvalidLength, "typed array");                                     \
-        return TRY(ClassName::create(realm, array_length, new_target));                                                          \
+        return TRY(ClassName::create(realm, array_length, new_target)).ptr();                                                    \
     }
     }
 
 
 #undef __JS_ENUMERATE
 #undef __JS_ENUMERATE

+ 40 - 41
Userland/Libraries/LibJS/Runtime/TypedArray.h

@@ -461,47 +461,46 @@ ThrowCompletionOr<TypedArrayBase*> typed_array_create(VM&, FunctionObject& const
 ThrowCompletionOr<TypedArrayBase*> typed_array_create_same_type(VM&, TypedArrayBase const& exemplar, MarkedVector<Value> arguments);
 ThrowCompletionOr<TypedArrayBase*> typed_array_create_same_type(VM&, TypedArrayBase const& exemplar, MarkedVector<Value> arguments);
 ThrowCompletionOr<double> compare_typed_array_elements(VM&, Value x, Value y, FunctionObject* comparefn);
 ThrowCompletionOr<double> compare_typed_array_elements(VM&, Value x, Value y, FunctionObject* comparefn);
 
 
-#define JS_DECLARE_TYPED_ARRAY(ClassName, snake_name, PrototypeName, ConstructorName, Type) \
-    class ClassName : public TypedArray<Type> {                                             \
-        JS_OBJECT(ClassName, TypedArray);                                                   \
-                                                                                            \
-    public:                                                                                 \
-        virtual ~ClassName();                                                               \
-        static ThrowCompletionOr<ClassName*> create(                                        \
-            Realm&, u32 length, FunctionObject& new_target);                                \
-        static ThrowCompletionOr<ClassName*> create(Realm&, u32 length);                    \
-        static ClassName* create(Realm&, u32 length, ArrayBuffer& buffer);                  \
-        virtual FlyString const& element_name() const override;                             \
-                                                                                            \
-    protected:                                                                              \
-        ClassName(Object& prototype, u32 length, ArrayBuffer& array_buffer);                \
-    };                                                                                      \
-    class PrototypeName final : public Object {                                             \
-        JS_OBJECT(PrototypeName, Object);                                                   \
-                                                                                            \
-    public:                                                                                 \
-        virtual void initialize(Realm&) override;                                           \
-        virtual ~PrototypeName() override;                                                  \
-                                                                                            \
-    private:                                                                                \
-        PrototypeName(Object& prototype);                                                   \
-    };                                                                                      \
-    class ConstructorName final : public TypedArrayConstructor {                            \
-        JS_OBJECT(ConstructorName, TypedArrayConstructor);                                  \
-                                                                                            \
-    public:                                                                                 \
-        virtual void initialize(Realm&) override;                                           \
-        virtual ~ConstructorName() override;                                                \
-                                                                                            \
-        virtual ThrowCompletionOr<Value> call() override;                                   \
-        virtual ThrowCompletionOr<Object*> construct(FunctionObject& new_target) override;  \
-                                                                                            \
-    private:                                                                                \
-        explicit ConstructorName(Realm&, Object& prototype);                                \
-        virtual bool has_constructor() const override                                       \
-        {                                                                                   \
-            return true;                                                                    \
-        }                                                                                   \
+#define JS_DECLARE_TYPED_ARRAY(ClassName, snake_name, PrototypeName, ConstructorName, Type)                       \
+    class ClassName : public TypedArray<Type> {                                                                   \
+        JS_OBJECT(ClassName, TypedArray);                                                                         \
+                                                                                                                  \
+    public:                                                                                                       \
+        virtual ~ClassName();                                                                                     \
+        static ThrowCompletionOr<NonnullGCPtr<ClassName>> create(Realm&, u32 length, FunctionObject& new_target); \
+        static ThrowCompletionOr<NonnullGCPtr<ClassName>> create(Realm&, u32 length);                             \
+        static NonnullGCPtr<ClassName> create(Realm&, u32 length, ArrayBuffer& buffer);                           \
+        virtual FlyString const& element_name() const override;                                                   \
+                                                                                                                  \
+    protected:                                                                                                    \
+        ClassName(Object& prototype, u32 length, ArrayBuffer& array_buffer);                                      \
+    };                                                                                                            \
+    class PrototypeName final : public Object {                                                                   \
+        JS_OBJECT(PrototypeName, Object);                                                                         \
+                                                                                                                  \
+    public:                                                                                                       \
+        virtual void initialize(Realm&) override;                                                                 \
+        virtual ~PrototypeName() override;                                                                        \
+                                                                                                                  \
+    private:                                                                                                      \
+        PrototypeName(Object& prototype);                                                                         \
+    };                                                                                                            \
+    class ConstructorName final : public TypedArrayConstructor {                                                  \
+        JS_OBJECT(ConstructorName, TypedArrayConstructor);                                                        \
+                                                                                                                  \
+    public:                                                                                                       \
+        virtual void initialize(Realm&) override;                                                                 \
+        virtual ~ConstructorName() override;                                                                      \
+                                                                                                                  \
+        virtual ThrowCompletionOr<Value> call() override;                                                         \
+        virtual ThrowCompletionOr<Object*> construct(FunctionObject& new_target) override;                        \
+                                                                                                                  \
+    private:                                                                                                      \
+        explicit ConstructorName(Realm&, Object& prototype);                                                      \
+        virtual bool has_constructor() const override                                                             \
+        {                                                                                                         \
+            return true;                                                                                          \
+        }                                                                                                         \
     };
     };
 
 
 #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, Type) \
 #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, Type) \