Skip to content

Commit 8439596

Browse files
committed
Add a couple more assertions,fixes, and tests for extracted ranges.
1 parent cd756ba commit 8439596

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

spec/truffle/thread_safe_objects_spec.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,40 @@ def wb; @wb; end
191191
ary.each { |e| shared?(e).should == true }
192192
end
193193

194+
it "Array#shift(n) with copy-on-write DelegatedArrayStorage" do
195+
ary = [Object.new, Object.new, Object.new, Object.new, Object.new]
196+
@share = ary
197+
new_ary = ary.shift(3)
198+
shared?(ary).should == true
199+
ary.each { |e| shared?(e).should == true }
200+
201+
shared?(new_ary).should == false
202+
new_ary.each { |e| shared?(e).should == true }
203+
end
204+
205+
it "Array#pop with copy-on-write DelegatedArrayStorage" do
206+
ary = [Object.new, Object.new, Object.new]
207+
ary.pop
208+
@share = ary
209+
shared?(ary).should == true
210+
ary.each { |e| shared?(e).should == true }
211+
212+
wb = Object.new
213+
ary[0] = wb
214+
ary.each { |e| shared?(e).should == true }
215+
end
216+
217+
it "Array#pop(n) with copy-on-write DelegatedArrayStorage" do
218+
ary = [Object.new, Object.new, Object.new, Object.new, Object.new]
219+
@share = ary
220+
new_ary = ary.pop(3)
221+
shared?(ary).should == true
222+
ary.each { |e| shared?(e).should == true }
223+
224+
shared?(new_ary).should == false
225+
new_ary.each { |e| shared?(e).should == true }
226+
end
227+
194228
it "Hash" do
195229
initial = Object.new
196230
hsh = { initial => Object.new }

src/main/java/org/truffleruby/core/array/ArrayHelpers.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ private static boolean assertValidElements(Object store, int size) {
3737

3838
public static RubyArray createArray(RubyContext context, RubyLanguage language, Object store, int size) {
3939
assert !(store instanceof Object[]) || store.getClass() == Object[].class;
40+
assert !(store instanceof SharedArrayStorage);
4041
assert assertValidElements(store, size);
4142
return new RubyArray(context.getCoreLibrary().arrayClass, language.arrayShape, store, size);
4243
}

src/main/java/org/truffleruby/core/array/ArrayNodes.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,7 +1703,7 @@ protected RubyArray popNotEmptySharedStorage(RubyArray array, int n,
17031703
final int size = arraySizeProfile.profile(array.size);
17041704
final int numPop = minProfile.profile(size < n) ? size : n;
17051705

1706-
final Object popped = stores.extractRange(store, size - numPop, size); // copy on write
1706+
final Object popped = stores.extractRangeAndUnshare(store, size - numPop, size); // copy on write
17071707
final Object prefix = stores.extractRange(store, 0, size - numPop); // copy on write
17081708
// NOTE(norswap): if one of these two arrays outlives the other, you get a memory leak
17091709

@@ -2168,9 +2168,9 @@ protected Object shiftMany(RubyArray array, int n,
21682168
@Cached ConditionProfile minProfile) {
21692169
final int size = array.size;
21702170
final int numShift = minProfile.profile(size < n) ? size : n;
2171-
final Object result = stores.extractRange(store, 0, numShift);
2172-
array.store = stores.extractRange(store, numShift, size);
2173-
setSize(array, size - numShift);
2171+
final Object result = stores.extractRangeAndUnshare(store, 0, numShift);
2172+
final Object newStore = stores.extractRange(store, numShift, size);
2173+
setStoreAndSize(array, newStore, size - numShift);
21742174
return createArray(result, numShift);
21752175
}
21762176

0 commit comments

Comments
 (0)