15
15
*/
16
16
17
17
#include " ecma-alloc.h"
18
+ #include " ecma-array-object.h"
18
19
#include " ecma-builtin-helpers.h"
19
20
#include " ecma-builtins.h"
20
21
#include " ecma-conversion.h"
@@ -383,6 +384,8 @@ ecma_builtin_string_prototype_object_locale_compare (ecma_value_t this_arg, /**<
383
384
return ret_value;
384
385
} /* ecma_builtin_string_prototype_object_locale_compare */
385
386
387
+ #ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_REGEXP_BUILTIN
388
+
386
389
/* *
387
390
* The String.prototype object's 'match' routine
388
391
*
@@ -396,8 +399,236 @@ static ecma_completion_value_t
396
399
ecma_builtin_string_prototype_object_match (ecma_value_t this_arg, /* *< this argument */
397
400
ecma_value_t arg) /* *< routine's argument */
398
401
{
399
- ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg);
402
+ ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
403
+
404
+ /* 1. */
405
+ ECMA_TRY_CATCH (this_check_coercible_value,
406
+ ecma_op_check_object_coercible (this_arg),
407
+ ret_value);
408
+
409
+ /* 2. */
410
+ ECMA_TRY_CATCH (this_to_string_value,
411
+ ecma_op_to_string (this_arg),
412
+ ret_value);
413
+
414
+ ecma_value_t regexp_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY);
415
+ /* 3. */
416
+ if (ecma_is_value_object (arg)
417
+ && ecma_object_get_class_name (ecma_get_object_from_value (arg)) == LIT_MAGIC_STRING_REGEXP_UL)
418
+ {
419
+ regexp_value = ecma_copy_value (arg, true );
420
+ }
421
+ else
422
+ {
423
+ /* 4. */
424
+ ecma_value_t regexp_arguments[1 ] = { arg };
425
+ ECMA_TRY_CATCH (new_regexp_value,
426
+ ecma_builtin_regexp_dispatch_construct (regexp_arguments, 1 ),
427
+ ret_value);
428
+
429
+ regexp_value = ecma_copy_value (new_regexp_value, true );
430
+
431
+ ECMA_FINALIZE (new_regexp_value);
432
+ }
433
+
434
+ if (ecma_is_completion_value_empty (ret_value))
435
+ {
436
+ JERRY_ASSERT (!ecma_is_value_empty (regexp_value));
437
+ ecma_object_t *regexp_obj_p = ecma_get_object_from_value (regexp_value);
438
+ ecma_string_t *global_string_p = ecma_get_magic_string (LIT_MAGIC_STRING_GLOBAL);
439
+
440
+ /* 5. */
441
+ ECMA_TRY_CATCH (global_value,
442
+ ecma_op_object_get (regexp_obj_p, global_string_p),
443
+ ret_value);
444
+
445
+ JERRY_ASSERT (ecma_is_value_boolean (global_value));
446
+
447
+ ecma_value_t exec_arguments[1 ] = { this_to_string_value };
448
+
449
+ if (!ecma_is_value_true (global_value))
450
+ {
451
+ /* 7. */
452
+ ret_value = ecma_builtin_regexp_prototype_dispatch_routine (LIT_MAGIC_STRING_EXEC,
453
+ regexp_value,
454
+ exec_arguments,
455
+ 1 );
456
+ }
457
+ else
458
+ {
459
+ /* 8.a. */
460
+ ecma_number_t *zero_number_p = ecma_alloc_number ();
461
+ *zero_number_p = 0 ;
462
+
463
+ ecma_string_t *index_zero_string_p = ecma_new_ecma_string_from_uint32 (0 );
464
+
465
+ ecma_string_t *last_index_string_p = ecma_get_magic_string (LIT_MAGIC_STRING_LASTINDEX_UL);
466
+
467
+ ECMA_TRY_CATCH (put_value,
468
+ ecma_op_object_put (regexp_obj_p,
469
+ last_index_string_p,
470
+ ecma_make_number_value (zero_number_p),
471
+ true ),
472
+ ret_value);
473
+
474
+ /* 8.b. */
475
+ ECMA_TRY_CATCH (new_array_value,
476
+ ecma_op_create_array_object (NULL , 0 , false ),
477
+ ret_value);
478
+
479
+ ecma_object_t *new_array_obj_p = ecma_get_object_from_value (new_array_value);
480
+
481
+ /* 8.c. */
482
+ ecma_number_t previous_last_index = 0 ;
483
+ /* 8.d. */
484
+ uint32_t n = 0 ;
485
+ /* 8.e. */
486
+ bool last_match = true ;
487
+
488
+ // ecma_completion_value_t exec_result = ecma_make_empty_completion_value ();
489
+
490
+ /* 8.f. */
491
+ while (last_match && ecma_is_completion_value_empty (ret_value))
492
+ {
493
+ /* 8.f.i. */
494
+ ECMA_TRY_CATCH (exec_value,
495
+ ecma_builtin_regexp_prototype_dispatch_routine (LIT_MAGIC_STRING_EXEC,
496
+ regexp_value,
497
+ exec_arguments,
498
+ 1 ),
499
+ ret_value);
500
+
501
+ if (ecma_is_value_null (exec_value))
502
+ {
503
+ /* 8.f.ii. */
504
+ last_match = false ;
505
+ }
506
+ else
507
+ {
508
+ /* 8.f.iii. */
509
+ ECMA_TRY_CATCH (this_index_value,
510
+ ecma_op_object_get (regexp_obj_p, last_index_string_p),
511
+ ret_value);
512
+
513
+ ECMA_TRY_CATCH (this_index_number,
514
+ ecma_op_to_number (this_index_value),
515
+ ret_value);
516
+
517
+ ecma_number_t this_index = *ecma_get_number_from_value (this_index_number);
518
+
519
+ /* 8.f.iii.2. */
520
+ if (this_index == previous_last_index)
521
+ {
522
+ ecma_number_t *new_last_index_p = ecma_alloc_number ();
523
+ *new_last_index_p = this_index + 1 ;
524
+ /* 8.f.iii.2.a. */
525
+ ECMA_TRY_CATCH (index_put_value,
526
+ ecma_op_object_put (regexp_obj_p,
527
+ last_index_string_p,
528
+ ecma_make_number_value (new_last_index_p),
529
+ true ),
530
+ ret_value);
531
+
532
+ /* 8.f.iii.2.b. */
533
+ previous_last_index = this_index + 1 ;
534
+
535
+ ECMA_FINALIZE (index_put_value);
536
+
537
+ ecma_dealloc_number (new_last_index_p);
538
+ }
539
+ else
540
+ {
541
+ /* 8.f.iii.3. */
542
+ previous_last_index = this_index;
543
+ }
544
+
545
+ if (ecma_is_completion_value_empty (ret_value))
546
+ {
547
+ /* 8.f.iii.4. */
548
+ JERRY_ASSERT (ecma_is_value_object (exec_value));
549
+ ecma_object_t *exec_obj_p = ecma_get_object_from_value (exec_value);
550
+
551
+ ECMA_TRY_CATCH (match_string_value,
552
+ ecma_op_object_get (exec_obj_p, index_zero_string_p),
553
+ ret_value);
554
+
555
+ /* 8.f.iii.5. */
556
+ ecma_property_descriptor_t prop_desc = ecma_make_empty_property_descriptor ();
557
+ {
558
+ prop_desc.is_value_defined = true ;
559
+ prop_desc.value = match_string_value;
560
+
561
+ prop_desc.is_writable_defined = true ;
562
+ prop_desc.is_writable = true ;
563
+
564
+ prop_desc.is_enumerable_defined = true ;
565
+ prop_desc.is_enumerable = true ;
566
+
567
+ prop_desc.is_configurable_defined = true ;
568
+ prop_desc.is_configurable = true ;
569
+ }
570
+
571
+ ecma_string_t *current_index_str_p = ecma_new_ecma_string_from_uint32 (n);
572
+
573
+ ecma_completion_value_t completion = ecma_op_object_define_own_property (new_array_obj_p,
574
+ current_index_str_p,
575
+ &prop_desc,
576
+ false );
577
+ JERRY_ASSERT (ecma_is_completion_value_normal_true (completion));
578
+
579
+ ecma_deref_ecma_string (current_index_str_p);
580
+
581
+ /* 8.f.iii.6. */
582
+ n++;
583
+
584
+ ECMA_FINALIZE (match_string_value);
585
+ }
586
+
587
+ ECMA_FINALIZE (this_index_number);
588
+
589
+ ECMA_FINALIZE (this_index_value);
590
+ }
591
+
592
+ ECMA_FINALIZE (exec_value);
593
+ }
594
+
595
+ if (ecma_is_completion_value_empty (ret_value))
596
+ {
597
+ if (n == 0 )
598
+ {
599
+ /* 8.g. */
600
+ ret_value = ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_NULL);
601
+ }
602
+ else
603
+ {
604
+ /* 8.h. */
605
+ ret_value = ecma_make_normal_completion_value (ecma_copy_value (new_array_value, true ));
606
+ }
607
+ }
608
+
609
+ ECMA_FINALIZE (new_array_value);
610
+
611
+ ECMA_FINALIZE (put_value);
612
+
613
+ ecma_deref_ecma_string (last_index_string_p);
614
+ ecma_deref_ecma_string (index_zero_string_p);
615
+ ecma_dealloc_number (zero_number_p);
616
+ }
617
+
618
+ ECMA_FINALIZE (global_value);
619
+
620
+ ecma_deref_ecma_string (global_string_p);
621
+
622
+ ecma_free_value (regexp_value, true );
623
+ }
624
+
625
+ ECMA_FINALIZE (this_to_string_value);
626
+
627
+ ECMA_FINALIZE (this_check_coercible_value);
628
+
629
+ return ret_value;
400
630
} /* ecma_builtin_string_prototype_object_match */
631
+ #endif /* !CONFIG_ECMA_COMPACT_PROFILE_DISABLE_REGEXP_BUILTIN */
401
632
402
633
/* *
403
634
* The String.prototype object's 'replace' routine
0 commit comments