From c1c1d55067224f050dcb41ab0efe16dbda803edf Mon Sep 17 00:00:00 2001 From: Gergo Csizi Date: Fri, 15 Nov 2024 09:11:25 +0100 Subject: [PATCH] Implement operations of atomics The implementation is based on PR #4956, only resolved the conflicts. JerryScript-DCO-1.0-Signed-off-by: Gergo Csizi gergocs@inf.u-szeged.hu --- .../ecma/base/ecma-error-messages.inc.h | 13 +- jerry-core/ecma/base/ecma-error-messages.ini | 1 - .../builtin-objects/ecma-builtin-atomics.c | 246 ++++++++++++++++- .../ecma/operations/ecma-arraybuffer-object.c | 154 +++++++++++ .../ecma/operations/ecma-arraybuffer-object.h | 8 + .../ecma/operations/ecma-atomics-object.c | 106 ++++--- .../ecma/operations/ecma-atomics-object.h | 8 +- .../ecma/operations/ecma-typedarray-object.c | 2 +- .../ecma/operations/ecma-typedarray-object.h | 1 + tests/jerry/atomics.js | 259 ++++++++++++++++-- tests/snapshot-tests-skiplist.txt | 1 + tests/test262-excludelist.xml | 91 ------ 12 files changed, 715 insertions(+), 175 deletions(-) diff --git a/jerry-core/ecma/base/ecma-error-messages.inc.h b/jerry-core/ecma/base/ecma-error-messages.inc.h index 7711f871be..17d978770d 100644 --- a/jerry-core/ecma/base/ecma-error-messages.inc.h +++ b/jerry-core/ecma/base/ecma-error-messages.inc.h @@ -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 diff --git a/jerry-core/ecma/base/ecma-error-messages.ini b/jerry-core/ecma/base/ecma-error-messages.ini index c2d7031ab3..83d6d7c6b5 100644 --- a/jerry-core/ecma/base/ecma-error-messages.ini +++ b/jerry-core/ecma/base/ecma-error-messages.ini @@ -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" diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-atomics.c b/jerry-core/ecma/builtin-objects/ecma-builtin-atomics.c index c7d1847ca3..934c598ad4 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-atomics.c +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-atomics.c @@ -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,122 @@ 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 (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 + { + 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 +274,80 @@ 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; + } + + uint8_t element_size = target_info.element_size; + + ecma_typedarray_type_t element_type = target_info.id; + + uint32_t offset = target_info.offset; + + uint32_t indexed_position = idx * element_size + offset; + + ecma_typedarray_setter_fn_t typedarray_setter_cb = ecma_get_typedarray_setter_fn (element_type); + ecma_object_t *buffer_obj_p = ecma_get_object_from_value (buffer); + + ecma_value_t value_to_store; + 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 + { + 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); + } + + if (ecma_arraybuffer_is_detached (ecma_get_object_from_value (buffer))) + { + ecma_free_value (value_to_store); + return ecma_raise_type_error (ECMA_ERR_ARRAYBUFFER_IS_DETACHED); + } + + // 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 */ /** diff --git a/jerry-core/ecma/operations/ecma-arraybuffer-object.c b/jerry-core/ecma/operations/ecma-arraybuffer-object.c index 3a436d1f80..33375fded4 100644 --- a/jerry-core/ecma/operations/ecma-arraybuffer-object.c +++ b/jerry-core/ecma/operations/ecma-arraybuffer-object.c @@ -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,157 @@ ecma_builtin_arraybuffer_slice (ecma_value_t this_arg, const ecma_value_t *argum 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, ecma_value_t val, ecma_atomics_op_t op) +{ + 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); + + ecma_value_t op_result = do_number_bitwise_logic ((number_bitwise_logic_op) (op), stored_value, val); + result = ecma_get_number_from_value (op_result); + ecma_free_value (op_result); + break; + } + } + + return ecma_make_number_value (result); +} /* ecma_atomics_operation_on_number */ + +/** + * 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 */ + +/** + * ArrayBuffer get, modify, set value in buffer + * + * See also: ES12 25.1.2.13 + * + * @param buffer: arrayBuffer + * @param indexed_position: position of the element in buffer + * @param val: parameter of op, number or bigint + * @param op: the atomics operation to execute + * @param element_type: type of the elements stored in typedArray + * @param typedarray_getter_cb: getter callback of typedArray + * @param typedarray_setter_cb: setter callback of typedArray + * + * @return ecma value + */ +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) +{ + /* 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 (ECMA_TYPEDARRAY_IS_BIGINT_TYPE (element_type)) + { + op_result = ecma_atomics_operation_on_bigint (stored_value, val, op); + } + else + { + 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 */ + /** * @} * @} diff --git a/jerry-core/ecma/operations/ecma-arraybuffer-object.h b/jerry-core/ecma/operations/ecma-arraybuffer-object.h index 6dc28b59bc..822e13f050 100644 --- a/jerry-core/ecma/operations/ecma-arraybuffer-object.h +++ b/jerry-core/ecma/operations/ecma-arraybuffer-object.h @@ -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); /** * @} diff --git a/jerry-core/ecma/operations/ecma-atomics-object.c b/jerry-core/ecma/operations/ecma-atomics-object.c index c42ad8752a..083223fe20 100644 --- a/jerry-core/ecma/operations/ecma-atomics-object.c +++ b/jerry-core/ecma/operations/ecma-atomics-object.c @@ -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,23 +164,30 @@ 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 (element_type == ECMA_BIGINT64_ARRAY || element_type == ECMA_BIGUINT64_ARRAY) { - val = ecma_bigint_to_bigint (value, true); + val = ecma_bigint_to_bigint (value, false); } else if (!ECMA_IS_VALUE_ERROR (ecma_op_to_integer (value, &tmp))) { @@ -183,33 +202,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 +229,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; } + if (ecma_arraybuffer_is_detached (ecma_get_object_from_value (buffer))) + { + ecma_raise_type_error (ECMA_ERR_ARRAYBUFFER_IS_DETACHED); + } + /* 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; + } + /* 4. */ uint8_t element_size = target_info.element_size; @@ -246,14 +268,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 */ /** diff --git a/jerry-core/ecma/operations/ecma-atomics-object.h b/jerry-core/ecma/operations/ecma-atomics-object.h index d73ae99b73..c4d409aa65 100644 --- a/jerry-core/ecma/operations/ecma-atomics-object.h +++ b/jerry-core/ecma/operations/ecma-atomics-object.h @@ -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); diff --git a/jerry-core/ecma/operations/ecma-typedarray-object.c b/jerry-core/ecma/operations/ecma-typedarray-object.c index c4c4f41052..becec42a11 100644 --- a/jerry-core/ecma/operations/ecma-typedarray-object.c +++ b/jerry-core/ecma/operations/ecma-typedarray-object.c @@ -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; diff --git a/jerry-core/ecma/operations/ecma-typedarray-object.h b/jerry-core/ecma/operations/ecma-typedarray-object.h index ab004265c9..eb783ae245 100644 --- a/jerry-core/ecma/operations/ecma-typedarray-object.h +++ b/jerry-core/ecma/operations/ecma-typedarray-object.h @@ -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); diff --git a/tests/jerry/atomics.js b/tests/jerry/atomics.js index 943d6f4553..d608ed228e 100644 --- a/tests/jerry/atomics.js +++ b/tests/jerry/atomics.js @@ -13,19 +13,9 @@ // limitations under the License. const buffer = new SharedArrayBuffer (16); -const uint8 = new Uint8Array (buffer); -uint8[0] = 7; - -Atomics.add (uint8, 0, 2); -Atomics.and (uint8, 0, 2); -Atomics.compareExchange (uint8, 0, 5, 2); -Atomics.exchange (uint8, 0, 2); -Atomics.or (uint8, 0, 2); -Atomics.sub (uint8, 0, 2); -Atomics.xor (uint8, 0, 2) -Atomics.isLockFree (3); -Atomics.load (uint8, 0); -Atomics.store (uint8, 0, 2); +const uint8 = new Uint8Array(buffer); + +Atomics.isLockFree(3); const sab = new SharedArrayBuffer (1024); const int32 = new Int32Array (sab); @@ -36,30 +26,261 @@ Atomics.notify (int32, 0, 1); try { let a; Atomics.add (a, 0, 0); + assert(false); } catch (ex) { assert (ex instanceof TypeError); } try { Atomics.add (new Float32Array(10), 0, 2); -} catch (ex) { - assert (ex instanceof TypeError); -} -try{ - const uint16 = new Uint16Array (new ArrayBuffer (16)); - Atomics.add(uint16, 0, 0); + assert(false); } catch (ex) { assert (ex instanceof TypeError); } try { Atomics.add (uint8, 100, 0); + assert(false); } catch (ex) { assert (ex instanceof RangeError); } try { Atomics.add (uint8, -1, 0); + assert(false); } catch (ex) { assert (ex instanceof RangeError); } + +assert(Atomics.store(uint8, 0, -1) === -1); +assert(Atomics.add(uint8, 0, 1) === 255); +assert(Atomics.load(uint8, 0) === 0); + +const ta_int8 = new Int8Array(new ArrayBuffer(2)); +ta_int8[0] = 5; + +Atomics.store(ta_int8, 1, 127) + +assert(Atomics.load(ta_int8, 0) === 5); + +assert(Atomics.add(ta_int8, 0, 12) === 5); +assert(Atomics.load(ta_int8, 0) === 17); + +assert(Atomics.and(ta_int8, 0, 1) === 17); +assert(Atomics.load(ta_int8, 0) === 1); + +assert(Atomics.compareExchange(ta_int8, 0, 5, 123) === 1); +assert(Atomics.load(ta_int8, 0) === 1); + +assert(Atomics.compareExchange(ta_int8, 1, 123, ta_int8[0]) === 127); +assert(Atomics.load(ta_int8, 0) === 1); + +assert(Atomics.compareExchange(ta_int8, 0, 1, 123) === 1); +assert(Atomics.load(ta_int8, 0) === 123); + +assert(Atomics.store(ta_int8, 0, -0) === 0); +assert(Atomics.load(ta_int8, 0) === 0); + +assert(Atomics.store(ta_int8, 0, 1) === 1); +assert(Atomics.load(ta_int8, 0) === 1); + +assert(Atomics.exchange(ta_int8, 0, 12) === 1); +assert(Atomics.load(ta_int8, 0) === 12); + +assert(Atomics.or(ta_int8, 0, 1) === 12); +assert(Atomics.load(ta_int8, 0) === 13); + +assert(Atomics.sub(ta_int8, 0, 2) === 13); +assert(Atomics.load(ta_int8, 0) === 11); + +assert(Atomics.xor(ta_int8, 0, 1) === 11); +assert(Atomics.load(ta_int8, 0) === 10); + +const ta_bigintint64 = new BigInt64Array(new ArrayBuffer(16)); +ta_bigintint64[0] = 5n; + +Atomics.store(ta_bigintint64, 1, 127n) + +assert(Atomics.load(ta_bigintint64, 0) === 5n); + +assert(Atomics.add(ta_bigintint64, 0, 12n) === 5n); +assert(Atomics.load(ta_bigintint64, 0) === 17n); + +assert(Atomics.and(ta_bigintint64, 0, 1n) === 17n); +assert(Atomics.load(ta_bigintint64, 0) === 1n); + +assert(Atomics.compareExchange(ta_bigintint64, 0, 5n, 123n) === 1n); +assert(Atomics.load(ta_bigintint64, 0) === 1n); + +assert(Atomics.compareExchange(ta_bigintint64, 1, 123n, ta_bigintint64[0]) === 127n); +assert(Atomics.load(ta_bigintint64, 0) === 1n); + +assert(Atomics.compareExchange(ta_bigintint64, 0, 1n, 123n) === 1n); +assert(Atomics.load(ta_bigintint64, 0) === 123n); + +assert(Atomics.store(ta_bigintint64, 0, -0n) === 0n); +assert(Atomics.load(ta_bigintint64, 0) === 0n); + +assert(Atomics.store(ta_bigintint64, 0, 1n) === 1n); +assert(Atomics.load(ta_bigintint64, 0) === 1n); + +assert(Atomics.exchange(ta_bigintint64, 0, 12n) === 1n); +assert(Atomics.load(ta_bigintint64, 0) === 12n); + +assert(Atomics.or(ta_bigintint64, 0, 1n) === 12n); +assert(Atomics.load(ta_bigintint64, 0) === 13n); + +assert(Atomics.sub(ta_bigintint64, 0, 2n) === 13n); +assert(Atomics.load(ta_bigintint64, 0) === 11n); + +assert(Atomics.xor(ta_bigintint64, 0, 1n) === 11n); +assert(Atomics.load(ta_bigintint64, 0) === 10n); + +try { + Atomics.store(ta_bigintint64, 0, 1); + assert(false); +} catch (e) { + assert(e instanceof TypeError); +} + +try { + Atomics.store(ta_int8, 0, 1n); + assert(false); +} catch (e) { + assert(e instanceof TypeError); +} + +try { + Atomics.store(ta_int8, 0n, 1); + assert(false); +} catch (e) { + assert(e instanceof TypeError); +} + +try { + Atomics.store(ta_int8, 2, 1); + assert(false); +} catch (e) { + assert(e instanceof RangeError); +} + +try { + Atomics.store(ta_int8, -1, 1); + assert(false); +} catch (e) { + assert(e instanceof RangeError); +} + +try { + Atomics.load(ta_int8, 0n); + assert(false); +} catch (e) { + assert(e instanceof TypeError); +} + +try { + Atomics.load(ta_int8, 2); + assert(false); +} catch (e) { + assert(e instanceof RangeError); +} + +try { + Atomics.load(ta_int8, -1); + assert(false); +} catch (e) { + assert(e instanceof RangeError); +} + +try { + Atomics.exchange(ta_bigintint64, 0, 1); + assert(false); +} catch (e) { + assert(e instanceof TypeError); +} + +try { + Atomics.exchange(ta_int8, 0, 1n); + assert(false); +} catch (e) { + assert(e instanceof TypeError); +} + +try { + Atomics.exchange(ta_int8, 0n, 1); + assert(false); +} catch (e) { + assert(e instanceof TypeError); +} + +try { + Atomics.compareExchange(ta_bigintint64, 0n, 1n, 0n); + assert(false); +} catch (e) { + assert(e instanceof TypeError); +} + +try { + Atomics.compareExchange(ta_bigintint64, 0, 1, 0n); + assert(false); +} catch (e) { + assert(e instanceof TypeError); +} + +try { + Atomics.compareExchange(ta_int8, 0, 1n, 0); + assert(false); +} catch (e) { + assert(e instanceof TypeError); +} + +try { + Atomics.compareExchange(ta_bigintint64, 0, 1n, 0); + assert(false); +} catch (e) { + assert(e instanceof TypeError); +} + +try { + Atomics.compareExchange(ta_int8, 0, 1, 0n); + assert(false); +} catch (e) { + assert(e instanceof TypeError); +} + +try { + Atomics.compareExchange(ta_int8, 2, 1, 0); + assert(false); +} catch (e) { + assert(e instanceof RangeError); +} + +try { + Atomics.compareExchange(ta_int8, -1, 1, 0); + assert(false); +} catch (e) { + assert(e instanceof RangeError); +} + +const ta_float32 = new Float32Array(new ArrayBuffer(4)); + +try { + Atomics.store(ta_float32, 0, 127); + assert(false); +} catch (e) { + assert(e instanceof TypeError); +} + +try { + Atomics.load(ta_float32, 0); + assert(false); +} catch (e) { + assert(e instanceof TypeError); +} + +try { + Atomics.compareExchange(ta_float32, 0, 1, 0); + assert(false); +} catch (e) { + assert(e instanceof TypeError); +} diff --git a/tests/snapshot-tests-skiplist.txt b/tests/snapshot-tests-skiplist.txt index 2952f12358..f3e4e118f2 100644 --- a/tests/snapshot-tests-skiplist.txt +++ b/tests/snapshot-tests-skiplist.txt @@ -4,6 +4,7 @@ tests/jerry/bigint6.js tests/jerry/bigint7.js tests/jerry/bigint8.js tests/jerry/bigint9.js +tests/jerry/atomics.js tests/jerry/logical-assignment.js tests/jerry/module-circular-01.mjs tests/jerry/module-circular-02.mjs diff --git a/tests/test262-excludelist.xml b/tests/test262-excludelist.xml index 742cb67d7c..3ed98bd295 100644 --- a/tests/test262-excludelist.xml +++ b/tests/test262-excludelist.xml @@ -2547,39 +2547,7 @@ features: [Atomics] https://github.com/tc39/ecmascript_sharedmem --> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -2619,27 +2587,6 @@ - - - - - - - - - - - - - - - - - - - - - @@ -2713,34 +2660,11 @@ - - - - - - - - - - - - - - - - - - - - - - - @@ -2766,18 +2690,6 @@ - - - - - - - - - - - - @@ -2926,9 +2838,6 @@ - - -