Skip to content

Commit a9a41cc

Browse files
committed
Lookup class literal values at runtime
1 parent 1e6d988 commit a9a41cc

File tree

5 files changed

+99
-8
lines changed

5 files changed

+99
-8
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved. This
3+
* code is released under a tri EPL/GPL/LGPL license. You can use it,
4+
* redistribute it and/or modify it under the terms of the:
5+
*
6+
* Eclipse Public License version 2.0, or
7+
* GNU General Public License version 2, or
8+
* GNU Lesser General Public License version 2.1.
9+
*/
10+
package org.truffleruby.language.literal;
11+
12+
import com.oracle.truffle.api.frame.VirtualFrame;
13+
import org.truffleruby.language.RubyContextSourceNode;
14+
15+
public class ObjectClassLiteralNode extends RubyContextSourceNode {
16+
17+
@Override
18+
public Object execute(VirtualFrame frame) {
19+
return getContext().getCoreLibrary().objectClass;
20+
}
21+
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved. This
3+
* code is released under a tri EPL/GPL/LGPL license. You can use it,
4+
* redistribute it and/or modify it under the terms of the:
5+
*
6+
* Eclipse Public License version 2.0, or
7+
* GNU General Public License version 2, or
8+
* GNU Lesser General Public License version 2.1.
9+
*/
10+
package org.truffleruby.language.literal;
11+
12+
import com.oracle.truffle.api.frame.VirtualFrame;
13+
import org.truffleruby.language.RubyContextSourceNode;
14+
15+
public class RangeClassLiteralNode extends RubyContextSourceNode {
16+
17+
@Override
18+
public Object execute(VirtualFrame frame) {
19+
return getContext().getCoreLibrary().rangeClass;
20+
}
21+
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved. This
3+
* code is released under a tri EPL/GPL/LGPL license. You can use it,
4+
* redistribute it and/or modify it under the terms of the:
5+
*
6+
* Eclipse Public License version 2.0, or
7+
* GNU General Public License version 2, or
8+
* GNU Lesser General Public License version 2.1.
9+
*/
10+
package org.truffleruby.language.literal;
11+
12+
import com.oracle.truffle.api.frame.VirtualFrame;
13+
import org.truffleruby.language.RubyContextSourceNode;
14+
15+
public class TruffleInternalModuleLiteralNode extends RubyContextSourceNode {
16+
17+
@Override
18+
public Object execute(VirtualFrame frame) {
19+
return getContext().getCoreLibrary().truffleInternalModule;
20+
}
21+
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved. This
3+
* code is released under a tri EPL/GPL/LGPL license. You can use it,
4+
* redistribute it and/or modify it under the terms of the:
5+
*
6+
* Eclipse Public License version 2.0, or
7+
* GNU General Public License version 2, or
8+
* GNU Lesser General Public License version 2.1.
9+
*/
10+
package org.truffleruby.language.literal;
11+
12+
import com.oracle.truffle.api.frame.VirtualFrame;
13+
import org.truffleruby.language.RubyContextSourceNode;
14+
15+
public class TruffleKernelOperationsModuleLiteralNode extends RubyContextSourceNode {
16+
17+
@Override
18+
public Object execute(VirtualFrame frame) {
19+
return getContext().getCoreLibrary().truffleKernelOperationsModule;
20+
}
21+
22+
}

src/main/java/org/truffleruby/parser/BodyTranslator.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,12 @@
116116
import org.truffleruby.language.literal.IntegerFixnumLiteralNode;
117117
import org.truffleruby.language.literal.LongFixnumLiteralNode;
118118
import org.truffleruby.language.literal.NilLiteralNode;
119+
import org.truffleruby.language.literal.ObjectClassLiteralNode;
119120
import org.truffleruby.language.literal.ObjectLiteralNode;
121+
import org.truffleruby.language.literal.RangeClassLiteralNode;
120122
import org.truffleruby.language.literal.StringLiteralNode;
123+
import org.truffleruby.language.literal.TruffleInternalModuleLiteralNode;
124+
import org.truffleruby.language.literal.TruffleKernelOperationsModuleLiteralNode;
121125
import org.truffleruby.language.locals.DeclarationFlipFlopStateNode;
122126
import org.truffleruby.language.locals.FlipFlopNode;
123127
import org.truffleruby.language.locals.FlipFlopStateNode;
@@ -775,7 +779,7 @@ public RubyNode visitCaseNode(CaseParseNode node) {
775779
method = "===";
776780
arguments = new RubyNode[]{ NodeUtil.cloneNode(readTemp) };
777781
} else {
778-
receiver = new ObjectLiteralNode(context.getCoreLibrary().truffleInternalModule);
782+
receiver = new TruffleInternalModuleLiteralNode();
779783
receiver.unsafeSetSourceSection(sourceSection);
780784
method = "when_splat";
781785
arguments = new RubyNode[]{ rubyExpression, NodeUtil.cloneNode(readTemp) };
@@ -984,7 +988,7 @@ public RubyNode visitColon3Node(Colon3ParseNode node) {
984988
final SourceIndexLength sourceSection = node.getPosition();
985989
final String name = node.getName();
986990

987-
final ObjectLiteralNode root = new ObjectLiteralNode(context.getCoreLibrary().objectClass);
991+
final ObjectClassLiteralNode root = new ObjectClassLiteralNode();
988992
root.unsafeSetSourceSection(sourceSection);
989993

990994
final RubyNode ret = new ReadConstantNode(root, name);
@@ -1001,7 +1005,7 @@ private RubyNode translateCPath(SourceIndexLength sourceSection, Colon3ParseNode
10011005
} else if (node instanceof Colon2ConstParseNode) { // A::B
10021006
ret = ((Colon2ConstParseNode) node).getLeftNode().accept(this);
10031007
} else { // Colon3ParseNode: on top-level (Object)
1004-
ret = new ObjectLiteralNode(context.getCoreLibrary().objectClass);
1008+
ret = new ObjectClassLiteralNode();
10051009
ret.unsafeSetSourceSection(sourceSection);
10061010
}
10071011

@@ -1035,7 +1039,7 @@ public RubyNode visitConstDeclNode(ConstDeclParseNode node) {
10351039
constNode = ((Colon2ParseNode) constNode).getLeftNode(); // Misleading doc, we only want the defined part.
10361040
moduleNode = constNode.accept(this);
10371041
} else if (constNode instanceof Colon3ParseNode) {
1038-
moduleNode = new ObjectLiteralNode(context.getCoreLibrary().objectClass);
1042+
moduleNode = new ObjectClassLiteralNode();
10391043
} else {
10401044
throw CompilerDirectives.shouldNotReachHere();
10411045
}
@@ -1298,7 +1302,7 @@ public RubyNode visitDotNode(DotParseNode node) {
12981302
final SourceIndexLength sourceSection = node.getPosition();
12991303
final RubyNode begin = node.getBeginNode().accept(this);
13001304
final RubyNode end = node.getEndNode().accept(this);
1301-
final RubyNode rangeClass = new ObjectLiteralNode(context.getCoreLibrary().rangeClass);
1305+
final RubyNode rangeClass = new RangeClassLiteralNode();
13021306
final RubyNode isExclusive = new ObjectLiteralNode(node.isExclusive());
13031307

13041308
final RubyNode ret = RangeNodesFactory.NewNodeFactory.create(rangeClass, begin, end, isExclusive);
@@ -2665,8 +2669,7 @@ public RubyNode visitPostExeNode(PostExeParseNode node) {
26652669
position,
26662670
new TruffleFragmentParseNode(
26672671
position,
2668-
new ObjectLiteralNode(
2669-
context.getCoreLibrary().truffleKernelOperationsModule)),
2672+
new TruffleKernelOperationsModuleLiteralNode()),
26702673
"at_exit",
26712674
new ArrayParseNode(position, new TrueParseNode(position)),
26722675
new IterParseNode(position, node.getArgsNode(), scope, node.getBodyNode())),
@@ -2706,7 +2709,7 @@ public RubyNode visitBigRationalNode(BigRationalParseNode node) {
27062709
private RubyNode translateRationalComplex(SourceIndexLength sourceSection, String name, RubyNode a, RubyNode b) {
27072710
// Translate as Rational.convert(a, b) # ignoring visibility
27082711

2709-
final RubyNode moduleNode = new ObjectLiteralNode(context.getCoreLibrary().objectClass);
2712+
final RubyNode moduleNode = new ObjectClassLiteralNode();
27102713
ReadConstantNode receiver = new ReadConstantNode(moduleNode, name);
27112714
RubyNode[] arguments = new RubyNode[]{ a, b };
27122715
RubyCallNodeParameters parameters = new RubyCallNodeParameters(

0 commit comments

Comments
 (0)