Skip to content

Commit

Permalink
[Truffle] Work towards break and lambda.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed Jan 11, 2015
1 parent 5189814 commit c3953d1
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 25 deletions.
Expand Up @@ -38,7 +38,8 @@ public Object execute(VirtualFrame frame) {

return new RubyProc(getContext().getCoreLibrary().getProcClass(), RubyProc.Type.LAMBDA,
method.getSharedMethodInfo(), method.getCallTarget(), method.getCallTarget(), method.getCallTarget(),
method.getDeclarationFrame(), method.getDeclaringModule(), method, RubyArguments.getSelf(frame.getArguments()), null);
method.getCallTarget(), method.getDeclarationFrame(), method.getDeclaringModule(), method,
RubyArguments.getSelf(frame.getArguments()), null);
}

@Override
Expand Down
Expand Up @@ -2510,7 +2510,7 @@ public Object max(VirtualFrame frame, RubyArray array) {

final RubyProc block = new RubyProc(getContext().getCoreLibrary().getProcClass(), RubyProc.Type.PROC,
maxBlock.getSharedMethodInfo(), maxBlock.getCallTarget(), maxBlock.getCallTarget(),
maxBlock.getCallTarget(), maximumClosureFrame.materialize(), null, null, array, null);
maxBlock.getCallTarget(), maxBlock.getCallTarget(), maximumClosureFrame.materialize(), null, null, array, null);

eachNode.call(frame, array, "each", block);

Expand Down Expand Up @@ -2629,7 +2629,8 @@ public Object min(VirtualFrame frame, RubyArray array) {

final RubyProc block = new RubyProc(getContext().getCoreLibrary().getProcClass(), RubyProc.Type.PROC,
minBlock.getSharedMethodInfo(), minBlock.getCallTarget(), minBlock.getCallTarget(),
minBlock.getCallTarget(), minimumClosureFrame.materialize(), null, null, array, null);
minBlock.getCallTarget(), minBlock.getCallTarget(), minimumClosureFrame.materialize(), null,
null, array, null);

eachNode.call(frame, array, "each", block);

Expand Down
11 changes: 6 additions & 5 deletions core/src/main/java/org/jruby/truffle/nodes/core/KernelNodes.java
Expand Up @@ -1239,9 +1239,9 @@ public RubyProc proc(RubyProc block) {
notDesignedForCompilation();

return new RubyProc(getContext().getCoreLibrary().getProcClass(), RubyProc.Type.LAMBDA,
block.getSharedMethodInfo(), block.getCallTargetForMethods(), block.getCallTargetForMethods(),
block.getCallTargetForMethods(), block.getDeclarationFrame(), block.getDeclaringModule(),
block.getMethod(), block.getSelfCapturedInScope(), block.getBlockCapturedInScope());
block.getSharedMethodInfo(), block.getCallTargetForLambdas(), block.getCallTargetForLambdas(),
block.getCallTargetForLambdas(), block.getCallTargetForLambdas(), block.getDeclarationFrame(),
block.getDeclaringModule(), block.getMethod(), block.getSelfCapturedInScope(), block.getBlockCapturedInScope());
}
}

Expand Down Expand Up @@ -1487,8 +1487,9 @@ public RubyProc proc(RubyProc block) {

return new RubyProc(getContext().getCoreLibrary().getProcClass(), RubyProc.Type.PROC,
block.getSharedMethodInfo(), block.getCallTargetForProcs(), block.getCallTargetForProcs(),
block.getCallTargetForMethods(), block.getDeclarationFrame(), block.getDeclaringModule(),
block.getMethod(), block.getSelfCapturedInScope(), block.getBlockCapturedInScope());
block.getCallTargetForLambdas(), block.getCallTargetForMethods(), block.getDeclarationFrame(),
block.getDeclaringModule(), block.getMethod(), block.getSelfCapturedInScope(),
block.getBlockCapturedInScope());
}
}

