Skip to content

Commit

Permalink
some docs and speed up value copying
Browse files Browse the repository at this point in the history
  • Loading branch information
enebo committed May 2, 2024
1 parent 0cc945b commit eb73e3b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
9 changes: 7 additions & 2 deletions core/src/main/java/org/jruby/runtime/Binding.java
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,15 @@ public BacktraceElement getBacktrace() {
new Frame(),
Visibility.PUBLIC);

/**
* Duplicate this binding and setup the proper cloned instance of the eval scope so that any previously
* captured variables still exist but are not shared with the original binding.
* @param context the current thread context
* @return the duplicated binding
*/
public Binding dup(ThreadContext context) {
Binding newBinding = new Binding(this);
DynamicScope scope = getEvalScope(context.runtime);
newBinding.evalScope = scope.dup();
newBinding.evalScope = getEvalScope(context.runtime).dupEvalScope();
return newBinding;
}
}
13 changes: 7 additions & 6 deletions core/src/main/java/org/jruby/runtime/DynamicScope.java
Original file line number Diff line number Diff line change
Expand Up @@ -600,12 +600,13 @@ public DynamicScope cloneScope() {
}
}

public DynamicScope dup() {
DynamicScope newScope = new ManyVarsDynamicScope(staticScope.duplicate(), parent);
IRubyObject[] values = getValues();
for (int i = 0; i < values.length; i++) {
newScope.setValueDepthZero(values[i], i);
}
/**
* Binding needs to clone its scope with all the current values.
* @return a duplicate of this scope with all the current values
*/
public DynamicScope dupEvalScope() {
ManyVarsDynamicScope newScope = new ManyVarsDynamicScope(staticScope.duplicate(), parent);
newScope.setVariableValues(getValues());
return newScope;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ private void allocate() {
if(variableValues == null) variableValues = IRubyObject.array(staticScope.getNumberOfVariables());
}

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

public IRubyObject[] getValues() {
return variableValues;
}
Expand Down

0 comments on commit eb73e3b

Please sign in to comment.