Skip to content

Commit 314c6c2

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 314c6c2

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,36 @@ 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. */
282+
/* 16. */
283283
ecma_number_t *length_p = ecma_alloc_number ();
284-
*length_p = 0;
284+
*length_p = ECMA_NUMBER_ZERO;
285+
286+
ecma_string_t *magic_string_length_p = ecma_get_magic_string (LIT_MAGIC_STRING_LENGTH);
287+
288+
/* 15. */
289+
if (ecma_object_get_class_name (this_arg_obj_p) == LIT_MAGIC_STRING_FUNCTION_UL)
290+
{
291+
ecma_completion_value_t get_len_completion = ecma_op_object_get (this_arg_obj_p,
292+
magic_string_length_p);
293+
JERRY_ASSERT (ecma_is_completion_value_normal (get_len_completion));
294+
295+
ecma_value_t len_value = ecma_get_completion_value_value (get_len_completion);
296+
JERRY_ASSERT (ecma_is_value_number (len_value));
297+
298+
const ecma_length_t bound_arg_count = arg_count > 1 ? arg_count - 1 : 0;
285299

300+
/* 15.a */
301+
*length_p = *ecma_get_number_from_value (len_value) - ecma_uint32_to_number (bound_arg_count);
302+
ecma_free_completion_value (get_len_completion);
303+
304+
/* 15.b */
305+
if (ecma_number_is_negative (*length_p))
306+
{
307+
*length_p = ECMA_NUMBER_ZERO;
308+
}
309+
}
310+
311+
/* 17. */
286312
ecma_property_descriptor_t length_prop_desc = ecma_make_empty_property_descriptor ();
287313
{
288314
length_prop_desc.is_value_defined = true;
@@ -297,7 +323,6 @@ ecma_builtin_function_prototype_object_bind (ecma_value_t this_arg, /**< this ar
297323
length_prop_desc.is_configurable_defined = true;
298324
length_prop_desc.is_configurable = false;
299325
}
300-
ecma_string_t *magic_string_length_p = ecma_get_magic_string (LIT_MAGIC_STRING_LENGTH);
301326
ecma_completion_value_t completion = ecma_op_object_define_own_property (function_p,
302327
magic_string_length_p,
303328
&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)