Expand Down
Expand Up @@ -114,7 +114,7 @@ public InitializeNode(InitializeNode prev) {
@Specialization
public RubyNilClass initialize(RubyProc proc, RubyProc block) {
proc.initialize(block.getSharedMethodInfo(), block.getCallTargetForProcs(),
block.getCallTargetForProcs(), block.getCallTargetForMethods(), block.getDeclarationFrame(),
block.getCallTargetForProcs(), block.getCallTargetForProcs(), block.getCallTargetForMethods(), block.getDeclarationFrame(),
block.getDeclaringModule(), block.getMethod(), block.getSelfCapturedInScope(),
block.getBlockCapturedInScope());

Expand Down
Expand Up @@ -34,18 +34,21 @@ public class BlockDefinitionNode extends RubyNode {

private final CallTarget callTargetForBlocks;
private final CallTarget callTargetForProcs;
private final CallTarget callTargetForLambdas;
private final CallTarget callTargetForMethods;

private final boolean requiresDeclarationFrame;

public BlockDefinitionNode(RubyContext context, SourceSection sourceSection, SharedMethodInfo sharedMethodInfo,
boolean requiresDeclarationFrame, CallTarget callTargetForBlocks,
CallTarget callTargetForProcs, CallTarget callTargetForMethods) {
CallTarget callTargetForProcs, CallTarget callTargetForLambdas,
CallTarget callTargetForMethods) {
super(context, sourceSection);
this.sharedMethodInfo = sharedMethodInfo;

this.callTargetForBlocks = callTargetForBlocks;
this.callTargetForProcs = callTargetForProcs;
this.callTargetForLambdas = callTargetForLambdas;
this.callTargetForMethods = callTargetForMethods;

this.requiresDeclarationFrame = requiresDeclarationFrame;
Expand Down Expand Up @@ -73,8 +76,8 @@ public Object execute(VirtualFrame frame) {
}

return new RubyProc(getContext().getCoreLibrary().getProcClass(), RubyProc.Type.PROC, sharedMethodInfo,
callTargetForBlocks, callTargetForProcs, callTargetForMethods, declarationFrame, declaringModule,
RubyArguments.getMethod(frame.getArguments()), RubyArguments.getSelf(frame.getArguments()),
callTargetForBlocks, callTargetForProcs, callTargetForLambdas, callTargetForMethods, declarationFrame,
declaringModule, RubyArguments.getMethod(frame.getArguments()), RubyArguments.getSelf(frame.getArguments()),
RubyArguments.getBlock(frame.getArguments()));
}

Expand Down
24 changes: 14 additions & 10 deletions core/src/main/java/org/jruby/truffle/runtime/core/RubyProc.java
Expand Up @@ -34,11 +34,9 @@ public static enum Type {

private final Type type;
@CompilationFinal private SharedMethodInfo sharedMethodInfo;
/** Call target for blocks, which have special arguments destructuring */
@CompilationFinal private CallTarget callTargetForBlocks;
/** Call target for actual Proc arguments, which handle break differently */
@CompilationFinal private CallTarget callTargetForProcs;
/** Call target for lambdas and methods, which have strict arguments destructuring */
@CompilationFinal private CallTarget callTargetForLambdas;
@CompilationFinal private CallTarget callTargetForMethods;
@CompilationFinal private MaterializedFrame declarationFrame;
@CompilationFinal private RubyModule declaringModule;
Expand All @@ -52,19 +50,21 @@ public RubyProc(RubyClass procClass, Type type) {
}

public RubyProc(RubyClass procClass, Type type, SharedMethodInfo sharedMethodInfo, CallTarget callTargetForBlocks,
CallTarget callTargetForProcs, CallTarget callTargetForMethods, MaterializedFrame declarationFrame,
RubyModule declaringModule, MethodLike method, Object self, RubyProc block) {
CallTarget callTargetForProcs, CallTarget callTargetForLambdas, CallTarget callTargetForMethods,
MaterializedFrame declarationFrame, RubyModule declaringModule, MethodLike method, Object self, RubyProc block) {
this(procClass, type);
initialize(sharedMethodInfo, callTargetForBlocks, callTargetForProcs, callTargetForMethods, declarationFrame,
declaringModule, method, self, block);
initialize(sharedMethodInfo, callTargetForBlocks, callTargetForProcs, callTargetForLambdas,
callTargetForMethods, declarationFrame, declaringModule, method, self, block);
}

public void initialize(SharedMethodInfo sharedMethodInfo, CallTarget callTargetForBlocks, CallTarget callTargetForProcs,
CallTarget callTargetForMethods, MaterializedFrame declarationFrame, RubyModule declaringModule,
MethodLike method, Object self, RubyProc block) {
CallTarget callTargetForLambdas, CallTarget callTargetForMethods,
MaterializedFrame declarationFrame, RubyModule declaringModule, MethodLike method,
Object self, RubyProc block) {
this.sharedMethodInfo = sharedMethodInfo;
this.callTargetForBlocks = callTargetForBlocks;
this.callTargetForProcs = callTargetForProcs;
this.callTargetForLambdas = callTargetForLambdas;
this.callTargetForMethods = callTargetForMethods;
this.declarationFrame = declarationFrame;
this.declaringModule = declaringModule;
Expand All @@ -80,7 +80,7 @@ public CallTarget getCallTargetForType() {
case PROC:
return callTargetForProcs;
case LAMBDA:
return callTargetForMethods;
return callTargetForLambdas;
}

throw new UnsupportedOperationException(type.toString());
Expand Down Expand Up @@ -110,6 +110,10 @@ public CallTarget getCallTargetForProcs() {
return callTargetForProcs;
}

public CallTarget getCallTargetForLambdas() {
return callTargetForLambdas;
}

public CallTarget getCallTargetForMethods() {
return callTargetForMethods;
}
Expand Down
Expand Up @@ -55,7 +55,7 @@ public RubyProc toProc(SourceSection sourceSection, final RubyNode currentNode)
final CallTarget callTarget = Truffle.getRuntime().createCallTarget(rootNode);

return new RubyProc(context.getCoreLibrary().getProcClass(), RubyProc.Type.PROC, sharedMethodInfo, callTarget,
callTarget, callTarget, null, null, null, getContext().getCoreLibrary().getNilObject(), null);
callTarget, callTarget, callTarget, null, null, null, getContext().getCoreLibrary().getNilObject(), null);
}

public org.jruby.RubySymbol getJRubySymbol() {
Expand Down
Expand Up @@ -190,6 +190,8 @@ public RubyNode compileFunctionNode(SourceSection sourceSection, String methodNa
behaveAsBlockNode.replace(behaveAsBlockNode.getAsBlock());
}

final CallTarget callTargetAsBlock = Truffle.getRuntime().createCallTarget(newRootNodeForBlocks);

for (BehaveAsProcNode behaveAsProcNode : NodeUtil.findAllNodeInstances(newRootNodeForBlocks, BehaveAsProcNode.class)) {
behaveAsProcNode.replace(behaveAsProcNode.getNotAsProc());
}
Expand All @@ -206,7 +208,17 @@ public RubyNode compileFunctionNode(SourceSection sourceSection, String methodNa

final CallTarget callTargetAsProc = Truffle.getRuntime().createCallTarget(newRootNodeForProcs);

final CallTarget callTargetAsBlock = Truffle.getRuntime().createCallTarget(newRootNodeForBlocks);
final RubyRootNode newRootNodeForLambdas = rootNode.cloneRubyRootNode();

for (BehaveAsBlockNode behaveAsBlockNode : NodeUtil.findAllNodeInstances(newRootNodeForLambdas, BehaveAsBlockNode.class)) {
behaveAsBlockNode.replace(behaveAsBlockNode.getAsBlock());
}

for (BehaveAsProcNode behaveAsProcNode : NodeUtil.findAllNodeInstances(newRootNodeForLambdas, BehaveAsProcNode.class)) {
behaveAsProcNode.replace(behaveAsProcNode.getAsProc());
}

final CallTarget callTargetAsLambda = Truffle.getRuntime().createCallTarget(newRootNodeForLambdas);

final RubyRootNode newRootNodeForMethods = rootNode.cloneRubyRootNode();

Expand All @@ -229,7 +241,7 @@ public RubyNode compileFunctionNode(SourceSection sourceSection, String methodNa
final CallTarget callTargetAsMethod = Truffle.getRuntime().createCallTarget(newRootNodeWithCatchReturn);

return new BlockDefinitionNode(context, sourceSection, environment.getSharedMethodInfo(),
environment.needsDeclarationFrame(), callTargetAsBlock, callTargetAsProc, callTargetAsMethod);
environment.needsDeclarationFrame(), callTargetAsBlock, callTargetAsProc, callTargetAsLambda, callTargetAsMethod);
} else {
return new MethodDefinitionNode(context, sourceSection, methodName, environment.getSharedMethodInfo(),
environment.needsDeclarationFrame(), Truffle.getRuntime().createCallTarget(rootNode));
Expand Down
1 change: 1 addition & 0 deletions spec/truffle/tags/language/block_tags.txt
Expand Up @@ -17,3 +17,4 @@ fails:A block yielded a single Array raises a TypeError if #to_hash does not ret
fails:A block yielded a single Array raises the error raised inside #to_hash
fails:A block yielded a single Object receives the object if #to_ary returns nil
fails:Block-local variables override shadowed variables from the outer scope
fails:Post-args are required

0 comments on commit c3953d1

Please sign in to comment.