Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Hack fix to align script body dynscopes with top-level-binding
* Not sure if this is the right fix, but pushing it into the repo
  so that at least interp doesn't break on the following tests:

  jruby -X-C -e "x = 1; eval 'p x', TOPLEVEL_BINDING"

* This fix lets me run bundle install for rails/activesupport.

* Once verified and we have a fix that works for both JIT and interp,
  we need tests for this.
  • Loading branch information
subbuss committed Oct 30, 2014
1 parent 5bc65dd commit 7e5e119
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
6 changes: 5 additions & 1 deletion core/src/main/java/org/jruby/ir/IRScope.java
Expand Up @@ -700,7 +700,11 @@ public void computeScopeFlags() {
// NOTE: bindingHasEscaped is the crucial flag and it effectively is
// unconditionally true whenever it has a call that receives a closure.
// See CallBase.computeRequiresCallersBindingFlag
if (this instanceof IREvalScript) { // for eval scopes, bindings are considered escaped ...
if (this instanceof IREvalScript || this instanceof IRScriptBody) {
// For eval scopes, bindings are considered escaped.
// For top-level script scopes, bindings are considered escaped as well
// because TOPLEVEL_BINDING can be used in places besides the file
// that is being parsed?
flags.add(BINDING_HAS_ESCAPED);
} else {
flags.remove(BINDING_HAS_ESCAPED);
Expand Down
11 changes: 11 additions & 0 deletions core/src/main/java/org/jruby/ir/IRScriptBody.java
Expand Up @@ -4,22 +4,33 @@
import org.jruby.ir.interpreter.BeginEndInterpreterContext;
import org.jruby.ir.interpreter.InterpreterContext;
import org.jruby.parser.StaticScope;
import org.jruby.runtime.DynamicScope;

import java.util.ArrayList;
import java.util.List;

public class IRScriptBody extends IRScope {
private List<IRClosure> beginBlocks;
private List<IRClosure> endBlocks;
private DynamicScope tlbScope;

public IRScriptBody(IRManager manager, String sourceName, StaticScope staticScope) {
super(manager, null, sourceName, sourceName, 0, staticScope);
this.tlbScope = null;
if (!getManager().isDryRun() && staticScope != null) {
staticScope.setIRScope(this);
staticScope.setScopeType(this.getScopeType());
}
}

public DynamicScope getTopLevelBindingScope() {
return tlbScope;
}

public void setTopLevelBindingScope(DynamicScope tlbScope) {
this.tlbScope = tlbScope;
}

@Override
public InterpreterContext allocateInterpreterContext(Instr[] instructionList) {
return new BeginEndInterpreterContext(this, instructionList);
Expand Down
1 change: 1 addition & 0 deletions core/src/main/java/org/jruby/ir/IRTranslator.java
Expand Up @@ -24,6 +24,7 @@ public R execute(Ruby runtime, ParseResult result, S specificObject) {
scope = (IRScriptBody) result;
} else if (result instanceof RootNode) { // Need to perform create IR from AST
scope = IRBuilder.createIRBuilder(runtime, runtime.getIRManager()).buildRoot((RootNode) result);
scope.setTopLevelBindingScope(((RootNode)result).getScope());

if (RubyInstanceConfig.IR_WRITING) {
try {
Expand Down
10 changes: 8 additions & 2 deletions core/src/main/java/org/jruby/ir/interpreter/Interpreter.java
Expand Up @@ -161,7 +161,13 @@ protected IRubyObject execute(Ruby runtime, IRScriptBody irScope, IRubyObject se
IRubyObject retVal;

scope.setModule(currModule);
if (!ic.isDynscopeEliminated()) context.preMethodScopeOnly(scope);
DynamicScope tlbScope = irScope.getTopLevelBindingScope();
if (tlbScope == null) {
context.preMethodScopeOnly(scope);
} else {
context.preScopedBody(tlbScope);
tlbScope.growIfNeeded();
}
context.setCurrentVisibility(Visibility.PRIVATE);

try {
Expand All @@ -173,7 +179,7 @@ protected IRubyObject execute(Ruby runtime, IRScriptBody irScope, IRubyObject se
throw IRException.BREAK_LocalJumpError.getException(context.runtime);
} finally {
Interpreter.runEndBlocks(ic.getEndBlocks(), context, self, scope, null);
if (!ic.isDynscopeEliminated()) context.popScope();
context.popScope();
}

return retVal;
Expand Down

0 comments on commit 7e5e119

Please sign in to comment.