Skip to content

Commit

Permalink
dup will dup binding state already saved so it will keep variable val…
Browse files Browse the repository at this point in the history
…ues but they will be their own thing from that point onward
  • Loading branch information
enebo committed May 1, 2024
1 parent 4c1473a commit d994845
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/RubyBinding.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public IRubyObject initialize(ThreadContext context) {

@JRubyMethod
public IRubyObject dup(ThreadContext context) {
return newBinding(context.runtime, binding.clone());
return newBinding(context.runtime, binding.dup(context));
}

@JRubyMethod(name = "initialize_copy", visibility = Visibility.PRIVATE)
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/parser/StaticScope.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
* will point to the previous scope of the enclosing module/class (cref).
*
*/
public class StaticScope implements Serializable {
public class StaticScope implements Serializable, Cloneable {
public static final int MAX_SPECIALIZED_SIZE = 50;
private static final long serialVersionUID = 3423852552352498148L;

Expand Down
7 changes: 7 additions & 0 deletions core/src/main/java/org/jruby/runtime/Binding.java
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,11 @@ public BacktraceElement getBacktrace() {
// Can't use Frame.DUMMY because of circular static init seeing it before it's assigned
new Frame(),
Visibility.PUBLIC);

public Binding dup(ThreadContext context) {
Binding newBinding = new Binding(this);
DynamicScope scope = getEvalScope(context.runtime);
newBinding.evalScope = scope.dup();
return newBinding;
}
}
4 changes: 4 additions & 0 deletions core/src/main/java/org/jruby/runtime/DynamicScope.java
Original file line number Diff line number Diff line change
Expand Up @@ -598,4 +598,8 @@ public DynamicScope cloneScope() {
throw new RuntimeException("BUG: failed to clone scope type " + getClass().getName());
}
}

public DynamicScope dup() {
return newDynamicScope(staticScope.duplicate(), parent);
}
}

0 comments on commit d994845

Please sign in to comment.