@@ -165,53 +165,34 @@ public void afterConstructed() {
165
165
getName ();
166
166
}
167
167
168
- public RubyConstant getAdoptedByLexicalParent (
168
+ private void nameModule (
169
169
RubyContext context ,
170
170
RubyModule lexicalParent ,
171
- String name ,
172
- Node currentNode ) {
171
+ String name ) {
173
172
assert name != null ;
174
173
175
- RubyConstant previous = lexicalParent .fields .setConstantInternal (
176
- context ,
177
- currentNode ,
178
- name ,
179
- rubyModule ,
180
- false );
181
-
182
174
if (!hasFullName ()) {
183
- // Tricky, we need to compare with the Object class, but we only have a Class at hand.
184
- final RubyClass classClass = getLogicalClass ().getLogicalClass ();
185
- final RubyClass objectClass = (RubyClass ) ((RubyClass ) classClass .superclass ).superclass ;
186
-
187
- if (lexicalParent == objectClass ) {
175
+ if (lexicalParent == getObjectClass ()) {
188
176
this .setFullName (name );
189
- updateAnonymousChildrenModules (context );
177
+ nameChildrenModules (context );
190
178
} else if (lexicalParent .fields .hasFullName ()) {
191
179
this .setFullName (lexicalParent .fields .getName () + "::" + name );
192
- updateAnonymousChildrenModules (context );
180
+ nameChildrenModules (context );
193
181
} else {
194
182
// Our lexicalParent is also an anonymous module
195
183
// and will name us when it gets named via updateAnonymousChildrenModules(),
196
184
// or we'll compute an anonymous name on #getName() if needed
197
185
}
198
186
}
199
- return previous ;
200
187
}
201
188
202
- public void updateAnonymousChildrenModules (RubyContext context ) {
189
+ private void nameChildrenModules (RubyContext context ) {
203
190
for (Map .Entry <String , ConstantEntry > entry : constants .entrySet ()) {
204
191
ConstantEntry constantEntry = entry .getValue ();
205
192
RubyConstant constant = constantEntry .getConstant ();
206
- if (constant != null && constant .hasValue () && constant .getValue () instanceof RubyModule ) {
207
- RubyModule module = (RubyModule ) constant .getValue ();
208
- if (!module .fields .hasFullName ()) {
209
- module .fields .getAdoptedByLexicalParent (
210
- context ,
211
- rubyModule ,
212
- entry .getKey (),
213
- null );
214
- }
193
+
194
+ if (constant != null && constant .hasValue () && constant .getValue () instanceof RubyModule childModule ) {
195
+ childModule .fields .nameModule (context , rubyModule , entry .getKey ());
215
196
}
216
197
}
217
198
}
@@ -429,15 +410,13 @@ private List<RubyModule> getPrependedModulesAndSelf() {
429
410
/** Set the value of a constant, possibly redefining it. */
430
411
@ TruffleBoundary
431
412
public RubyConstant setConstant (RubyContext context , Node currentNode , String name , Object value ) {
432
- if (value instanceof RubyModule ) {
433
- return ((RubyModule ) value ).fields .getAdoptedByLexicalParent (
434
- context ,
435
- rubyModule ,
436
- name ,
437
- currentNode );
438
- } else {
439
- return setConstantInternal (context , currentNode , name , value , false );
413
+ // A module fully qualified name should replace a temporary one before assigning a constant,
414
+ // and before calling in the #const_added callback.
415
+ if (value instanceof RubyModule childModule ) {
416
+ childModule .fields .nameModule (context , rubyModule , name );
440
417
}
418
+
419
+ return setConstantInternal (context , currentNode , name , value , false );
441
420
}
442
421
443
422
@ TruffleBoundary
@@ -792,7 +771,11 @@ public Object getRubyStringName() {
792
771
@ TruffleBoundary
793
772
private String createAnonymousName () {
794
773
if (givenBaseName != null ) {
795
- return lexicalParent .fields .getName () + "::" + givenBaseName ;
774
+ if (lexicalParent == getObjectClass ()) {
775
+ return givenBaseName ;
776
+ } else {
777
+ return lexicalParent .fields .getName () + "::" + givenBaseName ;
778
+ }
796
779
} else if (getLogicalClass () == rubyModule ) { // For the case of class Class during initialization
797
780
return "#<cyclic>" ;
798
781
} else if (RubyGuards .isSingletonClass (rubyModule )) {
@@ -1181,4 +1164,12 @@ private void refinedMethod(RubyContext context, String methodName, Node currentN
1181
1164
}
1182
1165
}
1183
1166
1167
+ private RubyClass getObjectClass () {
1168
+ // Tricky, we need to compare with the Object class, but we only have a Module at hand.
1169
+ final RubyClass classClass = getLogicalClass ().getLogicalClass ();
1170
+ final RubyClass objectClass = (RubyClass ) ((RubyClass ) classClass .superclass ).superclass ;
1171
+ assert objectClass .fields .givenBaseName == "Object" ;
1172
+ return objectClass ;
1173
+ }
1174
+
1184
1175
}
0 commit comments