1 package org.jruby.runtime; 2 3 import org.jruby.parser.BlockStaticScope; 4 import org.jruby.parser.StaticScope; 5 import org.jruby.runtime.builtin.IRubyObject; 6 7 23 public class DynamicScope { 24 private IRubyObject[] variableValues; 26 27 private StaticScope staticScope; 29 30 private DynamicScope parent; 32 33 private DynamicScope bindingScope; 38 39 public DynamicScope(StaticScope staticScope, DynamicScope parent) { 40 this.staticScope = staticScope; 41 this.parent = parent; 42 43 int size = staticScope.getNumberOfVariables(); 44 if (size > 0) variableValues = new IRubyObject[size]; 45 } 46 47 public DynamicScope cloneScope() { 48 return new DynamicScope(staticScope, parent); 49 } 50 51 56 public String [] getAllNamesInScope() { 57 return staticScope.getAllNamesInScope(); 58 } 59 60 public IRubyObject[] getValues() { 61 return variableValues; 62 } 63 64 74 public IRubyObject getValue(int offset, int depth) { 75 if (depth > 0) { 76 return parent.getValue(offset, depth - 1); 77 } 78 assert variableValues != null : "No variables in getValue for Off: " + offset + ", Dep: " + depth; 79 assert offset < variableValues.length : "Index to big for getValue Off: " + offset + ", Dep: " + depth + ", O: " + this; 80 return variableValues[offset]; 83 } 84 85 92 public void setValue(int offset, IRubyObject value, int depth) { 93 if (depth > 0) { 94 assert parent != null : "If depth > 0, then parent should not ever be null"; 95 96 parent.setValue(offset, value, depth - 1); 97 } else { 98 assert offset < variableValues.length : "Setting " + offset + " to " + value + ", O: " + this; 99 100 variableValues[offset] = value; 101 } 102 } 103 104 115 public void setArgValues(IRubyObject[] values, int size) { 116 for (int i = 0; i < size; i++) { 117 setValue(i + 2, values[i], 0); 118 } 119 } 120 121 129 public void growIfNeeded() { 130 int dynamicSize = variableValues == null ? 0: variableValues.length; 131 132 if (staticScope.getNumberOfVariables() > dynamicSize) { 133 IRubyObject values[] = new IRubyObject[staticScope.getNumberOfVariables()]; 134 135 if (dynamicSize > 0) { 136 System.arraycopy(variableValues, 0, values, 0, dynamicSize); 137 } 138 139 variableValues = values; 140 } 141 } 142 143 147 public void setLastLine(IRubyObject value) { 148 int location = staticScope.isDefined("$_"); 149 150 setValue(location & 0xffff, value, location >> 16); 151 } 152 153 public IRubyObject getLastLine() { 154 int location = staticScope.isDefined("$_"); 155 156 return getValue(location & 0xffff, location >> 16); 157 } 158 159 public void setBackRef(IRubyObject value) { 160 int location = staticScope.isDefined("$~"); 161 162 setValue(location & 0xffff, value, location >> 16); 163 } 164 165 public IRubyObject getBackRef() { 166 int location = staticScope.isDefined("$~"); 167 168 return getValue(location & 0xffff, location >> 16); 169 } 170 171 public DynamicScope getBindingScope() { 172 return bindingScope; 173 } 174 175 public void setBindingScope(DynamicScope bindingScope) { 176 this.bindingScope = bindingScope; 177 } 178 179 185 public DynamicScope getNextCapturedScope() { 186 return parent; 187 } 188 189 194 public StaticScope getStaticScope() { 195 return staticScope; 196 } 197 198 public String toString() { 199 return toString(new StringBuffer (), ""); 200 } 201 202 private String toString(StringBuffer buf, String indent) { 204 buf.append(indent).append("Static Type[" + hashCode() + "]: " + 205 (staticScope instanceof BlockStaticScope ? "block" : "local")+" ["); 206 int size = staticScope.getNumberOfVariables(); 207 208 if (size != 0) { 209 String names[] = staticScope.getVariables(); 210 for (int i = 0; i < size-1; i++) { 211 buf.append(names[i]).append("="); 212 213 if (variableValues[i] == null) { 214 buf.append("null"); 215 } else { 216 buf.append(variableValues[i]); 217 } 218 219 buf.append(","); 220 } 221 buf.append(names[size-1]).append("="); 222 223 assert variableValues.length == names.length : "V: " + variableValues.length + 224 " != N: " + names.length + " for " + buf; 225 226 if (variableValues[size-1] == null) { 227 buf.append("null"); 228 } else { 229 buf.append(variableValues[size-1]); 230 } 231 232 } 233 234 buf.append("]"); 235 if (parent != null) { 236 buf.append("\n"); 237 parent.toString(buf, indent + " "); 238 } 239 240 return buf.toString(); 241 } 242 } 243 | Popular Tags |