Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 8aac6ace6266
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 6cba9d34f11b
Choose a head ref
  • 2 commits
  • 2 files changed
  • 2 contributors

Commits on Oct 13, 2016

  1. Don't dup every element in Enumerator#drop

    In 06f0441 (JRUBY-6892) Enumerable#drop was changed to call #dup on every element that ended up in the result, but that might not work. Not all objects correctly implement #dup, or are even allocatable.
    
    #take was not changed, and looking at the differences between #take and #drop the only thing that stood out was that the signature of the #each call was different. Changing from Arity.NO_ARGUMENTS to Arity.ONE_REQUIRED make the issue that 06f0441 tried to fix go away.
    iconara committed Oct 13, 2016
    Copy the full SHA
    833660a View commit details

Commits on Oct 18, 2016

  1. Merge pull request #4226 from iconara/fix-4218-1.7

    Don't dup every element in Enumerator#drop (1.7)
    headius authored Oct 18, 2016
    Copy the full SHA
    6cba9d3 View commit details
Showing with 22 additions and 5 deletions.
  1. +2 −5 core/src/main/java/org/jruby/RubyEnumerable.java
  2. +20 −0 spec/regression/GH-4218_enumerable_drop_should_not_change_identities.rb
7 changes: 2 additions & 5 deletions core/src/main/java/org/jruby/RubyEnumerable.java
Original file line number Diff line number Diff line change
@@ -320,15 +320,12 @@ public static IRubyObject drop(ThreadContext context, IRubyObject self, IRubyObj
final RubyArray result = runtime.newArray();

try {
each(context, self, new JavaInternalBlockBody(runtime, Arity.NO_ARGUMENTS) {
each(context, self, new JavaInternalBlockBody(runtime, Arity.ONE_ARGUMENT) {
long i = len; // Atomic ?
public IRubyObject yield(ThreadContext context, IRubyObject arg) {
synchronized (result) {
if (i == 0) {
// While iterating over an RubyEnumerator, "arg"
// gets overwritten by the new value, leading to JRUBY-6892.
// So call .dup() whenever appropriate.
result.append(arg.isImmediate() ? arg : arg.dup());
result.append(arg);
} else {
--i;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class GH4218Enumerable
include Enumerable

def initialize(elements)
@elements = elements
end

def each(&block)
@elements.each(&block)
end
end

describe '#4218 Enumerable#drop' do
it 'does not change the identity of the elements' do
original = [Object.new, Object.new, Object.new]
enumerable = GH4218Enumerable.new(original)
expect(original[1]).to be_equal(enumerable.drop(1).first)
expect(enumerable.to_a[1]).to be_equal(enumerable.drop(1).first)
end
end