28
28
#include "ruby/encoding.h"
29
29
#include "ruby/util.h"
30
30
#include "ruby_assert.h"
31
+ #include "vm_core.h"
31
32
#include "vm_sync.h"
32
33
#include "ruby_atomic.h"
33
34
@@ -344,6 +345,7 @@ static int
344
345
enc_table_expand (struct enc_table * enc_table , int newsize )
345
346
{
346
347
if (newsize > ENCODING_LIST_CAPA ) {
348
+ RB_VM_UNLOCK ();
347
349
rb_raise (rb_eEncodingError , "too many encoding (> %d)" , ENCODING_LIST_CAPA );
348
350
}
349
351
return newsize ;
421
423
rb_enc_register (const char * name , rb_encoding * encoding )
422
424
{
423
425
int index ;
426
+ bool set_const = false;
424
427
425
428
GLOBAL_ENC_TABLE_LOCKING (enc_table ) {
426
429
index = enc_registered (enc_table , name );
@@ -439,9 +442,12 @@ rb_enc_register(const char *name, rb_encoding *encoding)
439
442
}
440
443
else {
441
444
index = enc_register (enc_table , name , encoding );
442
- set_encoding_const ( name , rb_enc_from_index ( index )) ;
445
+ set_const = true ;
443
446
}
444
447
}
448
+ if (set_const ) {
449
+ set_encoding_const (name , rb_enc_from_index (index ));
450
+ }
445
451
return index ;
446
452
}
447
453
@@ -472,22 +478,25 @@ rb_enc_registered(const char *name)
472
478
void
473
479
rb_encdb_declare (const char * name )
474
480
{
481
+ int idx ;
475
482
GLOBAL_ENC_TABLE_LOCKING (enc_table ) {
476
- int idx = enc_registered (enc_table , name );
483
+ idx = enc_registered (enc_table , name );
477
484
if (idx < 0 ) {
478
485
idx = enc_register (enc_table , name , 0 );
479
486
}
480
- set_encoding_const (name , rb_enc_from_index (idx ));
481
487
}
488
+ set_encoding_const (name , rb_enc_from_index (idx ));
482
489
}
483
490
484
491
static void
485
492
enc_check_addable (struct enc_table * enc_table , const char * name )
486
493
{
487
494
if (enc_registered (enc_table , name ) >= 0 ) {
495
+ RB_VM_UNLOCK ();
488
496
rb_raise (rb_eArgError , "encoding %s is already registered" , name );
489
497
}
490
498
else if (!valid_encoding_name_p (name )) {
499
+ RB_VM_UNLOCK ();
491
500
rb_raise (rb_eArgError , "invalid encoding name: %s" , name );
492
501
}
493
502
}
@@ -535,9 +544,11 @@ enc_replicate(struct enc_table *enc_table, const char *name, rb_encoding *encodi
535
544
536
545
enc_check_addable (enc_table , name );
537
546
idx = enc_register (enc_table , name , encoding );
538
- if (idx < 0 ) rb_raise (rb_eArgError , "invalid encoding name: %s" , name );
547
+ if (idx < 0 ) {
548
+ RB_VM_UNLOCK ();
549
+ rb_raise (rb_eArgError , "invalid encoding name: %s" , name );
550
+ }
539
551
set_base_encoding (enc_table , idx , encoding );
540
- set_encoding_const (name , rb_enc_from_index (idx ));
541
552
return idx ;
542
553
}
543
554
@@ -552,9 +563,9 @@ enc_replicate_with_index(struct enc_table *enc_table, const char *name, rb_encod
552
563
}
553
564
if (idx >= 0 ) {
554
565
set_base_encoding (enc_table , idx , origenc );
555
- set_encoding_const (name , rb_enc_from_index (idx ));
556
566
}
557
567
else {
568
+ RB_VM_UNLOCK ();
558
569
rb_raise (rb_eArgError , "failed to replicate encoding" );
559
570
}
560
571
return idx ;
@@ -575,6 +586,8 @@ rb_encdb_replicate(const char *name, const char *orig)
575
586
r = enc_replicate_with_index (enc_table , name , rb_enc_from_index (origidx ), idx );
576
587
}
577
588
589
+ set_encoding_const (name , rb_enc_from_index (r ));
590
+
578
591
return r ;
579
592
}
580
593
@@ -588,6 +601,7 @@ rb_define_dummy_encoding(const char *name)
588
601
rb_encoding * enc = enc_table -> list [index ].enc ;
589
602
ENC_SET_DUMMY ((rb_raw_encoding * )enc );
590
603
}
604
+ set_encoding_const (name , rb_enc_from_index (index ));
591
605
592
606
return index ;
593
607
}
@@ -605,6 +619,8 @@ rb_encdb_dummy(const char *name)
605
619
ENC_SET_DUMMY ((rb_raw_encoding * )enc );
606
620
}
607
621
622
+ set_encoding_const (name , rb_enc_from_index (index ));
623
+
608
624
return index ;
609
625
}
610
626
@@ -671,18 +687,20 @@ enc_alias_internal(struct enc_table *enc_table, const char *alias, int idx)
671
687
}
672
688
673
689
static int
674
- enc_alias (struct enc_table * enc_table , const char * alias , int idx )
690
+ enc_alias (struct enc_table * enc_table , const char * alias , int idx , bool * set_const )
675
691
{
676
692
if (!valid_encoding_name_p (alias )) return -1 ;
677
- if (!enc_alias_internal (enc_table , alias , idx ))
678
- set_encoding_const (alias , enc_from_index (enc_table , idx ));
693
+ if (!enc_alias_internal (enc_table , alias , idx )) {
694
+ * set_const = true;
695
+ }
679
696
return idx ;
680
697
}
681
698
682
699
int
683
700
rb_enc_alias (const char * alias , const char * orig )
684
701
{
685
702
int idx , r ;
703
+ bool set_const = false;
686
704
GLOBAL_ENC_TABLE_LOCKING (enc_table ) {
687
705
enc_check_addable (enc_table , alias ); // can raise
688
706
}
@@ -691,7 +709,11 @@ rb_enc_alias(const char *alias, const char *orig)
691
709
if (idx < 0 ) return -1 ;
692
710
693
711
GLOBAL_ENC_TABLE_LOCKING (enc_table ) {
694
- r = enc_alias (enc_table , alias , idx );
712
+ r = enc_alias (enc_table , alias , idx , & set_const );
713
+ }
714
+
715
+ if (set_const ) {
716
+ set_encoding_const (alias , rb_enc_from_index (idx ));
695
717
}
696
718
697
719
return r ;
@@ -701,14 +723,18 @@ int
701
723
rb_encdb_alias (const char * alias , const char * orig )
702
724
{
703
725
int r ;
726
+ bool set_const = false;
704
727
705
728
GLOBAL_ENC_TABLE_LOCKING (enc_table ) {
706
729
int idx = enc_registered (enc_table , orig );
707
730
708
731
if (idx < 0 ) {
709
732
idx = enc_register (enc_table , orig , 0 );
710
733
}
711
- r = enc_alias (enc_table , alias , idx );
734
+ r = enc_alias (enc_table , alias , idx , & set_const );
735
+ }
736
+ if (set_const ) {
737
+ set_encoding_const (alias , rb_enc_from_index (r ));
712
738
}
713
739
714
740
return r ;
@@ -717,34 +743,35 @@ rb_encdb_alias(const char *alias, const char *orig)
717
743
static void
718
744
rb_enc_init (struct enc_table * enc_table )
719
745
{
720
- ASSERT_vm_locking ();
721
- enc_table_expand (enc_table , ENCODING_COUNT + 1 );
722
- if (!enc_table -> names ) {
723
- enc_table -> names = st_init_strcasetable_with_size (ENCODING_LIST_CAPA );
724
- }
746
+ RB_VM_LOCKING () {
747
+ enc_table_expand (enc_table , ENCODING_COUNT + 1 );
748
+ if (!enc_table -> names ) {
749
+ enc_table -> names = st_init_strcasetable_with_size (ENCODING_LIST_CAPA );
750
+ }
725
751
#define OnigEncodingASCII_8BIT OnigEncodingASCII
726
752
#define ENC_REGISTER (enc ) enc_register_at(enc_table, ENCINDEX_##enc, rb_enc_name(&OnigEncoding##enc), &OnigEncoding##enc)
727
- ENC_REGISTER (ASCII_8BIT );
728
- ENC_REGISTER (UTF_8 );
729
- ENC_REGISTER (US_ASCII );
730
- global_enc_ascii = enc_table -> list [ENCINDEX_ASCII_8BIT ].enc ;
731
- global_enc_utf_8 = enc_table -> list [ENCINDEX_UTF_8 ].enc ;
732
- global_enc_us_ascii = enc_table -> list [ENCINDEX_US_ASCII ].enc ;
753
+ ENC_REGISTER (ASCII_8BIT );
754
+ ENC_REGISTER (UTF_8 );
755
+ ENC_REGISTER (US_ASCII );
756
+ global_enc_ascii = enc_table -> list [ENCINDEX_ASCII_8BIT ].enc ;
757
+ global_enc_utf_8 = enc_table -> list [ENCINDEX_UTF_8 ].enc ;
758
+ global_enc_us_ascii = enc_table -> list [ENCINDEX_US_ASCII ].enc ;
733
759
#undef ENC_REGISTER
734
760
#undef OnigEncodingASCII_8BIT
735
761
#define ENCDB_REGISTER (name , enc ) enc_register_at(enc_table, ENCINDEX_##enc, name, NULL)
736
- ENCDB_REGISTER ("UTF-16BE" , UTF_16BE );
737
- ENCDB_REGISTER ("UTF-16LE" , UTF_16LE );
738
- ENCDB_REGISTER ("UTF-32BE" , UTF_32BE );
739
- ENCDB_REGISTER ("UTF-32LE" , UTF_32LE );
740
- ENCDB_REGISTER ("UTF-16" , UTF_16 );
741
- ENCDB_REGISTER ("UTF-32" , UTF_32 );
742
- ENCDB_REGISTER ("UTF8-MAC" , UTF8_MAC );
743
-
744
- ENCDB_REGISTER ("EUC-JP" , EUC_JP );
745
- ENCDB_REGISTER ("Windows-31J" , Windows_31J );
762
+ ENCDB_REGISTER ("UTF-16BE" , UTF_16BE );
763
+ ENCDB_REGISTER ("UTF-16LE" , UTF_16LE );
764
+ ENCDB_REGISTER ("UTF-32BE" , UTF_32BE );
765
+ ENCDB_REGISTER ("UTF-32LE" , UTF_32LE );
766
+ ENCDB_REGISTER ("UTF-16" , UTF_16 );
767
+ ENCDB_REGISTER ("UTF-32" , UTF_32 );
768
+ ENCDB_REGISTER ("UTF8-MAC" , UTF8_MAC );
769
+
770
+ ENCDB_REGISTER ("EUC-JP" , EUC_JP );
771
+ ENCDB_REGISTER ("Windows-31J" , Windows_31J );
746
772
#undef ENCDB_REGISTER
747
- enc_table -> count = ENCINDEX_BUILTIN_MAX ;
773
+ enc_table -> count = ENCINDEX_BUILTIN_MAX ;
774
+ }
748
775
}
749
776
750
777
rb_encoding *
865
892
rb_enc_find_index (const char * name )
866
893
{
867
894
int i ;
868
- ASSERT_vm_unlocking (); // it needs to be unlocked so it can call `load_encoding` if necessary
895
+ #if RUBY_DEBUG
896
+ if (rb_multi_ractor_p () || !rb_enc_registered (name )) {
897
+ ASSERT_vm_unlocking (); // it needs to be unlocked so it can call `load_encoding` if necessary
898
+ }
899
+ #endif
869
900
GLOBAL_ENC_TABLE_LOCKING (enc_table ) {
870
901
i = enc_registered (enc_table , name );
871
902
}
@@ -1812,6 +1843,7 @@ set_default_internal(VALUE klass, VALUE encoding)
1812
1843
static void
1813
1844
set_encoding_const (const char * name , rb_encoding * enc )
1814
1845
{
1846
+ ASSERT_vm_unlocking ();
1815
1847
VALUE encoding = rb_enc_from_encoding (enc );
1816
1848
char * s = (char * )name ;
1817
1849
int haslower = 0 , hasupper = 0 , valid = 0 ;
0 commit comments