| 1 | /* |
|---|
| 2 | * CompiledClosure.java |
|---|
| 3 | * |
|---|
| 4 | * Copyright (C) 2004-2005 Peter Graves |
|---|
| 5 | * $Id: CompiledClosure.java 14023 2012-07-29 20:04:53Z 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(ArgumentListProcessor arglist) |
|---|
| 45 | { |
|---|
| 46 | super(arglist); |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | public CompiledClosure(LispObject lambdaList) |
|---|
| 51 | { |
|---|
| 52 | super(list(Symbol.LAMBDA, lambdaList), null); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | final public CompiledClosure setContext(ClosureBinding[] context) |
|---|
| 56 | { |
|---|
| 57 | ctx = context; |
|---|
| 58 | return this; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | final public CompiledClosure dup() |
|---|
| 62 | { |
|---|
| 63 | CompiledClosure result = null; |
|---|
| 64 | try { |
|---|
| 65 | result = (CompiledClosure)super.clone(); |
|---|
| 66 | } catch (CloneNotSupportedException e) { |
|---|
| 67 | } |
|---|
| 68 | return result; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | @Override |
|---|
| 72 | public LispObject typep(LispObject typeSpecifier) |
|---|
| 73 | { |
|---|
| 74 | if (typeSpecifier == Symbol.COMPILED_FUNCTION) |
|---|
| 75 | return T; |
|---|
| 76 | return super.typep(typeSpecifier); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | private final LispObject notImplemented() |
|---|
| 80 | { |
|---|
| 81 | return error(new WrongNumberOfArgumentsException(this)); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | // Zero args. |
|---|
| 86 | public LispObject execute() |
|---|
| 87 | { |
|---|
| 88 | LispObject[] args = new LispObject[0]; |
|---|
| 89 | return execute(args); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | // One arg. |
|---|
| 93 | public LispObject execute( LispObject first) |
|---|
| 94 | |
|---|
| 95 | { |
|---|
| 96 | LispObject[] args = new LispObject[1]; |
|---|
| 97 | args[0] = first; |
|---|
| 98 | return execute(args); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | // Two args. |
|---|
| 102 | public LispObject execute( LispObject first, |
|---|
| 103 | LispObject second) |
|---|
| 104 | |
|---|
| 105 | { |
|---|
| 106 | LispObject[] args = new LispObject[2]; |
|---|
| 107 | args[0] = first; |
|---|
| 108 | args[1] = second; |
|---|
| 109 | return execute(args); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | // Three args. |
|---|
| 113 | public LispObject execute( LispObject first, |
|---|
| 114 | LispObject second, LispObject third) |
|---|
| 115 | |
|---|
| 116 | { |
|---|
| 117 | LispObject[] args = new LispObject[3]; |
|---|
| 118 | args[0] = first; |
|---|
| 119 | args[1] = second; |
|---|
| 120 | args[2] = third; |
|---|
| 121 | return execute(args); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | // Four args. |
|---|
| 125 | public LispObject execute( LispObject first, |
|---|
| 126 | LispObject second, LispObject third, |
|---|
| 127 | LispObject fourth) |
|---|
| 128 | |
|---|
| 129 | { |
|---|
| 130 | LispObject[] args = new LispObject[4]; |
|---|
| 131 | args[0] = first; |
|---|
| 132 | args[1] = second; |
|---|
| 133 | args[2] = third; |
|---|
| 134 | args[3] = fourth; |
|---|
| 135 | return execute(args); |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | // Five args. |
|---|
| 139 | public LispObject execute( LispObject first, |
|---|
| 140 | LispObject second, LispObject third, |
|---|
| 141 | LispObject fourth, LispObject fifth) |
|---|
| 142 | |
|---|
| 143 | { |
|---|
| 144 | LispObject[] args = new LispObject[5]; |
|---|
| 145 | args[0] = first; |
|---|
| 146 | args[1] = second; |
|---|
| 147 | args[2] = third; |
|---|
| 148 | args[3] = fourth; |
|---|
| 149 | args[4] = fifth; |
|---|
| 150 | return execute(args); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | // Six args. |
|---|
| 154 | public LispObject execute( LispObject first, |
|---|
| 155 | LispObject second, LispObject third, |
|---|
| 156 | LispObject fourth, LispObject fifth, |
|---|
| 157 | LispObject sixth) |
|---|
| 158 | |
|---|
| 159 | { |
|---|
| 160 | LispObject[] args = new LispObject[6]; |
|---|
| 161 | args[0] = first; |
|---|
| 162 | args[1] = second; |
|---|
| 163 | args[2] = third; |
|---|
| 164 | args[3] = fourth; |
|---|
| 165 | args[4] = fifth; |
|---|
| 166 | args[5] = sixth; |
|---|
| 167 | return execute(args); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | // Seven args. |
|---|
| 171 | public LispObject execute( LispObject first, |
|---|
| 172 | LispObject second, LispObject third, |
|---|
| 173 | LispObject fourth, LispObject fifth, |
|---|
| 174 | LispObject sixth, LispObject seventh) |
|---|
| 175 | |
|---|
| 176 | { |
|---|
| 177 | LispObject[] args = new LispObject[7]; |
|---|
| 178 | args[0] = first; |
|---|
| 179 | args[1] = second; |
|---|
| 180 | args[2] = third; |
|---|
| 181 | args[3] = fourth; |
|---|
| 182 | args[4] = fifth; |
|---|
| 183 | args[5] = sixth; |
|---|
| 184 | args[6] = seventh; |
|---|
| 185 | return execute(args); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | // Eight args. |
|---|
| 189 | public LispObject execute( LispObject first, |
|---|
| 190 | LispObject second, LispObject third, |
|---|
| 191 | LispObject fourth, LispObject fifth, |
|---|
| 192 | LispObject sixth, LispObject seventh, |
|---|
| 193 | LispObject eighth) |
|---|
| 194 | |
|---|
| 195 | { |
|---|
| 196 | LispObject[] args = new LispObject[8]; |
|---|
| 197 | args[0] = first; |
|---|
| 198 | args[1] = second; |
|---|
| 199 | args[2] = third; |
|---|
| 200 | args[3] = fourth; |
|---|
| 201 | args[4] = fifth; |
|---|
| 202 | args[5] = sixth; |
|---|
| 203 | args[6] = seventh; |
|---|
| 204 | args[7] = eighth; |
|---|
| 205 | return execute(args); |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | // Arg array. |
|---|
| 209 | public LispObject execute(LispObject[] args) |
|---|
| 210 | |
|---|
| 211 | { |
|---|
| 212 | return notImplemented(); |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | // ### load-compiled-function |
|---|
| 216 | private static final Primitive LOAD_COMPILED_FUNCTION = |
|---|
| 217 | new Primitive("load-compiled-function", PACKAGE_SYS, true, "source") |
|---|
| 218 | { |
|---|
| 219 | @Override |
|---|
| 220 | public LispObject execute(LispObject arg) |
|---|
| 221 | { |
|---|
| 222 | String namestring = null; |
|---|
| 223 | if (arg instanceof Pathname) |
|---|
| 224 | namestring = ((Pathname)arg).getNamestring(); |
|---|
| 225 | else if (arg instanceof AbstractString) |
|---|
| 226 | namestring = arg.getStringValue(); |
|---|
| 227 | if(arg instanceof JavaObject) { |
|---|
| 228 | try { |
|---|
| 229 | return loadClassBytes((byte[]) arg.javaInstance(byte[].class)); |
|---|
| 230 | } catch(Throwable t) { |
|---|
| 231 | Debug.trace(t); |
|---|
| 232 | return error(new LispError("Unable to load " + arg.princToString())); |
|---|
| 233 | } |
|---|
| 234 | } |
|---|
| 235 | return error(new LispError("Unable to load " + arg.princToString())); |
|---|
| 236 | } |
|---|
| 237 | }; |
|---|
| 238 | |
|---|
| 239 | // ### varlist |
|---|
| 240 | private static final Primitive VARLIST = |
|---|
| 241 | new Primitive("varlist", PACKAGE_SYS, false) |
|---|
| 242 | { |
|---|
| 243 | @Override |
|---|
| 244 | public LispObject execute(LispObject arg) |
|---|
| 245 | { |
|---|
| 246 | if (arg instanceof Closure) |
|---|
| 247 | return ((Closure)arg).getVariableList(); |
|---|
| 248 | return type_error(arg, Symbol.COMPILED_FUNCTION); |
|---|
| 249 | } |
|---|
| 250 | }; |
|---|
| 251 | } |
|---|