Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.jruby.runtime.scope;
  
This is a DynamicScope that supports exactly three variables.
 
 public class ThreeVarDynamicScope extends TwoVarDynamicScope {
     private static final int SIZE = 3;
     private static final String SIZE_ERROR = "ThreeVarDynamicScope only supports scopes with three variables";
     private static final String GROW_ERROR = "ThreeVarDynamicScope cannot be grown; use ManyVarsDynamicScope";
     
     protected IRubyObject variableValueTwo;
 
     public ThreeVarDynamicScope(StaticScope staticScopeDynamicScope parent) {
         super(staticScopeparent);
     }
 
     public ThreeVarDynamicScope(StaticScope staticScope) {
         super(staticScope);
     }
     
     @Override
     public void growIfNeeded() {
         growIfNeeded();
     }
     
     @Override
     public DynamicScope cloneScope() {
         return new ThreeVarDynamicScope();
     }
 
     @Override
     public IRubyObject[] getValues() {
         return new IRubyObject[] {};
     }
    
    
Get value from current scope or one of its captured scopes. FIXME: block variables are not getting primed to nil so we need to null check those until we prime them properly. Also add assert back in.

Parameters:
offset zero-indexed value that represents where variable lives
depth how many captured scopes down this variable should be set
Returns:
the value here
 
     @Override
     public IRubyObject getValue(int offsetint depth) {
         if (depth > 0) {
             return .getValue(offsetdepth - 1);
         }
         assert offset <  : ;
         switch (offset) {
         case 0:
             return ;
         case 1:
             return ;
         case 2:
             return ;
         default:
             throw new RuntimeException();
         }
     }
    
    
Variation of getValue that checks for nulls, returning and setting the given value (presumably nil)
 
     @Override
     public IRubyObject getValueOrNil(int offsetint depthIRubyObject nil) {
         if (depth > 0) {
             return .getValueOrNil(offsetdepth - 1, nil);
         } else {
             return getValueDepthZeroOrNil(offsetnil);
         }
     }
     
     @Override
     public IRubyObject getValueDepthZeroOrNil(int offsetIRubyObject nil) {
         assert offset <  : ;
         switch (offset) {
         case 0:
             if ( == nullreturn  = nil;
             return ;
         case 1:
             if ( == nullreturn  = nil;
             return ;
         case 2:
             if ( == nullreturn  = nil;
             return ;
         default:
             throw new RuntimeException();
         }
     }
     @Override
        if ( == nullreturn  = nil;
        return ;
    }

    
Set value in current dynamic scope or one of its captured scopes.

Parameters:
offset zero-indexed value that represents where variable lives
value to set
depth how many captured scopes down this variable should be set
    @Override
    public IRubyObject setValue(int offsetIRubyObject valueint depth) {
        if (depth > 0) {
            assert  != null : "If depth > 0, then parent should not ever be null";
            
            return .setValue(offsetvaluedepth - 1);
        } else {
            assert offset <  : ;
            switch (offset) {
            case 0:
                return  = value;
            case 1:
                return  = value;
            case 2:
                return  = value;
            default:
                throw new RuntimeException();
            }
        }
    }
    @Override
    public IRubyObject setValueDepthZero(IRubyObject valueint offset) {
        assert offset <  : ;
        switch (offset) {
        case 0:
            return  = value;
        case 1:
            return  = value;
        case 2:
            return  = value;
        default:
            throw new RuntimeException();
        }
    }
    @Override
    public IRubyObject setValueTwoDepthZero(IRubyObject value) {
        return  = value;
    }

    
Set all values which represent 'normal' parameters in a call list to this dynamic scope. Function calls bind to local scopes by assuming that the indexes or the arg list correspond to that of the local scope (plus 2 since $_ and $~ always take the first two slots). We pass in a second argument because we sometimes get more values than we are expecting. The rest get compacted by original caller into rest args.

Parameters:
values up to size specified to be mapped as ordinary parm values
size is the number of values to assign as ordinary parm values
    @Override
    public void setArgValues(IRubyObject[] valuesint size) {
        assert size <=  : "ThreeVarDynamicScope only supports scopes with three variables, not " + size;
        switch (size) {
        case 3:
             = values[2];
        case 2:
             = values[1];
        case 1:
             = values[0];
        }
    }
    @Override
    public void setEndArgValues(IRubyObject[] valuesint indexint size) {
        assert size <= 2 : "ThreeVarDynamicScope only supports scopes with three variables, not " + size;
        assert index + size <= 3 : "ThreeVarDynamicScope only supports scopes with three variables, not " + (index + size);
        
        int start = values.length - size;
        switch (index) {
            case 0:
                switch (size) {
                    case 3:
                         = values[start + 2];
                    case 2:
                         = values[start + 1];
                    case 1:
                         = values[start];
                }
                break;
            case 1:
                switch (size) {
                    case 3:
                        // should never happen
                    case 2:
                         = values[start + 1];
                    case 1:
                         = values[start];
                        break;
                }
                break;
            case 2:
                switch (size) {
                    case 3:
                    case 2:
                        // should never happen
                    case 1:
                         = values[start];
                        break;
                }
                break;
        }
    }
    
    @Override
    public void setArgValues(IRubyObject arg0) {
         = arg0;
    }
    
    @Override
    public void setArgValues(IRubyObject arg0IRubyObject arg1) {
         = arg0;
         = arg1;
    }
    
    @Override
    public void setArgValues(IRubyObject arg0IRubyObject arg1IRubyObject arg2) {
         = arg0;
         = arg1;
         = arg2;
    }      
    @Override
    public IRubyObject[] getArgValues() {
        // if we're not the "argument scope" for zsuper, try our parent
        if (!.isArgumentScope()) {
            return .getArgValues();
        }
        int totalArgs = .getRequiredArgs() + .getOptionalArgs();
        assert totalArgs <=  : ;
        
        // copy and splat arguments out of the scope to use for zsuper call
        if (.getRestArg() < 0) {
            switch (totalArgs) {
            case 0:
                return .;
            case 1:
                return new IRubyObject[] {};
            case 2:
                return new IRubyObject[] {};
            case 3:
                return new IRubyObject[] {};
            default:
                throw new RuntimeException("more args requested than available variables");
            }
        } else {
            // rest arg must be splatted
            IRubyObject restArg = getValue(.getRestArg(), 0);
            assert restArg != null;
            
            // FIXME: not very efficient
            RubyArray splattedArgs = Helpers.splatValue(restArg);
            IRubyObject[] argValues = new IRubyObject[totalArgs + splattedArgs.size()];
            System.arraycopy(splattedArgs.toJavaArray(), 0, argValuestotalArgssplattedArgs.size());
            switch (totalArgs) {
            case 3:
                argValues[2] = ;
            case 2:
                argValues[1] = ;
            case 1:
                argValues[0] = ;
            }
            
            return argValues;
        }
    }
New to GrepCode? Check out our FAQ X