Skip to content

Commit

Permalink
[Truffle] New primitive array node for writing to an array by simple …
Browse files Browse the repository at this point in the history
…index.
  • Loading branch information
chrisseaton committed Feb 9, 2015
1 parent 4a5a146 commit a294f73
Show file tree
Hide file tree
Showing 7 changed files with 583 additions and 299 deletions.
Expand Up @@ -89,7 +89,7 @@ public RubyNilClass readOutOfBounds(RubyArray array, int index) {

// Guards

public static boolean isInBounds(RubyArray array, int index) {
protected static boolean isInBounds(RubyArray array, int index) {
return index >= 0 && index < array.getSize();
}

Expand Down
Expand Up @@ -144,15 +144,15 @@ public RubyArray readObjectOutOfBounds(RubyArray array, int index, int length) {

// Guards

public static boolean indexInBounds(RubyArray array, int index, int length) {
protected static boolean indexInBounds(RubyArray array, int index, int length) {
return index >= 0 && index <= array.getSize();
}

public static boolean lengthPositive(RubyArray array, int index, int length) {
protected static boolean lengthPositive(RubyArray array, int index, int length) {
return length >= 0;
}

public static boolean endInBounds(RubyArray array, int index, int length) {
protected static boolean endInBounds(RubyArray array, int index, int length) {
return index + length < array.getSize();
}

Expand Down
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2015 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.nodes.array;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.dsl.NodeChild;
import com.oracle.truffle.api.dsl.NodeChildren;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.source.SourceSection;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.RubyArray;

@NodeChildren({
@NodeChild(value="array", type=RubyNode.class),
@NodeChild(value="index", type=RubyNode.class),
@NodeChild(value="value", type=RubyNode.class)
})
public abstract class ArrayWriteDenormalizedNode extends RubyNode {

@Child private ArrayWriteNormalizedNode readNode;

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

public ArrayWriteDenormalizedNode(ArrayWriteDenormalizedNode prev) {
super(prev);
readNode = prev.readNode;
}

public abstract Object executeWrite(VirtualFrame frame, RubyArray array, int index, Object value);

@Specialization
public Object read(VirtualFrame frame, RubyArray array, int index, Object value) {
if (readNode == null) {
CompilerDirectives.transferToInterpreter();
readNode = insert(ArrayWriteNormalizedNodeFactory.create(getContext(), getSourceSection(), null, null, null));
}

final int normalizedIndex = array.normalizeIndex(index);

return readNode.executeWrite(frame, array, normalizedIndex, value);

This comment has been minimized.

Copy link
@eregon

eregon Feb 9, 2015

Member

This reads in a interesting way 😉

}

}

0 comments on commit a294f73

Please sign in to comment.