422
422
rb_enc_register (const char * name , rb_encoding * encoding )
423
423
{
424
424
int index ;
425
+ bool set_const = false;
425
426
426
427
GLOBAL_ENC_TABLE_LOCKING (enc_table ) {
427
428
index = enc_registered (enc_table , name );
@@ -440,9 +441,12 @@ rb_enc_register(const char *name, rb_encoding *encoding)
440
441
}
441
442
else {
442
443
index = enc_register (enc_table , name , encoding );
443
- set_encoding_const ( name , rb_enc_from_index ( index )) ;
444
+ set_const = true ;
444
445
}
445
446
}
447
+ if (set_const ) {
448
+ set_encoding_const (name , rb_enc_from_index (index ));
449
+ }
446
450
return index ;
447
451
}
448
452
@@ -473,13 +477,14 @@ rb_enc_registered(const char *name)
473
477
void
474
478
rb_encdb_declare (const char * name )
475
479
{
480
+ int idx ;
476
481
GLOBAL_ENC_TABLE_LOCKING (enc_table ) {
477
- int idx = enc_registered (enc_table , name );
482
+ idx = enc_registered (enc_table , name );
478
483
if (idx < 0 ) {
479
484
idx = enc_register (enc_table , name , 0 );
480
485
}
481
- set_encoding_const (name , rb_enc_from_index (idx ));
482
486
}
487
+ set_encoding_const (name , rb_enc_from_index (idx ));
483
488
}
484
489
485
490
static void
@@ -538,7 +543,6 @@ enc_replicate(struct enc_table *enc_table, const char *name, rb_encoding *encodi
538
543
idx = enc_register (enc_table , name , encoding );
539
544
if (idx < 0 ) rb_raise (rb_eArgError , "invalid encoding name: %s" , name );
540
545
set_base_encoding (enc_table , idx , encoding );
541
- set_encoding_const (name , rb_enc_from_index (idx ));
542
546
return idx ;
543
547
}
544
548
@@ -553,7 +557,6 @@ enc_replicate_with_index(struct enc_table *enc_table, const char *name, rb_encod
553
557
}
554
558
if (idx >= 0 ) {
555
559
set_base_encoding (enc_table , idx , origenc );
556
- set_encoding_const (name , rb_enc_from_index (idx ));
557
560
}
558
561
else {
559
562
rb_raise (rb_eArgError , "failed to replicate encoding" );
@@ -576,6 +579,8 @@ rb_encdb_replicate(const char *name, const char *orig)
576
579
r = enc_replicate_with_index (enc_table , name , rb_enc_from_index (origidx ), idx );
577
580
}
578
581
582
+ set_encoding_const (name , rb_enc_from_index (r ));
583
+
579
584
return r ;
580
585
}
581
586
@@ -589,6 +594,7 @@ rb_define_dummy_encoding(const char *name)
589
594
rb_encoding * enc = enc_table -> list [index ].enc ;
590
595
ENC_SET_DUMMY ((rb_raw_encoding * )enc );
591
596
}
597
+ set_encoding_const (name , rb_enc_from_index (index ));
592
598
593
599
return index ;
594
600
}
@@ -606,6 +612,8 @@ rb_encdb_dummy(const char *name)
606
612
ENC_SET_DUMMY ((rb_raw_encoding * )enc );
607
613
}
608
614
615
+ set_encoding_const (name , rb_enc_from_index (index ));
616
+
609
617
return index ;
610
618
}
611
619
@@ -672,18 +680,20 @@ enc_alias_internal(struct enc_table *enc_table, const char *alias, int idx)
672
680
}
673
681
674
682
static int
675
- 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 )
676
684
{
677
685
if (!valid_encoding_name_p (alias )) return -1 ;
678
- if (!enc_alias_internal (enc_table , alias , idx ))
679
- set_encoding_const (alias , enc_from_index (enc_table , idx ));
686
+ if (!enc_alias_internal (enc_table , alias , idx )) {
687
+ * set_const = true;
688
+ }
680
689
return idx ;
681
690
}
682
691
683
692
int
684
693
rb_enc_alias (const char * alias , const char * orig )
685
694
{
686
695
int idx , r ;
696
+ bool set_const = false;
687
697
GLOBAL_ENC_TABLE_LOCKING (enc_table ) {
688
698
enc_check_addable (enc_table , alias ); // can raise
689
699
}
@@ -692,7 +702,11 @@ rb_enc_alias(const char *alias, const char *orig)
692
702
if (idx < 0 ) return -1 ;
693
703
694
704
GLOBAL_ENC_TABLE_LOCKING (enc_table ) {
695
- 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 ));
696
710
}
697
711
698
712
return r ;
@@ -702,14 +716,18 @@ int
702
716
rb_encdb_alias (const char * alias , const char * orig )
703
717
{
704
718
int r ;
719
+ bool set_const = false;
705
720
706
721
GLOBAL_ENC_TABLE_LOCKING (enc_table ) {
707
722
int idx = enc_registered (enc_table , orig );
708
723
709
724
if (idx < 0 ) {
710
725
idx = enc_register (enc_table , orig , 0 );
711
726
}
712
- 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 ));
713
731
}
714
732
715
733
return r ;
@@ -1818,6 +1836,7 @@ set_default_internal(VALUE klass, VALUE encoding)
1818
1836
static void
1819
1837
set_encoding_const (const char * name , rb_encoding * enc )
1820
1838
{
1839
+ ASSERT_vm_unlocking ();
1821
1840
VALUE encoding = rb_enc_from_encoding (enc );
1822
1841
char * s = (char * )name ;
1823
1842
int haslower = 0 , hasupper = 0 , valid = 0 ;
0 commit comments