Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   ***** BEGIN LICENSE BLOCK *****
   * Version: CPL 1.0/GPL 2.0/LGPL 2.1
   *
   * The contents of this file are subject to the Common Public
   * License Version 1.0 (the "License"); you may not use this file
   * except in compliance with the License. You may obtain a copy of
   * the License at http://www.eclipse.org/legal/cpl-v10.html
   *
  * Software distributed under the License is distributed on an "AS
  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  * implied. See the License for the specific language governing
  * rights and limitations under the License.
  *
  * Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
  * Copyright (C) 2001-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
  * Copyright (C) 2002 Benoit Cerrina <b.cerrina@wanadoo.fr>
  * Copyright (C) 2004-2007 Thomas E Enebo <enebo@acm.org>
  * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
  * 
  * Alternatively, the contents of this file may be used under the terms of
  * either of the GNU General Public License Version 2 or later (the "GPL"),
  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  * in which case the provisions of the GPL or the LGPL are applicable instead
  * of those above. If you wish to allow use of your version of this file only
  * under the terms of either the GPL or the LGPL, and not to allow others to
  * use your version of this file under the terms of the CPL, indicate your
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the CPL, the GPL or the LGPL.
  ***** END LICENSE BLOCK *****/
 package org.jruby.runtime;
 
 import org.jruby.Ruby;
This branch of the BlockBody hierarchy represents an interpreted block that passes its AST nodes to the interpreter. It forms the top of the hierarchy of interpreted blocks. In a typical application, it is the most heavily consumed type of block.

