Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,6 @@ public static DynamicScope prepareScriptScope(ThreadContext context, StaticScope
DynamicScope tlbScope = ((IRScriptBody) irScope).getScriptDynamicScope();
if (tlbScope != null) {
context.preScopedBody(tlbScope);
tlbScope.growIfNeeded();
return tlbScope;
}
}
Expand Down
9 changes: 0 additions & 9 deletions core/src/main/java/org/jruby/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,6 @@ public Node parse(String file, LexerSource lexerSource, DynamicScope blockScope,
} catch (SyntaxException e) {
throw runtime.newSyntaxError(e.getFile() + ":" + (e.getLine() + 1) + ": " + e.getMessage());
}

// If variables were added then we may need to grow the dynamic scope to match the static
// one.
// FIXME: Make this so we only need to check this for blockScope != null. We cannot
// currently since we create the DynamicScope for a LocalStaticScope before parse begins.
// Refactoring should make this fixable.
if (result.getScope() != null) {
result.getScope().growIfNeeded();
}

Node ast = result.getAST();

Expand Down
39 changes: 22 additions & 17 deletions core/src/main/java/org/jruby/parser/ParserConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,13 @@
import org.jruby.ext.coverage.CoverageData;
import org.jruby.runtime.DynamicScope;
import org.jruby.runtime.encoding.EncodingService;
import org.jruby.runtime.scope.ManyVarsDynamicScope;
import org.jruby.util.KCode;

import java.util.Arrays;

public class ParserConfiguration {
private DynamicScope existingScope = null;
private boolean asBlock = false;

// What linenumber will the source think it starts from?
private int lineNumber = 0;
// Is this inline source (aka -e "...source...")
Expand Down Expand Up @@ -139,29 +138,35 @@ public int getLineNumber() {
* @param existingScope is the scope that captures new vars, etc...
*/
public void parseAsBlock(DynamicScope existingScope) {
this.asBlock = true;
this.existingScope = existingScope;
}

public Ruby getRuntime() {
return runtime;
}

/**
* This method returns the appropriate first scope for the parser.
*
* @return correct top scope for source to be parsed
* Returns the static scope which represents the top of the parse. If an eval
* then we will have an existing scope to return. If not then we make a new
* one since we are starting from scratch.
*
* @param file to name top scope if we have a new scope
* @return a static scope
*/
public DynamicScope getScope(String file) {
if (asBlock) return existingScope;

// FIXME: We should really not be creating the dynamic scope for the root
// of the AST before parsing. This makes us end up needing to readjust
// this dynamic scope coming out of parse (and for local static scopes it
// will always happen because of $~ and $_).
// FIXME: Because we end up adjusting this after-the-fact, we can't use
// any of the specific-size scopes.
return new ManyVarsDynamicScope(runtime.getStaticScopeFactory().newLocalScope(null, file), existingScope);
public StaticScope getTopStaticScope(String file) {
return existingScope != null ?
existingScope.getStaticScope() :
runtime.getStaticScopeFactory().newLocalScope(null, file);
}

public DynamicScope finalizeDynamicScope(StaticScope staticScope) {
// Eval scooped up some new variables changing the size of the scope.
if (existingScope != null) {
existingScope.growIfNeeded();
return existingScope;
}

return DynamicScope.newDynamicScope(staticScope, existingScope);
}

public boolean isCoverageEnabled() {
Expand Down
5 changes: 3 additions & 2 deletions core/src/main/java/org/jruby/parser/RubyParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -1999,6 +1999,7 @@ public Object yyparse (RubyLexer yyLex) throws java.io.IOException {
expr = p.remove_begin(expr);
p.void_expr(expr);
}
p.finalizeDynamicScope();
p.getResult().setAST(p.addRootNode(((Node)yyVals[0+yyTop].value)));
/*% %*/
/*% ripper[final]: program!($2) %*/
Expand Down Expand Up @@ -6798,7 +6799,7 @@ public Object yyparse (RubyLexer yyLex) throws java.io.IOException {
return yyVal;
};
}
// line 4768 "parse.y"
// line 4769 "parse.y"

}
// line 14584 "-"
// line 14585 "-"
1 change: 1 addition & 0 deletions core/src/main/java/org/jruby/parser/RubyParser.y
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ program : {
expr = p.remove_begin(expr);
p.void_expr(expr);
}
p.finalizeDynamicScope();
p.getResult().setAST(p.addRootNode($2));
/*% %*/
/*% ripper[final]: program!($2) %*/
Expand Down
9 changes: 5 additions & 4 deletions core/src/main/java/org/jruby/parser/RubyParserBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -1275,12 +1275,13 @@ public Node new_super(int line, Node args) {
* Description of the RubyMethod
*/
public void initTopLocalVariables() {
DynamicScope scope = configuration.getScope(lexer.getFile());
currentScope = scope.getStaticScope();
currentScope = configuration.getTopStaticScope(lexer.getFile());
scopedParserState = new ScopedParserState(null);
warnOnUnusedVariables = warnings.isVerbose() && !configuration.isEvalParse() && !configuration.isInlineSource();

result.setScope(scope);
}

public void finalizeDynamicScope() {
getResult().setScope(configuration.finalizeDynamicScope(currentScope));
}
/**
* Gets the result.
Expand Down