@@ -444,31 +444,34 @@ re_parse_alternative (re_compiler_ctx_t *re_ctx_p, /**< RegExp compiler context
444444 return ret_value ;
445445} /* re_parse_alternative */
446446
447- static const re_compiled_code_t * re_cache [RE_CHACHE_SIZE ];
447+ static const re_compiled_code_t * re_cache [RE_CACHE_SIZE ];
448448
449449/**
450450 * Search for the given pattern in the RegExp cache
451451 *
452452 * @return compiled bytecode - if found
453- * NULL - otherwise
453+ * NULL - otherwise
454454 */
455- static re_compiled_code_t *
455+ re_compiled_code_t *
456456re_find_bytecode_in_cache (ecma_string_t * pattern_str_p , /**< pattern string */
457+ uint16_t flags , /**< flags */
457458 uint32_t * idx ) /**< [out] index */
458459{
459- uint32_t free_idx = RE_CHACHE_SIZE ;
460+ uint32_t free_idx = RE_CACHE_SIZE ;
460461
461- for (* idx = 0u ; * idx < RE_CHACHE_SIZE && re_cache [ * idx ] != NULL ; (* idx )++ )
462+ for (* idx = 0u ; * idx < RE_CACHE_SIZE ; (* idx )++ )
462463 {
463464 re_compiled_code_t * cached_bytecode_p = re_cache [* idx ];
464465
465- if (( cached_bytecode_p -> flags >> ECMA_BYTECODE_REF_SHIFT ) > 0 )
466+ if (cached_bytecode_p != NULL )
466467 {
467468 ecma_string_t * cached_pattern_str_p ;
468469 cached_pattern_str_p = ECMA_GET_NON_NULL_POINTER (ecma_string_t , cached_bytecode_p -> pattern_cp );
469470
470- if (ecma_compare_ecma_strings (cached_pattern_str_p , pattern_str_p ))
471+ if ((cached_bytecode_p -> flags & RE_FLAGS_MASK ) == flags
472+ && ecma_compare_ecma_strings (cached_pattern_str_p , pattern_str_p ))
471473 {
474+ JERRY_DDLOG ("RegExp is found in cache\n" );
472475 return re_cache [* idx ];
473476 }
474477 }
@@ -479,11 +482,31 @@ re_find_bytecode_in_cache (ecma_string_t *pattern_str_p, /**< pattern string */
479482 }
480483 }
481484
485+ JERRY_DDLOG ("RegExp is NOT found in cache\n" );
482486 * idx = free_idx ;
483-
484487 return NULL ;
485488} /* re_find_bytecode_in_cache */
486489
490+ /**
491+ * Run gerbage collection in RegExp cache
492+ */
493+ void
494+ re_cache_gc_run ()
495+ {
496+ for (uint32_t i = 0u ; i < RE_CACHE_SIZE ; i ++ )
497+ {
498+ re_compiled_code_t * cached_bytecode_p = re_cache [i ];
499+
500+ if (cached_bytecode_p != NULL
501+ && (cached_bytecode_p -> flags >> ECMA_BYTECODE_REF_SHIFT ) == 1 )
502+ { /* Only the cache has reference for the bytecode */
503+
504+ ecma_bytecode_deref (cached_bytecode_p );
505+ re_cache [i ] = NULL ;
506+ }
507+ }
508+ } /* re_cache_gc_run */
509+
487510/**
488511 * Compilation of RegExp bytecode
489512 *
@@ -511,7 +534,7 @@ re_compile_bytecode (re_compiled_code_t **out_bytecode_p, /**< [out] pointer to
511534 re_ctx .bytecode_ctx_p = & bc_ctx ;
512535
513536 uint32_t cache_idx ;
514- * out_bytecode_p = re_find_bytecode_in_cache (pattern_str_p , & cache_idx );
537+ * out_bytecode_p = re_find_bytecode_in_cache (pattern_str_p , flags , & cache_idx );
515538
516539 if (* out_bytecode_p != NULL )
517540 {
@@ -578,8 +601,9 @@ re_compile_bytecode (re_compiled_code_t **out_bytecode_p, /**< [out] pointer to
578601 JERRY_ASSERT (bc_ctx .block_start_p != NULL );
579602 * out_bytecode_p = (re_compiled_code_t * ) bc_ctx .block_start_p ;
580603
581- if (cache_idx < RE_CHACHE_SIZE )
604+ if (cache_idx < RE_CACHE_SIZE )
582605 {
606+ ecma_bytecode_ref (* out_bytecode_p );
583607 re_cache [cache_idx ] = * out_bytecode_p ;
584608 }
585609 else
0 commit comments