@@ -652,21 +652,29 @@ public DynamicObject encodingGetObjectEncodingNil(DynamicObject object) {
652
652
653
653
}
654
654
655
+ public static abstract class EncodingCreationNode extends PrimitiveArrayArgumentsNode {
656
+
657
+ public DynamicObject setIndexOrRaiseError (String name , DynamicObject newEncoding ) {
658
+ if (newEncoding == null ) {
659
+ throw new RaiseException (getContext (), coreExceptions ().argumentErrorEncodingAlreadyRegistered (name , this ));
660
+ }
661
+
662
+ final int index = Layouts .ENCODING .getEncoding (newEncoding ).getIndex ();
663
+ return createArray (new Object []{ newEncoding , index }, 2 );
664
+ }
665
+
666
+ }
667
+
655
668
@ Primitive (name = "encoding_replicate" )
656
- public static abstract class EncodingReplicateNode extends PrimitiveArrayArgumentsNode {
669
+ public static abstract class EncodingReplicateNode extends EncodingCreationNode {
657
670
658
671
@ Specialization (guards = "isRubyString(nameObject)" )
659
672
public DynamicObject encodingReplicate (DynamicObject self , DynamicObject nameObject ) {
660
673
final String name = StringOperations .getString (nameObject );
661
674
final Encoding encoding = EncodingOperations .getEncoding (self );
662
675
663
676
final DynamicObject newEncoding = replicate (name , encoding );
664
- if (newEncoding == null ) {
665
- throw new RaiseException (getContext (), coreExceptions ().argumentErrorEncodingAlreadyRegistered (name , this ));
666
- }
667
-
668
- final int index = Layouts .ENCODING .getEncoding (newEncoding ).getIndex ();
669
- return createArray (new Object []{ newEncoding , index }, 2 );
677
+ return setIndexOrRaiseError (name , newEncoding );
670
678
}
671
679
672
680
@ TruffleBoundary
@@ -677,23 +685,18 @@ private DynamicObject replicate(String name, Encoding encoding) {
677
685
}
678
686
679
687
@ Primitive (name = "encoding_create_dummy" , needsSelf = false )
680
- public static abstract class DummyEncodingeNode extends PrimitiveArrayArgumentsNode {
688
+ public static abstract class DummyEncodingeNode extends EncodingCreationNode {
681
689
682
690
@ Specialization (guards = "isRubyString(nameObject)" )
683
691
public DynamicObject createDummyEncoding (DynamicObject nameObject ) {
684
692
final String name = StringOperations .getString (nameObject );
685
693
686
- final DynamicObject newEncoding = dummy (name );
687
- if (newEncoding == null ) {
688
- throw new RaiseException (getContext (), coreExceptions ().argumentErrorEncodingAlreadyRegistered (name , this ));
689
- }
690
-
691
- final int index = Layouts .ENCODING .getEncoding (newEncoding ).getIndex ();
692
- return createArray (new Object []{ newEncoding , index }, 2 );
694
+ final DynamicObject newEncoding = createDummy (name );
695
+ return setIndexOrRaiseError (name , newEncoding );
693
696
}
694
697
695
698
@ TruffleBoundary
696
- private DynamicObject dummy (String name ) {
699
+ private DynamicObject createDummy (String name ) {
697
700
return getContext ().getEncodingManager ().createDummyEncoding (name );
698
701
}
699
702
0 commit comments