Skip to content

Correct Binding#dup to properly propagate values it captures#7688

Merged
enebo merged 8 commits intojruby:masterfrom
enebo:weird_binding
May 2, 2024
Merged

Correct Binding#dup to properly propagate values it captures#7688
enebo merged 8 commits intojruby:masterfrom
enebo:weird_binding

Conversation

@enebo
Copy link
Member

@enebo enebo commented Feb 23, 2023

Binding#dup will get all variables from the original binding but when new ones are added to the new dup the old binding will not see them. This is a more broken down spec for the behavior:

  it "retains original binding variables but the list is distinct" do
    bind1 = binding
    eval "a = 1", bind1

    bind2 = bind1.dup
    eval("a = 2", bind2)
    eval("a", bind1).should == 2
    eval("a", bind2).should == 2

    eval("b = 2", bind2)
    -> { eval("b", bind1) }.should raise_error(NameError)
    eval("b", bind2).should == 2
    
    bind1.local_variables.sort.should == [:a, :bind1, :bind2]
    bind2.local_variables.sort.should == [:a, :b, :bind1, :bind2]
  end

This fixes the last two errors involving specs in erb. I added this spec to make the behavior explicit and not appear to be some artifact of erb.

When we eval with binding we will construct a new binding that
contains the original binding and that original binding will
construct a new evalDynamicScope.  If I do something like:

```ruby
p TOPLEVEL_BINDING.local_variable_defined? :c
p TOPLEVEL_BINDING.local_variable_defined? :_xxx_var_

b =TOPLEVEL_BINDING.dup
p b.local_variable_defined? :c
p b.local_variable_defined? :_xxx_var_
```

I should still see _xxx_var_ after the dup.  This will make that
do that but two different RubyBinding instances will end up
with the exact same backing Binding and I think this is wrong.
Landing anyways to see if anything breaks.
@enebo enebo added this to the JRuby 9.4.2.0 milestone Feb 23, 2023
@enebo enebo removed this from the JRuby 9.4.2.0 milestone Mar 8, 2023
@enebo enebo changed the title Try and appease the eval+binding gods Correct Binding#dup to properly propagate values it captures May 2, 2024
@enebo enebo added this to the JRuby 9.4.8.0 milestone May 2, 2024
@enebo enebo merged commit 540f511 into jruby:master May 2, 2024
@enebo enebo deleted the weird_binding branch March 4, 2025 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant