ReadBlockNode.java

  1. /*
  2.  * Copyright (c) 2013, 2014 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 org.jruby.truffle.nodes.methods.arguments;

  11. import com.oracle.truffle.api.frame.VirtualFrame;
  12. import com.oracle.truffle.api.source.SourceSection;
  13. import org.jruby.truffle.nodes.RubyNode;
  14. import org.jruby.truffle.runtime.RubyArguments;
  15. import org.jruby.truffle.runtime.RubyContext;
  16. import org.jruby.truffle.runtime.core.RubyProc;

  17. /**
  18.  * Read the block as a {@code Proc}.
  19.  */
  20. public class ReadBlockNode extends RubyNode {

  21.     private final Object valueIfNotPresent;

  22.     public ReadBlockNode(RubyContext context, SourceSection sourceSection, Object valueIfNotPresent) {
  23.         super(context, sourceSection);
  24.         this.valueIfNotPresent = valueIfNotPresent;
  25.     }

  26.     @Override
  27.     public Object execute(VirtualFrame frame) {
  28.         final RubyProc block = RubyArguments.getBlock(frame.getArguments());

  29.         if (block == null) {
  30.             return valueIfNotPresent;
  31.         } else {
  32.             return block;
  33.         }
  34.     }

  35. }