2021-07-10 22:53:54 -04:00
/*
2022-01-31 13:07:22 -05:00
* Copyright ( c ) 2021 , Tim Flynn < trflynn89 @ serenityos . org >
2021-07-10 22:53:54 -04:00
*
* SPDX - License - Identifier : BSD - 2 - Clause
*/
2021-07-11 11:29:35 -04:00
# include <AK/Atomic.h>
# include <AK/ByteBuffer.h>
2021-07-12 08:59:34 -04:00
# include <AK/Endian.h>
2021-07-10 22:53:54 -04:00
# include <LibJS/Runtime/AtomicsObject.h>
# include <LibJS/Runtime/GlobalObject.h>
2021-07-11 11:29:35 -04:00
# include <LibJS/Runtime/TypedArray.h>
# include <LibJS/Runtime/Value.h>
2021-07-10 22:53:54 -04:00
namespace JS {
2021-07-11 11:29:35 -04:00
// 25.4.2.1 ValidateIntegerTypedArray ( typedArray [ , waitable ] ), https://tc39.es/ecma262/#sec-validateintegertypedarray
2021-10-03 21:08:22 +01:00
static ThrowCompletionOr < ArrayBuffer * > validate_integer_typed_array ( GlobalObject & global_object , TypedArrayBase & typed_array , bool waitable = false )
2021-07-11 11:29:35 -04:00
{
auto & vm = global_object . vm ( ) ;
2021-10-03 21:08:22 +01:00
// 1. If waitable is not present, set waitable to false.
2021-07-11 11:29:35 -04:00
2021-10-03 21:08:22 +01:00
// 2. Perform ? ValidateTypedArray(typedArray).
TRY ( validate_typed_array ( global_object , typed_array ) ) ;
// 3. Let buffer be typedArray.[[ViewedArrayBuffer]].
auto * buffer = typed_array . viewed_array_buffer ( ) ;
2022-02-08 22:22:03 +02:00
auto const & type_name = typed_array . element_name ( ) ;
2021-07-11 11:29:35 -04:00
2022-04-15 11:13:58 -04:00
// 4. If waitable is true, then
2021-07-11 11:29:35 -04:00
if ( waitable ) {
2022-04-15 11:13:58 -04:00
// a. If typedArray.[[TypedArrayName]] is not "Int32Array" or "BigInt64Array", throw a TypeError exception.
2022-02-08 22:22:03 +02:00
if ( ( type_name ! = vm . names . Int32Array . as_string ( ) ) & & ( type_name ! = vm . names . BigInt64Array . as_string ( ) ) )
2021-10-03 21:08:22 +01:00
return vm . throw_completion < TypeError > ( global_object , ErrorType : : TypedArrayTypeIsNot , type_name , " Int32 or BigInt64 " sv ) ;
}
2022-04-15 11:13:58 -04:00
// 5. Else,
2021-10-03 21:08:22 +01:00
else {
2022-04-15 11:13:58 -04:00
// a. Let type be TypedArrayElementType(typedArray).
2022-05-02 20:54:39 +02:00
// b. If IsUnclampedIntegerElementType(type) is false and IsBigIntElementType(type) is false, throw a TypeError exception.
2021-07-11 11:29:35 -04:00
if ( ! typed_array . is_unclamped_integer_element_type ( ) & & ! typed_array . is_bigint_element_type ( ) )
2021-10-03 21:08:22 +01:00
return vm . throw_completion < TypeError > ( global_object , ErrorType : : TypedArrayTypeIsNot , type_name , " an unclamped integer or BigInt " sv ) ;
2021-07-11 11:29:35 -04:00
}
2021-10-03 21:08:22 +01:00
2022-04-15 11:13:58 -04:00
// 6. Return buffer.
2021-10-03 21:08:22 +01:00
return buffer ;
2021-07-11 11:29:35 -04:00
}
// 25.4.2.2 ValidateAtomicAccess ( typedArray, requestIndex ), https://tc39.es/ecma262/#sec-validateatomicaccess
2021-10-03 21:12:16 +01:00
static ThrowCompletionOr < size_t > validate_atomic_access ( GlobalObject & global_object , TypedArrayBase & typed_array , Value request_index )
2021-07-11 11:29:35 -04:00
{
auto & vm = global_object . vm ( ) ;
2021-10-03 21:12:16 +01:00
// 1. Let length be typedArray.[[ArrayLength]].
auto length = typed_array . array_length ( ) ;
// 2. Let accessIndex be ? ToIndex(requestIndex).
2021-10-18 00:05:01 +03:00
auto access_index = TRY ( request_index . to_index ( global_object ) ) ;
2021-07-11 11:29:35 -04:00
2021-10-03 21:12:16 +01:00
// 3. Assert: accessIndex ≥ 0.
// 4. If accessIndex ≥ length, throw a RangeError exception.
if ( access_index > = length )
return vm . throw_completion < RangeError > ( global_object , ErrorType : : IndexOutOfRange , access_index , typed_array . array_length ( ) ) ;
2021-07-11 11:29:35 -04:00
2022-04-15 11:13:58 -04:00
// 5. Let elementSize be TypedArrayElementSize(typedArray).
2021-10-03 21:12:16 +01:00
auto element_size = typed_array . element_size ( ) ;
2022-04-15 11:13:58 -04:00
// 6. Let offset be typedArray.[[ByteOffset]].
2021-10-03 21:12:16 +01:00
auto offset = typed_array . byte_offset ( ) ;
2022-04-15 11:13:58 -04:00
// 7. Return (accessIndex × elementSize) + offset.
2021-10-03 21:12:16 +01:00
return ( access_index * element_size ) + offset ;
2021-07-11 11:29:35 -04:00
}
// 25.4.2.11 AtomicReadModifyWrite ( typedArray, index, value, op ), https://tc39.es/ecma262/#sec-atomicreadmodifywrite
2021-10-03 21:21:13 +01:00
static ThrowCompletionOr < Value > atomic_read_modify_write ( GlobalObject & global_object , TypedArrayBase & typed_array , Value index , Value value , ReadWriteModifyFunction operation )
2021-07-11 11:29:35 -04:00
{
auto & vm = global_object . vm ( ) ;
2021-10-03 21:21:13 +01:00
// 1. Let buffer be ? ValidateIntegerTypedArray(typedArray).
auto * buffer = TRY ( validate_integer_typed_array ( global_object , typed_array ) ) ;
2021-07-11 11:29:35 -04:00
2021-10-03 21:21:13 +01:00
// 2. Let indexedPosition be ? ValidateAtomicAccess(typedArray, index).
auto indexed_position = TRY ( validate_atomic_access ( global_object , typed_array , index ) ) ;
2021-07-11 11:29:35 -04:00
Value value_to_set ;
2021-10-03 21:21:13 +01:00
2022-04-15 11:13:58 -04:00
// 3. If typedArray.[[ContentType]] is BigInt, let v be ? ToBigInt(value).
2021-10-18 19:19:57 +01:00
if ( typed_array . content_type ( ) = = TypedArrayBase : : ContentType : : BigInt )
2021-10-16 21:52:16 +03:00
value_to_set = TRY ( value . to_bigint ( global_object ) ) ;
2022-04-15 11:13:58 -04:00
// 4. Otherwise, let v be 𝔽 (? ToIntegerOrInfinity(value)).
2021-10-18 19:19:57 +01:00
else
value_to_set = Value ( TRY ( value . to_integer_or_infinity ( global_object ) ) ) ;
2021-07-11 11:29:35 -04:00
2022-04-15 11:13:58 -04:00
// 5. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
2021-10-03 21:21:13 +01:00
if ( buffer - > is_detached ( ) )
return vm . throw_completion < TypeError > ( global_object , ErrorType : : DetachedArrayBuffer ) ;
2022-04-15 11:13:58 -04:00
// 6. NOTE: The above check is not redundant with the check in ValidateIntegerTypedArray because the call to ToBigInt or ToIntegerOrInfinity on the preceding lines can have arbitrary side effects, which could cause the buffer to become detached.
2021-07-11 11:29:35 -04:00
2022-04-15 11:13:58 -04:00
// 7. Let elementType be TypedArrayElementType(typedArray).
// 8. Return GetModifySetValueInBuffer(buffer, indexedPosition, elementType, v, op).
2021-10-03 21:21:13 +01:00
return typed_array . get_modify_set_value_in_buffer ( indexed_position , value_to_set , move ( operation ) ) ;
2021-07-11 11:29:35 -04:00
}
template < typename T , typename AtomicFunction >
2021-10-03 21:22:38 +01:00
static ThrowCompletionOr < Value > perform_atomic_operation ( GlobalObject & global_object , TypedArrayBase & typed_array , AtomicFunction & & operation )
2021-07-11 11:29:35 -04:00
{
auto & vm = global_object . vm ( ) ;
auto index = vm . argument ( 1 ) ;
auto value = vm . argument ( 2 ) ;
auto operation_wrapper = [ & , operation = forward < AtomicFunction > ( operation ) ] ( ByteBuffer x_bytes , ByteBuffer y_bytes ) - > ByteBuffer {
if constexpr ( IsFloatingPoint < T > ) {
VERIFY_NOT_REACHED ( ) ;
} else {
using U = Conditional < IsSame < ClampedU8 , T > , u8 , T > ;
auto * x = reinterpret_cast < U * > ( x_bytes . data ( ) ) ;
auto * y = reinterpret_cast < U * > ( y_bytes . data ( ) ) ;
operation ( x , * y ) ;
return x_bytes ;
}
} ;
2021-10-03 21:22:38 +01:00
return atomic_read_modify_write ( global_object , typed_array , index , value , move ( operation_wrapper ) ) ;
2021-07-11 11:29:35 -04:00
}
2021-07-10 22:53:54 -04:00
AtomicsObject : : AtomicsObject ( GlobalObject & global_object )
: Object ( * global_object . object_prototype ( ) )
{
}
void AtomicsObject : : initialize ( GlobalObject & global_object )
{
Object : : initialize ( global_object ) ;
auto & vm = this - > vm ( ) ;
2021-07-11 11:29:35 -04:00
u8 attr = Attribute : : Writable | Attribute : : Configurable ;
2021-10-23 13:58:25 -04:00
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 ) ;
2021-07-11 11:29:35 -04:00
2021-07-10 22:53:54 -04:00
// 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 ) ;
}
2021-07-11 11:29:35 -04:00
// 25.4.3 Atomics.add ( typedArray, index, value ), https://tc39.es/ecma262/#sec-atomics.add
2021-10-23 13:58:25 -04:00
JS_DEFINE_NATIVE_FUNCTION ( AtomicsObject : : add )
2021-07-11 11:29:35 -04:00
{
2021-10-23 13:58:25 -04:00
auto * typed_array = TRY ( typed_array_from ( global_object , vm . argument ( 0 ) ) ) ;
2021-07-11 11:29:35 -04:00
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 ) ) \
2021-10-23 13:58:25 -04:00
return TRY ( perform_atomic_operation < Type > ( global_object , * typed_array , move ( atomic_add ) ) ) ;
2021-07-11 11:29:35 -04:00
JS_ENUMERATE_TYPED_ARRAYS
# undef __JS_ENUMERATE
VERIFY_NOT_REACHED ( ) ;
}
2021-07-11 14:10:43 -04:00
// 25.4.4 Atomics.and ( typedArray, index, value ), https://tc39.es/ecma262/#sec-atomics.and
2021-10-23 13:58:25 -04:00
JS_DEFINE_NATIVE_FUNCTION ( AtomicsObject : : and_ )
2021-07-11 14:10:43 -04:00
{
2021-10-23 13:58:25 -04:00
auto * typed_array = TRY ( typed_array_from ( global_object , vm . argument ( 0 ) ) ) ;
2021-07-11 14:10:43 -04:00
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 ) ) \
2021-10-23 13:58:25 -04:00
return TRY ( perform_atomic_operation < Type > ( global_object , * typed_array , move ( atomic_and ) ) ) ;
2021-07-11 14:10:43 -04:00
JS_ENUMERATE_TYPED_ARRAYS
# undef __JS_ENUMERATE
VERIFY_NOT_REACHED ( ) ;
}
2021-10-03 21:31:17 +01:00
// Implementation of 25.4.5 Atomics.compareExchange ( typedArray, index, expectedValue, replacementValue ), https://tc39.es/ecma262/#sec-atomics.compareexchange
2021-07-12 08:59:34 -04:00
template < typename T >
2021-10-03 21:31:17 +01:00
static ThrowCompletionOr < Value > atomic_compare_exchange_impl ( GlobalObject & global_object , TypedArrayBase & typed_array )
2021-07-12 08:59:34 -04:00
{
auto & vm = global_object . vm ( ) ;
2021-10-03 21:31:17 +01:00
// 1. Let buffer be ? ValidateIntegerTypedArray(typedArray).
auto * buffer = TRY ( validate_integer_typed_array ( global_object , typed_array ) ) ;
// 2. Let block be buffer.[[ArrayBufferData]].
auto & block = buffer - > buffer ( ) ;
2021-07-12 08:59:34 -04:00
2021-10-03 21:31:17 +01:00
// 3. Let indexedPosition be ? ValidateAtomicAccess(typedArray, index).
auto indexed_position = TRY ( validate_atomic_access ( global_object , typed_array , vm . argument ( 1 ) ) ) ;
2021-07-12 08:59:34 -04:00
Value expected ;
Value replacement ;
2021-10-03 21:31:17 +01:00
2022-04-15 11:13:58 -04:00
// 4. If typedArray.[[ContentType]] is BigInt, then
2021-07-12 08:59:34 -04:00
if ( typed_array . content_type ( ) = = TypedArrayBase : : ContentType : : BigInt ) {
2021-10-03 21:31:17 +01:00
// a. Let expected be ? ToBigInt(expectedValue).
2021-10-16 21:52:16 +03:00
expected = TRY ( vm . argument ( 2 ) . to_bigint ( global_object ) ) ;
2021-10-03 21:31:17 +01:00
// b. Let replacement be ? ToBigInt(replacementValue).
2021-10-16 21:52:16 +03:00
replacement = TRY ( vm . argument ( 3 ) . to_bigint ( global_object ) ) ;
2021-10-03 21:31:17 +01:00
}
2022-04-15 11:13:58 -04:00
// 5. Else,
2021-10-03 21:31:17 +01:00
else {
// a. Let expected be 𝔽 (? ToIntegerOrInfinity(expectedValue)).
2021-10-18 19:19:57 +01:00
expected = Value ( TRY ( vm . argument ( 2 ) . to_integer_or_infinity ( global_object ) ) ) ;
2021-10-03 21:31:17 +01:00
// b. Let replacement be 𝔽 (? ToIntegerOrInfinity(replacementValue)).
2021-10-18 19:19:57 +01:00
replacement = Value ( TRY ( vm . argument ( 3 ) . to_integer_or_infinity ( global_object ) ) ) ;
2021-07-12 08:59:34 -04:00
}
2022-04-15 11:13:58 -04:00
// 6. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
2021-10-03 21:31:17 +01:00
if ( buffer - > is_detached ( ) )
return vm . template throw_completion < TypeError > ( global_object , ErrorType : : DetachedArrayBuffer ) ;
2021-07-12 08:59:34 -04:00
2022-04-15 11:13:58 -04:00
// 7. NOTE: The above check is not redundant with the check in ValidateIntegerTypedArray because the call to ToBigInt or ToIntegerOrInfinity on the preceding lines can have arbitrary side effects, which could cause the buffer to become detached.
2021-10-03 21:31:17 +01:00
2022-04-15 11:13:58 -04:00
// 8. Let elementType be TypedArrayElementType(typedArray).
// 9. Let elementSize be TypedArrayElementSize(typedArray).
2021-10-03 21:31:17 +01:00
2022-04-15 11:13:58 -04:00
// 10. Let isLittleEndian be the value of the [[LittleEndian]] field of the surrounding agent's Agent Record.
2021-07-12 08:59:34 -04:00
constexpr bool is_little_endian = __BYTE_ORDER__ = = __ORDER_LITTLE_ENDIAN__ ;
2022-04-15 11:13:58 -04:00
// 11. Let expectedBytes be NumericToRawBytes(elementType, expected, isLittleEndian).
2021-07-12 08:59:34 -04:00
auto expected_bytes = numeric_to_raw_bytes < T > ( global_object , expected , is_little_endian ) ;
2021-10-03 21:31:17 +01:00
2022-04-15 11:13:58 -04:00
// 12. Let replacementBytes be NumericToRawBytes(elementType, replacement, isLittleEndian).
2021-07-12 08:59:34 -04:00
auto replacement_bytes = numeric_to_raw_bytes < T > ( global_object , replacement , is_little_endian ) ;
// FIXME: Implement SharedArrayBuffer case.
2022-04-15 11:13:58 -04:00
// 13. If IsSharedArrayBuffer(buffer) is true, then
2021-10-03 21:31:17 +01:00
// a-i.
2022-04-15 11:13:58 -04:00
// 14. Else,
2021-07-12 08:59:34 -04:00
2021-10-03 21:31:17 +01:00
// a. Let rawBytesRead be a List of length elementSize whose elements are the sequence of elementSize bytes starting with block[indexedPosition].
2021-10-03 21:12:16 +01:00
auto raw_bytes_read = block . slice ( indexed_position , sizeof ( T ) ) ;
2021-07-12 08:59:34 -04:00
2021-10-03 21:31:17 +01:00
// b. If ByteListEqual(rawBytesRead, expectedBytes) is true, then
// i. Store the individual bytes of replacementBytes into block, starting at block[indexedPosition].
2021-07-12 08:59:34 -04:00
if constexpr ( IsFloatingPoint < T > ) {
VERIFY_NOT_REACHED ( ) ;
} else {
using U = Conditional < IsSame < ClampedU8 , T > , u8 , T > ;
2021-10-03 21:12:16 +01:00
auto * v = reinterpret_cast < U * > ( block . span ( ) . slice ( indexed_position ) . data ( ) ) ;
2021-07-12 08:59:34 -04:00
auto * e = reinterpret_cast < U * > ( expected_bytes . data ( ) ) ;
auto * r = reinterpret_cast < U * > ( replacement_bytes . data ( ) ) ;
( void ) AK : : atomic_compare_exchange_strong ( v , * e , * r ) ;
}
2022-04-15 11:13:58 -04:00
// 15. Return RawBytesToNumeric(elementType, rawBytesRead, isLittleEndian).
2021-07-12 08:59:34 -04:00
return raw_bytes_to_numeric < T > ( global_object , raw_bytes_read , is_little_endian ) ;
}
// 25.4.5 Atomics.compareExchange ( typedArray, index, expectedValue, replacementValue ), https://tc39.es/ecma262/#sec-atomics.compareexchange
2021-10-23 13:58:25 -04:00
JS_DEFINE_NATIVE_FUNCTION ( AtomicsObject : : compare_exchange )
2021-07-12 08:59:34 -04:00
{
2021-10-23 13:58:25 -04:00
auto * typed_array = TRY ( typed_array_from ( global_object , vm . argument ( 0 ) ) ) ;
2021-07-12 08:59:34 -04:00
# define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, Type) \
if ( is < ClassName > ( typed_array ) ) \
2021-10-23 13:58:25 -04:00
return TRY ( atomic_compare_exchange_impl < Type > ( global_object , * typed_array ) ) ;
2021-07-12 08:59:34 -04:00
JS_ENUMERATE_TYPED_ARRAYS
# undef __JS_ENUMERATE
VERIFY_NOT_REACHED ( ) ;
}
2021-07-12 08:08:13 -04:00
// 25.4.6 Atomics.exchange ( typedArray, index, value ), https://tc39.es/ecma262/#sec-atomics.exchange
2021-10-23 13:58:25 -04:00
JS_DEFINE_NATIVE_FUNCTION ( AtomicsObject : : exchange )
2021-07-12 08:08:13 -04:00
{
2021-10-23 13:58:25 -04:00
auto * typed_array = TRY ( typed_array_from ( global_object , vm . argument ( 0 ) ) ) ;
2021-07-12 08:08:13 -04:00
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 ) ) \
2021-10-23 13:58:25 -04:00
return TRY ( perform_atomic_operation < Type > ( global_object , * typed_array , move ( atomic_exchange ) ) ) ;
2021-07-12 08:08:13 -04:00
JS_ENUMERATE_TYPED_ARRAYS
# undef __JS_ENUMERATE
VERIFY_NOT_REACHED ( ) ;
}
2021-07-12 10:35:14 -04:00
// 25.4.7 Atomics.isLockFree ( size ), https://tc39.es/ecma262/#sec-atomics.islockfree
2021-10-23 13:58:25 -04:00
JS_DEFINE_NATIVE_FUNCTION ( AtomicsObject : : is_lock_free )
2021-07-12 10:35:14 -04:00
{
2021-10-23 13:58:25 -04:00
auto size = TRY ( vm . argument ( 0 ) . to_integer_or_infinity ( global_object ) ) ;
2021-07-12 10:35:14 -04:00
if ( size = = 1 )
return Value ( AK : : atomic_is_lock_free < u8 > ( ) ) ;
if ( size = = 2 )
return Value ( AK : : atomic_is_lock_free < u16 > ( ) ) ;
if ( size = = 4 )
return Value ( true ) ;
if ( size = = 8 )
return Value ( AK : : atomic_is_lock_free < u64 > ( ) ) ;
return Value ( false ) ;
}
2021-07-11 11:45:21 -04:00
// 25.4.8 Atomics.load ( typedArray, index ), https://tc39.es/ecma262/#sec-atomics.load
2021-10-23 13:58:25 -04:00
JS_DEFINE_NATIVE_FUNCTION ( AtomicsObject : : load )
2021-07-11 11:45:21 -04:00
{
2022-04-15 11:13:58 -04:00
// 1. Let buffer be ? ValidateIntegerTypedArray(typedArray).
2021-10-23 13:58:25 -04:00
auto * typed_array = TRY ( typed_array_from ( global_object , vm . argument ( 0 ) ) ) ;
TRY ( validate_integer_typed_array ( global_object , * typed_array ) ) ;
2021-07-11 11:45:21 -04:00
2022-04-15 11:13:58 -04:00
// 2. Let indexedPosition be ? ValidateAtomicAccess(typedArray, index).
2021-10-23 13:58:25 -04:00
auto indexed_position = TRY ( validate_atomic_access ( global_object , * typed_array , vm . argument ( 1 ) ) ) ;
2021-07-11 11:45:21 -04:00
2022-04-15 11:13:58 -04:00
// 3. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
2021-10-23 13:58:25 -04:00
if ( typed_array - > viewed_array_buffer ( ) - > is_detached ( ) )
return vm . throw_completion < TypeError > ( global_object , ErrorType : : DetachedArrayBuffer ) ;
2021-07-11 11:45:21 -04:00
2022-04-15 11:13:58 -04:00
// 4. NOTE: The above check is not redundant with the check in ValidateIntegerTypedArray because the call to ValidateAtomicAccess on the preceding line can have arbitrary side effects, which could cause the buffer to become detached.
// 5. Let elementType be TypedArrayElementType(typedArray).
// 6. Return GetValueFromBuffer(buffer, indexedPosition, elementType, true, SeqCst).
2021-10-03 21:12:16 +01:00
return typed_array - > get_value_from_buffer ( indexed_position , ArrayBuffer : : Order : : SeqCst , true ) ;
2021-07-11 11:45:21 -04:00
}
2021-07-11 13:20:28 -04:00
// 25.4.9 Atomics.or ( typedArray, index, value ), https://tc39.es/ecma262/#sec-atomics.or
2021-10-23 13:58:25 -04:00
JS_DEFINE_NATIVE_FUNCTION ( AtomicsObject : : or_ )
2021-07-11 13:20:28 -04:00
{
2021-10-23 13:58:25 -04:00
auto * typed_array = TRY ( typed_array_from ( global_object , vm . argument ( 0 ) ) ) ;
2021-07-11 13:20:28 -04:00
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 ) ) \
2021-10-23 13:58:25 -04:00
return TRY ( perform_atomic_operation < Type > ( global_object , * typed_array , move ( atomic_or ) ) ) ;
2021-07-11 13:20:28 -04:00
JS_ENUMERATE_TYPED_ARRAYS
# undef __JS_ENUMERATE
VERIFY_NOT_REACHED ( ) ;
}
2021-07-11 14:37:54 -04:00
// 25.4.10 Atomics.store ( typedArray, index, value ), https://tc39.es/ecma262/#sec-atomics.store
2021-10-23 13:58:25 -04:00
JS_DEFINE_NATIVE_FUNCTION ( AtomicsObject : : store )
2021-07-11 14:37:54 -04:00
{
2022-04-15 11:13:58 -04:00
// 1. Let buffer be ? ValidateIntegerTypedArray(typedArray).
2021-10-23 13:58:25 -04:00
auto * typed_array = TRY ( typed_array_from ( global_object , vm . argument ( 0 ) ) ) ;
TRY ( validate_integer_typed_array ( global_object , * typed_array ) ) ;
2021-07-11 14:37:54 -04:00
2022-04-15 11:13:58 -04:00
// 2. Let indexedPosition be ? ValidateAtomicAccess(typedArray, index).
2021-10-23 13:58:25 -04:00
auto indexed_position = TRY ( validate_atomic_access ( global_object , * typed_array , vm . argument ( 1 ) ) ) ;
2021-07-11 14:37:54 -04:00
auto value = vm . argument ( 2 ) ;
Value value_to_set ;
2022-04-15 11:13:58 -04:00
// 3. If typedArray.[[ContentType]] is BigInt, let v be ? ToBigInt(value).
2021-10-18 19:19:57 +01:00
if ( typed_array - > content_type ( ) = = TypedArrayBase : : ContentType : : BigInt )
2021-10-23 13:58:25 -04:00
value_to_set = TRY ( value . to_bigint ( global_object ) ) ;
2022-04-15 11:13:58 -04:00
// 4. Otherwise, let v be 𝔽 (? ToIntegerOrInfinity(value)).
2021-10-18 19:19:57 +01:00
else
2021-10-23 13:58:25 -04:00
value_to_set = Value ( TRY ( value . to_integer_or_infinity ( global_object ) ) ) ;
2021-07-11 14:37:54 -04:00
2022-04-15 11:13:58 -04:00
// 5. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
2021-10-23 13:58:25 -04:00
if ( typed_array - > viewed_array_buffer ( ) - > is_detached ( ) )
return vm . throw_completion < TypeError > ( global_object , ErrorType : : DetachedArrayBuffer ) ;
2021-07-11 14:37:54 -04:00
2022-04-15 11:13:58 -04:00
// 6. NOTE: The above check is not redundant with the check in ValidateIntegerTypedArray because the call to ToBigInt or ToIntegerOrInfinity on the preceding lines can have arbitrary side effects, which could cause the buffer to become detached.
// 7. Let elementType be TypedArrayElementType(typedArray).
// 8. Perform SetValueInBuffer(buffer, indexedPosition, elementType, v, true, SeqCst).
2021-10-03 21:12:16 +01:00
typed_array - > set_value_in_buffer ( indexed_position , value_to_set , ArrayBuffer : : Order : : SeqCst , true ) ;
2022-04-15 11:13:58 -04:00
// 9. Return v.
2021-07-11 14:37:54 -04:00
return value_to_set ;
}
2021-07-11 13:28:52 -04:00
// 25.4.11 Atomics.sub ( typedArray, index, value ), https://tc39.es/ecma262/#sec-atomics.sub
2021-10-23 13:58:25 -04:00
JS_DEFINE_NATIVE_FUNCTION ( AtomicsObject : : sub )
2021-07-11 13:28:52 -04:00
{
2021-10-23 13:58:25 -04:00
auto * typed_array = TRY ( typed_array_from ( global_object , vm . argument ( 0 ) ) ) ;
2021-07-11 13:28:52 -04:00
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 ) ) \
2021-10-23 13:58:25 -04:00
return TRY ( perform_atomic_operation < Type > ( global_object , * typed_array , move ( atomic_sub ) ) ) ;
2021-07-11 13:28:52 -04:00
JS_ENUMERATE_TYPED_ARRAYS
# undef __JS_ENUMERATE
VERIFY_NOT_REACHED ( ) ;
}
2021-07-11 13:36:40 -04:00
// 25.4.14 Atomics.xor ( typedArray, index, value ), https://tc39.es/ecma262/#sec-atomics.xor
2021-10-23 13:58:25 -04:00
JS_DEFINE_NATIVE_FUNCTION ( AtomicsObject : : xor_ )
2021-07-11 13:36:40 -04:00
{
2021-10-23 13:58:25 -04:00
auto * typed_array = TRY ( typed_array_from ( global_object , vm . argument ( 0 ) ) ) ;
2021-07-11 13:36:40 -04:00
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 ) ) \
2021-10-23 13:58:25 -04:00
return TRY ( perform_atomic_operation < Type > ( global_object , * typed_array , move ( atomic_xor ) ) ) ;
2021-07-11 13:36:40 -04:00
JS_ENUMERATE_TYPED_ARRAYS
# undef __JS_ENUMERATE
VERIFY_NOT_REACHED ( ) ;
}
2021-07-10 22:53:54 -04:00
}