Skip to content

Commit 24e6c3f

Browse files
jiangzidongLaszloLango
authored andcommitted
Add typedarray routine: reduce and reduceRight (#1569)
JerryScript-DCO-1.0-Signed-off-by: Zidong Jiang [email protected]
1 parent 32674ff commit 24e6c3f

File tree

4 files changed

+202
-0
lines changed

4 files changed

+202
-0
lines changed

jerry-core/ecma/builtin-objects/typedarray/ecma-builtin-typedarray-prototype.c

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,166 @@ ecma_builtin_typedarray_prototype_map (ecma_value_t this_arg, /**< this argument
354354
return ret_value;
355355
} /* ecma_builtin_typedarray_prototype_map */
356356

357+
/**
358+
* Reduce and reduceRight routines share a similar structure.
359+
* And we use 'is_right' to distinguish between them.
360+
*
361+
* @return ecma value
362+
* Returned value must be freed with ecma_free_value.
363+
*/
364+
static ecma_value_t
365+
ecma_builtin_typedarray_prototype_reduce_with_direction (ecma_value_t this_arg, /**< this argument */
366+
ecma_value_t cb_func_val, /**< callback function */
367+
ecma_value_t initial_val, /**< initial value */
368+
bool is_right) /**< choose order, true is reduceRight */
369+
{
370+
if (!ecma_is_typedarray (this_arg))
371+
{
372+
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not a TypedArray."));
373+
}
374+
375+
if (!ecma_op_is_callable (cb_func_val))
376+
{
377+
return ecma_raise_type_error (ECMA_ERR_MSG ("Callback function is not callable."));
378+
}
379+
380+
ecma_object_t *obj_p = ecma_get_object_from_value (this_arg);
381+
uint32_t len = ecma_typedarray_get_length (obj_p);
382+
383+
if (len == 0)
384+
{
385+
if (ecma_is_value_undefined (initial_val))
386+
{
387+
return ecma_raise_type_error (ECMA_ERR_MSG ("Initial value cannot be undefined."));
388+
}
389+
390+
return ecma_copy_value (initial_val);
391+
}
392+
393+
JERRY_ASSERT (len > 0);
394+
395+
ecma_value_t accumulator = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED);
396+
uint32_t index = is_right ? (len - 1) : 0;
397+
398+
if (ecma_is_value_undefined (initial_val))
399+
{
400+
accumulator = ecma_op_typedarray_get_index_prop (obj_p, index);
401+
402+
JERRY_ASSERT (ecma_is_value_number (accumulator));
403+
404+
if (is_right)
405+
{
406+
if (index == 0)
407+
{
408+
return accumulator;
409+
}
410+
411+
index--;
412+
}
413+
else
414+
{
415+
index++;
416+
417+
if (index == len)
418+
{
419+
return accumulator;
420+
}
421+
}
422+
}
423+
else
424+
{
425+
accumulator = ecma_copy_value (initial_val);
426+
}
427+
428+
ecma_object_t *func_object_p = ecma_get_object_from_value (cb_func_val);
429+
430+
while (true)
431+
{
432+
ecma_value_t current_index = ecma_make_uint32_value (index);
433+
ecma_value_t get_value = ecma_op_typedarray_get_index_prop (obj_p, index);
434+
ecma_value_t call_args[] = { accumulator, get_value, current_index, this_arg };
435+
436+
JERRY_ASSERT (ecma_is_value_number (get_value));
437+
438+
ecma_value_t call_value = ecma_op_function_call (func_object_p,
439+
ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED),
440+
call_args,
441+
4);
442+
443+
ecma_fast_free_value (accumulator);
444+
ecma_fast_free_value (get_value);
445+
ecma_fast_free_value (current_index);
446+
447+
if (ECMA_IS_VALUE_ERROR (call_value))
448+
{
449+
return call_value;
450+
}
451+
452+
accumulator = call_value;
453+
454+
if (is_right)
455+
{
456+
if (index == 0)
457+
{
458+
break;
459+
}
460+
461+
index--;
462+
}
463+
else
464+
{
465+
index++;
466+
467+
if (index == len)
468+
{
469+
break;
470+
}
471+
}
472+
}
473+
474+
return accumulator;
475+
} /* ecma_builtin_typedarray_prototype_reduce_with_direction */
476+
477+
/**
478+
* The %TypedArray%.prototype object's 'reduce' routine
479+
*
480+
* See also:
481+
* ES2015, 22.2.3.19
482+
*
483+
* @return ecma value
484+
* Returned value must be freed with ecma_free_value.
485+
*/
486+
static ecma_value_t
487+
ecma_builtin_typedarray_prototype_reduce (ecma_value_t this_arg, /**< this argument */
488+
ecma_value_t cb_func_val, /**< callback function */
489+
ecma_value_t initial_val) /**< initial value */
490+
{
491+
return ecma_builtin_typedarray_prototype_reduce_with_direction (this_arg,
492+
cb_func_val,
493+
initial_val,
494+
false);
495+
} /* ecma_builtin_typedarray_prototype_reduce */
496+
497+
/**
498+
* The %TypedArray%.prototype object's 'reduceRight' routine
499+
*
500+
* See also:
501+
* ES2015, 22.2.3.20
502+
*
503+
* @return ecma value
504+
* Returned value must be freed with ecma_free_value.
505+
*/
506+
static ecma_value_t
507+
ecma_builtin_typedarray_prototype_reduce_right (ecma_value_t this_arg, /**< this argument */
508+
ecma_value_t cb_func_val, /**< callback function */
509+
ecma_value_t initial_val) /**< initial value */
510+
{
511+
return ecma_builtin_typedarray_prototype_reduce_with_direction (this_arg,
512+
cb_func_val,
513+
initial_val,
514+
true);
515+
} /* ecma_builtin_typedarray_prototype_reduce_right */
516+
357517
/**
358518
* @}
359519
* @}

jerry-core/ecma/builtin-objects/typedarray/ecma-builtin-typedarray-prototype.inc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ ROUTINE (LIT_MAGIC_STRING_EVERY, ecma_builtin_typedarray_prototype_every, 2, 1)
6060
ROUTINE (LIT_MAGIC_STRING_SOME, ecma_builtin_typedarray_prototype_some, 2, 1)
6161
ROUTINE (LIT_MAGIC_STRING_FOR_EACH_UL, ecma_builtin_typedarray_prototype_for_each, 2, 1)
6262
ROUTINE (LIT_MAGIC_STRING_MAP, ecma_builtin_typedarray_prototype_map, 2, 1)
63+
ROUTINE (LIT_MAGIC_STRING_REDUCE, ecma_builtin_typedarray_prototype_reduce, 2, 1)
64+
ROUTINE (LIT_MAGIC_STRING_REDUCE_RIGHT_UL, ecma_builtin_typedarray_prototype_reduce_right, 2, 1)
6365

6466
#undef SIMPLE_VALUE
6567
#undef NUMBER_VALUE
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* Copyright JS Foundation and other contributors, http://js.foundation
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
var total = new Float32Array([-1.5, 0, 1.5, 2]).reduce(function(a, b, c) {
17+
return a + b + c;
18+
}, 10);
19+
20+
assert(total === 18); // 10 + (-1.5) + 0 + 0 + 1 + 1.5 + 2 + 2 + 3
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* Copyright JS Foundation and other contributors, http://js.foundation
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
var total = new Float32Array([-1.5, 0, 1.5, 2]).reduceRight(function(a, b) {
17+
return a - b;
18+
});
19+
20+
assert (total === 2) // 2 - 1.5 - 0 - (-1.5)

0 commit comments

Comments
 (0)