1 | /* |
---|
2 | * CompiledClosure.java |
---|
3 | * |
---|
4 | * Copyright (C) 2004-2005 Peter Graves |
---|
5 | * $Id: CompiledClosure.java 12398 2010-01-24 21:59:56Z ehuelsmann $ |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU General Public License |
---|
9 | * as published by the Free Software Foundation; either version 2 |
---|
10 | * of the License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
20 | * |
---|
21 | * As a special exception, the copyright holders of this library give you |
---|
22 | * permission to link this library with independent modules to produce an |
---|
23 | * executable, regardless of the license terms of these independent |
---|
24 | * modules, and to copy and distribute the resulting executable under |
---|
25 | * terms of your choice, provided that you also meet, for each linked |
---|
26 | * independent module, the terms and conditions of the license of that |
---|
27 | * module. An independent module is a module which is not derived from |
---|
28 | * or based on this library. If you modify this library, you may extend |
---|
29 | * this exception to your version of the library, but you are not |
---|
30 | * obligated to do so. If you do not wish to do so, delete this |
---|
31 | * exception statement from your version. |
---|
32 | */ |
---|
33 | |
---|
34 | package org.armedbear.lisp; |
---|
35 | |
---|
36 | import static org.armedbear.lisp.Lisp.*; |
---|
37 | |
---|
38 | public class CompiledClosure extends Closure |
---|
39 | implements Cloneable |
---|
40 | { |
---|
41 | |
---|
42 | public ClosureBinding[] ctx; |
---|
43 | |
---|
44 | public CompiledClosure(Parameter[] required, |
---|
45 | Parameter[] optional, |
---|
46 | Parameter[] keyword, |
---|
47 | Symbol keys, Symbol rest, Symbol moreKeys) |
---|
48 | { |
---|
49 | super(required, optional, keyword, keys, rest, moreKeys); |
---|
50 | } |
---|
51 | |
---|
52 | |
---|
53 | public CompiledClosure(LispObject lambdaList) |
---|
54 | { |
---|
55 | super(list(Symbol.LAMBDA, lambdaList), null); |
---|
56 | } |
---|
57 | |
---|
58 | final public CompiledClosure setContext(ClosureBinding[] context) |
---|
59 | { |
---|
60 | ctx = context; |
---|
61 | return this; |
---|
62 | } |
---|
63 | |
---|
64 | final public CompiledClosure dup() |
---|
65 | { |
---|
66 | CompiledClosure result = null; |
---|
67 | try { |
---|
68 | result = (CompiledClosure)super.clone(); |
---|
69 | } catch (CloneNotSupportedException e) { |
---|
70 | } |
---|
71 | return result; |
---|
72 | } |
---|
73 | |
---|
74 | @Override |
---|
75 | public LispObject typep(LispObject typeSpecifier) |
---|
76 | { |
---|
77 | if (typeSpecifier == Symbol.COMPILED_FUNCTION) |
---|
78 | return T; |
---|
79 | return super.typep(typeSpecifier); |
---|
80 | } |
---|
81 | |
---|
82 | private final LispObject notImplemented() |
---|
83 | { |
---|
84 | return error(new WrongNumberOfArgumentsException(this)); |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | // Zero args. |
---|
89 | public LispObject execute() |
---|
90 | { |
---|
91 | LispObject[] args = new LispObject[0]; |
---|
92 | return execute(args); |
---|
93 | } |
---|
94 | |
---|
95 | // One arg. |
---|
96 | public LispObject execute( LispObject first) |
---|
97 | |
---|
98 | { |
---|
99 | LispObject[] args = new LispObject[1]; |
---|
100 | args[0] = first; |
---|
101 | return execute(args); |
---|
102 | } |
---|
103 | |
---|
104 | // Two args. |
---|
105 | public LispObject execute( LispObject first, |
---|
106 | LispObject second) |
---|
107 | |
---|
108 | { |
---|
109 | LispObject[] args = new LispObject[2]; |
---|
110 | args[0] = first; |
---|
111 | args[1] = second; |
---|
112 | return execute(args); |
---|
113 | } |
---|
114 | |
---|
115 | // Three args. |
---|
116 | public LispObject execute( LispObject first, |
---|
117 | LispObject second, LispObject third) |
---|
118 | |
---|
119 | { |
---|
120 | LispObject[] args = new LispObject[3]; |
---|
121 | args[0] = first; |
---|
122 | args[1] = second; |
---|
123 | args[2] = third; |
---|
124 | return execute(args); |
---|
125 | } |
---|
126 | |
---|
127 | // Four args. |
---|
128 | public LispObject execute( LispObject first, |
---|
129 | LispObject second, LispObject third, |
---|
130 | LispObject fourth) |
---|
131 | |
---|
132 | { |
---|
133 | LispObject[] args = new LispObject[4]; |
---|
134 | args[0] = first; |
---|
135 | args[1] = second; |
---|
136 | args[2] = third; |
---|
137 | args[3] = fourth; |
---|
138 | return execute(args); |
---|
139 | } |
---|
140 | |
---|
141 | // Five args. |
---|
142 | public LispObject execute( LispObject first, |
---|
143 | LispObject second, LispObject third, |
---|
144 | LispObject fourth, LispObject fifth) |
---|
145 | |
---|
146 | { |
---|
147 | LispObject[] args = new LispObject[5]; |
---|
148 | args[0] = first; |
---|
149 | args[1] = second; |
---|
150 | args[2] = third; |
---|
151 | args[3] = fourth; |
---|
152 | args[4] = fifth; |
---|
153 | return execute(args); |
---|
154 | } |
---|
155 | |
---|
156 | // Six args. |
---|
157 | public LispObject execute( LispObject first, |
---|
158 | LispObject second, LispObject third, |
---|
159 | LispObject fourth, LispObject fifth, |
---|
160 | LispObject sixth) |
---|
161 | |
---|
162 | { |
---|
163 | LispObject[] args = new LispObject[6]; |
---|
164 | args[0] = first; |
---|
165 | args[1] = second; |
---|
166 | args[2] = third; |
---|
167 | args[3] = fourth; |
---|
168 | args[4] = fifth; |
---|
169 | args[5] = sixth; |
---|
170 | return execute(args); |
---|
171 | } |
---|
172 | |
---|
173 | // Seven args. |
---|
174 | public LispObject execute( LispObject first, |
---|
175 | LispObject second, LispObject third, |
---|
176 | LispObject fourth, LispObject fifth, |
---|
177 | LispObject sixth, LispObject seventh) |
---|
178 | |
---|
179 | { |
---|
180 | LispObject[] args = new LispObject[7]; |
---|
181 | args[0] = first; |
---|
182 | args[1] = second; |
---|
183 | args[2] = third; |
---|
184 | args[3] = fourth; |
---|
185 | args[4] = fifth; |
---|
186 | args[5] = sixth; |
---|
187 | args[6] = seventh; |
---|
188 | return execute(args); |
---|
189 | } |
---|
190 | |
---|
191 | // Eight args. |
---|
192 | public LispObject execute( LispObject first, |
---|
193 | LispObject second, LispObject third, |
---|
194 | LispObject fourth, LispObject fifth, |
---|
195 | LispObject sixth, LispObject seventh, |
---|
196 | LispObject eighth) |
---|
197 | |
---|
198 | { |
---|
199 | LispObject[] args = new LispObject[8]; |
---|
200 | args[0] = first; |
---|
201 | args[1] = second; |
---|
202 | args[2] = third; |
---|
203 | args[3] = fourth; |
---|
204 | args[4] = fifth; |
---|
205 | args[5] = sixth; |
---|
206 | args[6] = seventh; |
---|
207 | args[7] = eighth; |
---|
208 | return execute(args); |
---|
209 | } |
---|
210 | |
---|
211 | // Arg array. |
---|
212 | public LispObject execute(LispObject[] args) |
---|
213 | |
---|
214 | { |
---|
215 | return notImplemented(); |
---|
216 | } |
---|
217 | |
---|
218 | // ### load-compiled-function |
---|
219 | private static final Primitive LOAD_COMPILED_FUNCTION = |
---|
220 | new Primitive("load-compiled-function", PACKAGE_SYS, true, "source") |
---|
221 | { |
---|
222 | @Override |
---|
223 | public LispObject execute(LispObject arg) |
---|
224 | { |
---|
225 | String namestring = null; |
---|
226 | if (arg instanceof Pathname) |
---|
227 | namestring = ((Pathname)arg).getNamestring(); |
---|
228 | else if (arg instanceof AbstractString) |
---|
229 | namestring = arg.getStringValue(); |
---|
230 | if (namestring != null) { |
---|
231 | // Debug.trace("autoloading preloaded ... " + namestring); |
---|
232 | return AutoloadedFunctionProxy.loadPreloadedFunction(namestring); |
---|
233 | } |
---|
234 | if(arg instanceof JavaObject) { |
---|
235 | try { |
---|
236 | return loadClassBytes((byte[]) arg.javaInstance(byte[].class)); |
---|
237 | } catch(Throwable t) { |
---|
238 | Debug.trace(t); |
---|
239 | return error(new LispError("Unable to load " + arg.writeToString())); |
---|
240 | } |
---|
241 | } |
---|
242 | return error(new LispError("Unable to load " + arg.writeToString())); |
---|
243 | } |
---|
244 | }; |
---|
245 | |
---|
246 | // ### varlist |
---|
247 | private static final Primitive VARLIST = |
---|
248 | new Primitive("varlist", PACKAGE_SYS, false) |
---|
249 | { |
---|
250 | @Override |
---|
251 | public LispObject execute(LispObject arg) |
---|
252 | { |
---|
253 | if (arg instanceof Closure) |
---|
254 | return ((Closure)arg).getVariableList(); |
---|
255 | return type_error(arg, Symbol.COMPILED_FUNCTION); |
---|
256 | } |
---|
257 | }; |
---|
258 | } |
---|