Skip to content

Commit

Permalink
When precompiling loaded files, set top-level module. Fixes #4416.
Browse files Browse the repository at this point in the history
The logic that precompiles a 'load'ed or 'require'd file did not
ever call setModule or determineModule on the root scope for that
script body. This caused module definitions to blow up, since they
had no containing scope in which to define the new module.
  • Loading branch information
headius committed Jan 9, 2017
1 parent fa9060d commit 8a8fa01
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions core/src/main/java/org/jruby/Ruby.java
Expand Up @@ -2862,16 +2862,11 @@ public void loadFile(String scriptName, InputStream in, boolean wrap) {
ThreadContext.pushBacktrace(context, "(root)", file, 0);
context.preNodeEval(self);
ParseResult parseResult = parseFile(scriptName, in, null);
RootNode root = (RootNode) parseResult;

if (wrap) {
// toss an anonymous module into the search path
RubyModule wrapper = RubyModule.newModule(this);
((RubyBasicObject)self).extend(new IRubyObject[] {wrapper});
RootNode root = (RootNode) parseResult;
StaticScope top = root.getStaticScope();
StaticScope newTop = staticScopeFactory.newLocalScope(null);
top.setPreviousCRefScope(newTop);
top.setModule(wrapper);
wrapRootForLoad((RubyBasicObject) self, root);
}

runInterpreter(context, parseResult, self);
Expand Down Expand Up @@ -2903,24 +2898,39 @@ public void loadScope(IRScope scope, boolean wrap) {
}

public void compileAndLoadFile(String filename, InputStream in, boolean wrap) {
InputStream readStream = in;

// script was not found in cache above, so proceed to compile
RootNode scriptNode = (RootNode) parseFile(readStream, filename, null);

IRubyObject self = wrap ? getTopSelf().rbClone() : getTopSelf();
ThreadContext context = getCurrentContext();
InputStream readStream = in;

String oldFile = context.getFile();
int oldLine = context.getLine();
try {
context.setFileAndLine(scriptNode.getFile(), scriptNode.getLine());
context.preNodeEval(self);
ParseResult parseResult = parseFile(filename, in, null);
RootNode root = (RootNode) parseResult;

if (wrap) {
wrapRootForLoad((RubyBasicObject) self, root);
} else {
root.getStaticScope().setModule(getObject());
}

runNormally(scriptNode, wrap);
runNormally(root, wrap);
} finally {
context.setFileAndLine(oldFile, oldLine);
context.postNodeEval();
}
}

private void wrapRootForLoad(RubyBasicObject self, RootNode root) {
// toss an anonymous module into the search path
RubyModule wrapper = RubyModule.newModule(this);
self.extend(new IRubyObject[] {wrapper});
StaticScope top = root.getStaticScope();
StaticScope newTop = staticScopeFactory.newLocalScope(null);
top.setPreviousCRefScope(newTop);
top.setModule(wrapper);
}

public void loadScript(Script script) {
loadScript(script, false);
}
Expand Down

0 comments on commit 8a8fa01

Please sign in to comment.