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

LibJS: Implement Atomics.load

This commit is contained in:
Timothy Flynn 2021-07-11 11:45:21 -04:00 committed by Linus Groh
parent cc3b96743a
commit 940875c9fd
4 changed files with 80 additions and 0 deletions

View file

@ -118,6 +118,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.load, load, 2, 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);
@ -141,4 +142,27 @@ JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::add)
VERIFY_NOT_REACHED();
}
// 25.4.8 Atomics.load ( typedArray, index ), https://tc39.es/ecma262/#sec-atomics.load
JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::load)
{
auto* typed_array = typed_array_from(global_object, vm.argument(0));
if (!typed_array)
return {};
validate_integer_typed_array(global_object, *typed_array);
if (vm.exception())
return {};
auto indexed_position = validate_atomic_access(global_object, *typed_array, vm.argument(1));
if (!indexed_position.has_value())
return {};
if (typed_array->viewed_array_buffer()->is_detached()) {
vm.throw_exception<TypeError>(global_object, ErrorType::DetachedArrayBuffer);
return {};
}
return typed_array->get_value_from_buffer(*indexed_position, ArrayBuffer::Order::SeqCst, true);
}
}