1 29 30 package com.caucho.quercus.program; 31 32 import com.caucho.quercus.Quercus; 33 import com.caucho.quercus.env.Env; 34 import com.caucho.quercus.env.LongValue; 35 import com.caucho.quercus.env.Value; 36 import com.caucho.quercus.page.QuercusPage; 37 import com.caucho.vfs.BasicDependencyContainer; 38 import com.caucho.vfs.Depend; 39 import com.caucho.vfs.Path; 40 import com.caucho.vfs.PersistentDependency; 41 42 import java.util.ArrayList ; 43 import java.util.Collection ; 44 import java.util.HashMap ; 45 import java.util.Map ; 46 47 50 public class QuercusProgram { 51 private Quercus _quercus; 52 53 private QuercusPage _compiledPage; 54 55 private Path _sourceFile; 56 57 private HashMap <String ,Function> _functionMap; 58 private HashMap <String ,Function> _functionMapLowerCase 59 = new HashMap <String ,Function>(); 60 61 private HashMap <String ,InterpretedClassDef> _classMap; 62 63 private FunctionInfo _functionInfo; 64 private Statement _statement; 65 66 private ArrayList <PersistentDependency> _dependList 67 = new ArrayList <PersistentDependency>(); 68 69 private BasicDependencyContainer _depend 70 = new BasicDependencyContainer(); 71 72 79 public QuercusProgram(Quercus quercus, Path sourceFile, 80 HashMap <String , Function> functionMap, 81 HashMap <String , InterpretedClassDef> classMap, 82 FunctionInfo functionInfo, 83 Statement statement) 84 { 85 _quercus = quercus; 86 87 _sourceFile = sourceFile; 88 if (sourceFile != null) 89 addDepend(sourceFile); 90 91 _functionMap = functionMap; 92 93 for (Map.Entry <String ,Function> entry : functionMap.entrySet()) { 94 _functionMapLowerCase.put(entry.getKey().toLowerCase(), 95 entry.getValue()); 96 } 97 98 _classMap = classMap; 99 100 _functionInfo = functionInfo; 101 _statement = statement; 102 } 103 104 107 public Quercus getPhp() 108 { 109 return _quercus; 110 } 111 112 115 public Path getSourcePath() 116 { 117 return _sourceFile; 118 } 119 120 public FunctionInfo getFunctionInfo() 121 { 122 return _functionInfo; 123 } 124 125 public Statement getStatement() 126 { 127 return _statement; 128 } 129 130 133 public void addDepend(Path path) 134 { 135 Depend depend = new Depend(path); 136 137 _dependList.add(depend); 138 _depend.add(depend); 139 } 140 141 public ArrayList <PersistentDependency> getDependencyList() 142 { 143 return _dependList; 144 } 145 146 149 public boolean isModified() 150 { 151 return _depend.isModified(); 152 } 153 154 157 public QuercusPage getCompiledPage() 158 { 159 return _compiledPage; 160 } 161 162 165 public void setCompiledPage(QuercusPage page) 166 { 167 _compiledPage = page; 168 } 169 170 173 public AbstractFunction findFunction(String name) 174 { 175 AbstractFunction fun = _functionMap.get(name); 176 177 if (fun != null) 178 return fun; 179 180 if (! _quercus.isStrict()) 181 fun = _functionMapLowerCase.get(name.toLowerCase()); 182 183 return fun; 184 } 185 186 189 public Collection <Function> getFunctions() 190 { 191 return _functionMap.values(); 192 } 193 194 197 public Collection <InterpretedClassDef> getClasses() 198 { 199 return _classMap.values(); 200 } 201 202 205 public QuercusProgram createExprReturn() 206 { 207 209 if (_statement instanceof ExprStatement) { 210 ExprStatement exprStmt = (ExprStatement) _statement; 211 212 _statement = new ReturnStatement(exprStmt.getExpr()); 213 } 214 else if (_statement instanceof BlockStatement) { 215 BlockStatement blockStmt = (BlockStatement) _statement; 216 217 Statement []statements = blockStmt.getStatements(); 218 219 if (statements.length > 0 && 220 statements[0] instanceof ExprStatement) { 221 ExprStatement exprStmt 222 = (ExprStatement) statements[0]; 223 224 _statement = new ReturnStatement(exprStmt.getExpr()); 225 } 226 } 227 228 return this; 229 } 230 231 237 public Value execute(Env env) 238 { 239 Value value = _statement.execute(env); 240 241 if (value != null) 242 return value; 243 else 244 return LongValue.ONE; 245 } 246 247 250 public void init(Env env) 251 { 252 257 } 258 259 262 public void importDefinitions(Env env) 263 { 264 for (Map.Entry <String ,Function> entry : _functionMap.entrySet()) { 265 Function fun = entry.getValue(); 266 267 if (fun.isGlobal()) 268 env.addFunction(entry.getKey(), fun); 269 } 270 271 for (Map.Entry <String ,InterpretedClassDef> entry : _classMap.entrySet()) { 272 env.addClassDef(entry.getKey(), entry.getValue()); 273 } 274 } 275 276 280 public String toString() 281 { 282 return "QuercusProgram[" + _sourceFile + "]"; 283 } 284 } 285 286 | Popular Tags |