Skip to content

Commit 0fca157

Browse files
committed
Refactor BooleanCastWithDefaultNode to later support DSL inlining
1 parent fd0b141 commit 0fca157

File tree

8 files changed

+140
-196
lines changed

8 files changed

+140
-196
lines changed

src/main/java/org/truffleruby/RubyLanguage.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@
100100
import org.truffleruby.interop.RubyInnerContext;
101101
import org.truffleruby.interop.RubySourceLocation;
102102
import org.truffleruby.language.LexicalScope;
103-
import org.truffleruby.language.RubyBaseNode;
104103
import org.truffleruby.language.RubyDynamicObject;
105104
import org.truffleruby.language.RubyEvalInteractiveRootNode;
106105
import org.truffleruby.language.RubyInlineParsingRequestNode;
@@ -136,6 +135,8 @@
136135
import com.oracle.truffle.api.utilities.CyclicAssumption;
137136
import org.truffleruby.stdlib.digest.RubyDigest;
138137

138+
import static org.truffleruby.language.RubyBaseNode.createArray;
139+
import static org.truffleruby.language.RubyBaseNode.createString;
139140
import static org.truffleruby.language.RubyBaseNode.nil;
140141

141142
@TruffleLanguage.Registration(
@@ -913,13 +914,13 @@ public static String filenameLine(SourceSection section) {
913914

914915
public Object rubySourceLocation(RubyContext context, SourceSection section,
915916
TruffleString.FromJavaStringNode fromJavaStringNode,
916-
RubyBaseNode node) {
917+
Node node) {
917918
if (!BacktraceFormatter.isAvailable(section)) {
918919
return nil;
919920
} else {
920-
var file = node.createString(fromJavaStringNode, getSourcePath(section.getSource()), Encodings.UTF_8);
921+
var file = createString(node, fromJavaStringNode, getSourcePath(section.getSource()), Encodings.UTF_8);
921922
Object[] objects = new Object[]{ file, RubySource.getStartLineAdjusted(context, section) };
922-
return node.createArray(objects);
923+
return createArray(node, objects);
923924
}
924925
}
925926

src/main/java/org/truffleruby/core/cast/BooleanCastWithDefaultNode.java

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,24 @@
1111

1212
import com.oracle.truffle.api.dsl.Cached;
1313
import com.oracle.truffle.api.dsl.Fallback;
14-
import org.truffleruby.language.RubyBaseNodeWithExecute;
14+
import org.truffleruby.language.RubyBaseNode;
1515
import org.truffleruby.language.NotProvided;
1616

17-
import com.oracle.truffle.api.dsl.NodeChild;
1817
import com.oracle.truffle.api.dsl.Specialization;
1918

2019
/** Casts a value into a boolean and defaults to the given value if not provided. */
21-
@NodeChild(value = "valueNode", type = RubyBaseNodeWithExecute.class)
22-
public abstract class BooleanCastWithDefaultNode extends RubyBaseNodeWithExecute {
20+
public abstract class BooleanCastWithDefaultNode extends RubyBaseNode {
2321

24-
public static BooleanCastWithDefaultNode create(boolean defaultValue, RubyBaseNodeWithExecute node) {
25-
return BooleanCastWithDefaultNodeGen.create(defaultValue, node);
26-
}
27-
28-
private final boolean defaultValue;
29-
30-
public BooleanCastWithDefaultNode(boolean defaultValue) {
31-
this.defaultValue = defaultValue;
32-
}
33-
34-
public abstract RubyBaseNodeWithExecute getValueNode();
22+
public abstract boolean execute(Object value, boolean defaultValue);
3523

3624
@Specialization
37-
protected boolean doDefault(NotProvided value) {
25+
protected boolean doDefault(NotProvided value, boolean defaultValue) {
3826
return defaultValue;
3927
}
4028

4129
@Fallback
42-
protected boolean fallback(Object value,
30+
protected boolean fallback(Object value, boolean defaultValue,
4331
@Cached BooleanCastNode booleanCastNode) {
4432
return booleanCastNode.execute(value);
4533
}
46-
47-
@Override
48-
public RubyBaseNodeWithExecute cloneUninitialized() {
49-
return create(defaultValue, getValueNode().cloneUninitialized());
50-
}
5134
}

src/main/java/org/truffleruby/core/kernel/KernelNodes.java

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@
149149
import com.oracle.truffle.api.RootCallTarget;
150150
import com.oracle.truffle.api.dsl.Cached;
151151
import com.oracle.truffle.api.dsl.Cached.Exclusive;
152-
import com.oracle.truffle.api.dsl.CreateCast;
153152
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
154153
import com.oracle.truffle.api.dsl.GenerateUncached;
155154
import com.oracle.truffle.api.dsl.ImportStatic;
@@ -1251,29 +1250,40 @@ protected RubyMethod method(Frame callerFrame, Object self, Object[] rubyArgs, R
12511250
@CoreMethod(names = "methods", optional = 1)
12521251
@NodeChild(value = "object", type = RubyNode.class)
12531252
@NodeChild(value = "regular", type = RubyBaseNodeWithExecute.class)
1254-
public abstract static class MethodsNode extends CoreMethodNode {
1253+
public abstract static class KernelMethodsNode extends CoreMethodNode {
1254+
1255+
@Specialization
1256+
protected RubyArray doMethods(Object self, Object maybeRegular,
1257+
@Cached BooleanCastWithDefaultNode booleanCastWithDefaultNode,
1258+
@Cached MethodsNode methodsNode) {
1259+
final boolean regular = booleanCastWithDefaultNode.execute(maybeRegular, true);
1260+
return methodsNode.execute(this, self, regular);
12551261

1256-
@CreateCast("regular")
1257-
protected RubyBaseNodeWithExecute coerceToBoolean(RubyBaseNodeWithExecute regular) {
1258-
return BooleanCastWithDefaultNode.create(true, regular);
12591262
}
1263+
}
1264+
1265+
@GenerateInline
1266+
@GenerateCached(false)
1267+
public abstract static class MethodsNode extends RubyBaseNode {
1268+
1269+
public abstract RubyArray execute(Node node, Object self, boolean regular);
12601270

12611271
@TruffleBoundary
12621272
@Specialization(guards = "regular")
1263-
protected RubyArray methodsRegular(Object self, boolean regular,
1273+
protected static RubyArray methodsRegular(Node node, Object self, boolean regular,
12641274
@Cached MetaClassNode metaClassNode) {
1265-
final RubyModule metaClass = metaClassNode.execute(this, self);
1275+
final RubyModule metaClass = metaClassNode.execute(node, self);
12661276

12671277
Object[] objects = metaClass.fields
1268-
.filterMethodsOnObject(getLanguage(), regular, MethodFilter.PUBLIC_PROTECTED)
1278+
.filterMethodsOnObject(getLanguage(node), regular, MethodFilter.PUBLIC_PROTECTED)
12691279
.toArray();
1270-
return createArray(objects);
1280+
return createArray(node, objects);
12711281
}
12721282

12731283
@Specialization(guards = "!regular")
1274-
protected RubyArray methodsSingleton(Object self, boolean regular,
1284+
protected static RubyArray methodsSingleton(Node node, Object self, boolean regular,
12751285
@Cached SingletonMethodsNode singletonMethodsNode) {
1276-
return singletonMethodsNode.execute(this, self, false);
1286+
return singletonMethodsNode.execute(node, self, false);
12771287
}
12781288

12791289
}
@@ -1312,15 +1322,12 @@ private void print(Object inspected) {
13121322
@NodeChild(value = "includeAncestors", type = RubyBaseNodeWithExecute.class)
13131323
public abstract static class PrivateMethodsNode extends CoreMethodNode {
13141324

1315-
@CreateCast("includeAncestors")
1316-
protected RubyBaseNodeWithExecute coerceToBoolean(RubyBaseNodeWithExecute includeAncestors) {
1317-
return BooleanCastWithDefaultNode.create(true, includeAncestors);
1318-
}
1319-
13201325
@TruffleBoundary
13211326
@Specialization
1322-
protected RubyArray privateMethods(Object self, boolean includeAncestors,
1327+
protected RubyArray privateMethods(Object self, Object maybeIncludeAncestors,
1328+
@Cached BooleanCastWithDefaultNode booleanCastWithDefaultNode,
13231329
@Cached MetaClassNode metaClassNode) {
1330+
final boolean includeAncestors = booleanCastWithDefaultNode.execute(maybeIncludeAncestors, true);
13241331
RubyClass metaClass = metaClassNode.execute(this, self);
13251332

13261333
Object[] objects = metaClass.fields
@@ -1347,15 +1354,12 @@ protected RubyProc proc(VirtualFrame frame, Object maybeBlock,
13471354
@NodeChild(value = "includeAncestors", type = RubyBaseNodeWithExecute.class)
13481355
public abstract static class ProtectedMethodsNode extends CoreMethodNode {
13491356

1350-
@CreateCast("includeAncestors")
1351-
protected RubyBaseNodeWithExecute coerceToBoolean(RubyBaseNodeWithExecute includeAncestors) {
1352-
return BooleanCastWithDefaultNode.create(true, includeAncestors);
1353-
}
1354-
13551357
@TruffleBoundary
13561358
@Specialization
1357-
protected RubyArray protectedMethods(Object self, boolean includeAncestors,
1359+
protected RubyArray protectedMethods(Object self, Object maybeIncludeAncestors,
1360+
@Cached BooleanCastWithDefaultNode booleanCastWithDefaultNode,
13581361
@Cached MetaClassNode metaClassNode) {
1362+
final boolean includeAncestors = booleanCastWithDefaultNode.execute(maybeIncludeAncestors, true);
13591363
final RubyClass metaClass = metaClassNode.execute(this, self);
13601364

13611365
Object[] objects = metaClass.fields
@@ -1386,16 +1390,13 @@ protected RubyMethod method(Frame callerFrame, Object self, Object[] rubyArgs, R
13861390
@NodeChild(value = "includeAncestors", type = RubyBaseNodeWithExecute.class)
13871391
public abstract static class PublicMethodsNode extends CoreMethodNode {
13881392

1389-
@CreateCast("includeAncestors")
1390-
protected RubyBaseNodeWithExecute coerceToBoolean(RubyBaseNodeWithExecute includeAncestors) {
1391-
return BooleanCastWithDefaultNode.create(true, includeAncestors);
1392-
}
1393-
13941393
@TruffleBoundary
13951394
@Specialization
1396-
protected RubyArray publicMethods(Object self, boolean includeAncestors,
1395+
protected RubyArray publicMethods(Object self, Object maybeIncludeAncestors,
1396+
@Cached BooleanCastWithDefaultNode booleanCastWithDefaultNode,
13971397
@Cached MetaClassNode metaClassNode) {
13981398
final RubyModule metaClass = metaClassNode.execute(this, self);
1399+
final boolean includeAncestors = booleanCastWithDefaultNode.execute(maybeIncludeAncestors, true);
13991400

14001401
Object[] objects = metaClass.fields
14011402
.filterMethodsOnObject(getLanguage(), includeAncestors, MethodFilter.PUBLIC)
@@ -1557,14 +1558,11 @@ protected RubyMethod singletonMethod(Object self, Object nameObject,
15571558
@NodeChild(value = "includeAncestors", type = RubyBaseNodeWithExecute.class)
15581559
public abstract static class KernelSingletonMethodsNode extends CoreMethodNode {
15591560

1560-
@CreateCast("includeAncestors")
1561-
protected RubyBaseNodeWithExecute coerceToBoolean(RubyBaseNodeWithExecute includeAncestors) {
1562-
return BooleanCastWithDefaultNode.create(true, includeAncestors);
1563-
}
1564-
15651561
@Specialization
1566-
protected RubyArray singletonMethods(Object self, boolean includeAncestors,
1562+
protected RubyArray singletonMethods(Object self, Object maybeIncludeAncestors,
1563+
@Cached BooleanCastWithDefaultNode booleanCastWithDefaultNode,
15671564
@Cached SingletonMethodsNode singletonMethodsNode) {
1565+
final boolean includeAncestors = booleanCastWithDefaultNode.execute(maybeIncludeAncestors, true);
15681566
return singletonMethodsNode.execute(this, self, includeAncestors);
15691567
}
15701568
}

0 commit comments

Comments
 (0)