1 4 package gnu.mapping; 5 import gnu.math.*; 6 import gnu.lists.*; 7 8 9 10 public class CallContext { 13 14 static ThreadLocal currentContext = new ThreadLocal (); 15 16 18 Thread currentThread; 19 20 Environment curEnvironment; 21 22 public final Environment getEnvironmentRaw() { return curEnvironment; } 23 public final void setEnvironmentRaw(Environment env) { curEnvironment = env;} 24 25 public final Environment getEnvironment() 26 { 27 if (curEnvironment == null) 28 { 29 Environment env 30 = Environment.make(currentThread.getName(), Environment.global); 31 env.flags |= Environment.THREAD_SAFE; 32 curEnvironment = env; 33 } 34 return curEnvironment; 35 } 36 37 public static void setInstance(CallContext ctx) 38 { 39 Thread thread = Thread.currentThread(); 40 ctx.currentThread = thread; 41 42 currentContext.set(ctx); 43 44 49 } 50 51 52 public static CallContext getOnlyInstance() 53 { 54 55 return (CallContext) currentContext.get(); 56 57 62 } 63 64 65 public static CallContext getInstance() 66 { 67 CallContext ctx = getOnlyInstance(); 68 if (ctx == null) 69 { 70 ctx = new CallContext(); 71 setInstance(ctx); 72 } 73 return ctx; 74 } 75 76 public Procedure proc; 77 78 80 public int pc; 81 82 85 86 88 public ValueStack vstack = new ValueStack(); 91 public Consumer consumer = vstack; 92 93 94 public Object value1; 95 public Object value2; 96 public Object value3; 97 public Object value4; 98 public Object [] values; 99 public int ivalue1; 100 public int ivalue2; 101 102 103 public int count; 104 105 107 public int next; 108 109 113 public int where; 114 public final static int ARG_IN_VALUES_ARRAY = 0; 115 public final static int ARG_IN_VALUE1 = 1; 116 public final static int ARG_IN_VALUE2 = 2; 117 public final static int ARG_IN_VALUE3 = 3; 118 public final static int ARG_IN_VALUE4 = 4; 119 public final static int ARG_IN_IVALUE1 = 5; 120 public final static int ARG_IN_IVALUE2 = 6; 121 122 Object getArgAsObject(int i) 123 { 124 if (i < 8) 125 { 126 switch ((this.where >> (4 * i)) & 15) 127 { 128 case ARG_IN_VALUE1: return value1; 129 case ARG_IN_VALUE2: return value2; 130 case ARG_IN_VALUE3: return value3; 131 case ARG_IN_VALUE4: return value4; 132 case ARG_IN_IVALUE1: return IntNum.make(ivalue1); 133 case ARG_IN_IVALUE2: return IntNum.make(ivalue2); 134 } 135 } 136 return values[i]; 137 } 138 139 145 public Object getNextArg() 146 { 147 if (next >= count) 148 throw new WrongArguments(null, count); 149 return getArgAsObject(next++); 150 } 151 152 public int getNextIntArg() 153 { 154 if (next >= count) 155 throw new WrongArguments(null, count); 156 Object arg = getArgAsObject(next++); 157 return ((Number ) arg).intValue(); 158 } 159 160 163 public Object getNextArg(Object defaultValue) 164 { 165 if (next >= count) 166 return defaultValue; 167 return getArgAsObject(next++); 168 } 169 170 public int getNextIntArg(int defaultValue) 171 { 172 if (next >= count) 173 return defaultValue; 174 return ((Number ) getArgAsObject(next++)).intValue(); 175 } 176 177 178 public final Object [] getRestArgsArray (int next) 179 { 180 Object [] args = new Object [count - next]; 181 int i = 0; 182 while (next < count) 183 { 184 args[i++] = getArgAsObject(next++); 185 } 186 return args; 187 } 188 189 191 public final LList getRestArgsList (int next) 192 { 193 LList nil = LList.Empty; 194 LList list = nil; 195 Pair last = null; 196 while (next < count) 197 { 198 Pair pair = new Pair(getArgAsObject(next++), nil); 199 if (last == null) 200 list = pair; 201 else 202 last.cdr = pair; 203 last = pair; 204 } 205 return list; 206 } 207 208 211 public void lastArg() 212 { 213 if (next < count) 214 throw new WrongArguments(null, count); 215 values = null; 216 } 217 218 public Object [] getArgs() 219 { 220 if (where == 0) 221 return values; 222 else 223 { 224 int n = count; 225 next = 0; 226 Object [] args = new Object [n]; 227 for (int i = 0; i < n; i++) 228 args[i] = getNextArg(); 229 return args; 230 } 231 } 232 233 public void runUntilDone() throws Throwable 234 { 235 for (;;) 236 { 237 Procedure proc = this.proc; 238 if (proc == null) 239 { 240 248 break; 249 } 250 this.proc = null; 251 proc.apply(this); 252 } 253 } 254 255 262 public final int startFromContext () 263 { 264 ValueStack vst = vstack; 265 int oindex = vst.find(consumer); 266 vst.ensureSpace(3); 267 int gapStart = vst.gapStart; 268 vst.data[gapStart++] = TreeList.INT_FOLLOWS; 269 vst.setIntN(gapStart, oindex); 270 gapStart += 2; 271 consumer = vst; 272 vst.gapStart = gapStart; 273 return gapStart; 274 } 275 276 278 public final Object getFromContext (int oldIndex) throws Throwable 279 { 280 runUntilDone(); 281 ValueStack vst = vstack; 282 Object result = Values.make(vst, oldIndex, vst.gapStart); 283 cleanupFromContext(oldIndex); 284 return result; 285 } 286 287 294 public final void cleanupFromContext (int oldIndex) throws Throwable 295 { 296 ValueStack vst = vstack; 297 char[] data = vst.data; 298 int oindex = (data[oldIndex-2] << 16) | (data[oldIndex -1] & 0xFFFF); 299 consumer = (Consumer) vst.objects[oindex]; 300 vst.objects[oindex] = null; 301 vst.oindex = oindex; 302 vst.gapStart = oldIndex - 3; 303 } 304 305 306 public final Object runUntilValue() throws Throwable 307 { 308 Consumer consumerSave = consumer; 309 ValueStack vst = vstack; 310 consumer = vst; 311 int dindexSave = vst.gapStart; 312 int oindexSave = vst.oindex; 313 try 314 { 315 runUntilDone(); 316 return Values.make(vst, dindexSave, vst.gapStart); 317 } 318 finally 319 { 320 consumer = consumerSave; 321 vst.gapStart = dindexSave; 322 vst.oindex = oindexSave; 323 } 324 } 325 326 327 public final void runUntilValue(Consumer out) throws Throwable 328 { 329 Consumer consumerSave = consumer; 330 consumer = out; 331 try 332 { 333 runUntilDone(); 334 } 335 finally 336 { 337 consumer = consumerSave; 338 } 339 } 340 341 342 public void writeValue(Object value) 343 { 344 Values.writeValues(value, consumer); 345 } 346 347 protected String baseUri; 348 protected static String baseUriDefault; 349 350 public static String getBaseUriDefault () 351 { 352 String uri = baseUriDefault; 353 if (uri == null) 354 { 355 356 uri = new java.io.File ("").toURI().toString(); 357 358 368 baseUriDefault = uri; 369 } 370 return uri; 371 } 372 373 public String getBaseUriRaw () 374 { 375 return baseUri; 376 } 377 378 380 public String getBaseUri () 381 { 382 String uri = baseUri; 383 if (uri == null) 384 baseUri = uri = getBaseUriDefault(); 385 return uri; 386 } 387 388 389 public void setBaseUri (String baseUri) 390 { 391 this.baseUri = baseUri; 392 } 393 394 396 Location[] pushedFluids; 397 398 int pushedFluidsCount; 399 400 public final void pushFluid (Location loc) 401 { 402 Location[] fluids = pushedFluids; 403 int count = pushedFluidsCount; 404 if (fluids == null) 405 { 406 pushedFluids = fluids = new Location[10]; 407 } 408 else if (count == fluids.length) 409 { 410 Location[] newFluids = new Location[2 * count]; 411 System.arraycopy(fluids, 0, newFluids, 0, count); 412 pushedFluids = fluids = newFluids; 413 } 414 fluids[count] = loc; 415 pushedFluidsCount = count + 1; 416 } 417 418 public final void popFluid () 419 { 420 pushedFluids[--pushedFluidsCount] = null; 421 } 422 423 424 public Object [][] evalFrames; 425 } 426 427 438 | Popular Tags |