Skip to content

Commit da5e086

Browse files
committed
[Truffle] Move Array#- to Ruby.
1 parent 9f1ad0a commit da5e086

File tree

7 files changed

+389
-146
lines changed

7 files changed

+389
-146
lines changed
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
fails:Array#- tries to convert the passed arguments to Arrays using #to_ary
2-
fails:Array#- does not return subclass instance for Array subclasses
3-
fails:Array#- does not call to_ary on array subclasses
4-
fails:Array#- removes an item identified as equivalent via #hash and #eql?
5-
fails:Array#- doesn't remove an item with the same hash but not #eql?
1+
fails:Array#- properly handles recursive arrays

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

Lines changed: 0 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -187,145 +187,6 @@ public RubyArray addEmptyObject(RubyArray a, RubyArray b) {
187187

188188
}
189189

190-
@CoreMethod(names = "-", required = 1)
191-
public abstract static class SubNode extends ArrayCoreMethodNode {
192-
193-
public SubNode(RubyContext context, SourceSection sourceSection) {
194-
super(context, sourceSection);
195-
}
196-
197-
public SubNode(SubNode prev) {
198-
super(prev);
199-
}
200-
201-
@Specialization(guards = "areBothIntegerFixnum")
202-
public RubyArray subIntegerFixnum(RubyArray a, RubyArray b) {
203-
notDesignedForCompilation();
204-
205-
final int[] as = (int[]) a.getStore();
206-
final int[] bs = (int[]) b.getStore();
207-
208-
final int[] sub = new int[a.getSize()];
209-
210-
int i = 0;
211-
212-
for (int n = 0; n < a.getSize(); n++) {
213-
if (!ArrayUtils.contains(bs, as[n])) {
214-
sub[i] = as[n];
215-
i++;
216-
}
217-
}
218-
219-
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), sub, i);
220-
}
221-
222-
@Specialization(guards = "areBothLongFixnum")
223-
public RubyArray subLongFixnum(RubyArray a, RubyArray b) {
224-
notDesignedForCompilation();
225-
226-
final long[] as = (long[]) a.getStore();
227-
final long[] bs = (long[]) b.getStore();
228-
229-
final long[] sub = new long[a.getSize()];
230-
231-
int i = 0;
232-
233-
for (int n = 0; n < a.getSize(); n++) {
234-
if (!ArrayUtils.contains(bs, as[n])) {
235-
sub[i] = as[n];
236-
i++;
237-
}
238-
}
239-
240-
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), sub, i);
241-
}
242-
243-
@Specialization(guards = "areBothFloat")
244-
public RubyArray subDouble(RubyArray a, RubyArray b) {
245-
notDesignedForCompilation();
246-
247-
final double[] as = (double[]) a.getStore();
248-
final double[] bs = (double[]) b.getStore();
249-
250-
final double[] sub = new double[a.getSize()];
251-
252-
int i = 0;
253-
254-
for (int n = 0; n < a.getSize(); n++) {
255-
if (!ArrayUtils.contains(bs, as[n])) {
256-
sub[i] = as[n];
257-
i++;
258-
}
259-
}
260-
261-
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), sub, i);
262-
}
263-
264-
@Specialization(guards = "areBothObject")
265-
public RubyArray subObject(RubyArray a, RubyArray b) {
266-
notDesignedForCompilation();
267-
268-
final Object[] as = (Object[]) a.getStore();
269-
final Object[] bs = (Object[]) b.getStore();
270-
271-
final Object[] sub = new Object[a.getSize()];
272-
273-
int i = 0;
274-
275-
for (int n = 0; n < a.getSize(); n++) {
276-
if (!ArrayUtils.contains(bs, b.getSize(), as[n])) {
277-
sub[i] = as[n];
278-
i++;
279-
}
280-
}
281-
282-
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), sub, i);
283-
}
284-
285-
@Specialization(guards = {"isObject", "isOtherIntegerFixnum"})
286-
public RubyArray subObjectIntegerFixnum(RubyArray a, RubyArray b) {
287-
notDesignedForCompilation();
288-
289-
final Object[] as = (Object[]) a.getStore();
290-
final Object[] bs = ArrayUtils.box((int[]) b.getStore());
291-
292-
final Object[] sub = new Object[a.getSize()];
293-
294-
int i = 0;
295-
296-
for (int n = 0; n < a.getSize(); n++) {
297-
if (!ArrayUtils.contains(bs, b.getSize(), as[n])) {
298-
sub[i] = as[n];
299-
i++;
300-
}
301-
}
302-
303-
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), sub, i);
304-
}
305-
306-
@Specialization
307-
public RubyArray sub(RubyArray a, RubyArray b) {
308-
notDesignedForCompilation();
309-
310-
final Object[] as = a.slowToArray();
311-
final Object[] bs = b.slowToArray();
312-
313-
final Object[] sub = new Object[a.getSize()];
314-
315-
int i = 0;
316-
317-
for (int n = 0; n < a.getSize(); n++) {
318-
if (!ArrayUtils.contains(bs, b.getSize(), as[n])) {
319-
sub[i] = as[n];
320-
i++;
321-
}
322-
}
323-
324-
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), sub, i);
325-
}
326-
327-
}
328-
329190
@CoreMethod(names = "*", required = 1, lowerFixnumParameters = 0)
330191
public abstract static class MulNode extends ArrayCoreMethodNode {
331192

truffle/src/main/java/org/jruby/truffle/nodes/rubinius/VMPrimitiveNodes.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,28 @@ public VMObjectEqualPrimitiveNode(VMObjectEqualPrimitiveNode prev) {
109109
}
110110

111111
@Specialization
112-
public Object vmObjectEqual(Object a, Object b) {
113-
throw new UnsupportedOperationException("vm_object_equal");
112+
public Object vmObjectEqual(boolean a, boolean b) {
113+
return a == b;
114+
}
115+
116+
@Specialization
117+
public Object vmObjectEqual(int a, int b) {
118+
return a == b;
119+
}
120+
121+
@Specialization
122+
public Object vmObjectEqual(long a, long b) {
123+
return a == b;
124+
}
125+
126+
@Specialization
127+
public Object vmObjectEqual(double a, double b) {
128+
return a == b;
129+
}
130+
131+
@Specialization
132+
public Object vmObjectEqual(RubyBasicObject a, RubyBasicObject b) {
133+
return a == b;
114134
}
115135

116136
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
require_relative 'core/rubinius/api/shims/rubinius'
1717
require_relative 'core/rubinius/api/shims/lookuptable'
1818
require_relative 'core/rubinius/api/shims/thread'
19+
require_relative 'core/rubinius/api/shims/tuple'
1920
require_relative 'core/rubinius/api/shims/undefined'
2021
require_relative 'core/rubinius/api/shims/metrics'
2122

@@ -43,6 +44,7 @@
4344
require_relative 'core/rubinius/kernel/common/kernel'
4445
require_relative 'core/rubinius/kernel/common/comparable'
4546
require_relative 'core/rubinius/kernel/common/numeric'
47+
require_relative 'core/rubinius/kernel/common/identity_map'
4648
require_relative 'core/rubinius/kernel/common/integer'
4749
require_relative 'core/rubinius/kernel/common/fixnum'
4850
require_relative 'core/rubinius/kernel/common/false'
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved. This
2+
# code is released under a tri EPL/GPL/LGPL license. You can use it,
3+
# redistribute it and/or modify it under the terms of the:
4+
#
5+
# Eclipse Public License version 1.0
6+
# GNU General Public License version 2
7+
# GNU Lesser General Public License version 2.1
8+
9+
module Rubinius
10+
11+
class Tuple < Array
12+
13+
def copy_from(other, start, length, dest)
14+
# TODO CS 6-Feb-15 use higher level indexing when it works
15+
length.times do |n|
16+
self[dest + n] = other[start + n]
17+
end
18+
end
19+
20+
end
21+
22+
end

truffle/src/main/ruby/jruby/truffle/core/rubinius/kernel/common/array.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,15 @@ def <=>(other)
240240
@total <=> total
241241
end
242242

243+
def -(other)
244+
other = Rubinius::Type.coerce_to other, Array, :to_ary
245+
246+
array = []
247+
im = Rubinius::IdentityMap.from other
248+
249+
each { |x| array << x unless im.include? x }
250+
251+
array
252+
end
253+
243254
end

0 commit comments

Comments
 (0)