See also:
SharedScopeBlock, CompiledBlock
 
 public class InterpretedBlock extends ContextAwareBlockBody {
    
This block has no arguments at all (simple secondary optimization

See also:
assignerFor for an explanation).
 
     private boolean noargblock;

    
The position for the block
 
     private final ISourcePosition position;

    
Filename from position
 
     private final String file;

    
Line from position
 
     private final int line;

    
The body of the block, pulled out of bodyNode
 
     private final Node bodyNode;
    
    
Logic for assigning the blocks local variables
 
     protected Assigner assigner;
    public static Block newInterpretedClosure(ThreadContext contextIterNode iterNodeIRubyObject self) {
        Binding binding = context.currentBinding(self);
        NodeType argsNodeId = getArgumentTypeWackyHack(iterNode);
        BlockBody body = new InterpretedBlock(
                iterNode,
                Arity.procArityOf(iterNode.getVarNode()),
                asArgumentType(argsNodeId));
        return new Block(bodybinding);
    }
    public static Block newInterpretedClosure(ThreadContext contextBlockBody bodyIRubyObject self) {
        Binding binding = context.currentBinding(self);
        return new Block(bodybinding);
    }
    public static BlockBody newBlockBody(IterNode iterArity arityint argumentType) {
        return new InterpretedBlock(iterarityargumentType);
    }
    /*
     * Determine what sort of assigner should be used for the provided 'iter' (e.g. block).
     * Assigner provides just the right logic for assigning values to local parameters of the
     * block.
     *
     * This method also has a second optimization which is to set 'noargblock' in the case that
     * the block is a block which accepts no arguments.  The primary reason for this second
     * optimization is that in the case of a yield with a RubyArray we will bypass some logic
     * processing the RubyArray into a proper form (only to then not do anythign with it).  A
     * secondary benefit is that a simple boolean seems to optimize by hotspot much faster
     * than the zero arg assigner.
     */
    private void assignerFor(IterNode iter) {
        Node varNode = iter.getVarNode();
        Node block = iter.getBlockVarNode();
        boolean hasBlock = block != null;
        if (varNode == null || varNode instanceof ZeroArgNode) { // No argument blocks
             = !hasBlock;
             = hasBlock ? new Pre0Rest0Post0BlockAssigner(block) :
                new Pre0Rest0Post0Assigner();
        } else if (varNode instanceof MultipleAsgnNode) {
            MultipleAsgnNode masgn = (MultipleAsgnNodevarNode;
            int preCount = masgn.getPreCount();
            boolean isRest = masgn.getRest() != null;
            Node rest = masgn.getRest();
            ListNode pre = masgn.getPre();
             = false;
            switch(preCount) {
                case 0:  // Not sure if this is actually possible, but better safe than sorry
                    if (isRest) {
                         = hasBlock ? new Pre0Rest1Post0BlockAssigner(restblock) :
                            new Pre0Rest1Post0Assigner(rest);
                    } else if (hasBlock) {
                         = new Pre0Rest0Post0BlockAssigner(block);
                    } else {
                         = true;
                         = new Pre0Rest0Post0Assigner();
                    }
                    break;
                case 1:
                    if (isRest) {
                         = hasBlock ? new Pre1Rest1Post0BlockAssigner(pre.get(0), restblock) :
                            new Pre1Rest1Post0Assigner(pre.get(0), rest);
                    } else if (hasBlock) {
                         = new Pre1Rest0Post0BlockAssigner(pre.get(0), block);
                    } else {
                         = new Pre1Rest0Post0Assigner(pre.get(0));
                    }
                    break;
                case 2:
                    if (isRest) {
                         = hasBlock ? new Pre2Rest1Post0BlockAssigner(pre.get(0), pre.get(1), restblock) :
                            new Pre2Rest1Post0Assigner(pre.get(0), pre.get(1), rest);
                    } else if (hasBlock) {
                         = new Pre2Rest0Post0BlockAssigner(pre.get(0), pre.get(1), block);
                    } else {
                         = new Pre2Rest0Post0Assigner(pre.get(0), pre.get(1));
                    }
                    break;
                case 3:
                    if (isRest) {
                         = hasBlock ? new Pre3Rest1Post0BlockAssigner(pre.get(0), pre.get(1), pre.get(2), restblock) :
                            new Pre3Rest1Post0Assigner(pre.get(0), pre.get(1), pre.get(2), rest);
                    } else if (hasBlock) {
                         = new Pre3Rest0Post0BlockAssigner(pre.get(0), pre.get(1), pre.get(2), block);
                    } else {
                         = new Pre3Rest0Post0Assigner(pre.get(0), pre.get(1), pre.get(2));
                    }
                    break;
                default:
                    if (isRest) {
                         = hasBlock ? new PreManyRest1Post0BlockAssigner(prepreCountrestblock) :
                            new PreManyRest1Post0Assigner(prepreCountrest);
                    } else if (hasBlock) {
                         = new PreManyRest0Post0BlockAssigner(prepreCountblock);
                    } else {
                         = new PreManyRest0Post0Assigner(prepreCount);
                    }
                    break;
            }
        } else {
             = hasBlock ? new Pre1ExpandedRest0Post0BlockAssigner(varNodeblock) :
                 new Pre1ExpandedRest0Post0Assigner(varNode);
        }
    }
    public InterpretedBlock(IterNode iterNodeint argumentType) {
        this(iterNode, Arity.procArityOf(iterNode == null ? null : iterNode.getVarNode()), argumentType);
    }
    
    public InterpretedBlock(IterNode iterNodeArity arityint argumentType) {
        super(iterNode.getScope(), arityargumentType);
        
        this. = iterNode.getBodyNode() == null ? . : iterNode.getBodyNode();
        this. = iterNode.getScope();
        this. = iterNode.getPosition();
        // precache these
        this. = .getFile();
        this. = .getLine();
        assignerFor(iterNode);
    }
    @Override
    public IRubyObject yieldSpecific(ThreadContext contextBinding bindingBlock.Type type) {
        return yield(contextbindingtype);
    }
    @Override
    public IRubyObject yieldSpecific(ThreadContext contextIRubyObject arg0Binding bindingBlock.Type type) {
        Visibility oldVis = binding.getFrame().getVisibility();
        Frame lastFrame = pre(contextnullbinding);
        IRubyObject self = prepareSelf(binding);
        try {
            if (!) {
                .assign(context.runtimecontextselfarg0.);
            }
            // This while loop is for restarting the block call in case a 'redo' fires.
            return evalBlockBody(contextbindingself);
        } catch (JumpException.NextJump nj) {
            return handleNextJump(contextnjtype);
        } finally {
            post(contextbindingoldVislastFrame);
        }
    }
    @Override
    public IRubyObject yieldSpecific(ThreadContext contextIRubyObject arg0IRubyObject arg1Binding bindingBlock.Type type) {
        Visibility oldVis = binding.getFrame().getVisibility();
        Frame lastFrame = pre(contextnullbinding);
        IRubyObject self = prepareSelf(binding);
        try {
            if (!) {
                .assign(context.runtimecontextselfarg0arg1.);
            }
            // This while loop is for restarting the block call in case a 'redo' fires.
            return evalBlockBody(contextbindingself);
        } catch (JumpException.NextJump nj) {
            return handleNextJump(contextnjtype);
        } finally {
            post(contextbindingoldVislastFrame);
        }
    }
    @Override
    public IRubyObject yieldSpecific(ThreadContext contextIRubyObject arg0IRubyObject arg1IRubyObject arg2Binding bindingBlock.Type type) {
        Visibility oldVis = binding.getFrame().getVisibility();
        Frame lastFrame = pre(contextnullbinding);
        IRubyObject self = prepareSelf(binding);
        try {
            if (!) {
                .assign(context.runtimecontextselfarg0arg1arg2.);
            }
            // This while loop is for restarting the block call in case a 'redo' fires.
            return evalBlockBody(contextbindingself);
        } catch (JumpException.NextJump nj) {
            return handleNextJump(contextnjtype);
        } finally {
            post(contextbindingoldVislastFrame);
        }
    }
    public IRubyObject yield(ThreadContext contextBinding bindingBlock.Type type) {
        IRubyObject self = prepareSelf(binding);
        Visibility oldVis = binding.getFrame().getVisibility();
        Frame lastFrame = pre(contextnullbinding);
        try {
            if (!) {
                .assign(context.runtimecontextself.);
            }
            return evalBlockBody(contextbindingself);
        } catch (JumpException.NextJump nj) {
            return handleNextJump(contextnjtype);
        } finally {
            post(contextbindingoldVislastFrame);
        }
    }
    public IRubyObject yield(ThreadContext contextIRubyObject valueBinding bindingBlock.Type type) {
        return yield(contextvaluebindingtype.);
    }
    @Override
    public IRubyObject yield(ThreadContext contextIRubyObject valueIRubyObject self,
            RubyModule klassboolean alreadyArrayBinding bindingBlock.Type typeBlock block) {
        if (klass == null) {
            self = prepareSelf(binding);
        }
        Visibility oldVis = binding.getFrame().getVisibility();
        Frame lastFrame = pre(contextklassbinding);
        Ruby runtime = context.runtime;
        try {
            if (!) {
                value = alreadyArray ? .convertIfAlreadyArray(runtimevalue) :
                    .convertToArray(runtimevalue);
                .assignArray(runtimecontextselfvalueblock);
            }
            // This while loop is for restarting the block call in case a 'redo' fires.
            return evalBlockBody(contextbindingself);
        } catch (JumpException.NextJump nj) {
            return handleNextJump(contextnjtype);
        } finally {
            post(contextbindingoldVislastFrame);
        }
    }
    @Override
    public IRubyObject yield(ThreadContext contextIRubyObject value,
            Binding bindingBlock.Type typeBlock block) {
        IRubyObject self = prepareSelf(binding);
        Visibility oldVis = binding.getFrame().getVisibility();
        Frame lastFrame = pre(contextnullbinding);
        try {
            if (!) {
                .assignArray(context.runtimecontextself,
                        .convertToArray(context.runtimevalue), block);
            }
            return evalBlockBody(contextbindingself);
        } catch (JumpException.NextJump nj) {
            return handleNextJump(contextnjtype);
        } finally {
            post(contextbindingoldVislastFrame);
        }
    }

    
Yield to this block, usually passed to the current call.

Parameters:
context represents the current thread-specific data
value The value to yield, either a single value or an array of values
self The current self
klass
alreadyArray do we need an array or should we assume it already is one?
Returns:
result of block invocation
    public IRubyObject yield(ThreadContext contextIRubyObject valueIRubyObject self
            RubyModule klassboolean alreadyArrayBinding bindingBlock.Type type) {
        return yield(contextvalueselfklassalreadyArraybindingtype.);
    }
    
    private IRubyObject evalBlockBody(ThreadContext contextBinding bindingIRubyObject self) {
        // This while loop is for restarting the block call in case a 'redo' fires.
        while (true) {
            try {
                return ASTInterpreter.INTERPRET_BLOCK(context.runtimecontextbinding.getMethod(), self.);
            } catch (JumpException.RedoJump rj) {
                context.pollThreadEvents();
                // do nothing, allow loop to redo
            } catch (StackOverflowError soe) {
                throw context.runtime.newSystemStackError("stack level too deep"soe);
            }
        }
    }
    
    private IRubyObject prepareSelf(Binding binding) {
        IRubyObject self = binding.getSelf();
        binding.getFrame().setSelf(self);
        
        return self;
    }
    
    private IRubyObject handleNextJump(ThreadContext contextJumpException.NextJump njBlock.Type type) {
        return nj.getValue() == null ? context.runtime.getNil() : (IRubyObject)nj.getValue();
    }
    public Node getBodyNode() {
        return ;
    }
    public String getFile() {
        return .getFile();
    }
    public int getLine() {
        return .getLine();
    }
New to GrepCode? Check out our FAQ X