Skip to content

Commit

Permalink
Implement Function.prototype.bind function
Browse files Browse the repository at this point in the history
JerryScript-DCO-1.0-Signed-off-by: Zsolt Borbély [email protected]
  • Loading branch information
Zsolt Borbély committed Jul 7, 2015
1 parent cadc8f4 commit 79ae006
Show file tree
Hide file tree
Showing 6 changed files with 422 additions and 13 deletions.
21 changes: 21 additions & 0 deletions jerry-core/ecma/base/ecma-gc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,31 @@ ecma_gc_mark (ecma_object_t *object_p) /**< object to mark from */
case ECMA_INTERNAL_PROPERTY_NON_INSTANTIATED_BUILT_IN_MASK_0_31: /* an integer (bit-mask) */
case ECMA_INTERNAL_PROPERTY_NON_INSTANTIATED_BUILT_IN_MASK_32_63: /* an integer (bit-mask) */
case ECMA_INTERNAL_PROPERTY_REGEXP_BYTECODE:
case ECMA_INTERNAL_PROPERTY_BOUND_FUNCTION_TARGET_FUNCTION:
case ECMA_INTERNAL_PROPERTY_BOUND_FUNCTION_BOUND_THIS:
{
break;
}

case ECMA_INTERNAL_PROPERTY_BOUND_FUNCTION_BOUND_ARGS:
{
ecma_value_t *arg_list_p = ECMA_GET_NON_NULL_POINTER (ecma_value_t, property_value);
ecma_number_t *count_p = ecma_get_number_from_value (arg_list_p[0]);
const uint32_t count = ecma_number_to_uint32 (*count_p);

for (uint32_t i = 1; i <= count; i++)
{
if (ecma_is_value_object (arg_list_p[i]))
{
ecma_object_t *obj_p = ecma_get_object_from_value (arg_list_p[i]);

ecma_gc_set_object_visited (obj_p, true);
}
}

break;
}

case ECMA_INTERNAL_PROPERTY_SCOPE: /* a lexical environment */
case ECMA_INTERNAL_PROPERTY_PARAMETERS_MAP: /* an object */
{
Expand Down
5 changes: 5 additions & 0 deletions jerry-core/ecma/base/ecma-globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@ typedef enum
/** Identifier of implementation-defined extension object */
ECMA_INTERNAL_PROPERTY_EXTENSION_ID,

/** Bound function internal properties **/
ECMA_INTERNAL_PROPERTY_BOUND_FUNCTION_TARGET_FUNCTION,
ECMA_INTERNAL_PROPERTY_BOUND_FUNCTION_BOUND_THIS,
ECMA_INTERNAL_PROPERTY_BOUND_FUNCTION_BOUND_ARGS,

/**
* Bit-mask of non-instantiated built-in's properties (bits 0-31)
*/
Expand Down
18 changes: 18 additions & 0 deletions jerry-core/ecma/base/ecma-helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -800,10 +800,28 @@ ecma_free_internal_property (ecma_property_t *property_p) /**< the property */
case ECMA_INTERNAL_PROPERTY_EXTENSION_ID: /* an integer */
case ECMA_INTERNAL_PROPERTY_NON_INSTANTIATED_BUILT_IN_MASK_0_31: /* an integer (bit-mask) */
case ECMA_INTERNAL_PROPERTY_NON_INSTANTIATED_BUILT_IN_MASK_32_63: /* an integer (bit-mask) */
case ECMA_INTERNAL_PROPERTY_BOUND_FUNCTION_TARGET_FUNCTION:
case ECMA_INTERNAL_PROPERTY_BOUND_FUNCTION_BOUND_THIS:
{
break;
}

case ECMA_INTERNAL_PROPERTY_BOUND_FUNCTION_BOUND_ARGS:
{
ecma_value_t *arg_list_p = ECMA_GET_NON_NULL_POINTER (ecma_value_t, property_value);
ecma_number_t *count_p = ecma_get_number_from_value (arg_list_p[0]);
const uint32_t count = ecma_number_to_uint32 (*count_p);

for (uint32_t i = 0; i <= count; i++)
{
ecma_free_value (arg_list_p[i], false);
}

mem_heap_free_block (arg_list_p);

break;
}

case ECMA_INTERNAL_PROPERTY__COUNT: /* not a real internal property type,
* but number of the real internal property types */
{
Expand Down
146 changes: 145 additions & 1 deletion jerry-core/ecma/builtin-objects/ecma-builtin-function-prototype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,151 @@ ecma_builtin_function_prototype_object_bind (ecma_value_t this_arg, /**< this ar
const ecma_value_t* arguments_list_p, /**< list of arguments */
ecma_length_t arguments_number) /**< number of arguments */
{
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arguments_list_p, arguments_number);
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();

/* 2. */
if (!ecma_op_is_callable (this_arg))
{
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE));
}
else
{
/* 4. 11. 18. */
ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_FUNCTION_PROTOTYPE);
ecma_object_t *function_p = ecma_create_object (prototype_obj_p, true, ECMA_OBJECT_TYPE_BOUND_FUNCTION);

