Skip to content

Commit eb73e3b

Browse files
committed
some docs and speed up value copying
1 parent 0cc945b commit eb73e3b

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

core/src/main/java/org/jruby/runtime/Binding.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,15 @@ public BacktraceElement getBacktrace() {
317317
new Frame(),
318318
Visibility.PUBLIC);
319319

320+
/**
321+
* Duplicate this binding and setup the proper cloned instance of the eval scope so that any previously
322+
* captured variables still exist but are not shared with the original binding.
323+
* @param context the current thread context
324+
* @return the duplicated binding
325+
*/
320326
public Binding dup(ThreadContext context) {
321327
Binding newBinding = new Binding(this);
322-
DynamicScope scope = getEvalScope(context.runtime);
323-
newBinding.evalScope = scope.dup();
328+
newBinding.evalScope = getEvalScope(context.runtime).dupEvalScope();
324329
return newBinding;
325330
}
326331
}

core/src/main/java/org/jruby/runtime/DynamicScope.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -600,12 +600,13 @@ public DynamicScope cloneScope() {
600600
}
601601
}
602602

603-
public DynamicScope dup() {
604-
DynamicScope newScope = new ManyVarsDynamicScope(staticScope.duplicate(), parent);
605-
IRubyObject[] values = getValues();
606-
for (int i = 0; i < values.length; i++) {
607-
newScope.setValueDepthZero(values[i], i);
608-
}
603+
/**
604+
* Binding needs to clone its scope with all the current values.
605+
* @return a duplicate of this scope with all the current values
606+
*/
607+
public DynamicScope dupEvalScope() {
608+
ManyVarsDynamicScope newScope = new ManyVarsDynamicScope(staticScope.duplicate(), parent);
609+
newScope.setVariableValues(getValues());
609610
return newScope;
610611
}
611612
}

core/src/main/java/org/jruby/runtime/scope/ManyVarsDynamicScope.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ private void allocate() {
5050
if(variableValues == null) variableValues = IRubyObject.array(staticScope.getNumberOfVariables());
5151
}
5252

53+
// WARNING: This is used when dup'ing an eval scope. We know the size and that it will 100% always be
54+
// a ManyVarsDynamicScope. This should not be used by anything else. If there ever ends up being another
55+
// use then it should be documented in this warning.
56+
public void setVariableValues(IRubyObject[] variableValues) {
57+
this.variableValues = variableValues;
58+
}
59+
5360
public IRubyObject[] getValues() {
5461
return variableValues;
5562
}

0 commit comments

Comments
 (0)