Implemented Number object routines (#2972)

Implemented Number.isNaN & Number.isFinite &
Number.isSafeInteger & Number.isInteger & Number.EPSILON.

JerryScript-DCO-1.0-Signed-off-by: Virag Orkenyi orkvi@inf.u-szeged.hu
This commit is contained in:
Virag Orkenyi
2019-08-27 10:18:51 +02:00
committed by Dániel Bátyai
parent 47f2f0ea8b
commit d0435e1db0
11 changed files with 249 additions and 2 deletions
@@ -24,6 +24,7 @@
#include "ecma-objects.h"
#include "ecma-try-catch-macro.h"
#include "jrt.h"
#include "math.h"
#if ENABLED (JERRY_BUILTIN_NUMBER)
@@ -91,6 +92,134 @@ ecma_builtin_number_dispatch_construct (const ecma_value_t *arguments_list_p, /*
}
} /* ecma_builtin_number_dispatch_construct */
#if ENABLED (JERRY_ES2015_BUILTIN)
/**
* The Number object 'isFinite' routine
*
* See also:
* ECMA-262 v6, 20.1.2.2
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_builtin_number_object_is_finite (ecma_value_t this_arg, /**< this argument */
ecma_value_t arg) /**< routine's argument */
{
JERRY_UNUSED (this_arg);
if (ecma_is_value_number (arg))
{
ecma_number_t num = ecma_get_number_from_value (arg);
if (!(ecma_number_is_nan (num) || ecma_number_is_infinity (num)))
{
return ECMA_VALUE_TRUE;
}
}
return ECMA_VALUE_FALSE;
} /* ecma_builtin_number_object_is_finite */
/**
* The Number object 'isNan' routine
*
* See also:
* ECMA-262 v6, 20.1.2.4
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_builtin_number_object_is_nan (ecma_value_t this_arg, /**< this argument */
ecma_value_t arg) /**< routine's argument */
{
JERRY_UNUSED (this_arg);
if (ecma_is_value_number (arg))
{
ecma_number_t num_val = ecma_get_number_from_value (arg);
if (ecma_number_is_nan (num_val))
{
return ECMA_VALUE_TRUE;
}
}
return ECMA_VALUE_FALSE;
} /* ecma_builtin_number_object_is_nan */
/**
* The Number object 'isInteger' routine
*
* See also:
* ECMA-262 v6, 20.1.2.3
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_builtin_number_object_is_integer (ecma_value_t this_arg, /**< this argument */
ecma_value_t arg) /**< routine's argument */
{
JERRY_UNUSED (this_arg);
if (!ecma_is_value_number (arg))
{
return ECMA_VALUE_FALSE;
}
ecma_number_t num = ecma_get_number_from_value (arg);
if (ecma_number_is_nan (num) || ecma_number_is_infinity (num))
{
return ECMA_VALUE_FALSE;
}
ecma_number_t int_num = ecma_number_trunc (num);
if (int_num != num)
{
return ECMA_VALUE_FALSE;
}
return ECMA_VALUE_TRUE;
} /* ecma_builtin_number_object_is_integer */
/**
* The Number object 'isSafeInteger' routine
*
* See also:
* ECMA-262 v6, 20.1.2.3
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_builtin_number_object_is_safe_integer (ecma_value_t this_arg, /**< this argument */
ecma_value_t arg) /**< routine's argument */
{
JERRY_UNUSED (this_arg);
if (!ecma_is_value_number (arg))
{
return ECMA_VALUE_FALSE;
}
ecma_number_t num = ecma_get_number_from_value (arg);
if (ecma_number_is_nan (num) || ecma_number_is_infinity (num))
{
return ECMA_VALUE_FALSE;
}
ecma_number_t int_num = ecma_number_trunc (num);
if (int_num == num && fabs (int_num) <= ECMA_NUMBER_MAX_SAFE_INTEGER)
{
return ECMA_VALUE_TRUE;
}
return ECMA_VALUE_FALSE;
} /* ecma_builtin_number_object_is_safe_integer */
#endif /* ENABLED (JERRY_ES2015_BUILTIN) */
/**
* @}
* @}
@@ -64,4 +64,12 @@ OBJECT_VALUE (LIT_MAGIC_STRING_PROTOTYPE,
#endif /* ENABLED (JERRY_BUILTIN_NUMBER) */
/* Routine properties:
* (property name, C routine name, arguments number or NON_FIXED, value of the routine's length property) */
#if ENABLED (JERRY_ES2015_BUILTIN)
ROUTINE (LIT_MAGIC_STRING_IS_FINITE, ecma_builtin_number_object_is_finite, 1, 1)
ROUTINE (LIT_MAGIC_STRING_IS_NAN, ecma_builtin_number_object_is_nan, 1, 1)
ROUTINE (LIT_MAGIC_STRING_IS_INTEGER, ecma_builtin_number_object_is_integer, 1, 1)
ROUTINE (LIT_MAGIC_STRING_IS_SAFE_INTEGER, ecma_builtin_number_object_is_safe_integer, 1, 1)
#endif /* ENABLED (JERRY_ES2015_BUILTIN) */
#include "ecma-builtin-helpers-macro-undefs.inc.h"
@@ -48,6 +48,10 @@ typedef enum
{
ECMA_BUILTIN_NUMBER_MAX = 256, /**< value of ECMA_NUMBER_MAX_VALUE */
ECMA_BUILTIN_NUMBER_MIN, /**< value of ECMA_NUMBER_MIN_VALUE */
#if ENABLED (JERRY_ES2015_BUILTIN)
ECMA_BUILTIN_NUMBER_EPSILON, /**< value of ECMA_NUMBER_EPSILON */
ECMA_BUILTIN_NUMBER_MAX_SAFE_INTEGER, /**< value of ECMA_NUMBER_MAX_SAFE_INTEGER */
#endif /* ENABLED (JERRY_BUILTIN_NUMBER) */
ECMA_BUILTIN_NUMBER_E, /**< value of ECMA_NUMBER_E */
ECMA_BUILTIN_NUMBER_PI, /**< value of ECMA_NUMBER_PI */
ECMA_BUILTIN_NUMBER_LN10, /**< value of ECMA_NUMBER_LN10 */
@@ -741,6 +741,10 @@ ecma_builtin_try_to_instantiate_property (ecma_object_t *object_p, /**< object *
{
ECMA_NUMBER_MAX_VALUE,
ECMA_NUMBER_MIN_VALUE,
#if ENABLED (JERRY_ES2015_BUILTIN)
ECMA_NUMBER_EPSILON,
ECMA_NUMBER_MAX_SAFE_INTEGER,
#endif /* ENABLED (JERRY_BUILTIN_NUMBER) */
ECMA_NUMBER_E,
ECMA_NUMBER_PI,
ECMA_NUMBER_LN10,
@@ -748,7 +752,7 @@ ecma_builtin_try_to_instantiate_property (ecma_object_t *object_p, /**< object *
ECMA_NUMBER_LOG2E,
ECMA_NUMBER_LOG10E,
ECMA_NUMBER_SQRT2,
ECMA_NUMBER_SQRT_1_2
ECMA_NUMBER_SQRT_1_2,
};
num = builtin_number_list[curr_property_p->value - ECMA_BUILTIN_NUMBER_MAX];