Implement BigInt primitve type and some of its operations (#4062)

Supported operations:
- parse BigInt (decimal, hexadecimal, binary)
- toString with any radix between 2 and 36
- arithmetic operations: negate, add, subtract, multiply, divide, modulo
- left and right shift

JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
Zoltan Herczeg
2020-07-30 12:08:34 +02:00
committed by GitHub
parent 3eb69075f7
commit df2f7782f7
29 changed files with 3237 additions and 192 deletions
+51 -21
View File
@@ -20,6 +20,8 @@
#include <math.h>
#include "ecma-alloc.h"
#include "ecma-bigint.h"
#include "ecma-bigint-object.h"
#include "ecma-boolean-object.h"
#include "ecma-conversion.h"
#include "ecma-exceptions.h"
@@ -279,12 +281,6 @@ ecma_op_to_number (ecma_value_t value) /**< ecma value */
ecma_string_t *str_p = ecma_get_string_from_value (value);
return ecma_make_number_value (ecma_string_to_number (str_p));
}
#if ENABLED (JERRY_ESNEXT)
if (ecma_is_value_symbol (value))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Cannot convert a Symbol value to a number."));
}
#endif /* ENABLED (JERRY_ESNEXT) */
if (ecma_is_value_undefined (value))
{
@@ -301,6 +297,20 @@ ecma_op_to_number (ecma_value_t value) /**< ecma value */
return ecma_make_integer_value (ecma_is_value_true (value) ? 1 : 0);
}
#if ENABLED (JERRY_ESNEXT)
if (ecma_is_value_symbol (value))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Cannot convert a Symbol value to a number"));
}
#endif /* ENABLED (JERRY_ESNEXT) */
#if ENABLED (JERRY_BUILTIN_BIGINT)
if (ecma_is_value_bigint (value))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Cannot convert a BigInt value to a number"));
}
#endif /* ENABLED (JERRY_BUILTIN_BIGINT) */
JERRY_ASSERT (ecma_is_value_object (value));
ecma_object_t *obj_p = ecma_get_object_from_value (value);
@@ -364,13 +374,6 @@ ecma_get_number (ecma_value_t value, /**< ecma value*/
return ECMA_VALUE_EMPTY;
}
#if ENABLED (JERRY_ESNEXT)
if (ecma_is_value_symbol (value))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Cannot convert a Symbol value to a number."));
}
#endif /* ENABLED (JERRY_ESNEXT) */
if (ecma_is_value_true (value))
{
*number_p = 1;
@@ -383,6 +386,20 @@ ecma_get_number (ecma_value_t value, /**< ecma value*/
return ECMA_VALUE_EMPTY;
}
#if ENABLED (JERRY_ESNEXT)
if (ecma_is_value_symbol (value))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Cannot convert a Symbol value to a number."));
}
#endif /* ENABLED (JERRY_ESNEXT) */
#if ENABLED (JERRY_BUILTIN_BIGINT)
if (ecma_is_value_bigint (value))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Cannot convert a BigInt value to a number"));
}
#endif /* ENABLED (JERRY_BUILTIN_BIGINT) */
JERRY_ASSERT (ecma_is_value_object (value));
ecma_object_t *obj_p = ecma_get_object_from_value (value);
@@ -452,14 +469,6 @@ ecma_op_to_string (ecma_value_t value) /**< ecma value */
return ecma_get_magic_string (LIT_MAGIC_STRING_NULL);
}
#if ENABLED (JERRY_ESNEXT)
if (ecma_is_value_symbol (value))
{
ecma_raise_type_error (ECMA_ERR_MSG ("Cannot convert a Symbol value to a string."));
return NULL;
}
#endif /* ENABLED (JERRY_ESNEXT) */
if (ecma_is_value_true (value))
{
return ecma_get_magic_string (LIT_MAGIC_STRING_TRUE);
@@ -470,6 +479,21 @@ ecma_op_to_string (ecma_value_t value) /**< ecma value */
return ecma_get_magic_string (LIT_MAGIC_STRING_FALSE);
}
#if ENABLED (JERRY_ESNEXT)
if (ecma_is_value_symbol (value))
{
ecma_raise_type_error (ECMA_ERR_MSG ("Cannot convert a Symbol value to a string."));
return NULL;
}
#endif /* ENABLED (JERRY_ESNEXT) */
#if ENABLED (JERRY_BUILTIN_BIGINT)
if (ecma_is_value_bigint (value))
{
return ecma_bigint_to_string (value, 10);
}
#endif /* ENABLED (JERRY_BUILTIN_BIGINT) */
JERRY_ASSERT (ecma_is_value_object (value));
ecma_object_t *obj_p = ecma_get_object_from_value (value);
@@ -567,6 +591,12 @@ ecma_op_to_object (ecma_value_t value) /**< ecma value */
return ecma_op_create_symbol_object (value);
}
#endif /* ENABLED (JERRY_ESNEXT) */
#if ENABLED (JERRY_BUILTIN_BIGINT)
else if (ecma_is_value_bigint (value))
{
return ecma_op_create_bigint_object (value);
}
#endif /* ENABLED (JERRY_BUILTIN_BIGINT) */
else
{
if (ecma_is_value_undefined (value)