1 package com.icl.saxon; 2 import com.icl.saxon.expr.*; 3 4 5 11 12 public final class Bindery { 13 14 private Object [] globals; private boolean[] busy; 16 private Object [][] stack = new Object [20][]; private Object [] currentStackFrame; 18 private ParameterSet globalParameters; private int top = -1; 20 private int allocated = 0; 21 private int globalSpace = 0; 22 private int localSpace = 0; 23 24 27 28 public void allocateGlobals(int n) { 29 globalSpace = n; 30 globals = new Object [n]; 31 busy = new boolean[n]; 32 for (int i=0; i<n; i++) { 33 globals[i] = null; 34 busy[i] = false; 35 } 36 } 37 38 42 43 public void defineGlobalParameters(ParameterSet params) { 44 globalParameters = params; 45 } 46 47 55 56 public boolean useGlobalParameter(int fingerprint, Binding binding) { 57 if (globalParameters==null) return false; 58 Value val = globalParameters.get(fingerprint); 59 if (val==null) return false; 60 globals[binding.getSlotNumber()] = val; 61 return true; 62 } 63 64 70 71 public void defineGlobalVariable(Binding binding, Value value) { 72 globals[binding.getSlotNumber()] = value; 73 } 74 75 81 82 public void setExecuting(Binding binding, boolean executing) 83 throws XPathException { 84 int slot = binding.getSlotNumber(); 85 if (executing) { 86 if (busy[slot]) { 87 throw new XPathException( 88 "Circular definition of variable " + 89 binding.getVariableName()); 90 } 91 busy[slot]=true; 95 } else { 96 busy[slot]=false; 97 } 98 } 99 100 103 104 public boolean isEvaluated(Binding binding) { 105 return globals[binding.getSlotNumber()]!=null; 106 } 107 108 112 113 public void allocateLocals(int n) { 114 if (n>localSpace) { 115 localSpace = n; 116 } 117 } 118 119 122 123 public void openStackFrame(ParameterSet localParameters) { 124 if (++top >= allocated) { 125 if (allocated==stack.length) { 126 Object [][] stack2 = new Object [allocated*2][]; 127 System.arraycopy(stack, 0, stack2, 0, allocated); 128 stack = stack2; 129 } 130 currentStackFrame = new Object [localSpace+1]; 131 stack[top]=currentStackFrame; 132 allocated++; 133 } else { 134 currentStackFrame = stack[top]; 135 } 136 137 currentStackFrame[0]=localParameters; 138 for (int i=1; i<currentStackFrame.length; i++) { 139 currentStackFrame[i] = null; 140 } 141 } 142 143 146 147 public void closeStackFrame() { 148 top--; 149 currentStackFrame = (top<0 ? null : stack[top]); 150 } 151 152 160 161 public boolean useLocalParameter(int fingerprint, Binding binding) { 162 ParameterSet params = (ParameterSet)currentStackFrame[0]; 163 if (params==null) return false; 164 Value val = params.get(fingerprint); 165 currentStackFrame[binding.getSlotNumber()+1] = val; 166 return val!=null; 167 } 168 169 176 177 public Value getLocalParameter(int fingerprint) { 178 ParameterSet params = (ParameterSet)currentStackFrame[0]; 179 if (params==null) return null; 180 return (Value)params.get(fingerprint); 181 } 182 183 188 189 public void defineLocalVariable(Binding binding, Value value) { 190 if (currentStackFrame==null) { 191 throw new IllegalArgumentException ("Can't define local variable: stack is empty"); 192 } 193 currentStackFrame[binding.getSlotNumber()+1] = value; 194 } 195 196 201 202 public Value getValue(Binding binding) { 203 if (binding.isGlobal()) { 204 return (Value)globals[binding.getSlotNumber()]; 205 } else { 206 if (currentStackFrame != null) { 207 return (Value)currentStackFrame[binding.getSlotNumber()+1]; 208 } else { 209 return null; 210 } 211 } 212 } 213 214 220 221 public Value getValue( Binding binding, int frameId ) { if (binding.isGlobal()) { 223 return (Value)globals[binding.getSlotNumber()]; 224 } else { 225 Object [] theStackFrame = stack[frameId]; 226 if (theStackFrame != null) { 227 return (Value)theStackFrame[binding.getSlotNumber()+1]; 228 } else { 229 return null; 230 } 231 } 232 } 233 234 238 239 public int getFrameId() { return top; 241 } 242 243 249 250 public void assignVariable( Binding binding, Value value ) { 251 if (binding.isGlobal()) { 252 defineGlobalVariable(binding, value); 253 } else { 254 defineLocalVariable(binding, value); 255 } 256 } 257 258 } 259 260 | Popular Tags |