Skip to content

Commit

Permalink
Showing 2 changed files with 68 additions and 24 deletions.
30 changes: 6 additions & 24 deletions truffle/src/main/java/org/jruby/truffle/core/array/ArrayNodes.java
Original file line number Diff line number Diff line change
@@ -47,6 +47,7 @@
import org.jruby.truffle.core.cast.ToAryNodeGen;
import org.jruby.truffle.core.cast.ToIntNode;
import org.jruby.truffle.core.cast.ToIntNodeGen;
import org.jruby.truffle.core.cast.ToIntRangeNode;
import org.jruby.truffle.core.format.BytesResult;
import org.jruby.truffle.core.format.FormatExceptionTranslator;
import org.jruby.truffle.core.format.exceptions.FormatException;
@@ -87,10 +88,8 @@
import org.jruby.truffle.language.objects.TaintNodeGen;
import org.jruby.truffle.language.yield.YieldNode;
import org.jruby.util.Memo;

import java.util.Arrays;
import java.util.Comparator;

import static org.jruby.truffle.core.array.ArrayHelpers.createArray;
import static org.jruby.truffle.core.array.ArrayHelpers.getSize;
import static org.jruby.truffle.core.array.ArrayHelpers.getStore;
@@ -508,30 +507,13 @@ public Object setRange(VirtualFrame frame, DynamicObject array, DynamicObject ra
return executeSet(frame, array, start, normalizeLength, value);
}

@Specialization(guards = "isObjectRange(range)")
public Object setObjectRange(VirtualFrame frame, DynamicObject array, DynamicObject range, Object value, NotProvided unused,
@Cached("createBinaryProfile()") ConditionProfile negativeBeginProfile,
@Cached("createBinaryProfile()") ConditionProfile negativeEndProfile,
@Cached("create()") BranchProfile errorProfile) {
final int size = getSize(array);
final int begin = toInt(frame, Layouts.OBJECT_RANGE.getBegin(range));

final int start = ArrayOperations.normalizeIndex(size, begin, negativeBeginProfile);
if (start < 0) {
errorProfile.enter();
throw new RaiseException(coreExceptions().rangeError(range, this));
}
final int end = ArrayOperations.normalizeIndex(size, toInt(frame, Layouts.OBJECT_RANGE.getEnd(range)), negativeEndProfile);
int inclusiveEnd = Layouts.OBJECT_RANGE.getExcludedEnd(range) ? end - 1 : end;
if (inclusiveEnd < 0) {
inclusiveEnd = -1;
}
final int length = inclusiveEnd - start + 1;
final int normalizeLength = length > -1 ? length : 0;
return executeSet(frame, array, start, normalizeLength, value);
@Specialization(guards = { "!isIntegerFixnumRange(range)", "isRubyRange(range)" })
public Object setOtherRange(VirtualFrame frame, DynamicObject array, DynamicObject range, Object value, NotProvided unused,
@Cached("create()") ToIntRangeNode toIntRangeNode) {
DynamicObject intRange = toIntRangeNode.executeToIntRange(frame, range);
return executeSet(frame, array, intRange, value, unused);
}


// Helpers

private void checkIndex(DynamicObject array, int index, int normalizedIndex) {
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2016 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
*/
package org.jruby.truffle.core.cast;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.dsl.NodeChild;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.object.DynamicObject;
import org.jruby.truffle.Layouts;
import org.jruby.truffle.language.RubyNode;

@NodeChild("range")
public abstract class ToIntRangeNode extends RubyNode {

public static ToIntRangeNode create() {
return ToIntRangeNodeGen.create(null);
}

@Child private ToIntNode toIntNode;

public abstract DynamicObject executeToIntRange(VirtualFrame frame, DynamicObject range);

@Specialization(guards = "isIntegerFixnumRange(range)")
public DynamicObject intRange(DynamicObject range) {
return range;
}

@Specialization(guards = "isLongFixnumRange(range)")
public DynamicObject longRange(VirtualFrame frame, DynamicObject range) {
int begin = toInt(frame, Layouts.LONG_FIXNUM_RANGE.getBegin(range));
int end = toInt(frame, Layouts.LONG_FIXNUM_RANGE.getEnd(range));
boolean excludedEnd = Layouts.LONG_FIXNUM_RANGE.getExcludedEnd(range);
return Layouts.INTEGER_FIXNUM_RANGE.createIntegerFixnumRange(
coreLibrary().getIntegerFixnumRangeFactory(), excludedEnd, begin, end);
}

@Specialization(guards = "isObjectRange(range)")
public DynamicObject objectRange(VirtualFrame frame, DynamicObject range) {
int begin = toInt(frame, Layouts.OBJECT_RANGE.getBegin(range));
int end = toInt(frame, Layouts.OBJECT_RANGE.getEnd(range));
boolean excludedEnd = Layouts.OBJECT_RANGE.getExcludedEnd(range);
return Layouts.INTEGER_FIXNUM_RANGE.createIntegerFixnumRange(
coreLibrary().getIntegerFixnumRangeFactory(), excludedEnd, begin, end);
}

private int toInt(VirtualFrame frame, Object indexObject) {
if (toIntNode == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
toIntNode = insert(ToIntNode.create());
}
return toIntNode.doInt(frame, indexObject);
}

}

0 comments on commit 2e8d714

Please sign in to comment.