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 OneVarDynamicScope extends NoVarsDynamicScope {
     private static final int SIZE = 1;
     private static final String SIZE_ERROR = "OneVarDynamicScope only supports scopes with one variable";
     private static final String GROW_ERROR = "OneVarDynamicScope cannot be grown; use ManyVarsDynamicScope";
     
     protected IRubyObject variableValueZero;
 
     public OneVarDynamicScope(StaticScope staticScopeDynamicScope parent) {
         super(staticScopeparent);
     }
 
     public OneVarDynamicScope(StaticScope staticScope) {
         super(staticScope);
     }
     
     @Override
     public void growIfNeeded() {
         growIfNeeded();
     }
     
     @Override
     public DynamicScope cloneScope() {
         return new OneVarDynamicScope();
     }
 
     @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 <  : ;
         return ;
     }
    
    
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) {
         IRubyObject value = ;
         if (value == nullreturn  = nil;
         return value;
     }
     @Override
         IRubyObject value = ;
         if (value == nullreturn  = nil;
         return value;
     }

    
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) {
             return .setValue(offsetvaluedepth - 1);
         } else {
             return  = value;
         }
    }
    @Override
    public IRubyObject setValueDepthZero(IRubyObject valueint offset) {
        return  = value;
    }
    @Override
        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) {
        if (size == 1) {
             = values[0];
        }
    }
    
    @Override
    public void setArgValues(IRubyObject arg0) {
         = arg0;
    }
    /*
     * If we are setting post arguments we can assume there are no pre or others
     */
    @Override
    public void setEndArgValues(IRubyObject[] valuesint indexint size) {
        assert index == 0;
        assert size <= 1;
        
        if (size == 1) {
             = values[values.length - 1];
        }
    }
    @Override
    public IRubyObject[] getArgValues() {
        // if we're not the "argument scope" for zsuper, try our parent
        if (!.isArgumentScope()) {
            return .getArgValues();
        }
        int totalArgs = .getRequiredArgs() + .getOptionalArgs();
        
        // copy and splat arguments out of the scope to use for zsuper call
        if (.getRestArg() < 0) {
            if (totalArgs == 1) {
                return new IRubyObject[] {};
            } else {
                return .;
            }
        } else {
            // rest arg must be splatted
            IRubyObject restArg = getValue(.getRestArg(), 0);
            
            // FIXME: not very efficient
            RubyArray splattedArgs = RuntimeHelpers.splatValue(restArg);            
            IRubyObject[] argValues = new IRubyObject[totalArgs + splattedArgs.size()];
            System.arraycopy(splattedArgs.toJavaArray(), 0, argValuestotalArgssplattedArgs.size());
            
            if (totalArgs == 1) {
                argValues[0] = ;
            }
            
            return argValues;
        }
    }
New to GrepCode? Check out our FAQ X