1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 14:37:46 +00:00

LibJS: Implement Atomics.exchange

This commit is contained in:
Timothy Flynn 2021-07-12 08:08:13 -04:00 committed by Linus Groh
parent 7236584132
commit 655ffce64d
4 changed files with 95 additions and 0 deletions

View file

@ -119,6 +119,7 @@ void AtomicsObject::initialize(GlobalObject& global_object)
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.add, add, 3, attr);
define_native_function(vm.names.and_, and_, 3, attr);
define_native_function(vm.names.exchange, exchange, 3, 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);
@ -165,6 +166,24 @@ JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::and_)
VERIFY_NOT_REACHED();
}
// 25.4.6 Atomics.exchange ( typedArray, index, value ), https://tc39.es/ecma262/#sec-atomics.exchange
JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::exchange)
{
auto* typed_array = typed_array_from(global_object, vm.argument(0));
if (!typed_array)
return {};
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 perform_atomic_operation<Type>(global_object, *typed_array, move(atomic_exchange));
JS_ENUMERATE_TYPED_ARRAYS
#undef __JS_ENUMERATE
VERIFY_NOT_REACHED();
}
// 25.4.8 Atomics.load ( typedArray, index ), https://tc39.es/ecma262/#sec-atomics.load
JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::load)
{