|
1 | 1 | /* Copyright 2014-2015 Samsung Electronics Co., Ltd.
|
| 2 | + * Copyright 2015 University of Szeged. |
2 | 3 | *
|
3 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
4 | 5 | * you may not use this file except in compliance with the License.
|
|
21 | 22 | #include "ecma-globals.h"
|
22 | 23 | #include "ecma-helpers.h"
|
23 | 24 | #include "ecma-function-object.h"
|
| 25 | +#include "ecma-objects.h" |
24 | 26 | #include "ecma-try-catch-macro.h"
|
25 | 27 | #include "jrt.h"
|
26 | 28 |
|
@@ -70,7 +72,94 @@ ecma_builtin_function_prototype_object_apply (ecma_value_t this_arg, /**< this a
|
70 | 72 | ecma_value_t arg1, /**< first argument */
|
71 | 73 | ecma_value_t arg2) /**< second argument */
|
72 | 74 | {
|
73 |
| - ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg1, arg2); |
| 75 | + ecma_completion_value_t ret_value = ecma_make_empty_completion_value (); |
| 76 | + |
| 77 | + /* 1. */ |
| 78 | + if (!ecma_op_is_callable (this_arg)) |
| 79 | + { |
| 80 | + ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE)); |
| 81 | + } |
| 82 | + else |
| 83 | + { |
| 84 | + ecma_object_t *func_obj_p = ecma_get_object_from_value (this_arg); |
| 85 | + |
| 86 | + /* 2. */ |
| 87 | + if (ecma_is_value_null (arg2) || ecma_is_value_undefined (arg2)) |
| 88 | + { |
| 89 | + ret_value = ecma_op_function_call (func_obj_p, |
| 90 | + arg1, |
| 91 | + NULL, |
| 92 | + 0); |
| 93 | + } |
| 94 | + else |
| 95 | + { |
| 96 | + /* 3. */ |
| 97 | + if (!ecma_is_value_object (arg2)) |
| 98 | + { |
| 99 | + ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE)); |
| 100 | + } |
| 101 | + else |
| 102 | + { |
| 103 | + ecma_object_t *obj_p = ecma_get_object_from_value (arg2); |
| 104 | + ecma_string_t *length_magic_string_p = ecma_get_magic_string (ECMA_MAGIC_STRING_LENGTH); |
| 105 | + |
| 106 | + /* 4. */ |
| 107 | + ECMA_TRY_CATCH (length_value, |
| 108 | + ecma_op_object_get (obj_p, length_magic_string_p), |
| 109 | + ret_value); |
| 110 | + |
| 111 | + ECMA_OP_TO_NUMBER_TRY_CATCH (length_number, |
| 112 | + length_value, |
| 113 | + ret_value); |
| 114 | + |
| 115 | + /* 5. */ |
| 116 | + const uint32_t length = ecma_number_to_uint32 (length_number); |
| 117 | + |
| 118 | + /* 6. */ |
| 119 | + MEM_DEFINE_LOCAL_ARRAY (arg_list, length, ecma_value_t); |
| 120 | + |
| 121 | + /* 7. */ |
| 122 | + uint32_t appended_num = 0; |
| 123 | + for (uint32_t index = 0; index < length && ecma_is_completion_value_empty (ret_value); index++) |
| 124 | + { |
| 125 | + ecma_string_t *curr_idx_str_p = ecma_new_ecma_string_from_uint32 (index); |
| 126 | + |
| 127 | + ECMA_TRY_CATCH (get_value, |
| 128 | + ecma_op_object_get (obj_p, curr_idx_str_p), |
| 129 | + ret_value); |
| 130 | + |
| 131 | + arg_list[index] = ecma_copy_value (get_value, true); |
| 132 | + appended_num++; |
| 133 | + |
| 134 | + ECMA_FINALIZE (get_value); |
| 135 | + ecma_deref_ecma_string (curr_idx_str_p); |
| 136 | + } |
| 137 | + |
| 138 | + JERRY_ASSERT (appended_num == length || !ecma_is_completion_value_empty (ret_value)); |
| 139 | + |
| 140 | + if (ecma_is_completion_value_empty (ret_value)) |
| 141 | + { |
| 142 | + ret_value = ecma_op_function_call (func_obj_p, |
| 143 | + arg1, |
| 144 | + arg_list, |
| 145 | + (ecma_length_t) appended_num); |
| 146 | + } |
| 147 | + |
| 148 | + for (uint32_t index = 0; index < appended_num; index++) |
| 149 | + { |
| 150 | + ecma_free_value (arg_list[index], true); |
| 151 | + } |
| 152 | + |
| 153 | + MEM_FINALIZE_LOCAL_ARRAY (arg_list); |
| 154 | + |
| 155 | + ECMA_OP_TO_NUMBER_FINALIZE (length_number); |
| 156 | + ECMA_FINALIZE (length_value); |
| 157 | + ecma_deref_ecma_string (length_magic_string_p); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + return ret_value; |
74 | 163 | } /* ecma_builtin_function_prototype_object_apply */
|
75 | 164 |
|
76 | 165 | /**
|
|
0 commit comments