Skip to content

Commit 1872c21

Browse files
committed
Fix bound function length
JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai.u-szeged@partner.samsung.com
1 parent a19dd05 commit 1872c21

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

jerry-core/ecma/builtin-objects/ecma-builtin-function-prototype.cpp

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,38 @@ ecma_builtin_function_prototype_object_bind (ecma_value_t this_arg, /**< this ar
279279
* See also: ecma_object_get_class_name
280280
*/
281281

282-
/* 17. */
283282
ecma_number_t *length_p = ecma_alloc_number ();
284-
*length_p = 0;
283+
ecma_string_t *magic_string_length_p = ecma_get_magic_string (LIT_MAGIC_STRING_LENGTH);
284+
285+
/* 15. */
286+
if (ecma_object_get_class_name (this_arg_obj_p) == LIT_MAGIC_STRING_FUNCTION_UL)
287+
{
288+
ecma_completion_value_t get_len_completion = ecma_op_object_get (this_arg_obj_p,
289+
magic_string_length_p);
290+
JERRY_ASSERT (ecma_is_completion_value_normal (get_len_completion));
291+
292+
ecma_value_t len_value = ecma_get_completion_value_value (get_len_completion);
293+
JERRY_ASSERT (ecma_is_value_number (len_value));
294+
295+
const ecma_length_t bound_arg_count = arg_count > 1 ? arg_count - 1 : 0;
296+
297+
/* 15.a */
298+
*length_p = *ecma_get_number_from_value (len_value) - ecma_uint32_to_number (bound_arg_count);
299+
ecma_free_completion_value (get_len_completion);
300+
301+
/* 15.b */
302+
if (ecma_number_is_negative (*length_p))
303+
{
304+
*length_p = ECMA_NUMBER_ZERO;
305+
}
306+
}
307+
else
308+
{
309+
/* 16. */
310+
*length_p = ECMA_NUMBER_ZERO;
311+
}
285312

313+
/* 17. */
286314
ecma_property_descriptor_t length_prop_desc = ecma_make_empty_property_descriptor ();
287315
{
288316
length_prop_desc.is_value_defined = true;
@@ -297,7 +325,6 @@ ecma_builtin_function_prototype_object_bind (ecma_value_t this_arg, /**< this ar
297325
length_prop_desc.is_configurable_defined = true;
298326
length_prop_desc.is_configurable = false;
299327
}
300-
ecma_string_t *magic_string_length_p = ecma_get_magic_string (LIT_MAGIC_STRING_LENGTH);
301328
ecma_completion_value_t completion = ecma_op_object_define_own_property (function_p,
302329
magic_string_length_p,
303330
&length_prop_desc,

tests/jerry/function-prototype-bind.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,15 @@ try {
126126
} catch (e) {
127127
assert (e instanceof TypeError);
128128
}
129+
130+
var foo = function(x, y) { }
131+
132+
var bound = foo.bind(null);
133+
assert(bound.length === 2);
134+
135+
bound = foo.bind(null, 9);
136+
assert(bound.length === 1);
137+
138+
bound = foo.bind(null, 9, 8);
139+
assert(bound.length === 0);
140+

0 commit comments

Comments
 (0)