Implemented Number.prototype.toFixed()

JerryScript-DCO-1.0-Signed-off-by: László Langó llango.u-szeged@partner.samsung.com
JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai.u-szeged@partner.samsung.com
JerryScript-DCO-1.0-Signed-off-by: Tamas Czene tczene.u-szeged@partner.samsung.com
This commit is contained in:
Tamas Czene
2015-05-29 11:28:50 +02:00
committed by Dániel Bátyai
parent 7d4569a6ff
commit 4836d3b615
5 changed files with 425 additions and 180 deletions
@@ -1,4 +1,5 @@
/* Copyright 2014-2015 Samsung Electronics Co., Ltd.
* Copyright 2015 University of Szeged.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -44,6 +45,34 @@
* @{
*/
/**
* Helper for rounding numbers
*
* @return rounded number
*/
static uint64_t
ecma_builtin_number_prototype_helper_round (uint64_t digits, /**< actual number **/
int32_t round_num) /**< number of digits to round off **/
{
int8_t digit = 0;
/* Remove unneeded precision digits. */
while (round_num > 0)
{
digit = (int8_t) (digits % 10);
digits /= 10;
round_num--;
}
/* Round the last digit up if neccessary */
if (digit >= 5)
{
digits++;
}
return digits;
} /* ecma_builtin_number_prototype_helper_round */
/**
* The Number.prototype object's 'toString' routine
*
@@ -166,7 +195,185 @@ static ecma_completion_value_t
ecma_builtin_number_prototype_object_to_fixed (ecma_value_t this_arg, /**< this argument */
ecma_value_t arg) /**< routine's argument */
{
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg);
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
ECMA_OP_TO_NUMBER_TRY_CATCH (this_num, this_arg, ret_value);
ECMA_OP_TO_NUMBER_TRY_CATCH (arg_num, arg, ret_value);
/* 1. */
int32_t frac_digits = ecma_number_to_int32 (arg_num);
/* 2. */
if (frac_digits < 0 || frac_digits > 20)
{
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_RANGE));
}
else
{
/* 4. */
if (ecma_number_is_nan (this_num))
{
ecma_string_t *nan_str_p = ecma_get_magic_string (ECMA_MAGIC_STRING_NAN);
ret_value = ecma_make_normal_completion_value (ecma_make_string_value (nan_str_p));
}
else
{
bool is_negative = false;
/* 6. */
if (ecma_number_is_negative (this_num))
{
is_negative = true;
this_num *= -1;
}
/* We handle infinities separately. */
if (ecma_number_is_infinity (this_num))
{
ecma_string_t *infinity_str_p = ecma_get_magic_string (ECMA_MAGIC_STRING_INFINITY_UL);
if (is_negative)
{
ecma_string_t *neg_str_p = ecma_new_ecma_string ((const ecma_char_t *) "-");
ecma_string_t *neg_inf_str_p = ecma_concat_ecma_strings (neg_str_p, infinity_str_p);
ecma_deref_ecma_string (infinity_str_p);
ecma_deref_ecma_string (neg_str_p);
ret_value = ecma_make_normal_completion_value (ecma_make_string_value (neg_inf_str_p));
}
else
{
ret_value = ecma_make_normal_completion_value (ecma_make_string_value (infinity_str_p));
}
}
else
{
uint64_t digits = 0;
int32_t num_digits = 0;
int32_t exponent = 1;
/* Get the parameters of the number if non-zero. */
if (!ecma_number_is_zero (this_num))
{
ecma_number_to_decimal (this_num, &digits, &num_digits, &exponent);
}
digits = ecma_builtin_number_prototype_helper_round (digits, num_digits - exponent - frac_digits);
/* 7. */
if (exponent > 21)
{
ret_value = ecma_builtin_number_prototype_object_to_string (this_arg, NULL, 0);
}
/* 8. */
else
{
/* Buffer that is used to construct the string. */
int buffer_size = (exponent > 0) ? exponent + frac_digits + 1 : frac_digits + 2;
JERRY_ASSERT (buffer_size > 0);
MEM_DEFINE_LOCAL_ARRAY (buff, buffer_size, ecma_char_t);
ecma_char_t* p = buff;
if (is_negative)
{
*p++ = '-';
}
int8_t digit = 0;
uint64_t s = 1;
/* Calculate the magnitude of the number. This is used to get the digits from left to right. */
while (s <= digits)
{
s *= 10;
}
if (exponent <= 0)
{
/* Add leading zeros. */
*p++ = '0';
if (frac_digits != 0)
{
*p++ = '.';
}
for (int i = 0; i < -exponent && i < frac_digits; i++)
{
*p++ = '0';
}
/* Add significant digits. */
for (int i = -exponent; i < frac_digits; i++)
{
digit = 0;
s /= 10;
while (digits >= s && s > 0)
{
digits -= s;
digit++;
}
*p = (ecma_char_t) ((ecma_char_t) digit + '0');
p++;
}
}
else
{
/* Add significant digits. */
for (int i = 0; i < exponent; i++)
{
digit = 0;
s /= 10;
while (digits >= s && s > 0)
{
digits -= s;
digit++;
}
*p = (ecma_char_t) ((ecma_char_t) digit + '0');
p++;
}
/* Add the decimal point after whole part. */
if (frac_digits != 0)
{
*p++ = '.';
}
/* Add neccessary fracion digits. */
for (int i = 0; i < frac_digits; i++)
{
digit = 0;
s /= 10;
while (digits >= s && s > 0)
{
digits -= s;
digit++;
}
*p = (ecma_char_t) ((ecma_char_t) digit + '0');
p++;
}
}
/* String terminator. */
*p = 0;
ecma_string_t* str = ecma_new_ecma_string ((ecma_char_t *) buff);
ret_value = ecma_make_normal_completion_value (ecma_make_string_value (str));
MEM_FINALIZE_LOCAL_ARRAY (buff);
}
}
}
}
ECMA_OP_TO_NUMBER_FINALIZE (arg_num);
ECMA_OP_TO_NUMBER_FINALIZE (this_num);
return ret_value;
} /* ecma_builtin_number_prototype_object_to_fixed */
/**