KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > lib > FunctionModule


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.lib;
31
32 import com.caucho.quercus.Quercus;
33 import com.caucho.quercus.QuercusException;
34 import com.caucho.quercus.annotation.VariableArguments;
35 import com.caucho.quercus.env.*;
36 import com.caucho.quercus.module.AbstractQuercusModule;
37 import com.caucho.util.L10N;
38
39 import java.io.IOException JavaDoc;
40 import java.util.logging.Level JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42
43 /**
44  * PHP function routines.
45  */

46 public class FunctionModule extends AbstractQuercusModule {
47   private static final L10N L = new L10N(FunctionModule.class);
48
49   private static final Logger JavaDoc log
50     = Logger.getLogger(FunctionModule.class.getName());
51
52   /**
53    * Calls a user function
54    */

55   public static Value call_user_func(Env env,
56                                      Callback function,
57                                      Value []args)
58   {
59     return function.call(env, args).copyReturn();
60   }
61
62   /**
63    * Calls a user function
64    */

65   public static Value call_user_func_array(Env env,
66                                            Callback function,
67                                            Value arg)
68   {
69     ArrayValue argArray;
70
71     if (arg instanceof ArrayValue)
72       argArray = (ArrayValue) arg;
73     else
74       argArray = new ArrayValueImpl().append(arg);
75
76     Value []args;
77
78     if (argArray != null) {
79       args = new Value[argArray.getSize()];
80
81       int i = 0;
82       for (ArrayValue.Entry head = argArray.getHead();
83        head != null;
84        head = head.getNext()) {
85     args[i++] = head.getRawValue(); // php/1c09
86
}
87     }
88     else
89       args = new Value[0];
90
91     return function.call(env, args).copyReturn();
92   }
93
94   /**
95    * Creates an anonymous function
96    */

97   public static Value create_function(Env env,
98                                       String JavaDoc args,
99                                       String JavaDoc code)
100   {
101     if (log.isLoggable(Level.FINER))
102       log.finer(code);
103
104     try {
105       Quercus quercus = env.getQuercus();
106       
107       return quercus.parseFunction(args, code);
108     } catch (IOException JavaDoc e) {
109       throw new QuercusException(e);
110     }
111   }
112
113   /**
114    * Returns an array of the defined functions
115    */

116   public static Value get_defined_functions(Env env)
117   {
118     return env.getDefinedFunctions();
119   }
120
121   /**
122    * Returns the nth function argument.
123    */

124   @VariableArguments
125   public static Value func_get_arg(Env env, int index)
126   {
127     Value []args = env.getFunctionArgs();
128
129     if (0 <= index && index < args.length)
130       return args[index];
131     else {
132       // XXX: warning
133
return NullValue.NULL;
134     }
135   }
136
137   /**
138    * Returns the function arguments as an array.
139    */

140   @VariableArguments
141   public static Value func_get_args(Env env)
142   {
143     Value []args = env.getFunctionArgs();
144
145     ArrayValue result = new ArrayValueImpl();
146     for (int i = 0; i < args.length; i++)
147       result.put(args[i]);
148
149     return result;
150   }
151
152   /**
153    * Returns the number of arguments to the function.
154    */

155   @VariableArguments
156   public static Value func_num_args(Env env)
157   {
158     Value []args = env.getFunctionArgs();
159
160     if (args != null && args.length > 0)
161       return new LongValue(args.length);
162     else
163       return LongValue.ZERO;
164   }
165
166   /**
167    * Returns true if the function exists.
168    *
169    * @param env the PHP environment
170    * @param name the function name
171    */

172   public static boolean function_exists(Env env, String JavaDoc name)
173   {
174     return env.findFunction(name) != null;
175   }
176
177   /**
178    * Registers a shutdown function.
179    */

180   public static Value register_shutdown_function(Env env,
181                          Callback fun,
182                          Value []args)
183   {
184     env.addShutdown(fun, args);
185
186     return NullValue.NULL;
187   }
188 }
189
Popular Tags