96
96
import org .truffleruby .language .library .RubyLibrary ;
97
97
import org .truffleruby .language .library .RubyStringLibrary ;
98
98
import org .truffleruby .language .loader .CodeLoader ;
99
- import org .truffleruby .language .methods .AddMethodNode ;
100
99
import org .truffleruby .language .methods .Arity ;
101
100
import org .truffleruby .language .methods .CanBindMethodToModuleNode ;
102
101
import org .truffleruby .language .methods .DeclarationContext ;
@@ -339,8 +338,6 @@ protected RubyNode coerceOldNameToString(RubyNode oldName) {
339
338
return NameToJavaStringNode .create (oldName );
340
339
}
341
340
342
- @ Child AddMethodNode addMethodNode = AddMethodNode .create (false );
343
-
344
341
@ Specialization
345
342
protected RubyModule aliasMethod (RubyModule module , String newName , String oldName ,
346
343
@ Cached BranchProfile errorProfile ) {
@@ -355,9 +352,8 @@ protected RubyModule aliasMethod(RubyModule module, String newName, String oldNa
355
352
this ));
356
353
}
357
354
358
- InternalMethod aliasMethod = method .withName (newName );
359
-
360
- addMethodNode .executeAddMethod (module , aliasMethod , aliasMethod .getVisibility ());
355
+ final InternalMethod aliasMethod = method .withName (newName );
356
+ module .addMethodConsiderNameVisibility (getContext (), aliasMethod , aliasMethod .getVisibility (), this );
361
357
return module ;
362
358
}
363
359
@@ -1243,7 +1239,6 @@ private void warnAlreadyInitializedConstant(RubyModule module, String name,
1243
1239
@ NodeChild (value = "block" , type = RubyNode .class )
1244
1240
public abstract static class DefineMethodNode extends CoreMethodNode {
1245
1241
1246
- @ Child private AddMethodNode addMethodNode = AddMethodNode .create (false );
1247
1242
@ Child private ReadCallerFrameNode readCallerFrame = ReadCallerFrameNode .create ();
1248
1243
1249
1244
@ CreateCast ("name" )
@@ -1375,7 +1370,7 @@ private RubySymbol addMethod(RubyModule module, String name, InternalMethod meth
1375
1370
method = method .withName (name );
1376
1371
1377
1372
final Visibility visibility = GetCurrentVisibilityNode .getVisibilityFromNameAndFrame (name , callerFrame );
1378
- addMethodNode . executeAddMethod ( module , method , visibility );
1373
+ module . addMethodConsiderNameVisibility ( getContext () , method , visibility , this );
1379
1374
return getSymbol (method .getName ());
1380
1375
}
1381
1376
@@ -1605,18 +1600,25 @@ protected RubyArray nesting() {
1605
1600
}
1606
1601
}
1607
1602
1608
- @ CoreMethod (names = "public" , rest = true , visibility = Visibility .PRIVATE )
1609
- public abstract static class PublicNode extends CoreMethodArrayArgumentsNode {
1610
-
1611
- @ Child private SetVisibilityNode setVisibilityNode = SetVisibilityNode .create ();
1612
-
1613
- public abstract RubyModule executePublic (VirtualFrame frame , RubyModule module , Object [] args );
1614
-
1615
- @ Specialization
1616
- protected RubyModule doPublic (VirtualFrame frame , RubyModule module , Object [] names ) {
1617
- return setVisibilityNode .execute (frame , module , names , Visibility .PUBLIC );
1603
+ @ GenerateUncached
1604
+ @ CoreMethod (names = "public" , rest = true , visibility = Visibility .PRIVATE , alwaysInlined = true )
1605
+ public abstract static class PublicNode extends AlwaysInlinedMethodNode {
1606
+ @ Specialization (guards = "names.length == 0" )
1607
+ protected RubyModule frame (
1608
+ Frame callerFrame , RubyModule module , Object [] names , Object block , RootCallTarget target ) {
1609
+ DeclarationContext .setCurrentVisibility (callerFrame , Visibility .PUBLIC );
1610
+ return module ;
1618
1611
}
1619
1612
1613
+ @ Specialization (guards = "names.length > 0" )
1614
+ protected RubyModule methods (
1615
+ Frame callerFrame , RubyModule module , Object [] names , Object block , RootCallTarget target ,
1616
+ @ Cached SetMethodVisibilityNode setMethodVisibilityNode ) {
1617
+ for (Object name : names ) {
1618
+ setMethodVisibilityNode .execute (module , name , Visibility .PUBLIC );
1619
+ }
1620
+ return module ;
1621
+ }
1620
1622
}
1621
1623
1622
1624
@ CoreMethod (names = "public_class_method" , rest = true )
@@ -1637,18 +1639,25 @@ protected RubyModule publicClassMethod(RubyModule module, Object[] names) {
1637
1639
}
1638
1640
}
1639
1641
1640
- @ CoreMethod (names = "private" , rest = true , visibility = Visibility .PRIVATE )
1641
- public abstract static class PrivateNode extends CoreMethodArrayArgumentsNode {
1642
-
1643
- @ Child private SetVisibilityNode setVisibilityNode = SetVisibilityNode .create ();
1644
-
1645
- public abstract RubyModule executePrivate (VirtualFrame frame , RubyModule module , Object [] args );
1646
-
1647
- @ Specialization
1648
- protected RubyModule doPrivate (VirtualFrame frame , RubyModule module , Object [] names ) {
1649
- return setVisibilityNode .execute (frame , module , names , Visibility .PRIVATE );
1642
+ @ GenerateUncached
1643
+ @ CoreMethod (names = "private" , rest = true , visibility = Visibility .PRIVATE , alwaysInlined = true )
1644
+ public abstract static class PrivateNode extends AlwaysInlinedMethodNode {
1645
+ @ Specialization (guards = "names.length == 0" )
1646
+ protected RubyModule frame (
1647
+ Frame callerFrame , RubyModule module , Object [] names , Object block , RootCallTarget target ) {
1648
+ DeclarationContext .setCurrentVisibility (callerFrame , Visibility .PRIVATE );
1649
+ return module ;
1650
1650
}
1651
1651
1652
+ @ Specialization (guards = "names.length > 0" )
1653
+ protected RubyModule methods (
1654
+ Frame callerFrame , RubyModule module , Object [] names , Object block , RootCallTarget target ,
1655
+ @ Cached SetMethodVisibilityNode setMethodVisibilityNode ) {
1656
+ for (Object name : names ) {
1657
+ setMethodVisibilityNode .execute (module , name , Visibility .PRIVATE );
1658
+ }
1659
+ return module ;
1660
+ }
1652
1661
}
1653
1662
1654
1663
@ CoreMethod (names = "prepend_features" , required = 1 , visibility = Visibility .PRIVATE , split = Split .NEVER )
@@ -1936,16 +1945,25 @@ protected RubyModule publicConstant(RubyModule module, Object[] args) {
1936
1945
}
1937
1946
}
1938
1947
1939
- @ CoreMethod ( names = "protected" , rest = true , visibility = Visibility . PRIVATE )
1940
- public abstract static class ProtectedNode extends CoreMethodArrayArgumentsNode {
1941
-
1942
- @ Child private SetVisibilityNode setVisibilityNode = SetVisibilityNode . create ();
1943
-
1944
- @ Specialization
1945
- protected RubyModule doProtected ( VirtualFrame frame , RubyModule module , Object [] names ) {
1946
- return setVisibilityNode . execute ( frame , module , names , Visibility . PROTECTED ) ;
1948
+ @ GenerateUncached
1949
+ @ CoreMethod ( names = "protected" , rest = true , visibility = Visibility . PRIVATE , alwaysInlined = true )
1950
+ public abstract static class ProtectedNode extends AlwaysInlinedMethodNode {
1951
+ @ Specialization ( guards = "names.length == 0" )
1952
+ protected RubyModule frame (
1953
+ Frame callerFrame , RubyModule module , Object [] names , Object block , RootCallTarget target ) {
1954
+ DeclarationContext . setCurrentVisibility ( callerFrame , Visibility . PROTECTED );
1955
+ return module ;
1947
1956
}
1948
1957
1958
+ @ Specialization (guards = "names.length > 0" )
1959
+ protected RubyModule methods (
1960
+ Frame callerFrame , RubyModule module , Object [] names , Object block , RootCallTarget target ,
1961
+ @ Cached SetMethodVisibilityNode setMethodVisibilityNode ) {
1962
+ for (Object name : names ) {
1963
+ setMethodVisibilityNode .execute (module , name , Visibility .PROTECTED );
1964
+ }
1965
+ return module ;
1966
+ }
1949
1967
}
1950
1968
1951
1969
@ CoreMethod (names = "remove_class_variable" , required = 1 )
@@ -2124,7 +2142,8 @@ public abstract RubyModule execute(VirtualFrame frame, RubyModule module, Object
2124
2142
protected RubyModule setVisibility (
2125
2143
VirtualFrame frame , RubyModule module , Object [] names , Visibility visibility ) {
2126
2144
if (names .length == 0 ) {
2127
- DeclarationContext .setCurrentVisibility (getContext (), visibility );
2145
+ final Frame callerFrame = getContext ().getCallStack ().getCallerFrame (FrameAccess .READ_WRITE );
2146
+ DeclarationContext .setCurrentVisibility (callerFrame , visibility );
2128
2147
} else {
2129
2148
for (Object name : names ) {
2130
2149
setMethodVisibilityNode .execute (module , name , visibility );
@@ -2136,29 +2155,29 @@ protected RubyModule setVisibility(
2136
2155
2137
2156
}
2138
2157
2139
- public abstract static class SetMethodVisibilityNode extends RubyContextNode {
2158
+ @ GenerateUncached
2159
+ public abstract static class SetMethodVisibilityNode extends RubyBaseNode {
2140
2160
2141
2161
public static SetMethodVisibilityNode create () {
2142
2162
return SetMethodVisibilityNodeGen .create ();
2143
2163
}
2144
2164
2145
- @ Child private NameToJavaStringNode nameToJavaStringNode = NameToJavaStringNode .create ();
2146
- @ Child private AddMethodNode addMethodNode = AddMethodNode .create (true );
2147
-
2148
2165
public abstract void execute (RubyModule module , Object name , Visibility visibility );
2149
2166
2150
2167
@ Specialization
2151
2168
protected void setMethodVisibility (RubyModule module , Object name , Visibility visibility ,
2152
- @ Cached BranchProfile errorProfile ) {
2169
+ @ CachedContext (RubyLanguage .class ) RubyContext context ,
2170
+ @ Cached BranchProfile errorProfile ,
2171
+ @ Cached NameToJavaStringNode nameToJavaStringNode ) {
2153
2172
final String methodName = nameToJavaStringNode .execute (name );
2154
2173
2155
- final InternalMethod method = module .fields .deepMethodSearch (getContext () , methodName );
2174
+ final InternalMethod method = module .fields .deepMethodSearch (context , methodName );
2156
2175
2157
2176
if (method == null ) {
2158
2177
errorProfile .enter ();
2159
2178
throw new RaiseException (
2160
- getContext () ,
2161
- coreExceptions ().nameErrorUndefinedMethod (methodName , module , this ));
2179
+ context ,
2180
+ context . getCoreExceptions ().nameErrorUndefinedMethod (methodName , module , this ));
2162
2181
}
2163
2182
2164
2183
// Do nothing if the method already exists with the same visibility, like MRI
@@ -2168,7 +2187,7 @@ protected void setMethodVisibility(RubyModule module, Object name, Visibility vi
2168
2187
2169
2188
/* If the method was already defined in this class, that's fine {@link addMethod} will overwrite it,
2170
2189
* otherwise we do actually want to add a copy of the method with a different visibility to this module. */
2171
- addMethodNode . executeAddMethod ( module , method , visibility );
2190
+ module . addMethodIgnoreNameVisibility ( context , method , visibility , this );
2172
2191
}
2173
2192
2174
2193
}
0 commit comments