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
421
422
rb_enc_register (const char * name , rb_encoding * encoding )
422
423
{
423
424
int index ;
425
+ bool set_const = false;
424
426
425
427
GLOBAL_ENC_TABLE_LOCKING (enc_table ) {
426
428
index = enc_registered (enc_table , name );
@@ -439,9 +441,12 @@ rb_enc_register(const char *name, rb_encoding *encoding)
439
441
}
440
442
else {
441
443
index = enc_register (enc_table , name , encoding );
442
- set_encoding_const ( name , rb_enc_from_index ( index )) ;
444
+ set_const = true ;
443
445
}
444
446
}
447
+ if (set_const ) {
448
+ set_encoding_const (name , rb_enc_from_index (index ));
449
+ }
445
450
return index ;
446
451
}
447
452
@@ -472,13 +477,14 @@ rb_enc_registered(const char *name)
472
477
void
473
478
rb_encdb_declare (const char * name )
474
479
{
480
+ int idx ;
475
481
GLOBAL_ENC_TABLE_LOCKING (enc_table ) {
476
- int idx = enc_registered (enc_table , name );
482
+ idx = enc_registered (enc_table , name );
477
483
if (idx < 0 ) {
478
484
idx = enc_register (enc_table , name , 0 );
479
485
}
480
- set_encoding_const (name , rb_enc_from_index (idx ));
481
486
}
487
+ set_encoding_const (name , rb_enc_from_index (idx ));
482
488
}
483
489
484
490
static void
@@ -537,7 +543,6 @@ enc_replicate(struct enc_table *enc_table, const char *name, rb_encoding *encodi
537
543
idx = enc_register (enc_table , name , encoding );
538
544
if (idx < 0 ) rb_raise (rb_eArgError , "invalid encoding name: %s" , name );
539
545
set_base_encoding (enc_table , idx , encoding );
540
- set_encoding_const (name , rb_enc_from_index (idx ));
541
546
return idx ;
542
547
}
543
548
@@ -552,7 +557,6 @@ enc_replicate_with_index(struct enc_table *enc_table, const char *name, rb_encod
552
557
}
553
558
if (idx >= 0 ) {
554
559
set_base_encoding (enc_table , idx , origenc );
555
- set_encoding_const (name , rb_enc_from_index (idx ));
556
560
}
557
561
else {
558
562
rb_raise (rb_eArgError , "failed to replicate encoding" );
@@ -575,6 +579,8 @@ rb_encdb_replicate(const char *name, const char *orig)
575
579
r = enc_replicate_with_index (enc_table , name , rb_enc_from_index (origidx ), idx );
576
580
}
577
581
582
+ set_encoding_const (name , rb_enc_from_index (r ));
583
+
578
584
return r ;
579
585
}
580
586
@@ -588,6 +594,7 @@ rb_define_dummy_encoding(const char *name)
588
594
rb_encoding * enc = enc_table -> list [index ].enc ;
589
595
ENC_SET_DUMMY ((rb_raw_encoding * )enc );
590
596
}
597
+ set_encoding_const (name , rb_enc_from_index (index ));
591
598
592
599
return index ;
593
600
}
@@ -605,6 +612,8 @@ rb_encdb_dummy(const char *name)
605
612
ENC_SET_DUMMY ((rb_raw_encoding * )enc );
606
613
}
607
614
615
+ set_encoding_const (name , rb_enc_from_index (index ));
616
+
608
617
return index ;
609
618
}
610
619
@@ -671,18 +680,20 @@ enc_alias_internal(struct enc_table *enc_table, const char *alias, int idx)
671
680
}
672
681
673
682
static int
674
- enc_alias (struct enc_table * enc_table , const char * alias , int idx )
683
+ enc_alias (struct enc_table * enc_table , const char * alias , int idx , bool * set_const )
675
684
{
676
685
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 ));
686
+ if (!enc_alias_internal (enc_table , alias , idx )) {
687
+ * set_const = true;
688
+ }
679
689
return idx ;
680
690
}
681
691
682
692
int
683
693
rb_enc_alias (const char * alias , const char * orig )
684
694
{
685
695
int idx , r ;
696
+ bool set_const = false;
686
697
GLOBAL_ENC_TABLE_LOCKING (enc_table ) {
687
698
enc_check_addable (enc_table , alias ); // can raise
688
699
}
@@ -691,7 +702,11 @@ rb_enc_alias(const char *alias, const char *orig)
691
702
if (idx < 0 ) return -1 ;
692
703
693
704
GLOBAL_ENC_TABLE_LOCKING (enc_table ) {
694
- r = enc_alias (enc_table , alias , idx );
705
+ r = enc_alias (enc_table , alias , idx , & set_const );
706
+ }
707
+
708
+ if (set_const ) {
709
+ set_encoding_const (alias , rb_enc_from_index (idx ));
695
710
}
696
711
697
712
return r ;
@@ -701,14 +716,18 @@ int
701
716
rb_encdb_alias (const char * alias , const char * orig )
702
717
{
703
718
int r ;
719
+ bool set_const = false;
704
720
705
721
GLOBAL_ENC_TABLE_LOCKING (enc_table ) {
706
722
int idx = enc_registered (enc_table , orig );
707
723
708
724
if (idx < 0 ) {
709
725
idx = enc_register (enc_table , orig , 0 );
710
726
}
711
- r = enc_alias (enc_table , alias , idx );
727
+ r = enc_alias (enc_table , alias , idx , & set_const );
728
+ }
729
+ if (set_const ) {
730
+ set_encoding_const (alias , rb_enc_from_index (r ));
712
731
}
713
732
714
733
return r ;
@@ -717,34 +736,35 @@ rb_encdb_alias(const char *alias, const char *orig)
717
736
static void
718
737
rb_enc_init (struct enc_table * enc_table )
719
738
{
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
- }
739
+ RB_VM_LOCKING () {
740
+ enc_table_expand (enc_table , ENCODING_COUNT + 1 );
741
+ if (!enc_table -> names ) {
742
+ enc_table -> names = st_init_strcasetable_with_size (ENCODING_LIST_CAPA );
743
+ }
725
744
#define OnigEncodingASCII_8BIT OnigEncodingASCII
726
745
#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 ;
746
+ ENC_REGISTER (ASCII_8BIT );
747
+ ENC_REGISTER (UTF_8 );
748
+ ENC_REGISTER (US_ASCII );
749
+ global_enc_ascii = enc_table -> list [ENCINDEX_ASCII_8BIT ].enc ;
750
+ global_enc_utf_8 = enc_table -> list [ENCINDEX_UTF_8 ].enc ;
751
+ global_enc_us_ascii = enc_table -> list [ENCINDEX_US_ASCII ].enc ;
733
752
#undef ENC_REGISTER
734
753
#undef OnigEncodingASCII_8BIT
735
754
#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 );
755
+ ENCDB_REGISTER ("UTF-16BE" , UTF_16BE );
756
+ ENCDB_REGISTER ("UTF-16LE" , UTF_16LE );
757
+ ENCDB_REGISTER ("UTF-32BE" , UTF_32BE );
758
+ ENCDB_REGISTER ("UTF-32LE" , UTF_32LE );
759
+ ENCDB_REGISTER ("UTF-16" , UTF_16 );
760
+ ENCDB_REGISTER ("UTF-32" , UTF_32 );
761
+ ENCDB_REGISTER ("UTF8-MAC" , UTF8_MAC );
762
+
763
+ ENCDB_REGISTER ("EUC-JP" , EUC_JP );
764
+ ENCDB_REGISTER ("Windows-31J" , Windows_31J );
746
765
#undef ENCDB_REGISTER
747
- enc_table -> count = ENCINDEX_BUILTIN_MAX ;
766
+ enc_table -> count = ENCINDEX_BUILTIN_MAX ;
767
+ }
748
768
}
749
769
750
770
rb_encoding *
865
885
rb_enc_find_index (const char * name )
866
886
{
867
887
int i ;
868
- ASSERT_vm_unlocking (); // it needs to be unlocked so it can call `load_encoding` if necessary
888
+ #if RUBY_DEBUG > 0
889
+ if (rb_multi_ractor_p () || !rb_enc_registered (name )) {
890
+ ASSERT_vm_unlocking (); // it needs to be unlocked so it can call `load_encoding` if necessary
891
+ }
892
+ #endif
869
893
GLOBAL_ENC_TABLE_LOCKING (enc_table ) {
870
894
i = enc_registered (enc_table , name );
871
895
}
@@ -1812,6 +1836,7 @@ set_default_internal(VALUE klass, VALUE encoding)
1812
1836
static void
1813
1837
set_encoding_const (const char * name , rb_encoding * enc )
1814
1838
{
1839
+ ASSERT_vm_unlocking ();
1815
1840
VALUE encoding = rb_enc_from_encoding (enc );
1816
1841
char * s = (char * )name ;
1817
1842
int haslower = 0 , hasupper = 0 , valid = 0 ;
0 commit comments