Implement Function.prototype.bind function
JerryScript-DCO-1.0-Signed-off-by: Zsolt Borbély zsborbely.u-szeged@partner.samsung.com
This commit is contained in:
@@ -331,6 +331,9 @@ 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:
|
||||
case ECMA_INTERNAL_PROPERTY_BOUND_FUNCTION_BOUND_ARGS:
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
*/
|
||||
|
||||
@@ -800,10 +800,23 @@ 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:
|
||||
{
|
||||
if (property_value != ECMA_NULL_POINTER)
|
||||
{
|
||||
ecma_free_values_collection (ECMA_GET_NON_NULL_POINTER (ecma_collection_header_t, property_value),
|
||||
false);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case ECMA_INTERNAL_PROPERTY__COUNT: /* not a real internal property type,
|
||||
* but number of the real internal property types */
|
||||
{
|
||||
|
||||
@@ -226,7 +226,135 @@ 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);
|
||||
|
||||
ecma_object_t *this_arg_obj_p = ecma_get_object_from_value (this_arg);
|
||||
ECMA_SET_NON_NULL_POINTER (target_function_prop_p->u.internal_property.value, this_arg_obj_p);
|
||||
|
||||
/* 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);
|
||||
const ecma_length_t arg_count = arguments_number;
|
||||
|
||||
if (arg_count > 0)
|
||||
{
|
||||
bound_this_prop_p->u.internal_property.value = ecma_copy_value (arguments_list_p[0], false);
|
||||
}
|
||||
else
|
||||
{
|
||||
bound_this_prop_p->u.internal_property.value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED);
|
||||
}
|
||||
|
||||
if (arg_count > 1)
|
||||
{
|
||||
ecma_collection_header_t *bound_args_collection_p;
|
||||
bound_args_collection_p = ecma_new_values_collection (&arguments_list_p[1], arg_count - 1, 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_NON_NULL_POINTER (bound_args_prop_p->u.internal_property.value, bound_args_collection_p);
|
||||
}
|
||||
|
||||
/*
|
||||
* [[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);
|
||||
|
||||
JERRY_ASSERT (ecma_is_completion_value_normal_true (completion)
|
||||
|| ecma_is_completion_value_normal_false (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);
|
||||
|
||||
JERRY_ASSERT (ecma_is_completion_value_normal_true (completion)
|
||||
|| ecma_is_completion_value_normal_false (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);
|
||||
|
||||
JERRY_ASSERT (ecma_is_completion_value_normal_true (completion)
|
||||
|| ecma_is_completion_value_normal_false (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 */
|
||||
|
||||
/**
|
||||
|
||||
@@ -143,6 +143,70 @@ ecma_is_constructor (ecma_value_t value) /**< ecma-value */
|
||||
|| ecma_get_object_type (obj_p) == ECMA_OBJECT_TYPE_EXTERNAL_FUNCTION);
|
||||
} /* ecma_is_constructor */
|
||||
|
||||
/**
|
||||
* Helper function to merge argument lists
|
||||
*
|
||||
* See also:
|
||||
* ECMA-262 v5, 15.3.4.5.1 step 4
|
||||
* ECMA-262 v5, 15.3.4.5.2 step 4
|
||||
*
|
||||
* Used by:
|
||||
* - [[Call]] implementation for Function objects.
|
||||
* - [[Construct]] implementation for Function objects.
|
||||
*
|
||||
* @return ecma_value_t* - pointer to the merged argument list.
|
||||
*/
|
||||
static ecma_value_t*
|
||||
ecma_function_bind_merge_arg_lists (ecma_object_t *func_obj_p, /**< Function object */
|
||||
const ecma_value_t *arguments_list_p, /**< arguments list */
|
||||
ecma_length_t arguments_list_len, /**< length of arguments list */
|
||||
ecma_length_t *total_args_count) /**< length of the merged argument list */
|
||||
{
|
||||
ecma_value_t *arg_list_p;
|
||||
ecma_length_t bound_args_count = 0;
|
||||
|
||||
ecma_property_t *bound_args_prop_p;
|
||||
bound_args_prop_p = ecma_find_internal_property (func_obj_p, ECMA_INTERNAL_PROPERTY_BOUND_FUNCTION_BOUND_ARGS);
|
||||
|
||||
if (bound_args_prop_p != NULL)
|
||||
{
|
||||
ecma_collection_header_t *bound_arg_list_p = ECMA_GET_POINTER (ecma_collection_header_t,
|
||||
bound_args_prop_p->u.internal_property.value);
|
||||
|
||||
ecma_collection_iterator_t bound_args_iterator;
|
||||
ecma_collection_iterator_init (&bound_args_iterator, bound_arg_list_p);
|
||||
|
||||
bound_args_count = bound_arg_list_p->unit_number;
|
||||
|
||||
*total_args_count = bound_args_count + arguments_list_len;
|
||||
|
||||
const size_t arg_list_size = (size_t) *total_args_count * sizeof (ecma_value_t);
|
||||
arg_list_p = static_cast <ecma_value_t *> (mem_heap_alloc_block (arg_list_size, MEM_HEAP_ALLOC_SHORT_TERM));
|
||||
|
||||
for (ecma_length_t i = 0; i < bound_args_count; i++)
|
||||
{
|
||||
bool is_moved = ecma_collection_iterator_next (&bound_args_iterator);
|
||||
JERRY_ASSERT (is_moved);
|
||||
|
||||
arg_list_p[i] = *bound_args_iterator.current_value_p;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
*total_args_count = arguments_list_len;
|
||||
|
||||
const size_t arg_list_size = (size_t) *total_args_count * sizeof (ecma_value_t);
|
||||
arg_list_p = static_cast <ecma_value_t *> (mem_heap_alloc_block (arg_list_size, MEM_HEAP_ALLOC_SHORT_TERM));
|
||||
}
|
||||
|
||||
for (ecma_length_t i = 0; i < arguments_list_len; i++)
|
||||
{
|
||||
arg_list_p[i + bound_args_count] = arguments_list_p[i];
|
||||
}
|
||||
|
||||
return arg_list_p;
|
||||
} /* ecma_function_bind_merge_arg_lists */
|
||||
|
||||
/**
|
||||
* Function object creation operation.
|
||||
*
|
||||
@@ -535,6 +599,8 @@ ecma_op_function_has_instance (ecma_object_t *func_obj_p, /**< Function object *
|
||||
JERRY_ASSERT (func_obj_p != NULL
|
||||
&& !ecma_is_lexical_environment (func_obj_p));
|
||||
|
||||
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
|
||||
|
||||
if (ecma_get_object_type (func_obj_p) == ECMA_OBJECT_TYPE_FUNCTION)
|
||||
{
|
||||
if (!ecma_is_value_object (value))
|
||||
@@ -546,8 +612,6 @@ ecma_op_function_has_instance (ecma_object_t *func_obj_p, /**< Function object *
|
||||
|
||||
ecma_string_t *prototype_magic_string_p = ecma_get_magic_string (LIT_MAGIC_STRING_PROTOTYPE);
|
||||
|
||||
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
|
||||
|
||||
ECMA_TRY_CATCH (prototype_obj_value,
|
||||
ecma_op_object_get (func_obj_p, prototype_magic_string_p),
|
||||
ret_value);
|
||||
@@ -583,19 +647,28 @@ ecma_op_function_has_instance (ecma_object_t *func_obj_p, /**< Function object *
|
||||
ECMA_FINALIZE (prototype_obj_value);
|
||||
|
||||
ecma_deref_ecma_string (prototype_magic_string_p);
|
||||
|
||||
return ret_value;
|
||||
}
|
||||
else if (ecma_get_object_type (func_obj_p) == ECMA_OBJECT_TYPE_BUILT_IN_FUNCTION)
|
||||
{
|
||||
return ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE));
|
||||
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE));
|
||||
}
|
||||
else
|
||||
{
|
||||
JERRY_ASSERT (ecma_get_object_type (func_obj_p) == ECMA_OBJECT_TYPE_BOUND_FUNCTION);
|
||||
|
||||
JERRY_UNIMPLEMENTED ("Bound functions are not implemented.");
|
||||
/* 1. */
|
||||
ecma_property_t *target_function_prop_p;
|
||||
target_function_prop_p = ecma_get_internal_property (func_obj_p,
|
||||
ECMA_INTERNAL_PROPERTY_BOUND_FUNCTION_TARGET_FUNCTION);
|
||||
|
||||
ecma_object_t *target_func_obj_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t,
|
||||
target_function_prop_p->u.internal_property.value);
|
||||
|
||||
/* 3. */
|
||||
ret_value = ecma_op_object_has_instance (target_func_obj_p, value);
|
||||
}
|
||||
|
||||
return ret_value;
|
||||
} /* ecma_op_function_has_instance */
|
||||
|
||||
/**
|
||||
@@ -731,11 +804,40 @@ ecma_op_function_call (ecma_object_t *func_obj_p, /**< Function object */
|
||||
{
|
||||
JERRY_ASSERT (ecma_get_object_type (func_obj_p) == ECMA_OBJECT_TYPE_BOUND_FUNCTION);
|
||||
|
||||
JERRY_UNIMPLEMENTED ("Bound functions are not implemented.");
|
||||
/* 2-3. */
|
||||
ecma_property_t *bound_this_prop_p;
|
||||
ecma_property_t *target_function_prop_p;
|
||||
|
||||
bound_this_prop_p = ecma_get_internal_property (func_obj_p, ECMA_INTERNAL_PROPERTY_BOUND_FUNCTION_BOUND_THIS);
|
||||
target_function_prop_p = ecma_get_internal_property (func_obj_p,
|
||||
ECMA_INTERNAL_PROPERTY_BOUND_FUNCTION_TARGET_FUNCTION);
|
||||
|
||||
ecma_object_t *target_func_obj_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t,
|
||||
target_function_prop_p->u.internal_property.value);
|
||||
|
||||
ecma_length_t total_args_count;
|
||||
|
||||
/* 4. */
|
||||
ecma_value_t *arg_list_p = ecma_function_bind_merge_arg_lists (func_obj_p,
|
||||
arguments_list_p,
|
||||
arguments_list_len,
|
||||
&total_args_count);
|
||||
|
||||
ecma_value_t bound_this_value = bound_this_prop_p->u.internal_property.value;
|
||||
|
||||
/* 5. */
|
||||
ret_value = ecma_op_function_call (target_func_obj_p,
|
||||
bound_this_value,
|
||||
arg_list_p,
|
||||
total_args_count);
|
||||
|
||||
if (arg_list_p != NULL)
|
||||
{
|
||||
mem_heap_free_block (arg_list_p);
|
||||
}
|
||||
}
|
||||
|
||||
JERRY_ASSERT (!ecma_is_completion_value_empty (ret_value));
|
||||
|
||||
return ret_value;
|
||||
} /* ecma_op_function_call */
|
||||
|
||||
@@ -847,27 +949,65 @@ ecma_op_function_construct (ecma_object_t *func_obj_p, /**< Function object */
|
||||
JERRY_ASSERT (ecma_is_constructor (ecma_make_object_value (func_obj_p)));
|
||||
JERRY_ASSERT (arguments_list_len == 0 || arguments_list_p != NULL);
|
||||
|
||||
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
|
||||
|
||||
if (ecma_get_object_type (func_obj_p) == ECMA_OBJECT_TYPE_FUNCTION)
|
||||
{
|
||||
if (unlikely (ecma_get_object_type (func_obj_p) == ECMA_OBJECT_TYPE_FUNCTION
|
||||
&& ecma_get_object_is_builtin (func_obj_p)))
|
||||
{
|
||||
return ecma_builtin_dispatch_construct (func_obj_p, arguments_list_p, arguments_list_len);
|
||||
ret_value = ecma_builtin_dispatch_construct (func_obj_p, arguments_list_p, arguments_list_len);
|
||||
}
|
||||
else
|
||||
{
|
||||
ret_value = ecma_op_function_construct_simple_or_external (func_obj_p, arguments_list_p, arguments_list_len);
|
||||
}
|
||||
|
||||
return ecma_op_function_construct_simple_or_external (func_obj_p, arguments_list_p, arguments_list_len);
|
||||
}
|
||||
else if (ecma_get_object_type (func_obj_p) == ECMA_OBJECT_TYPE_EXTERNAL_FUNCTION)
|
||||
{
|
||||
return ecma_op_function_construct_simple_or_external (func_obj_p, arguments_list_p, arguments_list_len);
|
||||
ret_value = ecma_op_function_construct_simple_or_external (func_obj_p, arguments_list_p, arguments_list_len);
|
||||
}
|
||||
else
|
||||
{
|
||||
JERRY_ASSERT (ecma_get_object_type (func_obj_p) == ECMA_OBJECT_TYPE_BOUND_FUNCTION);
|
||||
|
||||
JERRY_UNIMPLEMENTED ("Bound functions are not implemented.");
|
||||
/* 1. */
|
||||
ecma_property_t *target_function_prop_p;
|
||||
target_function_prop_p = ecma_get_internal_property (func_obj_p,
|
||||
ECMA_INTERNAL_PROPERTY_BOUND_FUNCTION_TARGET_FUNCTION);
|
||||
|
||||
ecma_object_t *target_func_obj_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t,
|
||||
target_function_prop_p->u.internal_property.value);
|
||||
|
||||
/* 2. */
|
||||
if (!ecma_is_constructor (ecma_make_object_value (target_func_obj_p)))
|
||||
{
|
||||
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE));
|
||||
}
|
||||
else
|
||||
{
|
||||
ecma_length_t total_args_count;
|
||||
|
||||
/* 4. */
|
||||
ecma_value_t *arg_list_p = ecma_function_bind_merge_arg_lists (func_obj_p,
|
||||
arguments_list_p,
|
||||
arguments_list_len,
|
||||
&total_args_count);
|
||||
|
||||
/* 5. */
|
||||
ret_value = ecma_op_function_construct (target_func_obj_p,
|
||||
arg_list_p,
|
||||
total_args_count);
|
||||
if (arg_list_p != NULL)
|
||||
{
|
||||
mem_heap_free_block (arg_list_p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret_value;
|
||||
} /* ecma_op_function_construct */
|
||||
|
||||
/**
|
||||
* Function declaration.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user