@@ -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
}
@@ -422,15 +403,13 @@ private List<RubyModule> getPrependedModulesAndSelf() {
422
403
/** Set the value of a constant, possibly redefining it. */
423
404
@ TruffleBoundary
424
405
public RubyConstant setConstant (RubyContext context , Node currentNode , String name , Object value ) {
425
- if (value instanceof RubyModule ) {
426
- return ((RubyModule ) value ).fields .getAdoptedByLexicalParent (
427
- context ,
428
- rubyModule ,
429
- name ,
430
- currentNode );
431
- } else {
432
- return setConstantInternal (context , currentNode , name , value , false );
406
+ // A module fully qualified name should replace a temporary one before assigning a constant,
407
+ // and before calling in the #const_added callback.
408
+ if (value instanceof RubyModule childModule ) {
409
+ childModule .fields .nameModule (context , rubyModule , name );
433
410
}
411
+
412
+ return setConstantInternal (context , currentNode , name , value , false );
434
413
}
435
414
436
415
@ TruffleBoundary
@@ -785,7 +764,11 @@ public Object getRubyStringName() {
785
764
@ TruffleBoundary
786
765
private String createAnonymousName () {
787
766
if (givenBaseName != null ) {
788
- return lexicalParent .fields .getName () + "::" + givenBaseName ;
767
+ if (lexicalParent == getObjectClass ()) {
768
+ return givenBaseName ;
769
+ } else {
770
+ return lexicalParent .fields .getName () + "::" + givenBaseName ;
771
+ }
789
772
} else if (getLogicalClass () == rubyModule ) { // For the case of class Class during initialization
790
773
return "#<cyclic>" ;
791
774
} else if (RubyGuards .isSingletonClass (rubyModule )) {
@@ -1174,4 +1157,12 @@ private void refinedMethod(RubyContext context, String methodName, Node currentN
1174
1157
}
1175
1158
}
1176
1159
1160
+ private RubyClass getObjectClass () {
1161
+ // Tricky, we need to compare with the Object class, but we only have a Module at hand.
1162
+ final RubyClass classClass = getLogicalClass ().getLogicalClass ();
1163
+ final RubyClass objectClass = (RubyClass ) ((RubyClass ) classClass .superclass ).superclass ;
1164
+ assert objectClass .fields .givenBaseName == "Object" ;
1165
+ return objectClass ;
1166
+ }
1167
+
1177
1168
}
0 commit comments