LibJS: Convert TypedArray::create() to NonnullGCPtr

This commit is contained in:
Linus Groh 2022-12-13 20:49:50 +00:00
parent dd3d133968
commit 37c85fa07e
Notes: sideshowbarker 2024-07-17 05:02:35 +09:00
3 changed files with 50 additions and 51 deletions

View file

@ -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())

View file

@ -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

View file

@ -467,10 +467,9 @@ ThrowCompletionOr<double> compare_typed_array_elements(VM&, Value x, Value y, Fu
\ \
public: \ public: \
virtual ~ClassName(); \ virtual ~ClassName(); \
static ThrowCompletionOr<ClassName*> create( \ static ThrowCompletionOr<NonnullGCPtr<ClassName>> create(Realm&, u32 length, FunctionObject& new_target); \
Realm&, u32 length, FunctionObject& new_target); \ static ThrowCompletionOr<NonnullGCPtr<ClassName>> create(Realm&, u32 length); \
static ThrowCompletionOr<ClassName*> create(Realm&, u32 length); \ static NonnullGCPtr<ClassName> create(Realm&, u32 length, ArrayBuffer& buffer); \
static ClassName* create(Realm&, u32 length, ArrayBuffer& buffer); \
virtual FlyString const& element_name() const override; \ virtual FlyString const& element_name() const override; \
\ \
protected: \ protected: \