summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2021-10-23 13:58:25 -0400
committerLinus Groh <mail@linusgroh.de>2021-10-23 19:16:03 +0100
commit20f73d2abc6bd30b5fa684d4793b531c7c713b92 (patch)
treef3dbbb8fe61aae942878bf1197dc6a61cf7ac3b2 /Userland
parent3edf86462bc81ea6e0efce0ddf51d45318508cb5 (diff)
downloadserenity-20f73d2abc6bd30b5fa684d4793b531c7c713b92.zip
LibJS: Convert Atomics functions to ThrowCompletionOr
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp98
-rw-r--r--Userland/Libraries/LibJS/Runtime/AtomicsObject.h20
2 files changed, 57 insertions, 61 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp b/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp
index f3bc20f3de..94d84b7fd6 100644
--- a/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp
+++ b/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp
@@ -146,31 +146,31 @@ void AtomicsObject::initialize(GlobalObject& global_object)
auto& vm = this->vm();
u8 attr = Attribute::Writable | Attribute::Configurable;
- define_old_native_function(vm.names.add, add, 3, attr);
- define_old_native_function(vm.names.and_, and_, 3, attr);
- define_old_native_function(vm.names.compareExchange, compare_exchange, 4, attr);
- define_old_native_function(vm.names.exchange, exchange, 3, attr);
- define_old_native_function(vm.names.isLockFree, is_lock_free, 1, attr);
- define_old_native_function(vm.names.load, load, 2, attr);
- define_old_native_function(vm.names.or_, or_, 3, attr);
- define_old_native_function(vm.names.store, store, 3, attr);
- define_old_native_function(vm.names.sub, sub, 3, attr);
- define_old_native_function(vm.names.xor_, xor_, 3, attr);
+ define_native_function(vm.names.add, add, 3, attr);
+ define_native_function(vm.names.and_, and_, 3, attr);
+ define_native_function(vm.names.compareExchange, compare_exchange, 4, attr);
+ define_native_function(vm.names.exchange, exchange, 3, attr);
+ define_native_function(vm.names.isLockFree, is_lock_free, 1, attr);
+ define_native_function(vm.names.load, load, 2, attr);
+ define_native_function(vm.names.or_, or_, 3, attr);
+ define_native_function(vm.names.store, store, 3, attr);
+ define_native_function(vm.names.sub, sub, 3, attr);
+ define_native_function(vm.names.xor_, xor_, 3, attr);
// 25.4.15 Atomics [ @@toStringTag ], https://tc39.es/ecma262/#sec-atomics-@@tostringtag
define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(global_object.heap(), "Atomics"), Attribute::Configurable);
}
// 25.4.3 Atomics.add ( typedArray, index, value ), https://tc39.es/ecma262/#sec-atomics.add
-JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::add)
+JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::add)
{
- auto* typed_array = TRY_OR_DISCARD(typed_array_from(global_object, vm.argument(0)));
+ auto* typed_array = TRY(typed_array_from(global_object, vm.argument(0)));
auto atomic_add = [](auto* storage, auto value) { return AK::atomic_fetch_add(storage, value); };
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, Type) \
if (is<ClassName>(typed_array)) \
- return TRY_OR_DISCARD(perform_atomic_operation<Type>(global_object, *typed_array, move(atomic_add)));
+ return TRY(perform_atomic_operation<Type>(global_object, *typed_array, move(atomic_add)));
JS_ENUMERATE_TYPED_ARRAYS
#undef __JS_ENUMERATE
@@ -178,15 +178,15 @@ 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_)
+JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::and_)
{
- auto* typed_array = TRY_OR_DISCARD(typed_array_from(global_object, vm.argument(0)));
+ auto* typed_array = TRY(typed_array_from(global_object, vm.argument(0)));
auto atomic_and = [](auto* storage, auto value) { return AK::atomic_fetch_and(storage, value); };
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, Type) \
if (is<ClassName>(typed_array)) \
- return TRY_OR_DISCARD(perform_atomic_operation<Type>(global_object, *typed_array, move(atomic_and)));
+ return TRY(perform_atomic_operation<Type>(global_object, *typed_array, move(atomic_and)));
JS_ENUMERATE_TYPED_ARRAYS
#undef __JS_ENUMERATE
@@ -274,13 +274,13 @@ 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)
+JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::compare_exchange)
{
- auto* typed_array = TRY_OR_DISCARD(typed_array_from(global_object, vm.argument(0)));
+ auto* typed_array = TRY(typed_array_from(global_object, vm.argument(0)));
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, Type) \
if (is<ClassName>(typed_array)) \
- return TRY_OR_DISCARD(atomic_compare_exchange_impl<Type>(global_object, *typed_array));
+ return TRY(atomic_compare_exchange_impl<Type>(global_object, *typed_array));
JS_ENUMERATE_TYPED_ARRAYS
#undef __JS_ENUMERATE
@@ -288,15 +288,15 @@ 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)
+JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::exchange)
{
- auto* typed_array = TRY_OR_DISCARD(typed_array_from(global_object, vm.argument(0)));
+ auto* typed_array = TRY(typed_array_from(global_object, vm.argument(0)));
auto atomic_exchange = [](auto* storage, auto value) { return AK::atomic_exchange(storage, value); };
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, Type) \
if (is<ClassName>(typed_array)) \
- return TRY_OR_DISCARD(perform_atomic_operation<Type>(global_object, *typed_array, move(atomic_exchange)));
+ return TRY(perform_atomic_operation<Type>(global_object, *typed_array, move(atomic_exchange)));
JS_ENUMERATE_TYPED_ARRAYS
#undef __JS_ENUMERATE
@@ -304,9 +304,9 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::exchange)
}
// 25.4.7 Atomics.isLockFree ( size ), https://tc39.es/ecma262/#sec-atomics.islockfree
-JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::is_lock_free)
+JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::is_lock_free)
{
- auto size = TRY_OR_DISCARD(vm.argument(0).to_integer_or_infinity(global_object));
+ auto size = TRY(vm.argument(0).to_integer_or_infinity(global_object));
if (size == 1)
return Value(AK::atomic_is_lock_free<u8>());
if (size == 2)
@@ -319,32 +319,30 @@ 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)
+JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::load)
{
- auto* typed_array = TRY_OR_DISCARD(typed_array_from(global_object, vm.argument(0)));
+ auto* typed_array = TRY(typed_array_from(global_object, vm.argument(0)));
- TRY_OR_DISCARD(validate_integer_typed_array(global_object, *typed_array));
+ TRY(validate_integer_typed_array(global_object, *typed_array));
- auto indexed_position = TRY_OR_DISCARD(validate_atomic_access(global_object, *typed_array, vm.argument(1)));
+ auto indexed_position = TRY(validate_atomic_access(global_object, *typed_array, vm.argument(1)));
- if (typed_array->viewed_array_buffer()->is_detached()) {
- vm.throw_exception<TypeError>(global_object, ErrorType::DetachedArrayBuffer);
- return {};
- }
+ if (typed_array->viewed_array_buffer()->is_detached())
+ return vm.throw_completion<TypeError>(global_object, ErrorType::DetachedArrayBuffer);
return typed_array->get_value_from_buffer(indexed_position, ArrayBuffer::Order::SeqCst, true);
}
// 25.4.9 Atomics.or ( typedArray, index, value ), https://tc39.es/ecma262/#sec-atomics.or
-JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::or_)
+JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::or_)
{
- auto* typed_array = TRY_OR_DISCARD(typed_array_from(global_object, vm.argument(0)));
+ auto* typed_array = TRY(typed_array_from(global_object, vm.argument(0)));
auto atomic_or = [](auto* storage, auto value) { return AK::atomic_fetch_or(storage, value); };
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, Type) \
if (is<ClassName>(typed_array)) \
- return TRY_OR_DISCARD(perform_atomic_operation<Type>(global_object, *typed_array, move(atomic_or)));
+ return TRY(perform_atomic_operation<Type>(global_object, *typed_array, move(atomic_or)));
JS_ENUMERATE_TYPED_ARRAYS
#undef __JS_ENUMERATE
@@ -352,40 +350,38 @@ 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)
+JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::store)
{
- auto* typed_array = TRY_OR_DISCARD(typed_array_from(global_object, vm.argument(0)));
+ auto* typed_array = TRY(typed_array_from(global_object, vm.argument(0)));
- TRY_OR_DISCARD(validate_integer_typed_array(global_object, *typed_array));
+ TRY(validate_integer_typed_array(global_object, *typed_array));
- auto indexed_position = TRY_OR_DISCARD(validate_atomic_access(global_object, *typed_array, vm.argument(1)));
+ auto indexed_position = TRY(validate_atomic_access(global_object, *typed_array, vm.argument(1)));
auto value = vm.argument(2);
Value value_to_set;
if (typed_array->content_type() == TypedArrayBase::ContentType::BigInt)
- value_to_set = TRY_OR_DISCARD(value.to_bigint(global_object));
+ value_to_set = TRY(value.to_bigint(global_object));
else
- value_to_set = Value(TRY_OR_DISCARD(value.to_integer_or_infinity(global_object)));
+ value_to_set = Value(TRY(value.to_integer_or_infinity(global_object)));
- if (typed_array->viewed_array_buffer()->is_detached()) {
- vm.throw_exception<TypeError>(global_object, ErrorType::DetachedArrayBuffer);
- return {};
- }
+ if (typed_array->viewed_array_buffer()->is_detached())
+ return vm.throw_completion<TypeError>(global_object, ErrorType::DetachedArrayBuffer);
typed_array->set_value_in_buffer(indexed_position, value_to_set, ArrayBuffer::Order::SeqCst, true);
return value_to_set;
}
// 25.4.11 Atomics.sub ( typedArray, index, value ), https://tc39.es/ecma262/#sec-atomics.sub
-JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::sub)
+JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::sub)
{
- auto* typed_array = TRY_OR_DISCARD(typed_array_from(global_object, vm.argument(0)));
+ auto* typed_array = TRY(typed_array_from(global_object, vm.argument(0)));
auto atomic_sub = [](auto* storage, auto value) { return AK::atomic_fetch_sub(storage, value); };
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, Type) \
if (is<ClassName>(typed_array)) \
- return TRY_OR_DISCARD(perform_atomic_operation<Type>(global_object, *typed_array, move(atomic_sub)));
+ return TRY(perform_atomic_operation<Type>(global_object, *typed_array, move(atomic_sub)));
JS_ENUMERATE_TYPED_ARRAYS
#undef __JS_ENUMERATE
@@ -393,15 +389,15 @@ 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_)
+JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::xor_)
{
- auto* typed_array = TRY_OR_DISCARD(typed_array_from(global_object, vm.argument(0)));
+ auto* typed_array = TRY(typed_array_from(global_object, vm.argument(0)));
auto atomic_xor = [](auto* storage, auto value) { return AK::atomic_fetch_xor(storage, value); };
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, Type) \
if (is<ClassName>(typed_array)) \
- return TRY_OR_DISCARD(perform_atomic_operation<Type>(global_object, *typed_array, move(atomic_xor)));
+ return TRY(perform_atomic_operation<Type>(global_object, *typed_array, move(atomic_xor)));
JS_ENUMERATE_TYPED_ARRAYS
#undef __JS_ENUMERATE
diff --git a/Userland/Libraries/LibJS/Runtime/AtomicsObject.h b/Userland/Libraries/LibJS/Runtime/AtomicsObject.h
index 84b891f396..2b55f9671b 100644
--- a/Userland/Libraries/LibJS/Runtime/AtomicsObject.h
+++ b/Userland/Libraries/LibJS/Runtime/AtomicsObject.h
@@ -19,16 +19,16 @@ public:
virtual ~AtomicsObject() override = default;
private:
- JS_DECLARE_OLD_NATIVE_FUNCTION(add);
- JS_DECLARE_OLD_NATIVE_FUNCTION(and_);
- JS_DECLARE_OLD_NATIVE_FUNCTION(compare_exchange);
- JS_DECLARE_OLD_NATIVE_FUNCTION(exchange);
- JS_DECLARE_OLD_NATIVE_FUNCTION(is_lock_free);
- JS_DECLARE_OLD_NATIVE_FUNCTION(load);
- JS_DECLARE_OLD_NATIVE_FUNCTION(or_);
- JS_DECLARE_OLD_NATIVE_FUNCTION(store);
- JS_DECLARE_OLD_NATIVE_FUNCTION(sub);
- JS_DECLARE_OLD_NATIVE_FUNCTION(xor_);
+ JS_DECLARE_NATIVE_FUNCTION(add);
+ JS_DECLARE_NATIVE_FUNCTION(and_);
+ JS_DECLARE_NATIVE_FUNCTION(compare_exchange);
+ JS_DECLARE_NATIVE_FUNCTION(exchange);
+ JS_DECLARE_NATIVE_FUNCTION(is_lock_free);
+ JS_DECLARE_NATIVE_FUNCTION(load);
+ JS_DECLARE_NATIVE_FUNCTION(or_);
+ JS_DECLARE_NATIVE_FUNCTION(store);
+ JS_DECLARE_NATIVE_FUNCTION(sub);
+ JS_DECLARE_NATIVE_FUNCTION(xor_);
};
}