Skip to content

Commit

Permalink
Showing 6 changed files with 37 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -17,11 +17,11 @@
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.api.profiles.ConditionProfile;
import com.oracle.truffle.api.source.SourceSection;
import org.jruby.truffle.Layouts;
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.language.RubyNode;

import static org.jruby.truffle.core.array.ArrayHelpers.getSize;
import static org.jruby.truffle.core.array.ArrayHelpers.setSize;
import static org.jruby.truffle.core.array.ArrayHelpers.setStoreAndSize;

@NodeChildren({
@@ -81,7 +81,7 @@ public DynamicObject appendManySameType(DynamicObject array, DynamicObject other
setStoreAndSize(array, newStoreMirror.getArray(), newSize);
} else {
otherStoreMirror.copyTo(storeMirror, 0, oldSize, otherSize);
Layouts.ARRAY.setSize(array, newSize);
setSize(array, newSize);
}
return array;
}
Original file line number Diff line number Diff line change
@@ -21,6 +21,8 @@
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.language.RubyNode;

import static org.jruby.truffle.core.array.ArrayHelpers.setSize;

@NodeChildren({
@NodeChild("array"),
@NodeChild("value"),
@@ -42,7 +44,7 @@ public DynamicObject appendOneEmpty(DynamicObject array, Object value,
final ArrayMirror storeMirror = strategy.newArray(1);
storeMirror.set(0, value);
Layouts.ARRAY.setStore(array, storeMirror.getArray());
Layouts.ARRAY.setSize(array, 1);
setSize(array, 1);
return array;
}

@@ -60,10 +62,10 @@ public DynamicObject appendOneSameType(DynamicObject array, Object value,
final ArrayMirror newStoreMirror = storeMirror.copyArrayAndMirror(ArrayUtils.capacityForOneMore(getContext(), storeMirror.getLength()));
newStoreMirror.set(oldSize, value);
Layouts.ARRAY.setStore(array, newStoreMirror.getArray());
Layouts.ARRAY.setSize(array, newSize);
setSize(array, newSize);
} else {
storeMirror.set(oldSize, value);
Layouts.ARRAY.setSize(array, newSize);
setSize(array, newSize);
}
return array;
}
@@ -85,7 +87,7 @@ public DynamicObject appendOneGeneralize(DynamicObject array, Object value,
currentMirror.copyTo(storeMirror, 0, 0, oldSize);
storeMirror.set(oldSize, value);
Layouts.ARRAY.setStore(array, storeMirror.getArray());
Layouts.ARRAY.setSize(array, newSize);
setSize(array, newSize);
return array;
}

Original file line number Diff line number Diff line change
@@ -13,6 +13,8 @@
import org.jruby.truffle.Layouts;
import org.jruby.truffle.RubyContext;

import java.lang.reflect.Array;

public abstract class ArrayHelpers {

public static Object getStore(DynamicObject array) {
@@ -26,6 +28,21 @@ public static int getSize(DynamicObject array) {
public static void setStoreAndSize(DynamicObject array, Object store, int size) {
assert !(store instanceof ArrayMirror);
Layouts.ARRAY.setStore(array, store);
setSize(array, size);
}

/**
* Sets the size of the given array
*
* Asserts that the size is valid for the current store of the array.
* If setting both size and store, use setStoreAndSize or be sure to setStore before
* setSize as this assertion may fail.
* @param array
* @param size
*/
public static void setSize(DynamicObject array, int size) {
assert getStore(array) != null || size == 0;
assert getStore(array) == null || Array.getLength(getStore(array)) >= size;
Layouts.ARRAY.setSize(array, size);
}

Original file line number Diff line number Diff line change
@@ -96,6 +96,7 @@
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;
import static org.jruby.truffle.core.array.ArrayHelpers.setSize;
import static org.jruby.truffle.core.array.ArrayHelpers.setStoreAndSize;

@CoreClass("Array")
@@ -479,9 +480,9 @@ public Object setOtherArray(VirtualFrame frame, DynamicObject array, int rawStar

// Set size
if (needsTail) {
Layouts.ARRAY.setSize(array, endOfReplacementInArray + tailSize);
setSize(array, endOfReplacementInArray + tailSize);
} else {
Layouts.ARRAY.setSize(array, endOfReplacementInArray);
setSize(array, endOfReplacementInArray);
}

return replacement;
@@ -1752,7 +1753,7 @@ public Object popNotEmpty(DynamicObject array, int n,
// Null out the popped values from the store
final ArrayMirror filler = strategy.newArray(numPop);
filler.copyTo(store, 0, size - numPop, numPop);
Layouts.ARRAY.setSize(array, size - numPop);
setSize(array, size - numPop);

return createArray(getContext(), popped.getArray(), numPop);
}
@@ -1914,7 +1915,7 @@ public Object rejectInPlaceOther(VirtualFrame frame, DynamicObject array, Dynami
// Null out the elements behind the size
final ArrayMirror filler = strategy.newArray(n - i);
filler.copyTo(store, 0, i, n - i);
Layouts.ARRAY.setSize(array, i);
setSize(array, i);

if (CompilerDirectives.inInterpreter()) {
LoopNode.reportLoopCount(this, n);
@@ -2030,7 +2031,7 @@ public Object shiftOther(DynamicObject array, NotProvided n,
// Null out the element behind the size
final ArrayMirror filler = strategy.newArray(1);
filler.copyTo(store, 0, size - 1, 1);
Layouts.ARRAY.setSize(array, size - 1);
setSize(array, size - 1);

return value;
}
@@ -2069,7 +2070,7 @@ public Object shiftMany(DynamicObject array, int n,
// Null out the element behind the size
final ArrayMirror filler = strategy.newArray(numShift);
filler.copyTo(store, 0, size - numShift, numShift);
Layouts.ARRAY.setSize(array, size - numShift);
setSize(array, size - numShift);

return createArray(getContext(), result.getArray(), numShift);
}
Original file line number Diff line number Diff line change
@@ -20,6 +20,8 @@
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.language.RubyNode;

import static org.jruby.truffle.core.array.ArrayHelpers.setSize;

@NodeChildren({
@NodeChild("array")
})
@@ -46,7 +48,7 @@ public Object popOne(DynamicObject array,
@Cached("of(array)") ArrayStrategy strategy) {
final int size = Layouts.ARRAY.getSize(array);
final Object value = strategy.newMirror(array).get(size - 1);
Layouts.ARRAY.setSize(array, size - 1);
setSize(array, size - 1);
return value;
}

Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@
import org.jruby.truffle.language.RubyNode;

import static org.jruby.truffle.core.array.ArrayHelpers.getSize;
import static org.jruby.truffle.core.array.ArrayHelpers.setSize;
import static org.jruby.truffle.core.array.ArrayHelpers.setStoreAndSize;

@NodeChildren({
@@ -132,7 +133,7 @@ public Object writeBeyondObject(DynamicObject array, int index, Object value,
objectStore[n] = nil();
}
objectStore[index] = value;
Layouts.ARRAY.setSize(array, index + 1);
setSize(array, index + 1);
return value;
}

0 comments on commit 49bff26

Please sign in to comment.