Skip to content

Commit

Permalink
[Truffle] Move Array#- to Ruby.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed Feb 6, 2015
1 parent 9f1ad0a commit da5e086
Show file tree
Hide file tree
Showing 7 changed files with 389 additions and 146 deletions.
6 changes: 1 addition & 5 deletions spec/truffle/tags/core/array/minus_tags.txt
@@ -1,5 +1 @@
fails:Array#- tries to convert the passed arguments to Arrays using #to_ary
fails:Array#- does not return subclass instance for Array subclasses
fails:Array#- does not call to_ary on array subclasses
fails:Array#- removes an item identified as equivalent via #hash and #eql?
fails:Array#- doesn't remove an item with the same hash but not #eql?
fails:Array#- properly handles recursive arrays
139 changes: 0 additions & 139 deletions truffle/src/main/java/org/jruby/truffle/nodes/core/ArrayNodes.java
Expand Up @@ -187,145 +187,6 @@ public RubyArray addEmptyObject(RubyArray a, RubyArray b) {

}

@CoreMethod(names = "-", required = 1)
public abstract static class SubNode extends ArrayCoreMethodNode {

public SubNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

public SubNode(SubNode prev) {
super(prev);
}

@Specialization(guards = "areBothIntegerFixnum")
public RubyArray subIntegerFixnum(RubyArray a, RubyArray b) {
notDesignedForCompilation();

final int[] as = (int[]) a.getStore();
final int[] bs = (int[]) b.getStore();

final int[] sub = new int[a.getSize()];

int i = 0;

for (int n = 0; n < a.getSize(); n++) {
if (!ArrayUtils.contains(bs, as[n])) {
sub[i] = as[n];
i++;
}
}

return new RubyArray(getContext().getCoreLibrary().getArrayClass(), sub, i);
}

@Specialization(guards = "areBothLongFixnum")
public RubyArray subLongFixnum(RubyArray a, RubyArray b) {
notDesignedForCompilation();

final long[] as = (long[]) a.getStore();
final long[] bs = (long[]) b.getStore();

final long[] sub = new long[a.getSize()];

int i = 0;

for (int n = 0; n < a.getSize(); n++) {
if (!ArrayUtils.contains(bs, as[n])) {
sub[i] = as[n];
i++;
}
}

return new RubyArray(getContext().getCoreLibrary().getArrayClass(), sub, i);
}

@Specialization(guards = "areBothFloat")
public RubyArray subDouble(RubyArray a, RubyArray b) {
notDesignedForCompilation();

final double[] as = (double[]) a.getStore();
final double[] bs = (double[]) b.getStore();

final double[] sub = new double[a.getSize()];

int i = 0;

for (int n = 0; n < a.getSize(); n++) {
if (!ArrayUtils.contains(bs, as[n])) {
sub[i] = as[n];
i++;
}
}

return new RubyArray(getContext().getCoreLibrary().getArrayClass(), sub, i);
}

@Specialization(guards = "areBothObject")
public RubyArray subObject(RubyArray a, RubyArray b) {
notDesignedForCompilation();

final Object[] as = (Object[]) a.getStore();
final Object[] bs = (Object[]) b.getStore();

final Object[] sub = new Object[a.getSize()];

int i = 0;

for (int n = 0; n < a.getSize(); n++) {
if (!ArrayUtils.contains(bs, b.getSize(), as[n])) {
sub[i] = as[n];
i++;
}
}

return new RubyArray(getContext().getCoreLibrary().getArrayClass(), sub, i);
}

@Specialization(guards = {"isObject", "isOtherIntegerFixnum"})
public RubyArray subObjectIntegerFixnum(RubyArray a, RubyArray b) {
notDesignedForCompilation();

final Object[] as = (Object[]) a.getStore();
final Object[] bs = ArrayUtils.box((int[]) b.getStore());

final Object[] sub = new Object[a.getSize()];

int i = 0;

for (int n = 0; n < a.getSize(); n++) {
if (!ArrayUtils.contains(bs, b.getSize(), as[n])) {
sub[i] = as[n];
i++;
}
}

return new RubyArray(getContext().getCoreLibrary().getArrayClass(), sub, i);
}

@Specialization
public RubyArray sub(RubyArray a, RubyArray b) {
notDesignedForCompilation();

final Object[] as = a.slowToArray();
final Object[] bs = b.slowToArray();

final Object[] sub = new Object[a.getSize()];

int i = 0;

for (int n = 0; n < a.getSize(); n++) {
if (!ArrayUtils.contains(bs, b.getSize(), as[n])) {
sub[i] = as[n];
i++;
}
}

return new RubyArray(getContext().getCoreLibrary().getArrayClass(), sub, i);
}

}

@CoreMethod(names = "*", required = 1, lowerFixnumParameters = 0)
public abstract static class MulNode extends ArrayCoreMethodNode {

Expand Down
Expand Up @@ -109,8 +109,28 @@ public VMObjectEqualPrimitiveNode(VMObjectEqualPrimitiveNode prev) {
}

@Specialization
public Object vmObjectEqual(Object a, Object b) {
throw new UnsupportedOperationException("vm_object_equal");
public Object vmObjectEqual(boolean a, boolean b) {
return a == b;
}

@Specialization
public Object vmObjectEqual(int a, int b) {
return a == b;
}

@Specialization
public Object vmObjectEqual(long a, long b) {
return a == b;
}

@Specialization
public Object vmObjectEqual(double a, double b) {
return a == b;
}

@Specialization
public Object vmObjectEqual(RubyBasicObject a, RubyBasicObject b) {
return a == b;
}

}
Expand Down
2 changes: 2 additions & 0 deletions truffle/src/main/ruby/jruby/truffle/core.rb
Expand Up @@ -16,6 +16,7 @@
require_relative 'core/rubinius/api/shims/rubinius'
require_relative 'core/rubinius/api/shims/lookuptable'
require_relative 'core/rubinius/api/shims/thread'
require_relative 'core/rubinius/api/shims/tuple'
require_relative 'core/rubinius/api/shims/undefined'
require_relative 'core/rubinius/api/shims/metrics'

Expand Down Expand Up @@ -43,6 +44,7 @@
require_relative 'core/rubinius/kernel/common/kernel'
require_relative 'core/rubinius/kernel/common/comparable'
require_relative 'core/rubinius/kernel/common/numeric'
require_relative 'core/rubinius/kernel/common/identity_map'
require_relative 'core/rubinius/kernel/common/integer'
require_relative 'core/rubinius/kernel/common/fixnum'
require_relative 'core/rubinius/kernel/common/false'
Expand Down
@@ -0,0 +1,22 @@
# Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved. This
# code is released under a tri EPL/GPL/LGPL license. You can use it,
# redistribute it and/or modify it under the terms of the:
#
# Eclipse Public License version 1.0
# GNU General Public License version 2
# GNU Lesser General Public License version 2.1

module Rubinius

class Tuple < Array

def copy_from(other, start, length, dest)
# TODO CS 6-Feb-15 use higher level indexing when it works
length.times do |n|
self[dest + n] = other[start + n]
end
end

end

end
Expand Up @@ -240,4 +240,15 @@ def <=>(other)
@total <=> total
end

def -(other)
other = Rubinius::Type.coerce_to other, Array, :to_ary

array = []
im = Rubinius::IdentityMap.from other

each { |x| array << x unless im.include? x }

array
end

end

0 comments on commit da5e086

Please sign in to comment.