@@ -354,6 +354,166 @@ ecma_builtin_typedarray_prototype_map (ecma_value_t this_arg, /**< this argument
354
354
return ret_value ;
355
355
} /* ecma_builtin_typedarray_prototype_map */
356
356
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
+
357
517
/**
358
518
* @}
359
519
* @}
0 commit comments