From 37c85fa07e7ed5d1e34c855f5479528b47da2627 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Tue, 13 Dec 2022 20:49:50 +0000 Subject: [PATCH] LibJS: Convert TypedArray::create() to NonnullGCPtr --- Tests/LibWasm/test-wasm.cpp | 2 +- .../Libraries/LibJS/Runtime/TypedArray.cpp | 18 ++--- Userland/Libraries/LibJS/Runtime/TypedArray.h | 81 +++++++++---------- 3 files changed, 50 insertions(+), 51 deletions(-) diff --git a/Tests/LibWasm/test-wasm.cpp b/Tests/LibWasm/test-wasm.cpp index c339c70726f..a35551eaa0a 100644 --- a/Tests/LibWasm/test-wasm.cpp +++ b/Tests/LibWasm/test-wasm.cpp @@ -24,7 +24,7 @@ TESTJS_GLOBAL_FUNCTION(read_binary_wasm_file, readBinaryWasmFile) if (file_size.is_error()) return vm.throw_completion(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()); if (read.is_error()) diff --git a/Userland/Libraries/LibJS/Runtime/TypedArray.cpp b/Userland/Libraries/LibJS/Runtime/TypedArray.cpp index f0de705e982..84ed2757a65 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArray.cpp +++ b/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) \ - ThrowCompletionOr ClassName::create(Realm& realm, u32 length, FunctionObject& new_target) \ + ThrowCompletionOr> 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 array_buffer = TRY(ArrayBuffer::create(realm, length * sizeof(UnderlyingBufferDataType))); \ - return realm.heap().allocate(realm, *prototype, length, *array_buffer); \ + return NonnullGCPtr { *realm.heap().allocate(realm, *prototype, length, *array_buffer) }; \ } \ \ - ThrowCompletionOr ClassName::create(Realm& realm, u32 length) \ + ThrowCompletionOr> ClassName::create(Realm& realm, u32 length) \ { \ auto array_buffer = TRY(ArrayBuffer::create(realm, length * sizeof(UnderlyingBufferDataType))); \ return create(realm, length, *array_buffer); \ } \ \ - ClassName* ClassName::create(Realm& realm, u32 length, ArrayBuffer& array_buffer) \ + NonnullGCPtr ClassName::create(Realm& realm, u32 length, ArrayBuffer& array_buffer) \ { \ - return realm.heap().allocate(realm, *realm.intrinsics().snake_name##_prototype(), length, array_buffer); \ + return *realm.heap().allocate(realm, *realm.intrinsics().snake_name##_prototype(), length, 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(); \ \ 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); \ 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()) { \ auto& arg_typed_array = static_cast(first_argument.as_object()); \ 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())); \ } \ } \ - return typed_array; \ + return typed_array.ptr(); \ } \ \ 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? */ \ if (Checked::multiplication_would_overflow(array_length, sizeof(Type))) \ return vm.throw_completion(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 diff --git a/Userland/Libraries/LibJS/Runtime/TypedArray.h b/Userland/Libraries/LibJS/Runtime/TypedArray.h index 60268897549..9993b6f5e8f 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArray.h +++ b/Userland/Libraries/LibJS/Runtime/TypedArray.h @@ -461,47 +461,46 @@ ThrowCompletionOr typed_array_create(VM&, FunctionObject& const ThrowCompletionOr typed_array_create_same_type(VM&, TypedArrayBase const& exemplar, MarkedVector arguments); ThrowCompletionOr 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 { \ - JS_OBJECT(ClassName, TypedArray); \ - \ - public: \ - virtual ~ClassName(); \ - static ThrowCompletionOr create( \ - Realm&, u32 length, FunctionObject& new_target); \ - static ThrowCompletionOr 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 call() override; \ - virtual ThrowCompletionOr 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 { \ + JS_OBJECT(ClassName, TypedArray); \ + \ + public: \ + virtual ~ClassName(); \ + static ThrowCompletionOr> create(Realm&, u32 length, FunctionObject& new_target); \ + static ThrowCompletionOr> create(Realm&, u32 length); \ + static NonnullGCPtr 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 call() override; \ + virtual ThrowCompletionOr 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) \