1 29 30 package com.caucho.quercus.expr; 31 32 import com.caucho.quercus.Location; 33 import com.caucho.quercus.env.Env; 34 import com.caucho.quercus.env.NullValue; 35 import com.caucho.quercus.env.Value; 36 import com.caucho.quercus.parser.QuercusParser; 37 import com.caucho.quercus.program.AbstractFunction; 38 import com.caucho.util.L10N; 39 40 import java.util.ArrayList ; 41 42 45 public class FunctionExpr extends Expr { 46 private static final L10N L = new L10N(FunctionExpr.class); 47 48 protected final String _name; 49 protected final Expr []_args; 50 51 public FunctionExpr(Location location, String name, ArrayList <Expr> args) 52 { 53 super(location); 55 _name = name.intern(); 56 57 _args = new Expr[args.size()]; 58 args.toArray(_args); 59 } 60 61 public FunctionExpr(Location location, String name, Expr []args) 62 { 63 super(location); 65 _name = name.intern(); 66 67 _args = args; 68 } 69 70 public FunctionExpr(String name, ArrayList <Expr> args) 71 { 72 this(Location.UNKNOWN, name, args); 73 } 74 75 public FunctionExpr(String name, Expr []args) 76 { 77 this(Location.UNKNOWN, name, args); 78 } 79 80 83 public String getName() 84 { 85 return _name; 86 } 87 88 91 public String getFunctionLocation() 92 { 93 return " [" + _name + "]"; 94 } 95 96 100 @Override 101 public Expr createRef(QuercusParser parser) 102 { 103 return parser.getFactory().createRef(this); 104 } 105 106 110 @Override 111 public Expr createCopy(ExprFactory factory) 112 { 113 return this; 114 } 115 116 123 public Value eval(Env env) 124 { 125 return evalImpl(env, false, false); 126 } 127 128 135 public Value evalRef(Env env) 136 { 137 return evalImpl(env, true, false); 138 } 139 140 147 public Value evalCopy(Env env) 148 { 149 return evalImpl(env, false, true); 150 } 151 152 159 private Value evalImpl(Env env, boolean isRef, boolean isCopy) 160 { 161 AbstractFunction fun = env.findFunction(_name); 162 163 if (fun == null) { 164 env.error(getLocationLine(), L.l("'{0}' is an unknown function.", _name)); 165 166 return NullValue.NULL; 167 } 168 169 Value []args = fun.evalArguments(env, this, _args); 170 171 env.pushCall(this, NullValue.NULL); 172 try { 173 env.checkTimeout(); 174 175 if (isRef) 176 return fun.callRef(env, args); 177 else if (isCopy) 178 return fun.callCopy(env, args); 179 else 180 return fun.call(env, args); 181 } finally { 182 env.popCall(); 183 } 184 } 185 186 public String toString() 187 { 188 return _name + "()"; 189 } 190 } 191 192 | Popular Tags |