-
Notifications
You must be signed in to change notification settings - Fork 676
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Array.prototype.reduce() #104
Changes from 3 commits
76425f9
346b16a
8427cb2
7c2f3b7
35441b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1237,6 +1237,147 @@ ecma_builtin_array_prototype_object_slice (ecma_value_t this_arg, /**< 'this' ar | |
return ret_value; | ||
} /* ecma_builtin_array_prototype_object_slice */ | ||
|
||
/** | ||
* The Array.prototype object's 'reduce' routine | ||
* | ||
* See also: | ||
* ECMA-262 v5, 15.4.4.21 | ||
* | ||
* @return completion value | ||
* Returned value must be freed with ecma_free_completion_value. | ||
*/ | ||
static ecma_completion_value_t | ||
ecma_builtin_array_prototype_object_reduce (ecma_value_t this_arg, /**< this argument */ | ||
ecma_value_t arg1, /**< callbackfn */ | ||
ecma_value_t arg2) /**< initialValue */ | ||
{ | ||
ecma_completion_value_t ret_value = ecma_make_empty_completion_value (); | ||
|
||
/* 1 */ | ||
ECMA_TRY_CATCH (obj_this, | ||
ecma_op_to_object (this_arg), | ||
ret_value); | ||
|
||
ecma_object_t *obj_p = ecma_get_object_from_value (obj_this); | ||
ecma_string_t *magic_string_length_p = ecma_get_magic_string (ECMA_MAGIC_STRING_LENGTH); | ||
|
||
/* 2 */ | ||
ECMA_TRY_CATCH (len_value, | ||
ecma_op_object_get (obj_p, magic_string_length_p), | ||
ret_value); | ||
|
||
ECMA_OP_TO_NUMBER_TRY_CATCH (len_number, len_value, ret_value); | ||
|
||
/* 3 */ | ||
uint32_t len = ecma_number_to_uint32 (len_number); | ||
|
||
/* 4 */ | ||
if (!ecma_op_is_callable (arg1)) | ||
{ | ||
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE)); | ||
} | ||
else | ||
{ | ||
ecma_completion_value_t accumulator = ecma_make_empty_completion_value (); | ||
ecma_number_t *num_p = ecma_alloc_number (); | ||
ecma_object_t *func_object_p; | ||
ecma_value_t current_index; | ||
|
||
ecma_completion_value_t to_object_comp = ecma_op_to_object (arg1); | ||
JERRY_ASSERT (ecma_is_completion_value_normal (to_object_comp)); | ||
func_object_p = ecma_get_object_from_completion_value (to_object_comp); | ||
|
||
/* 5 */ | ||
if (len_number == ECMA_NUMBER_ZERO && ecma_is_value_undefined (arg1)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
{ | ||
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE)); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code following this should be in the |
||
|
||
/* 6 */ | ||
uint32_t index = 0; | ||
|
||
/* 7a */ | ||
if (!ecma_is_value_undefined (arg2)) | ||
{ | ||
accumulator = ecma_copy_completion_value (arg2); | ||
} | ||
else | ||
{ | ||
/* 8a */ | ||
bool kPresent = false; | ||
/* 8b */ | ||
while (!kPresent && index < len && ecma_is_completion_value_empty (ret_value)) | ||
{ | ||
/* 8b-i */ | ||
ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (index); | ||
|
||
/* 8b-ii-iii */ | ||
if ((kPresent = (ecma_op_object_get_property (obj_p, index_str_p) != NULL))) | ||
{ | ||
ECMA_TRY_CATCH (current_value, ecma_op_object_get (obj_p, index_str_p), ret_value); | ||
accumulator = ecma_copy_completion_value (current_value); | ||
ECMA_FINALIZE (current_value); | ||
} | ||
/* 8b-iv */ | ||
index++; | ||
|
||
ecma_deref_ecma_string (index_str_p); | ||
} | ||
/* 8c */ | ||
if (!kPresent) | ||
{ | ||
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE)); | ||
} | ||
} | ||
/* 9 */ | ||
for (; index < len && ecma_is_completion_value_empty (ret_value); ++index) | ||
{ | ||
/* 9a */ | ||
ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (index); | ||
/* 9b */ | ||
if (ecma_op_object_get_property (obj_p, index_str_p) != NULL) | ||
{ | ||
/* 9c-i */ | ||
ECMA_TRY_CATCH (current_value, ecma_op_object_get (obj_p, index_str_p), ret_value); | ||
/* 9c-ii */ | ||
*num_p = ecma_uint32_to_number (index); | ||
current_index = ecma_make_number_value (num_p); | ||
ecma_value_t prev_value = ecma_get_completion_value_value (accumulator); | ||
ecma_value_t call_args[] = {prev_value, current_value, current_index, obj_this}; | ||
ecma_value_t undefined_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really need to create the undefined value for each iteration? Can't we just move this outside the loop? |
||
|
||
ECMA_TRY_CATCH (call_value, | ||
ecma_op_function_call (func_object_p, undefined_value, call_args, 4), | ||
ret_value); | ||
|
||
ecma_free_completion_value (accumulator); | ||
accumulator = ecma_copy_completion_value (call_value); | ||
|
||
ECMA_FINALIZE (call_value); | ||
ecma_free_value (undefined_value, false); | ||
ECMA_FINALIZE (current_value); | ||
} | ||
ecma_deref_ecma_string (index_str_p); | ||
/* 9d in for loop */ | ||
} | ||
|
||
if (ecma_is_completion_value_empty (ret_value)) | ||
{ | ||
ret_value = ecma_copy_completion_value (accumulator); | ||
} | ||
ecma_free_completion_value (accumulator); | ||
ecma_free_completion_value (to_object_comp); | ||
ecma_dealloc_number (num_p); | ||
} | ||
|
||
ECMA_OP_TO_NUMBER_FINALIZE (len_number); | ||
ECMA_FINALIZE (len_value); | ||
ecma_deref_ecma_string (magic_string_length_p); | ||
ECMA_FINALIZE (obj_this); | ||
|
||
return ret_value; | ||
} /* ecma_builtin_array_prototype_object_reduce */ | ||
|
||
/** | ||
* @} | ||
* @} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright 2015 Samsung Electronics Co., Ltd. | ||
// Copyright 2015 University of Szeged. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
var func = function(a, b) { | ||
return a + b; | ||
} | ||
|
||
// check function type | ||
try { | ||
[0].reduce(new Object()); | ||
assert(false); | ||
} catch(e) { | ||
assert(e instanceof TypeError); | ||
} | ||
|
||
// check for init value | ||
try { | ||
[].reduce(func); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should add an |
||
catch(e) { | ||
assert(e instanceof TypeError); | ||
} | ||
|
||
// various checks | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should have a check for empty array + no/undefined initialValue to see if that results a TypeError as described in the 5th step. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check is in lines 28-35 with empty array + no initial value. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ahh... true :) sorry |
||
assert ([].reduce(func, 1) === 1); | ||
|
||
assert ([0].reduce(func) === 0); | ||
|
||
assert ([0, 1].reduce(func) === 1); | ||
|
||
assert ([0, 1].reduce(func, 1) === 2); | ||
|
||
assert ([0, 1, 2, 3].reduce(func, 1) === 7); | ||
|
||
assert (["A","B"].reduce(func) === "AB"); | ||
|
||
assert (["A","B"].reduce(func, "Init:") === "Init:AB"); | ||
|
||
assert ([0, 1].reduce(func, 3.2) === 4.2); | ||
|
||
assert ([0, "x", 1].reduce(func) === "0x1"); | ||
|
||
assert ([0, "x", 1].reduce(func, 3.2) === "3.2x1"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need to perform the conversion, since
ecma_op_is_callable
returned true for the value.So, we could just assert that
ecma_is_value_object
and performecma_get_object_from_value
.