KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > program > AbstractFunction


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.quercus.program;
31
32 import com.caucho.quercus.Location;
33 import com.caucho.quercus.env.Env;
34 import com.caucho.quercus.env.Value;
35 import com.caucho.quercus.expr.Expr;
36 import com.caucho.util.L10N;
37
38 /**
39  * Represents a function
40  */

41 abstract public class AbstractFunction {
42   private static final L10N L = new L10N(AbstractFunction.class);
43
44   private static final Arg []NULL_ARGS = new Arg[0];
45   private static final Value []NULL_ARG_VALUES = new Value[0];
46
47   private final Location _location;
48
49   private boolean _isGlobal = true;
50
51   public AbstractFunction()
52   {
53     // XXX:
54
_location = Location.UNKNOWN;
55   }
56
57   public AbstractFunction(Location location)
58   {
59     _location = location;
60   }
61
62   public String JavaDoc getName()
63   {
64     return "unknown";
65   }
66
67   /**
68    * Returns true for a global function.
69    */

70   public final boolean isGlobal()
71   {
72     return _isGlobal;
73   }
74
75   /**
76    * Returns true for an abstract function.
77    */

78   public boolean isAbstract()
79   {
80     return false;
81   }
82
83   public final Location getLocation()
84   {
85     return _location;
86   }
87
88   /**
89    * Returns true for a global function.
90    */

91   public final void setGlobal(boolean isGlobal)
92   {
93     _isGlobal = isGlobal;
94   }
95
96   /**
97    * Returns true for a boolean function.
98    */

99   public boolean isBoolean()
100   {
101     return false;
102   }
103
104   /**
105    * Returns true for a string function.
106    */

107   public boolean isString()
108   {
109     return false;
110   }
111
112   /**
113    * Returns true for a long function.
114    */

115   public boolean isLong()
116   {
117     return false;
118   }
119
120   /**
121    * Returns true for a double function.
122    */

123   public boolean isDouble()
124   {
125     return false;
126   }
127
128   /**
129    * Returns true if the function uses variable args.
130    */

131   public boolean isCallUsesVariableArgs()
132   {
133     return false;
134   }
135
136   /**
137    * Returns true if the function uses/modifies the local symbol table
138    */

139   public boolean isCallUsesSymbolTable()
140   {
141     return false;
142   }
143
144   /**
145    * True for a returns reference.
146    */

147   public boolean isReturnsReference()
148   {
149     return true;
150   }
151
152   /**
153    * Returns the args.
154    */

155   public Arg []getArgs()
156   {
157     return NULL_ARGS;
158   }
159
160   /**
161    * Binds the user's arguments to the actual arguments.
162    *
163    * @param args the user's arguments
164    * @return the user arguments augmented by any defaults
165    */

166   public Value []evalArguments(Env env, Expr fun, Expr []args)
167   {
168     Value[]values = new Value[args.length];
169
170     for (int i = 0; i < args.length; i++)
171       values[i] = args[i].evalArg(env);
172
173     return values;
174   }
175
176   /**
177    * Evaluates the function.
178    */

179   abstract public Value call(Env env, Value []args);
180
181   /**
182    * Evaluates the function, returning a reference.
183    */

184   public Value callRef(Env env, Value []args)
185   {
186     return call(env, args);
187   }
188
189   /**
190    * Evaluates the function, returning a copy
191    */

192   public Value callCopy(Env env, Value []args)
193   {
194     return call(env, args).copyReturn();
195   }
196   
197   /**
198    * Evaluates the function as a method call.
199    */

200   public Value callMethod(Env env, Value obj, Value []args)
201   {
202     Value oldThis = env.getThis();
203
204     try {
205       if (obj != null)
206     env.setThis(obj);
207
208       return call(env, args);
209     } finally {
210       env.setThis(oldThis);
211     }
212   }
213   
214   /**
215    * Evaluates the function as a method call, returning a reference.
216    */

217   public Value callMethodRef(Env env, Value obj, Value []args)
218   {
219     Value oldThis = env.getThis();
220
221     try {
222       env.setThis(obj);
223
224       return callRef(env, args);
225     } finally {
226       env.setThis(oldThis);
227     }
228   }
229
230   /**
231    * Evaluates the function.
232    */

233   public Value call(Env env)
234   {
235     return call(env, NULL_ARG_VALUES);
236   }
237
238   /**
239    * Evaluates the function with an argument .
240    */

241   public Value call(Env env, Value a1)
242   {
243     return call(env, new Value[] { a1 });
244   }
245
246   /**
247    * Evaluates the function with arguments
248    */

249   public Value call(Env env, Value a1, Value a2)
250   {
251     return call(env, new Value[] { a1, a2 });
252   }
253
254   /**
255    * Evaluates the function with arguments
256    */

257   public Value call(Env env, Value a1, Value a2, Value a3)
258   {
259     return call(env, new Value[] { a1, a2, a3 });
260   }
261
262   /**
263    * Evaluates the function with arguments
264    */

265   public Value call(Env env, Value a1, Value a2, Value a3, Value a4)
266   {
267     return call(env, new Value[] { a1, a2, a3, a4 });
268   }
269
270   /**
271    * Evaluates the function with arguments
272    */

273   public Value call(Env env, Value a1, Value a2, Value a3, Value a4, Value a5)
274   {
275     return call(env, new Value[] { a1, a2, a3, a4, a5 });
276   }
277
278   /**
279    * Evaluates the function.
280    */

281   public Value call(Env env, Expr []exprs)
282   {
283     Value []argValues = new Value[exprs.length];
284     Arg []args = getArgs();
285
286     for (int i = 0; i < exprs.length; i++) {
287       // quercus/0d19
288
if (i < args.length && args[i].isReference())
289     argValues[i] = exprs[i].evalArg(env);
290       else
291     argValues[i] = exprs[i].eval(env);
292     }
293
294     return call(env, argValues);
295   }
296
297   /**
298    * Evaluates the function.
299    */

300   public Value callCopy(Env env, Expr []exprs)
301   {
302     return call(env, exprs).copy();
303   }
304
305   /**
306    * Evaluates the function.
307    */

308   public Value callRef(Env env)
309   {
310     return callRef(env, NULL_ARG_VALUES);
311   }
312
313   /**
314    * Evaluates the function with an argument .
315    */

316   public Value callRef(Env env, Value a1)
317   {
318     return callRef(env, new Value[] { a1 });
319   }
320
321   /**
322    * Evaluates the function with arguments
323    */

324   public Value callRef(Env env, Value a1, Value a2)
325   {
326     return callRef(env, new Value[] { a1, a2 });
327   }
328
329   /**
330    * Evaluates the function with arguments
331    */

332   public Value callRef(Env env, Value a1, Value a2, Value a3)
333   {
334     return callRef(env, new Value[] { a1, a2, a3 });
335   }
336
337   /**
338    * Evaluates the function with arguments
339    */

340   public Value callRef(Env env, Value a1, Value a2, Value a3, Value a4)
341   {
342     return callRef(env, new Value[] { a1, a2, a3, a4 });
343   }
344
345   /**
346    * Evaluates the function with arguments
347    */

348   public Value callRef(Env env,
349                Value a1, Value a2, Value a3, Value a4, Value a5)
350   {
351     return callRef(env, new Value[] { a1, a2, a3, a4, a5 });
352   }
353
354   /**
355    * Evaluates the function.
356    */

357   public Value callRef(Env env, Expr []exprs)
358   {
359     Value []argValues = new Value[exprs.length];
360     Arg []args = getArgs();
361
362     for (int i = 0; i < exprs.length; i++) {
363       // quercus/0d19
364
if (i < args.length && args[i].isReference())
365     argValues[i] = exprs[i].evalArg(env);
366       else
367     argValues[i] = exprs[i].eval(env);
368     }
369
370     return callRef(env, argValues);
371   }
372
373   /**
374    * Evaluates the function as a method call.
375    */

376   public Value callMethod(Env env, Value obj)
377   {
378     return callMethod(env, obj, NULL_ARG_VALUES);
379   }
380
381   /**
382    * Evaluates the function as a method call.
383    */

384   public Value callMethod(Env env, Value obj, Value a1)
385   {
386     return callMethod(env, obj, new Value[] { a1 });
387   }
388
389   /**
390    * Evaluates the function as a method call.
391    */

392   public Value callMethod(Env env, Value obj, Value a1, Value a2)
393   {
394     return callMethod(env, obj, new Value[] { a1, a2 });
395   }
396
397   /**
398    * Evaluates the function as a method call.
399    */

400   public Value callMethod(Env env, Value obj,
401               Value a1, Value a2, Value a3)
402   {
403     return callMethod(env, obj, new Value[] { a1, a2, a3 });
404   }
405
406   /**
407    * Evaluates the function as a method call.
408    */

409   public Value callMethod(Env env, Value obj,
410               Value a1, Value a2, Value a3, Value a4)
411   {
412     return callMethod(env, obj, new Value[] { a1, a2, a3, a4 });
413   }
414
415   /**
416    * Evaluates the function as a method call.
417    */

418   public Value callMethod(Env env, Value obj,
419               Value a1, Value a2, Value a3, Value a4, Value a5)
420   {
421     return callMethod(env, obj, new Value[] { a1, a2, a3, a4, a5 });
422   }
423
424   /**
425    * Evaluates the function.
426    */

427   public Value callMethod(Env env, Value obj, Expr []exprs)
428   {
429     Value []argValues = new Value[exprs.length];
430     Arg []args = getArgs();
431
432     for (int i = 0; i < exprs.length; i++) {
433       if (i < args.length && args[i].isReference()) {
434     argValues[i] = exprs[i].evalArg(env);
435       }
436       else
437     argValues[i] = exprs[i].eval(env);
438     }
439
440     return callMethod(env, obj, argValues);
441   }
442
443   /**
444    * Evaluates the function as a method call.
445    */

446   public Value callMethodRef(Env env, Value obj)
447   {
448     return callMethodRef(env, obj, NULL_ARG_VALUES);
449   }
450
451   /**
452    * Evaluates the function as a method call.
453    */

454   public Value callMethodRef(Env env, Value obj, Value a1)
455   {
456     return callMethodRef(env, obj, new Value[] { a1 });
457   }
458
459   /**
460    * Evaluates the function as a method call.
461    */

462   public Value callMethodRef(Env env, Value obj, Value a1, Value a2)
463   {
464     return callMethodRef(env, obj, new Value[] { a1, a2 });
465   }
466
467   /**
468    * Evaluates the function as a method call.
469    */

470   public Value callMethodRef(Env env, Value obj,
471                  Value a1, Value a2, Value a3)
472   {
473     return callMethodRef(env, obj, new Value[] { a1, a2, a3 });
474   }
475
476   /**
477    * Evaluates the function as a method call.
478    */

479   public Value callMethodRef(Env env, Value obj,
480                  Value a1, Value a2, Value a3, Value a4)
481   {
482     return callMethodRef(env, obj, new Value[] { a1, a2, a3, a4 });
483   }
484
485   /**
486    * Evaluates the function as a method call.
487    */

488   public Value callMethodRef(Env env, Value obj,
489                  Value a1, Value a2, Value a3, Value a4, Value a5)
490   {
491     return callMethodRef(env, obj, new Value[] { a1, a2, a3, a4, a5 });
492   }
493
494   /**
495    * Evaluates the function.
496    */

497   public Value callMethodRef(Env env, Value obj, Expr []exprs)
498   {
499     Value []argValues = new Value[exprs.length];
500     Arg []args = getArgs();
501
502     for (int i = 0; i < exprs.length; i++) {
503       if (i < args.length && args[i].isReference())
504     argValues[i] = exprs[i].evalArg(env);
505       else
506     argValues[i] = exprs[i].eval(env);
507     }
508
509     return callMethodRef(env, obj, argValues);
510   }
511 }
512
513
Popular Tags