Skip to content

Commit

Permalink
[Truffle] Basic support for END.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed Oct 30, 2016
1 parent 19f7e01 commit 1c18c37
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
Expand Up @@ -245,6 +245,7 @@ public class CoreLibrary {
private final DynamicObject truffleModule;
private final DynamicObject truffleBootModule;
private final DynamicObject truffleInteropModule;
private final DynamicObject truffleKernelModule;
private final DynamicObject bigDecimalClass;
private final DynamicObject encodingCompatibilityErrorClass;
private final DynamicObject methodClass;
Expand Down Expand Up @@ -597,7 +598,7 @@ public CoreLibrary(RubyContext context) {
defineModule(truffleModule, "Fixnum");
defineModule(truffleModule, "Safe");
defineModule(truffleModule, "System");
defineModule(truffleModule, "Kernel");
truffleKernelModule = defineModule(truffleModule, "Kernel");
defineModule(truffleModule, "Process");
defineModule(truffleModule, "Binding");
defineModule(truffleModule, "POSIX");
Expand Down Expand Up @@ -1608,6 +1609,10 @@ public Object getTruffleInteropModule() {
return truffleInteropModule;
}

public Object getTruffleKernelModule() {
return truffleKernelModule;
}

private static final String[] coreFiles = {
"/core/pre.rb",
"/core/lookuptable.rb",
Expand Down
20 changes: 18 additions & 2 deletions truffle/src/main/java/org/jruby/truffle/parser/BodyTranslator.java
Expand Up @@ -259,6 +259,7 @@
import org.jruby.truffle.parser.lexer.ISourcePosition;
import org.jruby.truffle.parser.lexer.InvalidSourcePosition;
import org.jruby.truffle.parser.parser.ParserSupport;
import org.jruby.truffle.parser.scope.StaticScope;
import org.jruby.truffle.platform.graal.AssertConstantNodeGen;
import org.jruby.truffle.platform.graal.AssertNotCompiledNodeGen;
import org.jruby.truffle.tools.ChaosNodeGen;
Expand Down Expand Up @@ -2719,14 +2720,29 @@ public RubyNode visitOrNode(OrParseNode node) {

@Override
public RubyNode visitPreExeNode(PreExeParseNode node) {
// The parser seems to visit BEGIN blocks for us first, so we just need to translate them in place
final RubyNode ret = node.getBodyNode().accept(this);
return addNewlineIfNeeded(node, ret);
}

@Override
public RubyNode visitPostExeNode(PostExeParseNode node) {
final RubyNode ret = node.getBodyNode().accept(this);
return addNewlineIfNeeded(node, ret);
// END blocks run after any other code - not just code in the same file

// Turn into a call to Truffle::Kernel.at_exit

// The scope is empty - we won't be able to access local variables
// TODO fix this
// https://github.com/jruby/jruby/issues/4257
final StaticScope scope = new StaticScope(StaticScope.Type.BLOCK, null);

return translateCallNode(
new CallParseNode(node.getPosition(),
new TruffleFragmentParseNode(node.getPosition(), false, new ObjectLiteralNode(context, null, context.getCoreLibrary().getTruffleKernelModule())),
"at_exit",
new ListParseNode(node.getPosition(), new TrueParseNode(node.getPosition())),
new IterParseNode(node.getPosition(), node.getArgsNode(), scope, node.getBodyNode())),
false, false, false);
}

@Override
Expand Down
Expand Up @@ -124,7 +124,7 @@ protected StaticScope(Type type, StaticScope enclosingScope, String file) {
* @param type the type of scope
* @param enclosingScope the lexically containing scope.
*/
protected StaticScope(Type type, StaticScope enclosingScope) {
public StaticScope(Type type, StaticScope enclosingScope) {
this(type, enclosingScope, NO_NAMES);
}

Expand Down

0 comments on commit 1c18c37

Please sign in to comment.