Skip to content

Commit 0557d23

Browse files
committed
Array strategy fixes
PullRequest: truffleruby/532
2 parents 02674db + da1b2f5 commit 0557d23

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

spec/truffle/array/new_spec.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved. This
2+
# code is released under a tri EPL/GPL/LGPL license. You can use it,
3+
# redistribute it and/or modify it under the terms of the:
4+
#
5+
# Eclipse Public License version 1.0, or
6+
# GNU General Public License version 2, or
7+
# GNU Lesser General Public License version 2.1.
8+
9+
require_relative '../../ruby/spec_helper'
10+
11+
describe "Array.new" do
12+
def storage(ary)
13+
Truffle::Debug.array_storage(ary)
14+
end
15+
16+
def capacity(ary)
17+
Truffle::Debug.array_capacity(ary)
18+
end
19+
20+
before :each do
21+
@long = 1 << 52
22+
end
23+
24+
it "creates arrays with the requested capacity" do
25+
a = Array.new(17)
26+
capacity(a).should == 17
27+
end
28+
29+
it "creates arrays with the requested capacity with a default value" do
30+
a = Array.new(17, 1)
31+
capacity(a).should == 17
32+
end
33+
34+
it "creates arrays with the requested capacity with a value block" do
35+
a = Array.new(17) { 1 }
36+
capacity(a).should == 17
37+
end
38+
end
39+

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,13 @@ public Object appendNewStrategy(Object store, int index, Object value) {
198198
final ArrayStrategy valueStrategy = ArrayStrategy.forValue(value);
199199
final ArrayStrategy generalized = currentStrategy.generalize(valueStrategy);
200200

201-
final int neededCapacity = ArrayUtils.capacityForOneMore(context, currentStrategy.capacityNode().execute(store));
201+
int currentCapacity = currentStrategy.capacityNode().execute(store);
202+
final int neededCapacity;
203+
if (index >= currentCapacity) {
204+
neededCapacity = ArrayUtils.capacityForOneMore(context, currentCapacity);
205+
} else {
206+
neededCapacity = currentCapacity;
207+
}
202208

203209
replaceNodes(generalized, neededCapacity);
204210

src/main/java/org/truffleruby/debug/TruffleDebugNodes.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,17 @@ public DynamicObject arrayStorage(DynamicObject array) {
295295

296296
}
297297

298+
@CoreMethod(names = "array_capacity", onSingleton = true, required = 1)
299+
public abstract static class ArrayCapacityNode extends CoreMethodArrayArgumentsNode {
300+
301+
@TruffleBoundary
302+
@Specialization(guards = "isRubyArray(array)")
303+
public int arrayStorage(DynamicObject array) {
304+
return ArrayStrategy.of(array).capacityNode().execute(Layouts.ARRAY.getStore(array));
305+
}
306+
307+
}
308+
298309
@CoreMethod(names = "hash_storage", onSingleton = true, required = 1)
299310
public abstract static class HashStorageNode extends CoreMethodArrayArgumentsNode {
300311

0 commit comments

Comments
 (0)