Skip to content

Number.prototype functions should throw TypeError if 'this' is not number. #382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 17 additions & 38 deletions jerry-core/ecma/builtin-objects/ecma-builtin-number-prototype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,36 +89,10 @@ ecma_builtin_number_prototype_object_to_string (ecma_value_t this_arg, /**< this
const ecma_value_t* arguments_list_p, /**< arguments list */
ecma_length_t arguments_list_len) /**< number of arguments */
{
ecma_number_t this_arg_number;

if (ecma_is_value_number (this_arg))
{
ecma_number_t *this_arg_number_p = ecma_get_number_from_value (this_arg);

this_arg_number = *this_arg_number_p;
}
else if (ecma_is_value_object (this_arg))
{
ecma_object_t *obj_p = ecma_get_object_from_value (this_arg);
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();

if (ecma_object_get_class_name (obj_p) == LIT_MAGIC_STRING_NUMBER_UL)
{
ecma_property_t *prim_value_prop_p = ecma_get_internal_property (obj_p,
ECMA_INTERNAL_PROPERTY_PRIMITIVE_NUMBER_VALUE);

ecma_number_t *prim_value_num_p = ECMA_GET_NON_NULL_POINTER (ecma_number_t,
prim_value_prop_p->u.internal_property.value);
this_arg_number = *prim_value_num_p;
}
else
{
return ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE));
}
}
else
{
return ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE));
}
ECMA_TRY_CATCH (this_value, ecma_builtin_number_prototype_object_value_of (this_arg), ret_value);
ecma_number_t this_arg_number = *ecma_get_number_from_value (this_value);

