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"
@@ -331,6 +332,8 @@ ecma_builtin_string_prototype_object_locale_compare (ecma_value_t this_arg, /**<
331
332
return ret_value;
332
333
} /* ecma_builtin_string_prototype_object_locale_compare */
333
334
335
+ #ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_REGEXP_BUILTIN
336
+
334
337
/* *
335
338
* The String.prototype object's 'match' routine
336
339
*
@@ -344,8 +347,237 @@ static ecma_completion_value_t
344
347
ecma_builtin_string_prototype_object_match (ecma_value_t this_arg, /* *< this argument */
345
348
ecma_value_t arg) /* *< routine's argument */
346
349
{
347
- ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg);
350
+ ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
351
+
352
+ /* 1. */
353
+ ECMA_TRY_CATCH (this_check_coercible_value,
354
+ ecma_op_check_object_coercible (this_arg),
355
+ ret_value);
356
+
357
+ /* 2. */
358
+ ECMA_TRY_CATCH (this_to_string_value,
359
+ ecma_op_to_string (this_arg),
360
+ ret_value);
361
+
362
+ ecma_value_t regexp_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY);
363
+ /* 3. */
364
+ if (ecma_is_value_object (arg)
365
+ && ecma_object_get_class_name (ecma_get_object_from_value (arg)) == LIT_MAGIC_STRING_REGEXP_UL)
366
+ {
367
+ regexp_value = ecma_copy_value (arg, true );
368
+ }
369
+ else
370
+ {
371
+ /* 4. */
372
+ ecma_value_t regexp_arguments[1 ] = { arg };
373
+ ECMA_TRY_CATCH (new_regexp_value,
374
+ ecma_builtin_regexp_dispatch_construct (regexp_arguments, 1 ),
375
+ ret_value);
376
+
377
+ regexp_value = ecma_copy_value (new_regexp_value, true );
378
+
379
+ ECMA_FINALIZE (new_regexp_value);
380
+ }
381
+
382
+ if (ecma_is_completion_value_empty (ret_value))
383
+ {
384
+ JERRY_ASSERT (!ecma_is_value_empty (regexp_value));
385
+ ecma_object_t *regexp_obj_p = ecma_get_object_from_value (regexp_value);
386
+ ecma_string_t *global_string_p = ecma_get_magic_string (LIT_MAGIC_STRING_GLOBAL);
387
+
388
+ /* 5. */
389
+ ECMA_TRY_CATCH (global_value,
390
+ ecma_op_object_get (regexp_obj_p, global_string_p),
391
+ ret_value);
392
+
393
+ JERRY_ASSERT (ecma_is_completion_value_normal_true (global_value)
394
+ || ecma_is_completion_value_normal_false (global_value));
395
+
396
+ ecma_value_t exec_arguments[1 ] = { this_to_string_value };
397
+
398
+ if (!ecma_is_completion_value_normal_true (global_value))
399
+ {
400
+ /* 7. */
401
+ ret_value = ecma_builtin_regexp_prototype_dispatch_routine (LIT_MAGIC_STRING_EXEC,
402
+ regexp_value,
403
+ exec_arguments,
404
+ 1 );
405
+ }
406
+ else
407
+ {
408
+ /* 8.a. */
409
+ ecma_number_t *zero_number_p = ecma_alloc_number ();
410
+ *zero_number_p = 0 ;
411
+
412
+ ecma_string_t *index_zero_string_p = ecma_new_ecma_string_from_uint32 (0 );
413
+
414
+ ecma_string_t *last_index_string_p = ecma_get_magic_string (LIT_MAGIC_STRING_LASTINDEX_UL);
415
+
416
+ ECMA_TRY_CATCH (put_value,
417
+ ecma_op_object_put (regexp_obj_p,
418
+ last_index_string_p,
419
+ ecma_make_number_value (zero_number_p),
420
+ true ),
421
+ ret_value);
422
+
423
+ /* 8.b. */
424
+ ECMA_TRY_CATCH (new_array_value,
425
+ ecma_op_create_array_object (NULL , 0 , false ),
426
+ ret_value);
427
+
428
+ ecma_object_t *new_array_obj_p = ecma_get_object_from_value (new_array_value);
429
+
430
+ /* 8.c. */
431
+ ecma_number_t previous_last_index = 0 ;
432
+ /* 8.d. */
433
+ uint32_t n = 0 ;
434
+ /* 8.e. */
435
+ bool last_match = true ;
436
+
437
+ // ecma_completion_value_t exec_result = ecma_make_empty_completion_value ();
438
+
439
+ /* 8.f. */
440
+ while (last_match && ecma_is_completion_value_empty (ret_value))
441
+ {
442
+ /* 8.f.i. */
443
+ ECMA_TRY_CATCH (exec_value,
444
+ ecma_builtin_regexp_prototype_dispatch_routine (LIT_MAGIC_STRING_EXEC,
445
+ regexp_value,
446
+ exec_arguments,
447
+ 1 ),
448
+ ret_value);
449
+
450
+ if (ecma_is_value_null (exec_value))
451
+ {
452
+ /* 8.f.ii. */
453
+ last_match = false ;
454
+ }
455
+ else
456
+ {
457
+ /* 8.f.iii. */
458
+ ECMA_TRY_CATCH (this_index_value,
459
+ ecma_op_object_get (regexp_obj_p, last_index_string_p),
460
+ ret_value);
461
+
462
+ ECMA_TRY_CATCH (this_index_number,
463
+ ecma_op_to_number (this_index_value),
464
+ ret_value);
465
+
466
+ ecma_number_t this_index = *ecma_get_number_from_value (this_index_number);
467
+
468
+ /* 8.f.iii.2. */
469
+ if (this_index == previous_last_index)
470
+ {
471
+ ecma_number_t *new_last_index_p = ecma_alloc_number ();
472
+ *new_last_index_p = this_index + 1 ;
473
+ /* 8.f.iii.2.a. */
474
+ ECMA_TRY_CATCH (index_put_value,
475
+ ecma_op_object_put (regexp_obj_p,
476
+ last_index_string_p,
477
+ ecma_make_number_value (new_last_index_p),
478
+ true ),
479
+ ret_value);
480
+
481
+ /* 8.f.iii.2.b. */
482
+ previous_last_index = this_index + 1 ;
483
+
484
+ ECMA_FINALIZE (index_put_value);
485
+
486
+ ecma_dealloc_number (new_last_index_p);
487
+ }
488
+ else
489
+ {
490
+ /* 8.f.iii.3. */
491
+ previous_last_index = this_index;
492
+ }
493
+
494
+ if (ecma_is_completion_value_empty (ret_value))
495
+ {
496
+ /* 8.f.iii.4. */
497
+ JERRY_ASSERT (ecma_is_value_object (exec_value));
498
+ ecma_object_t *exec_obj_p = ecma_get_object_from_value (exec_value);
499
+
500
+ ECMA_TRY_CATCH (match_string_value,
501
+ ecma_op_object_get (exec_obj_p, index_zero_string_p),
502
+ ret_value);
503
+
504
+ /* 8.f.iii.5. */
505
+ ecma_property_descriptor_t prop_desc = ecma_make_empty_property_descriptor ();
506
+ {
507
+ prop_desc.is_value_defined = true ;
508
+ prop_desc.value = match_string_value;
509
+
510
+ prop_desc.is_writable_defined = true ;
511
+ prop_desc.is_writable = true ;
512
+
513
+ prop_desc.is_enumerable_defined = true ;
514
+ prop_desc.is_enumerable = true ;
515
+
516
+ prop_desc.is_configurable_defined = true ;
517
+ prop_desc.is_configurable = true ;
518
+ }
519
+
520
+ ecma_string_t *current_index_str_p = ecma_new_ecma_string_from_uint32 (n);
521
+
522
+ ecma_completion_value_t completion = ecma_op_object_define_own_property (new_array_obj_p,
523
+ current_index_str_p,
524
+ &prop_desc,
525
+ false );
526
+ JERRY_ASSERT (ecma_is_completion_value_normal_true (completion));
527
+
528
+ ecma_deref_ecma_string (current_index_str_p);
529
+
530
+ /* 8.f.iii.6. */
531
+ n++;
532
+
533
+ ECMA_FINALIZE (match_string_value);
534
+ }
535
+
536
+ ECMA_FINALIZE (this_index_number);
537
+
538
+ ECMA_FINALIZE (this_index_value);
539
+ }
540
+
541
+ ECMA_FINALIZE (exec_value);
542
+ }
543
+
544
+ if (ecma_is_completion_value_empty (ret_value))
545
+ {
546
+ if (n == 0 )
547
+ {
548
+ /* 8.g. */
549
+ ret_value = ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_NULL);
550
+ }
551
+ else
552
+ {
553
+ /* 8.h. */
554
+ ret_value = ecma_make_normal_completion_value (ecma_copy_value (new_array_value, true ));
555
+ }
556
+ }
557
+
558
+ ECMA_FINALIZE (new_array_value);
559
+
560
+ ECMA_FINALIZE (put_value);
561
+
562
+ ecma_deref_ecma_string (last_index_string_p);
563
+ ecma_deref_ecma_string (index_zero_string_p);
564
+ ecma_dealloc_number (zero_number_p);
565
+ }
566
+
567
+ ECMA_FINALIZE (global_value);
568
+
569
+ ecma_deref_ecma_string (global_string_p);
570
+
571
+ ecma_free_value (regexp_value, true );
572
+ }
573
+
574
+ ECMA_FINALIZE (this_to_string_value);
575
+
576
+ ECMA_FINALIZE (this_check_coercible_value);
577
+
578
+ return ret_value;
348
579
} /* ecma_builtin_string_prototype_object_match */
580
+ #endif /* !CONFIG_ECMA_COMPACT_PROFILE_DISABLE_REGEXP_BUILTIN */
349
581
350
582
/* *
351
583
* The String.prototype object's 'replace' routine
0 commit comments