ecma_deref_object (prototype_obj_p);

/* 7. */
ecma_property_t *target_function_prop_p;
target_function_prop_p = ecma_create_internal_property (function_p,
ECMA_INTERNAL_PROPERTY_BOUND_FUNCTION_TARGET_FUNCTION);

target_function_prop_p->u.internal_property.value = ecma_copy_value (this_arg, false);

/* 8. */
ecma_property_t *bound_this_prop_p;
bound_this_prop_p = ecma_create_internal_property (function_p, ECMA_INTERNAL_PROPERTY_BOUND_FUNCTION_BOUND_THIS);

size_t arg_list_size;

if (arguments_number > 0)
{
bound_this_prop_p->u.internal_property.value = ecma_copy_value (arguments_list_p[0], false);

arg_list_size = (size_t) (arguments_number) * sizeof (ecma_value_t);
}
else
{
ecma_object_t *glob_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_GLOBAL);
bound_this_prop_p->u.internal_property.value = ecma_copy_value (ecma_make_object_value (glob_obj_p), false);
ecma_deref_object (glob_obj_p);

arg_list_size = (size_t) sizeof (ecma_value_t);
}

/* 9. */
ecma_value_t *arg_list = static_cast <ecma_value_t *> (mem_heap_alloc_block (arg_list_size,
MEM_HEAP_ALLOC_SHORT_TERM));

ecma_number_t *count = ecma_alloc_number ();
*count = ecma_uint32_to_number (arguments_number);

if (arguments_number > 0)
{
(*count)--;
}

/*
* Set the first element of the arguments array to the count of optional arguments.
* We need the number of bounded arguments, when the target function is called.
*/
arg_list[0] = ecma_make_number_value (count);

for (uint32_t i = 1; i < arguments_number; i++)
{
arg_list[i] = ecma_copy_value (arguments_list_p[i], false);
}

ecma_property_t *bound_args_prop_p;
bound_args_prop_p = ecma_create_internal_property (function_p, ECMA_INTERNAL_PROPERTY_BOUND_FUNCTION_BOUND_ARGS);
ECMA_SET_POINTER (bound_args_prop_p->u.internal_property.value, arg_list);

/*
* [[Class]] property is not stored explicitly for objects of ECMA_OBJECT_TYPE_FUNCTION type.
*
* See also: ecma_object_get_class_name
*/

/* 17. */
ecma_number_t *length_p = ecma_alloc_number ();
*length_p = 0;

ecma_property_descriptor_t length_prop_desc = ecma_make_empty_property_descriptor ();
length_prop_desc.is_value_defined = true;
length_prop_desc.value = ecma_make_number_value (length_p);

length_prop_desc.is_writable_defined = true;
length_prop_desc.is_writable = false;

length_prop_desc.is_enumerable_defined = true;
length_prop_desc.is_enumerable = false;

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,
false);

ecma_free_completion_value (completion);
ecma_deref_ecma_string (magic_string_length_p);
ecma_dealloc_number (length_p);

/* 19-21. */
ecma_object_t *thrower_p = ecma_builtin_get (ECMA_BUILTIN_ID_TYPE_ERROR_THROWER);

ecma_property_descriptor_t prop_desc = ecma_make_empty_property_descriptor ();
{
prop_desc.is_enumerable_defined = true;
prop_desc.is_enumerable = false;

prop_desc.is_configurable_defined = true;
prop_desc.is_configurable = false;

prop_desc.is_get_defined = true;
prop_desc.get_p = thrower_p;

prop_desc.is_set_defined = true;
prop_desc.set_p = thrower_p;
}

ecma_string_t *magic_string_caller_p = ecma_get_magic_string (LIT_MAGIC_STRING_CALLER);
completion = ecma_op_object_define_own_property (function_p,
magic_string_caller_p,
&prop_desc,
false);

ecma_free_completion_value (completion);
ecma_deref_ecma_string (magic_string_caller_p);

ecma_string_t *magic_string_arguments_p = ecma_get_magic_string (LIT_MAGIC_STRING_ARGUMENTS);
completion = ecma_op_object_define_own_property (function_p,
magic_string_arguments_p,
&prop_desc,
false);

ecma_free_completion_value (completion);
ecma_deref_ecma_string (magic_string_arguments_p);
ecma_deref_object (thrower_p);

/* 22. */
ret_value = ecma_make_normal_completion_value (ecma_make_object_value (function_p));
}

return ret_value;
} /* ecma_builtin_function_prototype_object_bind */

/**
Expand Down
Loading

0 comments on commit 79ae006

Please sign in to comment.