Add Atomics support (#4721)

Creating atomics interface

JerryScript-DCO-1.0-Signed-off-by: Gergo Csizi csgergo92@gmail.com
This commit is contained in:
Gergo Csizi
2021-10-20 15:50:12 +02:00
committed by GitHub
parent c446871214
commit 74f98ec4d7
12 changed files with 803 additions and 122 deletions
@@ -0,0 +1,259 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ecma-atomics-object.h"
#include "ecma-builtins.h"
#include "ecma-globals.h"
#include "ecma-helpers.h"
#include "jrt.h"
#if JERRY_BUILTIN_ATOMICS
#define ECMA_BUILTINS_INTERNAL
#include "ecma-builtins-internal.h"
/**
* This object has a custom dispatch function.
*/
#define BUILTIN_CUSTOM_DISPATCH
/**
* List of built-in routine identifiers.
*/
enum
{
ECMA_ATOMICS_ROUTINE_START = 0, /**< Special value, should be ignored */
ECMA_ATOMICS_ROUTINE_ADD, /**< Atomics add routine */
ECMA_ATOMICS_ROUTINE_AND, /**< Atomics and routine */
ECMA_ATOMICS_ROUTINE_COMPAREEXCHANGE, /**< Atomics compare exchange routine */
ECMA_ATOMICS_ROUTINE_EXCHANGE, /**< Atomics exchange routine */
ECMA_ATOMICS_ROUTINE_ISLOCKFREE, /**< Atomics is lock free routine */
ECMA_ATOMICS_ROUTINE_LOAD, /**< Atomics load routine */
ECMA_ATOMICS_ROUTINE_OR, /**< Atomics or routine */
ECMA_ATOMICS_ROUTINE_STORE, /**< Atomics store routine */
ECMA_ATOMICS_ROUTINE_SUB, /**< Atomics sub routine */
ECMA_ATOMICS_ROUTINE_WAIT, /**< Atomics wait routine */
ECMA_ATOMICS_ROUTINE_NOTIFY, /**< Atomics notify routine */
ECMA_ATOMICS_ROUTINE_XOR, /**< Atomics xor routine */
};
#define BUILTIN_INC_HEADER_NAME "ecma-builtin-atomics.inc.h"
#define BUILTIN_UNDERSCORED_ID atomics
#include "ecma-builtin-internal-routines-template.inc.h"
/** \addtogroup ecma ECMA
* @{
*
* \addtogroup ecmabuiltins
* @{
*
* \addtogroup atomics ECMA Atomics object built-in
* @{
*/
/**
* The Atomics object's 'compareExchange' routine
*
* See also: ES11 24.4.4
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_builtin_atomics_compare_exchange (ecma_value_t typedarray, /**< typedArray argument */
ecma_value_t index, /**< index argument */
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);
return ecma_make_uint32_value (0);
} /* ecma_builtin_atomics_compare_exchange */
/**
* The Atomics object's 'isLockFree' routine
*
* See also: ES11 24.4.6
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_builtin_atomics_is_lock_free (ecma_value_t size) /**< size argument */
{
JERRY_UNUSED (size);
return ECMA_VALUE_FALSE;
} /* ecma_builtin_atomics_is_lock_free */
/**
* The Atomics object's 'store' routine
*
* See also: ES11 24.4.9
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
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);
return ecma_make_uint32_value (0);
} /* ecma_builtin_atomics_store */
/**
* The Atomics object's 'wait' routine
*
* See also: ES11 24.4.11
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_builtin_atomics_wait (ecma_value_t typedarray, /**< typedArray argument */
ecma_value_t index, /**< index argument */
ecma_value_t value, /**< value argument */
ecma_value_t timeout) /**< timeout argument */
{
JERRY_UNUSED (typedarray);
JERRY_UNUSED (index);
JERRY_UNUSED (value);
JERRY_UNUSED (timeout);
return ecma_make_uint32_value (0);
} /* ecma_builtin_atomics_wait */
/**
* The Atomics object's 'notify' routine
*
* See also: ES11 24.4.12
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_builtin_atomics_notify (ecma_value_t typedarray, /**< typedArray argument */
ecma_value_t index, /**< index argument */
ecma_value_t count) /**< count argument */
{
JERRY_UNUSED (typedarray);
JERRY_UNUSED (index);
JERRY_UNUSED (count);
return ecma_make_uint32_value (0);
} /* ecma_builtin_atomics_notify */
/**
* Dispatcher of the built-in's routines
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
ecma_value_t
ecma_builtin_atomics_dispatch_routine (uint8_t builtin_routine_id, /**< built-in wide routine identifier */
ecma_value_t this_arg, /**< 'this' argument value */
const ecma_value_t arguments_list_p[], /**< list of arguments
* passed to routine */
uint32_t arguments_number) /**< length of arguments' list */
{
JERRY_UNUSED (this_arg);
ecma_value_t arg1 = arguments_list_p[0];
ecma_value_t arg2 = arguments_list_p[1];
ecma_value_t arg3 = arguments_list_p[2];
ecma_value_t arg4 = (arguments_number > 3) ? arguments_list_p[3] : ECMA_VALUE_UNDEFINED;
ecma_atomics_op_t type;
switch (builtin_routine_id)
{
case ECMA_ATOMICS_ROUTINE_ADD:
{
type = ECMA_ATOMICS_ADD;
break;
}
case ECMA_ATOMICS_ROUTINE_AND:
{
type = ECMA_ATOMICS_AND;
break;
}
case ECMA_ATOMICS_ROUTINE_COMPAREEXCHANGE:
{
return ecma_builtin_atomics_compare_exchange (arg1, arg2, arg3, arg4);
}
case ECMA_ATOMICS_ROUTINE_EXCHANGE:
{
type = ECMA_ATOMICS_EXCHANGE;
break;
}
case ECMA_ATOMICS_ROUTINE_ISLOCKFREE:
{
return ecma_builtin_atomics_is_lock_free (arg1);
}
case ECMA_ATOMICS_ROUTINE_LOAD:
{
return ecma_atomic_load (arg1, arg2);
}
case ECMA_ATOMICS_ROUTINE_OR:
{
type = ECMA_ATOMICS_OR;
break;
}
case ECMA_ATOMICS_ROUTINE_STORE:
{
return ecma_builtin_atomics_store (arg1, arg2, arg3);
}
case ECMA_ATOMICS_ROUTINE_SUB:
{
type = ECMA_ATOMICS_SUBTRACT;
break;
}
case ECMA_ATOMICS_ROUTINE_WAIT:
{
return ecma_builtin_atomics_wait (arg1, arg2, arg3, arg4);
}
case ECMA_ATOMICS_ROUTINE_NOTIFY:
{
return ecma_builtin_atomics_notify (arg1, arg2, arg3);
}
case ECMA_ATOMICS_ROUTINE_XOR:
{
type = ECMA_ATOMICS_XOR;
break;
}
default:
{
JERRY_UNREACHABLE ();
}
}
return ecma_atomic_read_modify_write (arg1, arg2, arg3, type);
} /* ecma_builtin_atomics_dispatch_routine */
/**
* @}
* @}
* @}
*/
#endif /* JERRY_BUILTIN_ATOMICS */
@@ -0,0 +1,46 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Atomics built-in description
*/
#include "ecma-builtin-helpers-macro-defines.inc.h"
#if JERRY_BUILTIN_ATOMICS
/* ECMA-262 v11, 24.4.14 */
STRING_VALUE (LIT_GLOBAL_SYMBOL_TO_STRING_TAG,
LIT_MAGIC_STRING_ATOMICS_U,
ECMA_PROPERTY_FLAG_CONFIGURABLE)
/* Routine properties:
* (property name, C routine name, arguments number or NON_FIXED, value of the routine's length property) */
ROUTINE (LIT_MAGIC_STRING_ADD, ECMA_ATOMICS_ROUTINE_ADD, 3, 3)
ROUTINE (LIT_MAGIC_STRING_ATOMICS_AND, ECMA_ATOMICS_ROUTINE_AND, 3, 3)
ROUTINE (LIT_MAGIC_STRING_ATOMICS_COMPAREEXCHANGE, ECMA_ATOMICS_ROUTINE_COMPAREEXCHANGE, 4, 4)
ROUTINE (LIT_MAGIC_STRING_ATOMICS_EXCHANGE, ECMA_ATOMICS_ROUTINE_EXCHANGE, 3, 3)
ROUTINE (LIT_MAGIC_STRING_ATOMICS_ISLOCKFREE, ECMA_ATOMICS_ROUTINE_ISLOCKFREE, 1, 1)
ROUTINE (LIT_MAGIC_STRING_ATOMICS_LOAD, ECMA_ATOMICS_ROUTINE_LOAD, 2, 2)
ROUTINE (LIT_MAGIC_STRING_ATOMICS_OR, ECMA_ATOMICS_ROUTINE_OR, 3, 3)
ROUTINE (LIT_MAGIC_STRING_ATOMICS_STORE, ECMA_ATOMICS_ROUTINE_STORE, 3, 3)
ROUTINE (LIT_MAGIC_STRING_ATOMICS_SUB, ECMA_ATOMICS_ROUTINE_SUB, 3, 3)
ROUTINE (LIT_MAGIC_STRING_ATOMICS_WAIT, ECMA_ATOMICS_ROUTINE_WAIT, 4, 4)
ROUTINE (LIT_MAGIC_STRING_ATOMICS_NOTIFY, ECMA_ATOMICS_ROUTINE_NOTIFY, 3, 3)
ROUTINE (LIT_MAGIC_STRING_ATOMICS_XOR, ECMA_ATOMICS_ROUTINE_XOR, 3, 3)
#endif /* JERRY_BUILTIN_ATOMICS */
#include "ecma-builtin-helpers-macro-undefs.inc.h"
@@ -153,6 +153,13 @@ OBJECT_VALUE (LIT_MAGIC_STRING_JSON_U,
ECMA_PROPERTY_CONFIGURABLE_WRITABLE)
#endif /* JERRY_BUILTIN_JSON */
#if JERRY_BUILTIN_ATOMICS
/* ECMA-262 v5, 15.1.5.2 */
OBJECT_VALUE (LIT_MAGIC_STRING_ATOMICS_U,
ECMA_BUILTIN_ID_ATOMICS,
ECMA_PROPERTY_CONFIGURABLE_WRITABLE)
#endif /* JERRY_BUILTIN_ATOMICS */
#if JERRY_BUILTIN_TYPEDARRAY
OBJECT_VALUE (LIT_MAGIC_STRING_ARRAY_BUFFER_UL,
ECMA_BUILTIN_ID_ARRAYBUFFER,
@@ -174,6 +174,15 @@ BUILTIN (ECMA_BUILTIN_ID_JSON,
json)
#endif /* JERRY_BUILTIN_JSON */
#if JERRY_BUILTIN_ATOMICS
/* The Atomics object (24.4) */
BUILTIN (ECMA_BUILTIN_ID_ATOMICS,
ECMA_OBJECT_TYPE_BUILT_IN_GENERAL,
ECMA_BUILTIN_ID_OBJECT_PROTOTYPE,
true,
atomics)
#endif /* JERRY_BUILTIN_ATOMICS */
#if JERRY_BUILTIN_DATE
/* The Date object (15.9.3) */
BUILTIN_ROUTINE (ECMA_BUILTIN_ID_DATE,
@@ -0,0 +1,259 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ecma-atomics-object.h"
#include "ecma-arraybuffer-object.h"
#include "ecma-shared-arraybuffer-object.h"
#include "ecma-bigint.h"
#include "ecma-typedarray-object.h"
#include "ecma-objects.h"
#include "ecma-builtins.h"
#include "ecma-exceptions.h"
#include "ecma-gc.h"
#include "ecma-globals.h"
#include "ecma-helpers.h"
#include "jmem.h"
#include "jcontext.h"
#include "ecma-function-object.h"
#if JERRY_BUILTIN_ATOMICS
/** \addtogroup ecma ECMA
* @{
*
* \addtogroup ecmaatomicsobject ECMA builtin Atomics helper functions
* @{
*/
/**
* Atomics validate Shared integer typedArray
*
* See also: ES11 24.4.1.1
*
* @return ecma value
*/
ecma_value_t
ecma_validate_shared_integer_typedarray (ecma_value_t typedarray, /**< typedArray argument */
bool waitable) /**< waitable argument */
{
/* 2. */
if (!ecma_is_value_object (typedarray))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not an object"));
}
/* 3-4. */
ecma_object_t *typedarray_p = ecma_get_object_from_value (typedarray);
ecma_typedarray_info_t target_info = ecma_typedarray_get_info (typedarray_p);
/* 5-6. */
if (waitable)
{
if (!(target_info.id == ECMA_BIGINT64_ARRAY || target_info.id == ECMA_INT32_ARRAY))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument is not supported"));
}
}
else
{
if (target_info.id == ECMA_UINT8_CLAMPED_ARRAY || target_info.id == ECMA_FLOAT32_ARRAY
|| target_info.id == ECMA_FLOAT64_ARRAY)
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument is not supported"));
}
}
/* 7. */
JERRY_ASSERT (target_info.array_buffer_p != NULL);
/* 8-10. */
ecma_object_t *buffer = ecma_typedarray_get_arraybuffer (typedarray_p);
if (!ecma_object_class_is (buffer, ECMA_OBJECT_CLASS_SHARED_ARRAY_BUFFER))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument is not SharedArrayBuffer"));
}
return ecma_make_object_value (buffer);
} /* ecma_validate_shared_integer_typedarray */
/**
* Atomics validate Atomic Access
*
* See also: ES11 24.4.1.2
*
* @return ecma value
*/
ecma_value_t
ecma_validate_atomic_access (ecma_value_t typedarray, /**< typedArray argument */
ecma_value_t request_index) /**< request_index argument */
{
/* 1. */
JERRY_ASSERT (ecma_is_value_object (typedarray)
&& ecma_typedarray_get_arraybuffer (ecma_get_object_from_value (typedarray)) != NULL);
ecma_object_t *typedarray_p = ecma_get_object_from_value (typedarray);
/* 2. */
ecma_number_t access_index;
if (ECMA_IS_VALUE_ERROR (ecma_op_to_index (request_index, &access_index)))
{
return ECMA_VALUE_ERROR;
}
/* 3. */
ecma_typedarray_info_t target_info = ecma_typedarray_get_info (typedarray_p);
/* 4. */
JERRY_ASSERT (access_index >= 0);
/* 5-6. */
if (JERRY_UNLIKELY (access_index >= target_info.length))
{
return ecma_raise_range_error (ECMA_ERR_MSG ("Invalid length"));
}
return ecma_make_number_value (access_index);
} /* ecma_validate_atomic_access */
/**
* Atomics read, modify, write
*
* See also: ES11 24.4.1.11
*
* @return ecma value
*/
ecma_value_t
ecma_atomic_read_modify_write (ecma_value_t typedarray, /**< typedArray argument */
ecma_value_t index, /**< index argument */
ecma_value_t value, /**< value argument */
ecma_atomics_op_t op) /**< operation argument */
{
/* 1. */
ecma_value_t buffer = ecma_validate_shared_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))
{
return idx;
}
/* 3. */
ecma_object_t *typedarray_p = ecma_get_object_from_value (typedarray);
ecma_typedarray_info_t target_info = ecma_typedarray_get_info (typedarray_p);
/* 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)
{
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);
}
if (ECMA_IS_VALUE_ERROR (val))
{
return val;
}
/* 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;
JERRY_UNUSED (indexed_position);
JERRY_UNUSED (element_type);
JERRY_UNUSED (val);
JERRY_UNUSED (buffer);
JERRY_UNUSED (op);
ecma_free_value (val);
/* 10. */
return ecma_make_uint32_value (0);
} /* ecma_atomic_read_modify_write */
/**
* Atomics load
*
* See also: ES11 24.4.1.12
*
* @return ecma value
*/
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);
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))
{
return idx;
}
/* 3. */
ecma_object_t *typedarray_p = ecma_get_object_from_value (typedarray);
ecma_typedarray_info_t target_info = ecma_typedarray_get_info (typedarray_p);
/* 4. */
uint8_t element_size = target_info.element_size;
/* 5. */
ecma_typedarray_type_t element_type = target_info.id;
/* 6. */
uint32_t offset = target_info.offset;
/* 7. */
uint32_t indexed_position = ecma_number_to_uint32 (idx) * element_size + offset;
JERRY_UNUSED (indexed_position);
JERRY_UNUSED (element_type);
JERRY_UNUSED (buffer);
/* 8. */
return ecma_make_uint32_value (0);
} /* ecma_atomic_load */
/**
* @}
* @}
*/
#endif /* JERRY_BUILTIN_ATOMICS */
@@ -0,0 +1,65 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ECMA_ATOMICS_OBJECT_H
#define ECMA_ATOMICS_OBJECT_H
#include "ecma-globals.h"
#if JERRY_BUILTIN_ATOMICS
/** \addtogroup ecma ECMA
* @{
*
* \addtogroup ecmaatomicsobject ECMA builtin Atomics helper functions
* @{
*/
/**
* Atomics flags.
*/
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_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_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);
/**
* @}
* @}
*/
#endif /* JERRY_BUILTIN_ATOMICS */
#endif /* ECMA_ATOMICS_OBJECT_H */