Skip to content

Commit ac9c832

Browse files
committed
[Truffle] Import Complex from Rubinius.
1 parent 8b259b9 commit ac9c832

38 files changed

+411
-132
lines changed

core/src/main/java/org/jruby/truffle/nodes/core/HashNodes.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public boolean equalNull(RubyHash a, RubyHash b) {
5959
}
6060

6161
@Specialization
62-
public boolean equal(VirtualFrame frame, RubyHash a, RubyHash b) {
62+
public boolean equal(RubyHash a, RubyHash b) {
6363
notDesignedForCompilation();
6464

6565
final List<KeyValue> aEntries = HashOperations.verySlowToKeyValues(a);
@@ -95,6 +95,12 @@ public boolean equal(VirtualFrame frame, RubyHash a, RubyHash b) {
9595

9696
return true;
9797
}
98+
99+
@Specialization(guards = "!isRubyHash(arguments[1])")
100+
public boolean equalNonHash(RubyHash a, Object b) {
101+
return false;
102+
}
103+
98104
}
99105

100106
@CoreMethod(names = "[]", onSingleton = true, argumentsAsArray = true)

core/src/main/java/org/jruby/truffle/translator/BodyTranslator.java

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -839,8 +839,9 @@ private RubyNode translateCPath(SourceSection sourceSection, org.jruby.ast.Colon
839839
public RubyNode visitComplexNode(ComplexNode node) {
840840
final SourceSection sourceSection = translate(node.getPosition());
841841

842-
// TODO: implement Complex
843-
return node.getNumber().accept(this);
842+
return translateRationalComplex(sourceSection, "Complex",
843+
new FixnumLiteralNode.IntegerFixnumLiteralNode(context, sourceSection, 0),
844+
node.getNumber().accept(this));
844845
}
845846

846847
@Override
@@ -1007,7 +1008,7 @@ protected RubyNode translateMethodDefinition(SourceSection sourceSection, RubyNo
10071008
* In the top-level, methods are defined in the class of the main object. This is
10081009
* counter-intuitive - I would have expected them to be defined in the singleton class.
10091010
* Apparently this is a design decision to make top-level methods sort of global.
1010-
*
1011+
*
10111012
* http://stackoverflow.com/questions/1761148/where-are-methods-defined-at-the-ruby-top-level
10121013
*/
10131014

@@ -1105,39 +1106,39 @@ public RubyNode visitFloatNode(org.jruby.ast.FloatNode node) {
11051106
public RubyNode visitForNode(org.jruby.ast.ForNode node) {
11061107
/**
11071108
* A Ruby for-loop, such as:
1108-
*
1109+
*
11091110
* <pre>
11101111
* for x in y
11111112
* z = x
11121113
* puts z
11131114
* end
11141115
* </pre>
1115-
*
1116+
*
11161117
* naively desugars to:
1117-
*
1118+
*
11181119
* <pre>
11191120
* y.each do |x|
11201121
* z = x
11211122
* puts z
11221123
* end
11231124
* </pre>
1124-
*
1125+
*
11251126
* The main difference is that z is always going to be local to the scope outside the block,
11261127
* so it's a bit more like:
1127-
*
1128+
*
11281129
* <pre>
11291130
* z = nil unless z is already defined
11301131
* y.each do |x|
11311132
* z = x
11321133
* puts x
11331134
* end
11341135
* </pre>
1135-
*
1136+
*
11361137
* Which forces z to be defined in the correct scope. The parser already correctly calls z a
11371138
* local, but then that causes us a problem as if we're going to translate to a block we
11381139
* need a formal parameter - not a local variable. My solution to this is to add a
11391140
* temporary:
1140-
*
1141+
*
11411142
* <pre>
11421143
* z = nil unless z is already defined
11431144
* y.each do |temp|
@@ -1146,25 +1147,25 @@ public RubyNode visitForNode(org.jruby.ast.ForNode node) {
11461147
* puts x
11471148
* end
11481149
* </pre>
1149-
*
1150+
*
11501151
* We also need that temp because the expression assigned in the for could be index
11511152
* assignment, multiple assignment, or whatever:
1152-
*
1153+
*
11531154
* <pre>
11541155
* for x[0] in y
11551156
* z = x[0]
11561157
* puts z
11571158
* end
11581159
* </pre>
1159-
*
1160+
*
11601161
* http://blog.grayproductions.net/articles/the_evils_of_the_for_loop
11611162
* http://stackoverflow.com/questions/3294509/for-vs-each-in-ruby
1162-
*
1163+
*
11631164
* The other complication is that normal locals should be defined in the enclosing scope,
11641165
* unlike a normal block. We do that by setting a flag on this translator object when we
11651166
* visit the new iter, translatingForStatement, which we recognise when visiting an iter
11661167
* node.
1167-
*
1168+
*
11681169
* Finally, note that JRuby's terminology is strange here. Normally 'iter' is a different
11691170
* term for a block. Here, JRuby calls the object being iterated over the 'iter'.
11701171
*/
@@ -1229,7 +1230,7 @@ private static org.jruby.ast.Node setRHS(org.jruby.ast.Node node, org.jruby.ast.
12291230

12301231
private final Set<String> readOnlyGlobalVariables = new HashSet<String>();
12311232
private final Map<String, String> globalVariableAliases = new HashMap<String, String>();
1232-
1233+
12331234
private void initReadOnlyGlobalVariables() {
12341235
Set<String> s = readOnlyGlobalVariables;
12351236
s.add("$:");
@@ -1244,7 +1245,7 @@ private void initReadOnlyGlobalVariables() {
12441245
s.add("$-l");
12451246
s.add("$-p");
12461247
}
1247-
1248+
12481249
private void initGlobalVariableAliases() {
12491250
Map<String, String> m = globalVariableAliases;
12501251
m.put("$-I", "$LOAD_PATH");
@@ -2032,7 +2033,7 @@ public RubyNode visitOpAsgnNode(org.jruby.ast.OpAsgnNode node) {
20322033
/*
20332034
* We're going to de-sugar a.foo += c into a.foo = a.foo + c. Note that we can't evaluate a
20342035
* more than once, so we put it into a temporary, and we're doing something more like:
2035-
*
2036+
*
20362037
* temp = a; temp.foo = temp.foo + c
20372038
*/
20382039

@@ -2168,19 +2169,23 @@ public RubyNode visitPostExeNode(PostExeNode node) {
21682169
public RubyNode visitRationalNode(RationalNode node) {
21692170
final SourceSection sourceSection = translate(node.getPosition());
21702171

2171-
// Translate as Rubinius.privately { Rubinius.convert(a, b) }
2172-
21732172
// TODO(CS): use IntFixnumLiteralNode where possible
21742173

2174+
return translateRationalComplex(sourceSection, "Rational",
2175+
new FixnumLiteralNode.LongFixnumLiteralNode(context, sourceSection, node.getNumerator()),
2176+
new FixnumLiteralNode.LongFixnumLiteralNode(context, sourceSection, node.getDenominator()));
2177+
}
2178+
2179+
private RubyNode translateRationalComplex(SourceSection sourceSection, String name, RubyNode a, RubyNode b) {
2180+
// Translate as Rubinius.privately { Rational.convert(a, b) }
2181+
21752182
final LexicalScope lexicalScope = environment.getLexicalScope();
21762183
final RubyNode moduleNode = new LexicalScopeNode(context, sourceSection, lexicalScope);
21772184
return new RubyCallNode(
21782185
context, sourceSection, "convert",
2179-
new ReadConstantNode(context, sourceSection, "Rational", moduleNode, lexicalScope),
2186+
new ReadConstantNode(context, sourceSection, name, moduleNode, lexicalScope),
21802187
null, false, true, false,
2181-
new RubyNode[]{
2182-
new FixnumLiteralNode.LongFixnumLiteralNode(context, sourceSection, node.getNumerator()),
2183-
new FixnumLiteralNode.LongFixnumLiteralNode(context, sourceSection, node.getDenominator())});
2188+
new RubyNode[]{a, b});
21842189
}
21852190

21862191
@Override

core/src/main/ruby/jruby/truffle/core.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,6 @@
5353
require_relative 'core/rubinius/kernel/common/immediate'
5454
require_relative 'core/rubinius/kernel/common/true'
5555
require_relative 'core/rubinius/kernel/common/false'
56+
require_relative 'core/rubinius/kernel/common/complex'
5657

5758
require_relative 'core/shims'

core/src/main/ruby/jruby/truffle/core/main.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
# GNU General Public License version 2
77
# GNU Lesser General Public License version 2.1
88

9-
TRUE = true
10-
FALSE = false
11-
129
module STDIN
1310
def self.external_encoding
1411
@external || Encoding.default_external

0 commit comments

Comments
 (0)