if (arguments_list_len == 0
|| ecma_number_is_nan (this_arg_number)
Expand All @@ -127,7 +101,7 @@ ecma_builtin_number_prototype_object_to_string (ecma_value_t this_arg, /**< this
{
ecma_string_t *ret_str_p = ecma_new_ecma_string_from_number (this_arg_number);

return ecma_make_normal_completion_value (ecma_make_string_value (ret_str_p));
ret_value = ecma_make_normal_completion_value (ecma_make_string_value (ret_str_p));
}
else
{
Expand All @@ -139,7 +113,6 @@ ecma_builtin_number_prototype_object_to_string (ecma_value_t this_arg, /**< this
'u', 'v', 'w', 'x', 'y', 'z'
};

ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
ECMA_OP_TO_NUMBER_TRY_CATCH (arg_num, arguments_list_p[0], ret_value);

uint32_t radix = ecma_number_to_uint32 (arg_num);
Expand Down Expand Up @@ -333,8 +306,9 @@ ecma_builtin_number_prototype_object_to_string (ecma_value_t this_arg, /**< this
MEM_FINALIZE_LOCAL_ARRAY (buff);
}
ECMA_OP_TO_NUMBER_FINALIZE (arg_num);
return ret_value;
}
ECMA_FINALIZE (this_value);
return ret_value;
} /* ecma_builtin_number_prototype_object_to_string */

/**
Expand Down Expand Up @@ -405,7 +379,9 @@ ecma_builtin_number_prototype_object_to_fixed (ecma_value_t this_arg, /**< this
{
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();

ECMA_OP_TO_NUMBER_TRY_CATCH (this_num, this_arg, ret_value);
ECMA_TRY_CATCH (this_value, ecma_builtin_number_prototype_object_value_of (this_arg), ret_value);
ecma_number_t this_num = *ecma_get_number_from_value (this_value);

ECMA_OP_TO_NUMBER_TRY_CATCH (arg_num, arg, ret_value);

/* 2. */
Expand Down Expand Up @@ -587,7 +563,7 @@ ecma_builtin_number_prototype_object_to_fixed (ecma_value_t this_arg, /**< this
}

ECMA_OP_TO_NUMBER_FINALIZE (arg_num);
ECMA_OP_TO_NUMBER_FINALIZE (this_num);
ECMA_FINALIZE (this_value);
return ret_value;
} /* ecma_builtin_number_prototype_object_to_fixed */

Expand All @@ -607,7 +583,9 @@ ecma_builtin_number_prototype_object_to_exponential (ecma_value_t this_arg, /**<
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();

/* 1. */
ECMA_OP_TO_NUMBER_TRY_CATCH (this_num, this_arg, ret_value);
ECMA_TRY_CATCH (this_value, ecma_builtin_number_prototype_object_value_of (this_arg), ret_value);
ecma_number_t this_num = *ecma_get_number_from_value (this_value);

ECMA_OP_TO_NUMBER_TRY_CATCH (arg_num, arg, ret_value);

/* 7. */
Expand Down Expand Up @@ -770,7 +748,7 @@ ecma_builtin_number_prototype_object_to_exponential (ecma_value_t this_arg, /**<
}

ECMA_OP_TO_NUMBER_FINALIZE (arg_num);
ECMA_OP_TO_NUMBER_FINALIZE (this_num);
ECMA_FINALIZE (this_value);
return ret_value;
} /* ecma_builtin_number_prototype_object_to_exponential */

Expand All @@ -790,7 +768,8 @@ ecma_builtin_number_prototype_object_to_precision (ecma_value_t this_arg, /**< t
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();

/* 1. */
ECMA_OP_TO_NUMBER_TRY_CATCH (this_num, this_arg, ret_value);
ECMA_TRY_CATCH (this_value, ecma_builtin_number_prototype_object_value_of (this_arg), ret_value);
ecma_number_t this_num = *ecma_get_number_from_value (this_value);

/* 2. */
if (ecma_is_value_undefined (arg))
Expand Down Expand Up @@ -1001,7 +980,7 @@ ecma_builtin_number_prototype_object_to_precision (ecma_value_t this_arg, /**< t
}
ECMA_OP_TO_NUMBER_FINALIZE (arg_num);
}
ECMA_OP_TO_NUMBER_FINALIZE (this_num);
ECMA_FINALIZE (this_value);

return ret_value;
} /* ecma_builtin_number_prototype_object_to_precision */
Expand Down
7 changes: 7 additions & 0 deletions tests/jerry/number-prototype-to-exponential.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,10 @@ try {
} catch (e) {
assert(e instanceof RangeError)
}

try {
Number.prototype.toExponential.call(new Object());
assert(false);
} catch (e) {
assert(e instanceof TypeError)
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,11 @@ assert((123456789012345678901.0).toFixed(20) === "123456789012345680000.00000000
assert((123.56).toFixed(NaN) === "124");
assert((123.56).toFixed(-0.9) === "124");

var obj = { toFixed : Number.prototype.toFixed };
assert(obj.toFixed(0) === "NaN");

try {
assert(obj.toFixed(Infinity));
assert(false);
} catch (e) {
assert(e instanceof RangeError);
}

try {
assert(obj.toFixed(-Infinity));
assert(false);
} catch (e) {
assert(e instanceof RangeError);
}

try {
assert(obj.toFixed(-1));
Number.prototype.toExponential.call(new Object());
assert(false);
} catch (e) {
assert(e instanceof RangeError);
assert(e instanceof TypeError)
}

try {
Expand Down
9 changes: 7 additions & 2 deletions tests/jerry/number-prototype-to-precision.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ assert((123456789012345678901.0).toPrecision(20) === "1.2345678901234568000e+20"
assert((123456789012345678901.0).toPrecision(21) === "123456789012345680000");
assert((123456789012345678901.0).toPrecision("6") === "1.23457e+20");

var obj = { toPrecision : Number.prototype.toPrecision };
assert(obj.toPrecision(1) === "NaN");
assert((123.56).toPrecision(1.3) === "1e+2");
assert((123.56).toPrecision(21.9) === "123.560000000000000000");

Expand All @@ -52,3 +50,10 @@ try {
} catch (e) {
assert(e instanceof RangeError)
}

try {
Number.prototype.toExponential.call(new Object());
assert(false);
} catch (e) {
assert(e instanceof TypeError)
}