Implement operations of atomics (#5166)

The following methods were implemented:
 - Atomics.compareExchange
 - Atomics.store

The implementation is based on PR #4956, only resolved the conflicts.

Co-authored-by: Csaba Repasi repasics@inf.u-szeged.hu
JerryScript-DCO-1.0-Signed-off-by: Gergo Csizi gergocs@inf.u-szeged.hu
This commit is contained in:
Gergo Csizi
2024-11-26 10:56:28 +01:00
committed by GitHub
parent dfa9afbf6e
commit 7db6a9a372
12 changed files with 725 additions and 180 deletions
@@ -253,9 +253,10 @@ ECMA_ERROR_DEF (ECMA_ERR_MODULE_CANNOT_BE_INSTANTIATED, "Module cannot be instan
#endif /* JERRY_MODULE_SYSTEM */
ECMA_ERROR_DEF (ECMA_ERR_SPECIES_MUST_BE_A_CONSTRUCTOR, "Species must be a constructor")
ECMA_ERROR_DEF (ECMA_ERR_ARGUMENT_IS_NOT_A_PROXY, "Argument is not a Proxy object")
#if JERRY_BUILTIN_TYPEDARRAY
#if JERRY_BUILTIN_ATOMICS || JERRY_BUILTIN_TYPEDARRAY
ECMA_ERROR_DEF (ECMA_ERR_ARGUMENT_NOT_ARRAY_BUFFER, "Argument is not an ArrayBuffer")
#endif /* JERRY_BUILTIN_TYPEDARRAY */
#endif /* JERRY_BUILTIN_ATOMICS \
|| JERRY_BUILTIN_TYPEDARRAY */
#if JERRY_BUILTIN_CONTAINER
ECMA_ERROR_DEF (ECMA_ERR_CONSTRUCTOR_MAP_REQUIRES_NEW, "Constructor Map requires 'new'")
ECMA_ERROR_DEF (ECMA_ERR_CONSTRUCTOR_SET_REQUIRES_NEW, "Constructor Set requires 'new'")
@@ -330,9 +331,6 @@ ECMA_ERROR_DEF (ECMA_ERR_STATIC_SNAPSHOTS_ARE_NOT_ENABLED, "Static snapshots are
ECMA_ERROR_DEF (ECMA_ERR_WEAKREF_TARGET_MUST_BE_AN_OBJECT, "WeakRef target must be an object")
#endif /* JERRY_BUILTIN_WEAKREF */
ECMA_ERROR_DEF (ECMA_ERR_ARGUMENT_THIS_NOT_FUNCTION, "Argument 'this' is not a function")
#if JERRY_BUILTIN_ATOMICS
ECMA_ERROR_DEF (ECMA_ERR_ARGUMENT_NOT_SHARED_ARRAY_BUFFER, "Argument is not SharedArrayBuffer")
#endif /* JERRY_BUILTIN_ATOMICS */
#if JERRY_BUILTIN_ARRAY || JERRY_BUILTIN_CONTAINER || JERRY_BUILTIN_TYPEDARRAY
ECMA_ERROR_DEF (ECMA_ERR_CALLBACK_IS_NOT_CALLABLE, "Callback function is not callable")
#endif /* JERRY_BUILTIN_ARRAY \
@@ -508,8 +506,11 @@ ECMA_ERROR_DEF (ECMA_ERR_ARGUMENT_THIS_NOT_GENERATOR_OBJECT, "Argument 'this' is
ECMA_ERROR_DEF (ECMA_ERR_ARGUMENT_CANNOT_CONVERT_TO_OBJECT, "Argument cannot be converted to an object")
#if JERRY_BUILTIN_BIGINT
ECMA_ERROR_DEF (ECMA_ERR_ALLOCATE_BIGINT_VALUE, "Cannot allocate memory for a BigInt value")
ECMA_ERROR_DEF (ECMA_ERR_CONVERT_BIGINT_TO_NUMBER, "Cannot convert a BigInt value to a number")
#endif /* JERRY_BUILTIN_BIGINT */
#if JERRY_BUILTIN_ATOMICS || JERRY_BUILTIN_BIGINT
ECMA_ERROR_DEF (ECMA_ERR_CONVERT_BIGINT_TO_NUMBER, "Cannot convert a BigInt value to a number")
#endif /* JERRY_BUILTIN_ATOMICS \
|| JERRY_BUILTIN_BIGINT */
ECMA_ERROR_DEF (ECMA_ERR_CONVERT_SYMBOL_TO_NUMBER, "Cannot convert a Symbol value to a number")
ECMA_ERROR_DEF (ECMA_ERR_CONVERT_SYMBOL_TO_STRING, "Cannot convert a Symbol value to a string")
#if JERRY_BUILTIN_BIGINT && JERRY_BUILTIN_TYPEDARRAY
@@ -40,7 +40,6 @@ ECMA_ERR_ARGUMENT_THIS_NOT_ITERATOR = "Argument 'this' is not an iterator"
ECMA_ERR_ARGUMENT_THIS_NOT_OBJECT = "Argument 'this' is not an object"
ECMA_ERR_ARGUMENT_THIS_NOT_SYMBOL = "Argument 'this' must be a Symbol"
ECMA_ERR_ARGUMENT_CANNOT_CONVERT_TO_OBJECT = "Argument cannot be converted to an object"
ECMA_ERR_ARGUMENT_NOT_SHARED_ARRAY_BUFFER = "Argument is not SharedArrayBuffer"
ECMA_ERR_ARGUMENT_NOT_ARRAY_BUFFER = "Argument is not an ArrayBuffer"
ECMA_ERR_ARGUMENT_NOT_SUPPORTED = "Argument is not supported"
ECMA_ERR_ARRAY_BUFFER_DETACHED = "ArrayBuffer has already been detached"
@@ -13,10 +13,17 @@
* limitations under the License.
*/
#include "ecma-arraybuffer-object.h"
#include "ecma-atomics-object.h"
#include "ecma-bigint.h"
#include "ecma-builtins.h"
#include "ecma-errors.h"
#include "ecma-exceptions.h"
#include "ecma-gc.h"
#include "ecma-globals.h"
#include "ecma-helpers.h"
#include "ecma-shared-arraybuffer-object.h"
#include "ecma-typedarray-object.h"
#include "jrt.h"
@@ -66,10 +73,50 @@ enum
* @{
*/
/**
* Convert ecma_number to the appropriate type according to the element type of typedarray.
*/
static ecma_value_t
ecma_convert_number_to_typed_array_type (ecma_number_t num, /**< ecma_number argument */
ecma_typedarray_type_t element_type) /**< element type of typedarray */
{
uint32_t value = ecma_typedarray_setter_number_to_uint32 (num);
switch (element_type)
{
case ECMA_INT8_ARRAY:
{
return ecma_make_number_value ((int8_t) value);
}
case ECMA_UINT8_ARRAY:
{
return ecma_make_number_value ((uint8_t) value);
}
case ECMA_INT16_ARRAY:
{
return ecma_make_number_value ((int16_t) value);
}
case ECMA_UINT16_ARRAY:
{
return ecma_make_number_value ((uint16_t) value);
}
case ECMA_INT32_ARRAY:
{
return ecma_make_number_value ((int32_t) value);
}
default:
{
JERRY_ASSERT (element_type == ECMA_UINT32_ARRAY);
return ecma_make_number_value (value);
}
}
} /* ecma_convert_number_to_typed_array_type */
/**
* The Atomics object's 'compareExchange' routine
*
* See also: ES11 24.4.4
* See also: ES12 25.4.4
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
@@ -80,12 +127,124 @@ ecma_builtin_atomics_compare_exchange (ecma_value_t typedarray, /**< typedArray
ecma_value_t expected_value, /**< expectedValue argument */
ecma_value_t replacement_value) /**< replacementValue argument*/
{
JERRY_UNUSED (typedarray);
JERRY_UNUSED (index);
JERRY_UNUSED (expected_value);
JERRY_UNUSED (replacement_value);
ecma_value_t buffer = ecma_validate_integer_typedarray (typedarray, false);
return ecma_make_uint32_value (0);
if (ECMA_IS_VALUE_ERROR (buffer))
{
return buffer;
}
uint32_t idx = ecma_validate_atomic_access (typedarray, index);
if (idx == ECMA_STRING_NOT_ARRAY_INDEX)
{
return ECMA_VALUE_ERROR;
}
ecma_object_t *typedarray_p = ecma_get_object_from_value (typedarray);
ecma_typedarray_info_t target_info = ecma_typedarray_get_info (typedarray_p);
if (ECMA_ARRAYBUFFER_LAZY_ALLOC (target_info.array_buffer_p))
{
return ECMA_VALUE_ERROR;
}
ecma_typedarray_type_t element_type = target_info.id;
ecma_value_t expected;
ecma_value_t replacement;
#if JERRY_BUILTIN_BIGINT
if (ECMA_TYPEDARRAY_IS_BIGINT_TYPE (element_type))
{
expected = ecma_bigint_to_bigint (expected_value, false);
if (ECMA_IS_VALUE_ERROR (expected))
{
return expected;
}
if (element_type == ECMA_BIGUINT64_ARRAY)
{
uint64_t num;
bool sign;
ecma_bigint_get_digits_and_sign (expected, &num, 1, &sign);
if (sign)
{
num = (uint64_t) (-(int64_t) num);
}
if (expected != ECMA_BIGINT_ZERO)
{
ecma_deref_bigint (ecma_get_extended_primitive_from_value (expected));
}
expected = ecma_bigint_create_from_digits (&num, 1, false);
}
replacement = ecma_bigint_to_bigint (replacement_value, false);
if (ECMA_IS_VALUE_ERROR (replacement))
{
ecma_free_value (expected);
return replacement;
}
}
else
#endif /* JERRY_BUILTIN_BIGINT */
{
ecma_number_t tmp_exp;
ecma_number_t tmp_rep;
expected = ecma_op_to_integer (expected_value, &tmp_exp);
if (ECMA_IS_VALUE_ERROR (expected))
{
return expected;
}
expected = ecma_convert_number_to_typed_array_type (tmp_exp, element_type);
replacement = ecma_op_to_integer (replacement_value, &tmp_rep);
if (ECMA_IS_VALUE_ERROR (replacement))
{
ecma_free_value (expected);
return replacement;
}
replacement = ecma_make_number_value (tmp_rep);
}
uint8_t element_size = target_info.element_size;
uint32_t offset = target_info.offset;
uint32_t indexed_position = idx * element_size + offset;
if (ecma_arraybuffer_is_detached (ecma_get_object_from_value (buffer)))
{
ecma_free_value (expected);
ecma_free_value (replacement);
return ecma_raise_type_error (ECMA_ERR_ARRAYBUFFER_IS_DETACHED);
}
ecma_typedarray_getter_fn_t typedarray_getter_cb = ecma_get_typedarray_getter_fn (element_type);
ecma_object_t *buffer_obj_p = ecma_get_object_from_value (buffer);
lit_utf8_byte_t *pos = ecma_arraybuffer_get_buffer (buffer_obj_p) + indexed_position;
ecma_value_t stored_value = typedarray_getter_cb (pos);
// TODO: Handle shared array buffers differently.
if (ecma_op_same_value (stored_value, expected))
{
ecma_typedarray_setter_fn_t typedarray_setter_cb = ecma_get_typedarray_setter_fn (element_type);
typedarray_setter_cb (ecma_arraybuffer_get_buffer (buffer_obj_p) + indexed_position, replacement);
}
ecma_free_value (expected);
ecma_free_value (replacement);
return stored_value;
} /* ecma_builtin_atomics_compare_exchange */
/**
@@ -117,11 +276,83 @@ ecma_builtin_atomics_store (ecma_value_t typedarray, /**< typedArray argument */
ecma_value_t index, /**< index argument */
ecma_value_t value) /**< value argument */
{
JERRY_UNUSED (typedarray);
JERRY_UNUSED (index);
JERRY_UNUSED (value);
/* 1. */
ecma_value_t buffer = ecma_validate_integer_typedarray (typedarray, false);
return ecma_make_uint32_value (0);
if (ECMA_IS_VALUE_ERROR (buffer))
{
return buffer;
}
/* 2. */
uint32_t idx = ecma_validate_atomic_access (typedarray, index);
if (idx == ECMA_STRING_NOT_ARRAY_INDEX)
{
return ECMA_VALUE_ERROR;
}
ecma_object_t *typedarray_p = ecma_get_object_from_value (typedarray);
ecma_typedarray_info_t target_info = ecma_typedarray_get_info (typedarray_p);
if (ECMA_ARRAYBUFFER_LAZY_ALLOC (target_info.array_buffer_p))
{
return ECMA_VALUE_ERROR;
}
ecma_typedarray_type_t element_type = target_info.id;
ecma_typedarray_setter_fn_t typedarray_setter_cb = ecma_get_typedarray_setter_fn (element_type);
ecma_value_t value_to_store;
#if JERRY_BUILTIN_BIGINT
if (element_type == ECMA_BIGINT64_ARRAY || element_type == ECMA_BIGUINT64_ARRAY)
{
value_to_store = ecma_bigint_to_bigint (value, false);
if (ECMA_IS_VALUE_ERROR (value_to_store))
{
return value_to_store;
}
}
else
#endif /* JERRY_BUILTIN_BIGINT */
{
ecma_number_t num_int;
value_to_store = ecma_op_to_integer (value, &num_int);
if (ECMA_IS_VALUE_ERROR (value_to_store))
{
return value_to_store;
}
if (ecma_number_is_zero (num_int) && ecma_number_is_negative (num_int))
{
num_int = (ecma_number_t) 0;
}
value_to_store = ecma_make_number_value (num_int);
}
ecma_object_t *buffer_obj_p = ecma_get_object_from_value (buffer);
if (ecma_arraybuffer_is_detached (buffer_obj_p))
{
ecma_free_value (value_to_store);
return ecma_raise_type_error (ECMA_ERR_ARRAYBUFFER_IS_DETACHED);
}
uint8_t element_size = target_info.element_size;
uint32_t offset = target_info.offset;
uint32_t indexed_position = idx * element_size + offset;
// TODO: Handle shared array buffers differently.
typedarray_setter_cb (ecma_arraybuffer_get_buffer (buffer_obj_p) + indexed_position, value_to_store);
return value_to_store;
} /* ecma_builtin_atomics_store */
/**
@@ -15,6 +15,7 @@
#include "ecma-arraybuffer-object.h"
#include "ecma-bigint.h"
#include "ecma-builtin-helpers.h"
#include "ecma-builtins.h"
#include "ecma-exceptions.h"
@@ -27,6 +28,8 @@
#include "ecma-typedarray-object.h"
#include "jcontext.h"
#include "jrt.h"
#include "opcodes.h"
#if JERRY_BUILTIN_TYPEDARRAY
@@ -553,6 +556,154 @@ free_new_arraybuffer:
return ret_value;
} /* ecma_builtin_arraybuffer_slice */
/**
* Apply atomics operation on numbers.
*
* Note: ECMA_ATOMICS_COMPAREEXCHANGE is not handled here.
*
* @return ecma value
*/
static ecma_value_t
ecma_atomics_operation_on_number (ecma_value_t stored_value, /**< stored value */
ecma_value_t val, /**< value to operate with */
ecma_atomics_op_t op) /**< operation */
{
ecma_number_t result = ecma_get_number_from_value (stored_value);
switch (op)
{
case ECMA_ATOMICS_ADD:
{
result += ecma_get_number_from_value (val);
break;
}
case ECMA_ATOMICS_EXCHANGE:
{
result = ecma_get_number_from_value (val);
break;
}
case ECMA_ATOMICS_SUBTRACT:
{
result -= ecma_get_number_from_value (val);
break;
}
default:
{
JERRY_STATIC_ASSERT (
(number_bitwise_logic_op) ECMA_ATOMICS_AND == NUMBER_BITWISE_LOGIC_AND,
logical_operations_must_be_on_the_same_index_in_ecma_atomics_op_t_and_number_bitwise_logic_op);
JERRY_STATIC_ASSERT (
(number_bitwise_logic_op) ECMA_ATOMICS_OR == NUMBER_BITWISE_LOGIC_OR,
logical_operations_must_be_on_the_same_index_in_ecma_atomics_op_t_and_number_bitwise_logic_op);
JERRY_STATIC_ASSERT (
(number_bitwise_logic_op) ECMA_ATOMICS_XOR == NUMBER_BITWISE_LOGIC_XOR,
logical_operations_must_be_on_the_same_index_in_ecma_atomics_op_t_and_number_bitwise_logic_op);
return do_number_bitwise_logic ((number_bitwise_logic_op) (op), stored_value, val);
}
}
return ecma_make_number_value (result);
} /* ecma_atomics_operation_on_number */
#if JERRY_BUILTIN_BIGINT
/**
* Apply atomics operation on numbers.
*
* Note: ECMA_ATOMICS_COMPAREEXCHANGE is not handled here.
*
* @return ecma value
*/
static ecma_value_t
ecma_atomics_operation_on_bigint (ecma_value_t stored_value, ecma_value_t val, ecma_atomics_op_t op)
{
switch (op)
{
case ECMA_ATOMICS_ADD:
{
return ecma_bigint_add_sub (stored_value, val, true);
}
case ECMA_ATOMICS_AND:
{
return ecma_bigint_and (stored_value, val);
}
case ECMA_ATOMICS_EXCHANGE:
{
return ecma_copy_value (val);
}
case ECMA_ATOMICS_OR:
{
return ecma_bigint_or (stored_value, val);
}
case ECMA_ATOMICS_SUBTRACT:
{
return ecma_bigint_add_sub (stored_value, val, false);
}
default:
{
JERRY_ASSERT (op == ECMA_ATOMICS_XOR);
return ecma_bigint_xor (stored_value, val);
}
}
} /* ecma_atomics_operation_on_bigint */
#endif /* JERRY_BUILTIN_BIGINT */
/**
* ArrayBuffer get, modify, set value in buffer
*
* See also: ES12 25.1.2.13
* @return ecma value
*/
ecma_value_t
ecma_arraybuffer_get_modify_set_value_in_buffer (
ecma_value_t buffer, /**< arrayBuffer */
uint32_t indexed_position, /**< position of the element in buffer */
ecma_value_t val, /**< parameter of op, number or bigint */
ecma_atomics_op_t op, /**< the atomics operation to execute */
ecma_typedarray_type_t element_type, /**< type of the elements stored in typedArray */
ecma_typedarray_getter_fn_t typedarray_getter_cb, /**< getter callback of typedArray */
ecma_typedarray_setter_fn_t typedarray_setter_cb) /**< setter callback of typedArray */
{
#if !JERRY_BUILTIN_BIGINT
JERRY_UNUSED (element_type);
#endif /* !JERRY_BUILTIN_BIGINT */
/* 1. */
JERRY_ASSERT (!ecma_arraybuffer_is_detached (ecma_get_object_from_value (buffer)));
/* 3. */
JERRY_ASSERT (ecma_is_value_number (val) || ecma_is_value_bigint (val));
ecma_object_t *buffer_obj_p = ecma_get_object_from_value (buffer);
lit_utf8_byte_t *pos = ecma_arraybuffer_get_buffer (buffer_obj_p) + indexed_position;
ecma_value_t stored_value = typedarray_getter_cb (pos);
ecma_value_t op_result;
#if JERRY_BUILTIN_BIGINT
if (ECMA_TYPEDARRAY_IS_BIGINT_TYPE (element_type))
{
op_result = ecma_atomics_operation_on_bigint (stored_value, val, op);
}
else
#endif /* JERRY_BUILTIN_BIGINT */
{
op_result = ecma_atomics_operation_on_number (stored_value, val, op);
}
ecma_free_value (val);
if (ECMA_IS_VALUE_ERROR (op_result))
{
return op_result;
}
// TODO: Handle shared array buffers differently.
typedarray_setter_cb (pos, op_result);
ecma_free_value (op_result);
return stored_value;
} /* ecma_arraybuffer_get_modify_set_value_in_buffer */
/**
* @}
* @}
@@ -16,6 +16,7 @@
#ifndef ECMA_ARRAYBUFFER_OBJECT_H
#define ECMA_ARRAYBUFFER_OBJECT_H
#include "ecma-atomics-object.h"
#include "ecma-globals.h"
#if JERRY_BUILTIN_TYPEDARRAY
@@ -58,6 +59,13 @@ bool ecma_arraybuffer_detach (ecma_object_t *obj_p);
bool ecma_is_arraybuffer (ecma_value_t val);
ecma_value_t
ecma_builtin_arraybuffer_slice (ecma_value_t this_arg, const ecma_value_t *argument_list_p, uint32_t arguments_number);
ecma_value_t ecma_arraybuffer_get_modify_set_value_in_buffer (ecma_value_t buffer,
uint32_t indexed_position,
ecma_value_t val,
ecma_atomics_op_t op,
ecma_typedarray_type_t element_type,
ecma_typedarray_getter_fn_t typedarray_getter_cb,
ecma_typedarray_setter_fn_t typedarray_setter_cb);
/**
* @}
@@ -40,15 +40,15 @@
*/
/**
* Atomics validate Shared integer typedArray
* Atomics validate integer typedArray
*
* See also: ES11 24.4.1.1
* See also: ES12 25.4.1.1
*
* @return ecma value
*/
ecma_value_t
ecma_validate_shared_integer_typedarray (ecma_value_t typedarray, /**< typedArray argument */
bool waitable) /**< waitable argument */
ecma_validate_integer_typedarray (ecma_value_t typedarray, /**< typedArray argument */
bool waitable) /**< waitable argument */
{
/* 2. */
if (!ecma_is_typedarray (typedarray))
@@ -60,6 +60,11 @@ ecma_validate_shared_integer_typedarray (ecma_value_t typedarray, /**< typedArra
ecma_object_t *typedarray_p = ecma_get_object_from_value (typedarray);
ecma_typedarray_info_t target_info = ecma_typedarray_get_info (typedarray_p);
if (ECMA_ARRAYBUFFER_LAZY_ALLOC (target_info.array_buffer_p))
{
return ECMA_VALUE_ERROR;
}
/* 5-6. */
if (waitable)
{
@@ -83,13 +88,14 @@ ecma_validate_shared_integer_typedarray (ecma_value_t typedarray, /**< typedArra
/* 8-10. */
ecma_object_t *buffer = ecma_typedarray_get_arraybuffer (typedarray_p);
if (!ecma_object_class_is (buffer, ECMA_OBJECT_CLASS_SHARED_ARRAY_BUFFER))
if (!ecma_object_class_is (buffer, ECMA_OBJECT_CLASS_SHARED_ARRAY_BUFFER)
&& !ecma_object_class_is (buffer, ECMA_OBJECT_CLASS_ARRAY_BUFFER))
{
return ecma_raise_type_error (ECMA_ERR_ARGUMENT_NOT_SHARED_ARRAY_BUFFER);
return ecma_raise_type_error (ECMA_ERR_ARGUMENT_NOT_ARRAY_BUFFER);
}
return ecma_make_object_value (buffer);
} /* ecma_validate_shared_integer_typedarray */
} /* ecma_validate_integer_typedarray */
/**
* Atomics validate Atomic Access
@@ -98,7 +104,7 @@ ecma_validate_shared_integer_typedarray (ecma_value_t typedarray, /**< typedArra
*
* @return ecma value
*/
ecma_value_t
uint32_t
ecma_validate_atomic_access (ecma_value_t typedarray, /**< typedArray argument */
ecma_value_t request_index) /**< request_index argument */
{
@@ -112,22 +118,28 @@ ecma_validate_atomic_access (ecma_value_t typedarray, /**< typedArray argument *
ecma_number_t access_index;
if (ECMA_IS_VALUE_ERROR (ecma_op_to_index (request_index, &access_index)))
{
return ECMA_VALUE_ERROR;
return ECMA_STRING_NOT_ARRAY_INDEX;
}
/* 3. */
ecma_typedarray_info_t target_info = ecma_typedarray_get_info (typedarray_p);
if (ECMA_ARRAYBUFFER_LAZY_ALLOC (target_info.array_buffer_p))
{
return ECMA_STRING_NOT_ARRAY_INDEX;
}
/* 4. */
JERRY_ASSERT (access_index >= 0);
/* 5-6. */
if (JERRY_UNLIKELY (access_index >= target_info.length))
{
return ecma_raise_range_error (ECMA_ERR_INVALID_LENGTH);
ecma_raise_range_error (ECMA_ERR_INVALID_LENGTH);
return ECMA_STRING_NOT_ARRAY_INDEX;
}
return ecma_make_number_value (access_index);
return (uint32_t) access_index;
} /* ecma_validate_atomic_access */
/**
@@ -144,7 +156,7 @@ ecma_atomic_read_modify_write (ecma_value_t typedarray, /**< typedArray argument
ecma_atomics_op_t op) /**< operation argument */
{
/* 1. */
ecma_value_t buffer = ecma_validate_shared_integer_typedarray (typedarray, false);
ecma_value_t buffer = ecma_validate_integer_typedarray (typedarray, false);
if (ECMA_IS_VALUE_ERROR (buffer))
{
@@ -152,28 +164,38 @@ ecma_atomic_read_modify_write (ecma_value_t typedarray, /**< typedArray argument
}
/* 2. */
ecma_value_t idx = ecma_validate_atomic_access (typedarray, index);
uint32_t idx = ecma_validate_atomic_access (typedarray, index);
if (ECMA_IS_VALUE_ERROR (idx))
if (idx == ECMA_STRING_NOT_ARRAY_INDEX)
{
return idx;
return ECMA_VALUE_ERROR;
}
/* 3. */
ecma_object_t *typedarray_p = ecma_get_object_from_value (typedarray);
ecma_typedarray_info_t target_info = ecma_typedarray_get_info (typedarray_p);
if (ECMA_ARRAYBUFFER_LAZY_ALLOC (target_info.array_buffer_p))
{
return ECMA_VALUE_ERROR;
}
ecma_typedarray_type_t element_type = target_info.id;
/* 4-5. */
ecma_value_t val = ECMA_VALUE_ERROR;
ecma_number_t tmp;
if (target_info.id == ECMA_BIGINT64_ARRAY || target_info.id == ECMA_BIGUINT64_ARRAY)
#if JERRY_BUILTIN_BIGINT
if (element_type == ECMA_BIGINT64_ARRAY || element_type == ECMA_BIGUINT64_ARRAY)
{
val = ecma_bigint_to_bigint (value, true);
}
else if (!ECMA_IS_VALUE_ERROR (ecma_op_to_integer (value, &tmp)))
{
val = ecma_make_number_value (tmp);
val = ecma_bigint_to_bigint (value, false);
}
else
#endif /* JERRY_BUILTIN_BIGINT */
if (!ECMA_IS_VALUE_ERROR (ecma_op_to_integer (value, &tmp)))
{
val = ecma_make_number_value (tmp);
}
if (ECMA_IS_VALUE_ERROR (val))
{
@@ -183,33 +205,26 @@ ecma_atomic_read_modify_write (ecma_value_t typedarray, /**< typedArray argument
/* 6. */
uint8_t element_size = target_info.element_size;
/* 7. */
ecma_typedarray_type_t element_type = target_info.id;
/* 8. */
uint32_t offset = target_info.offset;
/* 9. */
uint32_t indexed_position = ecma_number_to_uint32 (idx) * element_size + offset;
ecma_free_value (idx);
JERRY_UNUSED (indexed_position);
JERRY_UNUSED (element_type);
JERRY_UNUSED (val);
JERRY_UNUSED (buffer);
JERRY_UNUSED (op);
ecma_free_value (val);
uint32_t indexed_position = idx * element_size + offset;
/* 10. */
return ecma_make_uint32_value (0);
return ecma_arraybuffer_get_modify_set_value_in_buffer (buffer,
indexed_position,
val,
op,
element_type,
ecma_get_typedarray_getter_fn (element_type),
ecma_get_typedarray_setter_fn (element_type));
} /* ecma_atomic_read_modify_write */
/**
* Atomics load
*
* See also: ES11 24.4.1.12
* See also: ES12 25.4.7
*
* @return ecma value
*/
@@ -217,25 +232,35 @@ ecma_value_t
ecma_atomic_load (ecma_value_t typedarray, /**< typedArray argument */
ecma_value_t index) /**< index argument */
{
ecma_value_t buffer = ecma_validate_shared_integer_typedarray (typedarray, false);
ecma_value_t buffer = ecma_validate_integer_typedarray (typedarray, false);
if (ECMA_IS_VALUE_ERROR (buffer))
{
return buffer;
}
/* 2. */
ecma_value_t idx = ecma_validate_atomic_access (typedarray, index);
if (ECMA_IS_VALUE_ERROR (idx))
if (ecma_arraybuffer_is_detached (ecma_get_object_from_value (buffer)))
{
return idx;
ecma_raise_type_error (ECMA_ERR_ARRAYBUFFER_IS_DETACHED);
}
/* 2. */
uint32_t idx = ecma_validate_atomic_access (typedarray, index);
if (idx == ECMA_STRING_NOT_ARRAY_INDEX)
{
return ECMA_VALUE_ERROR;
}
/* 3. */
ecma_object_t *typedarray_p = ecma_get_object_from_value (typedarray);
ecma_typedarray_info_t target_info = ecma_typedarray_get_info (typedarray_p);
if (ECMA_ARRAYBUFFER_LAZY_ALLOC (target_info.array_buffer_p))
{
return ECMA_VALUE_ERROR;
}
/* 4. */
uint8_t element_size = target_info.element_size;
@@ -246,14 +271,12 @@ ecma_atomic_load (ecma_value_t typedarray, /**< typedArray argument */
uint32_t offset = target_info.offset;
/* 7. */
uint32_t indexed_position = ecma_number_to_uint32 (idx) * element_size + offset;
uint32_t indexed_position = idx * element_size + offset;
JERRY_UNUSED (indexed_position);
JERRY_UNUSED (element_type);
JERRY_UNUSED (buffer);
ecma_typedarray_getter_fn_t typedarray_getter_cb = ecma_get_typedarray_getter_fn (element_type);
uint8_t *buffer_p = ecma_arraybuffer_get_buffer (ecma_get_object_from_value (buffer));
/* 8. */
return ecma_make_uint32_value (0);
return typedarray_getter_cb (buffer_p + indexed_position);
} /* ecma_atomic_load */
/**
@@ -32,17 +32,17 @@
*/
typedef enum
{
ECMA_ATOMICS_ADD, /**< Atomics add operation */
ECMA_ATOMICS_SUBTRACT, /**< Atomics subtract operation */
ECMA_ATOMICS_AND, /**< Atomics and operation */
ECMA_ATOMICS_OR, /**< Atomics or operation */
ECMA_ATOMICS_XOR, /**< Atomics xor operation */
ECMA_ATOMICS_ADD, /**< Atomics add operation */
ECMA_ATOMICS_SUBTRACT, /**< Atomics subtract operation */
ECMA_ATOMICS_EXCHANGE, /**< Atomics exchange operation */
ECMA_ATOMICS_COMPARE_EXCHANGE /**< Atomics compare exchange operation */
} ecma_atomics_op_t;
ecma_value_t ecma_validate_shared_integer_typedarray (ecma_value_t typedarray, bool waitable);
ecma_value_t ecma_validate_atomic_access (ecma_value_t typedarray, ecma_value_t request_index);
ecma_value_t ecma_validate_integer_typedarray (ecma_value_t typedarray, bool waitable);
uint32_t ecma_validate_atomic_access (ecma_value_t typedarray, ecma_value_t request_index);
ecma_value_t
ecma_atomic_read_modify_write (ecma_value_t typedarray, ecma_value_t index, ecma_value_t value, ecma_atomics_op_t op);
ecma_value_t ecma_atomic_load (ecma_value_t typedarray, ecma_value_t index);
@@ -197,7 +197,7 @@ ecma_typedarray_get_biguint64_element (lit_utf8_byte_t *src) /**< the location i
/**
* Normalize the given ecma_number_t to an uint32_t value
*/
static uint32_t
uint32_t
ecma_typedarray_setter_number_to_uint32 (ecma_number_t value) /**< the number value to normalize */
{
uint32_t uint32_value = 0;
@@ -28,6 +28,7 @@
* @{
*/
uint32_t ecma_typedarray_setter_number_to_uint32 (ecma_number_t value);
uint8_t ecma_typedarray_helper_get_shift_size (ecma_typedarray_type_t typedarray_id);
lit_magic_string_id_t ecma_get_typedarray_magic_string_id (ecma_typedarray_type_t typedarray_id);
ecma_typedarray_getter_fn_t ecma_get_typedarray_getter_fn (ecma_typedarray_type_t typedarray_id);