Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 8 commits into from May 2, 2024

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
74 checks passed
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.

None yet

1 participant