Skip to content

Fix bound function length #547

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

Merged
merged 1 commit into from
Aug 10, 2015
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,38 @@ ecma_builtin_function_prototype_object_bind (ecma_value_t this_arg, /**< this ar
* See also: ecma_object_get_class_name
*/

/* 17. */
ecma_number_t *length_p = ecma_alloc_number ();
*length_p = 0;
ecma_string_t *magic_string_length_p = ecma_get_magic_string (LIT_MAGIC_STRING_LENGTH);

/* 15. */
if (ecma_object_get_class_name (this_arg_obj_p) == LIT_MAGIC_STRING_FUNCTION_UL)
{
ecma_completion_value_t get_len_completion = ecma_op_object_get (this_arg_obj_p,
magic_string_length_p);
JERRY_ASSERT (ecma_is_completion_value_normal (get_len_completion));

ecma_value_t len_value = ecma_get_completion_value_value (get_len_completion);
JERRY_ASSERT (ecma_is_value_number (len_value));

const ecma_length_t bound_arg_count = arg_count > 1 ? arg_count - 1 : 0;

/* 15.a */
*length_p = *ecma_get_number_from_value (len_value) - ecma_uint32_to_number (bound_arg_count);
ecma_free_completion_value (get_len_completion);

/* 15.b */
if (ecma_number_is_negative (*length_p))
{
*length_p = ECMA_NUMBER_ZERO;
}
}
else
{
/* 16. */
*length_p = ECMA_NUMBER_ZERO;
}

/* 17. */
ecma_property_descriptor_t length_prop_desc = ecma_make_empty_property_descriptor ();
{
length_prop_desc.is_value_defined = true;
Expand All @@ -297,7 +325,6 @@ ecma_builtin_function_prototype_object_bind (ecma_value_t this_arg, /**< this ar
length_prop_desc.is_configurable_defined = true;
length_prop_desc.is_configurable = false;
}
ecma_string_t *magic_string_length_p = ecma_get_magic_string (LIT_MAGIC_STRING_LENGTH);
ecma_completion_value_t completion = ecma_op_object_define_own_property (function_p,
magic_string_length_p,
&length_prop_desc,
Expand Down
12 changes: 12 additions & 0 deletions tests/jerry/function-prototype-bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,15 @@ try {
} catch (e) {
assert (e instanceof TypeError);
}

var foo = function(x, y) { }

var bound = foo.bind(null);
assert(bound.length === 2);

bound = foo.bind(null, 9);
assert(bound.length === 1);

bound = foo.bind(null, 9, 8);
assert(bound.length === 0);