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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user