13514
|
1 /*
|
|
2 * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. This
|
|
3 * code is released under a tri EPL/GPL/LGPL license. You can use it,
|
|
4 * redistribute it and/or modify it under the terms of the:
|
|
5 *
|
|
6 * Eclipse Public License version 1.0
|
|
7 * GNU General Public License version 2
|
|
8 * GNU Lesser General Public License version 2.1
|
|
9 */
|
|
10 package com.oracle.truffle.ruby.nodes.yield;
|
|
11
|
|
12 import com.oracle.truffle.api.*;
|
|
13 import com.oracle.truffle.api.frame.*;
|
|
14 import com.oracle.truffle.api.nodes.*;
|
|
15 import com.oracle.truffle.ruby.nodes.*;
|
|
16 import com.oracle.truffle.ruby.runtime.*;
|
|
17 import com.oracle.truffle.ruby.runtime.core.*;
|
|
18
|
|
19 /**
|
|
20 * Yield to the current block.
|
|
21 */
|
|
22 @NodeInfo(shortName = "yield")
|
|
23 public class YieldNode extends RubyNode {
|
|
24
|
|
25 @Children protected final RubyNode[] arguments;
|
|
26 @Child protected YieldDispatchNode dispatch;
|
|
27
|
|
28 public YieldNode(RubyContext context, SourceSection sourceSection, RubyNode[] arguments) {
|
|
29 super(context, sourceSection);
|
|
30 this.arguments = adoptChildren(arguments);
|
|
31 dispatch = adoptChild(new UninitializedYieldDispatchNode(getContext(), getSourceSection()));
|
|
32 }
|
|
33
|
|
34 @ExplodeLoop
|
|
35 @Override
|
|
36 public final Object execute(VirtualFrame frame) {
|
|
37 final Object[] argumentsObjects = new Object[arguments.length];
|
|
38
|
|
39 for (int i = 0; i < arguments.length; i++) {
|
|
40 argumentsObjects[i] = arguments[i].execute(frame);
|
|
41 }
|
|
42
|
|
43 final RubyProc block = frame.getArguments(RubyArguments.class).getBlock();
|
|
44
|
|
45 if (block == null) {
|
|
46 CompilerDirectives.transferToInterpreter();
|
|
47 // TODO(CS): convert to the proper Ruby exception
|
|
48 throw new RuntimeException("No block to yield to");
|
|
49 }
|
|
50
|
|
51 return dispatch.dispatch(frame, block, argumentsObjects);
|
|
52 }
|
|
53
|
|
54 @Override
|
|
55 public Object isDefined(VirtualFrame frame) {
|
|
56 final RubyArguments args = frame.getArguments(RubyArguments.class);
|
|
57
|
|
58 if (args.getBlock() == null) {
|
|
59 return NilPlaceholder.INSTANCE;
|
|
60 } else {
|
|
61 return getContext().makeString("yield");
|
|
62 }
|
|
63 }
|
|
64
|
|
65 }
|