LibJS: Convert typed_array_from to ThrowCompletionOr

This commit is contained in:
Timothy Flynn 2021-10-23 13:57:04 -04:00 committed by Linus Groh
parent 50698a0db4
commit 3edf86462b
Notes: sideshowbarker 2024-07-18 01:58:07 +09:00
3 changed files with 14 additions and 34 deletions

View file

@ -164,9 +164,7 @@ void AtomicsObject::initialize(GlobalObject& global_object)
// 25.4.3 Atomics.add ( typedArray, index, value ), https://tc39.es/ecma262/#sec-atomics.add
JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::add)
{
auto* typed_array = typed_array_from(global_object, vm.argument(0));
if (!typed_array)
return {};
auto* typed_array = TRY_OR_DISCARD(typed_array_from(global_object, vm.argument(0)));
auto atomic_add = [](auto* storage, auto value) { return AK::atomic_fetch_add(storage, value); };
@ -182,9 +180,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::add)
// 25.4.4 Atomics.and ( typedArray, index, value ), https://tc39.es/ecma262/#sec-atomics.and
JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::and_)
{
auto* typed_array = typed_array_from(global_object, vm.argument(0));
if (!typed_array)
return {};
auto* typed_array = TRY_OR_DISCARD(typed_array_from(global_object, vm.argument(0)));
auto atomic_and = [](auto* storage, auto value) { return AK::atomic_fetch_and(storage, value); };
@ -280,9 +276,7 @@ static ThrowCompletionOr<Value> atomic_compare_exchange_impl(GlobalObject& globa
// 25.4.5 Atomics.compareExchange ( typedArray, index, expectedValue, replacementValue ), https://tc39.es/ecma262/#sec-atomics.compareexchange
JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::compare_exchange)
{
auto* typed_array = typed_array_from(global_object, vm.argument(0));
if (!typed_array)
return {};
auto* typed_array = TRY_OR_DISCARD(typed_array_from(global_object, vm.argument(0)));
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, Type) \
if (is<ClassName>(typed_array)) \
@ -296,9 +290,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::compare_exchange)
// 25.4.6 Atomics.exchange ( typedArray, index, value ), https://tc39.es/ecma262/#sec-atomics.exchange
JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::exchange)
{
auto* typed_array = typed_array_from(global_object, vm.argument(0));
if (!typed_array)
return {};
auto* typed_array = TRY_OR_DISCARD(typed_array_from(global_object, vm.argument(0)));
auto atomic_exchange = [](auto* storage, auto value) { return AK::atomic_exchange(storage, value); };
@ -329,9 +321,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::is_lock_free)
// 25.4.8 Atomics.load ( typedArray, index ), https://tc39.es/ecma262/#sec-atomics.load
JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::load)
{
auto* typed_array = typed_array_from(global_object, vm.argument(0));
if (!typed_array)
return {};
auto* typed_array = TRY_OR_DISCARD(typed_array_from(global_object, vm.argument(0)));
TRY_OR_DISCARD(validate_integer_typed_array(global_object, *typed_array));
@ -348,9 +338,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::load)
// 25.4.9 Atomics.or ( typedArray, index, value ), https://tc39.es/ecma262/#sec-atomics.or
JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::or_)
{
auto* typed_array = typed_array_from(global_object, vm.argument(0));
if (!typed_array)
return {};
auto* typed_array = TRY_OR_DISCARD(typed_array_from(global_object, vm.argument(0)));
auto atomic_or = [](auto* storage, auto value) { return AK::atomic_fetch_or(storage, value); };
@ -366,9 +354,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::or_)
// 25.4.10 Atomics.store ( typedArray, index, value ), https://tc39.es/ecma262/#sec-atomics.store
JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::store)
{
auto* typed_array = typed_array_from(global_object, vm.argument(0));
if (!typed_array)
return {};
auto* typed_array = TRY_OR_DISCARD(typed_array_from(global_object, vm.argument(0)));
TRY_OR_DISCARD(validate_integer_typed_array(global_object, *typed_array));
@ -393,9 +379,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::store)
// 25.4.11 Atomics.sub ( typedArray, index, value ), https://tc39.es/ecma262/#sec-atomics.sub
JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::sub)
{
auto* typed_array = typed_array_from(global_object, vm.argument(0));
if (!typed_array)
return {};
auto* typed_array = TRY_OR_DISCARD(typed_array_from(global_object, vm.argument(0)));
auto atomic_sub = [](auto* storage, auto value) { return AK::atomic_fetch_sub(storage, value); };
@ -411,9 +395,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::sub)
// 25.4.14 Atomics.xor ( typedArray, index, value ), https://tc39.es/ecma262/#sec-atomics.xor
JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::xor_)
{
auto* typed_array = typed_array_from(global_object, vm.argument(0));
if (!typed_array)
return {};
auto* typed_array = TRY_OR_DISCARD(typed_array_from(global_object, vm.argument(0)));
auto atomic_xor = [](auto* storage, auto value) { return AK::atomic_fetch_xor(storage, value); };

View file

@ -17,15 +17,13 @@
namespace JS {
TypedArrayBase* typed_array_from(GlobalObject& global_object, Value typed_array_value)
ThrowCompletionOr<TypedArrayBase*> typed_array_from(GlobalObject& global_object, Value typed_array_value)
{
auto& vm = global_object.vm();
auto* this_object = TRY_OR_DISCARD(typed_array_value.to_object(global_object));
if (!this_object->is_typed_array()) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObjectOfType, "TypedArray");
return nullptr;
}
auto* this_object = TRY(typed_array_value.to_object(global_object));
if (!this_object->is_typed_array())
return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObjectOfType, "TypedArray");
return static_cast<TypedArrayBase*>(this_object);
}

View file

@ -20,7 +20,7 @@ namespace JS {
class TypedArrayBase;
TypedArrayBase* typed_array_from(GlobalObject&, Value);
ThrowCompletionOr<TypedArrayBase*> typed_array_from(GlobalObject&, Value);
ThrowCompletionOr<void> validate_typed_array(GlobalObject&, TypedArrayBase&);
class TypedArrayBase : public Object {