@@ -397,6 +397,119 @@ ecma_builtin_array_prototype_object_index_of (ecma_value_t this_arg, /**< this a
397
397
return ret_value;
398
398
} /* ecma_builtin_array_prototype_object_index_of */
399
399
400
+ /* *
401
+ * The Array.prototype object's 'shift' routine
402
+ *
403
+ * See also:
404
+ * ECMA-262 v5, 15.4.4.9
405
+ *
406
+ * @return completion value
407
+ * Returned value must be freed with ecma_free_completion_value.
408
+ */
409
+ static ecma_completion_value_t
410
+ ecma_builtin_array_prototype_object_shift (ecma_value_t this_arg) /* *< this argument */
411
+ {
412
+ ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
413
+
414
+ /* 1. */
415
+ ECMA_TRY_CATCH (obj_this,
416
+ ecma_op_to_object (this_arg),
417
+ ret_value);
418
+
419
+ ecma_object_t *obj_p = ecma_get_object_from_value (obj_this);
420
+ ecma_string_t *magic_string_length_p = ecma_get_magic_string (ECMA_MAGIC_STRING_LENGTH);
421
+
422
+ /* 2. */
423
+ ECMA_TRY_CATCH (len_value,
424
+ ecma_op_object_get (obj_p, magic_string_length_p),
425
+ ret_value);
426
+
427
+ ECMA_OP_TO_NUMBER_TRY_CATCH (len_number, len_value, ret_value);
428
+
429
+ /* 3. */
430
+ uint32_t len = ecma_number_to_uint32 (len_number);
431
+
432
+ /* 4. */
433
+ if (len == 0 )
434
+ {
435
+ ECMA_TRY_CATCH (set_length_value,
436
+ ecma_builtin_array_prototype_helper_set_length (obj_p, 0 ),
437
+ ret_value);
438
+
439
+ ret_value = ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_UNDEFINED);
440
+
441
+ ECMA_FINALIZE (set_length_value);
442
+ }
443
+ else
444
+ {
445
+ ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (0 );
446
+
447
+ /* 5. */
448
+ ECMA_TRY_CATCH (first_value, ecma_op_object_get (obj_p, index_str_p), ret_value);
449
+
450
+ /* 6. and 7. */
451
+ for (uint32_t k = 1 ; k < len && ecma_is_completion_value_empty (ret_value); k++)
452
+ {
453
+ /* 7.a */
454
+ ecma_string_t *from_str_p = ecma_new_ecma_string_from_uint32 (k);
455
+ /* 7.b */
456
+ ecma_string_t *to_str_p = ecma_new_ecma_string_from_uint32 (k - 1 );
457
+
458
+ /* 7.c */
459
+ if (ecma_op_object_get_property (obj_p, from_str_p) != NULL )
460
+ {
461
+ /* 7.d.i */
462
+ ECMA_TRY_CATCH (curr_value, ecma_op_object_get (obj_p, from_str_p), ret_value);
463
+
464
+ /* 7.d.ii*/
465
+ ECMA_TRY_CATCH (put_value, ecma_op_object_put (obj_p, to_str_p, curr_value, true ), ret_value);
466
+
467
+ ECMA_FINALIZE (put_value);
468
+ ECMA_FINALIZE (curr_value);
469
+ }
470
+ else
471
+ {
472
+ /* 7.e.i */
473
+ ECMA_TRY_CATCH (del_value, ecma_op_object_delete (obj_p, to_str_p, true ), ret_value);
474
+ ECMA_FINALIZE (del_value);
475
+ }
476
+
477
+ ecma_deref_ecma_string (to_str_p);
478
+ ecma_deref_ecma_string (from_str_p);
479
+ }
480
+
481
+ if (ecma_is_completion_value_empty (ret_value))
482
+ {
483
+ len--;
484
+ ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (len);
485
+
486
+ /* 8. */
487
+ ECMA_TRY_CATCH (del_value, ecma_op_object_delete (obj_p, index_str_p, true ), ret_value);
488
+
489
+ /* 9. */
490
+ ECMA_TRY_CATCH (set_length_value,
491
+ ecma_builtin_array_prototype_helper_set_length (obj_p, len),
492
+ ret_value);
493
+ /* 10. */
494
+ ret_value = ecma_make_normal_completion_value (ecma_copy_value (first_value, true ));
495
+
496
+ ECMA_FINALIZE (set_length_value);
497
+ ECMA_FINALIZE (del_value);
498
+ ecma_deref_ecma_string (index_str_p);
499
+ }
500
+
501
+ ECMA_FINALIZE (first_value);
502
+ ecma_deref_ecma_string (index_str_p);
503
+ }
504
+
505
+ ECMA_OP_TO_NUMBER_FINALIZE (len_number);
506
+ ECMA_FINALIZE (len_value);
507
+ ecma_deref_ecma_string (magic_string_length_p);
508
+ ECMA_FINALIZE (obj_this);
509
+
510
+ return ret_value;
511
+ } /* ecma_builtin_array_prototype_object_shift */
512
+
400
513
/* *
401
514
* @}
402
515
* @}
0 commit comments