source: branches/0.21.x/abcl/src/org/armedbear/lisp/Function.java

Last change on this file was 12771, checked in by astalla, 15 years ago

Fixed the handling of disassemble: functions store the *load-truename* they were loaded from, and use that to try to load bytecode for disassembly. If the loading fails, NIL is returned (it crashed hard before this fix).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 11.2 KB
Line 
1/*
2 * Function.java
3 *
4 * Copyright (C) 2002-2005 Peter Graves
5 * $Id: Function.java 12771 2010-06-27 21:38:09Z astalla $
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
34package org.armedbear.lisp;
35
36import static org.armedbear.lisp.Lisp.*;
37
38public abstract class Function extends Operator
39{
40    private LispObject propertyList = NIL;
41    private int callCount;
42    private int hotCount;
43    /**
44     * The value of *load-truename* which was current when this function
45     * was loaded, used for fetching the class bytes in case of disassebly.
46     */
47    private final LispObject loadedFrom;
48
49    protected Function() {
50  LispObject loadTruename = Symbol.LOAD_TRUENAME.symbolValueNoThrow();
51  loadedFrom = loadTruename != null ? loadTruename : NIL;
52    }
53
54    public Function(String name)
55    {
56  this();
57        if (name != null) {
58            Symbol symbol = Symbol.addFunction(name.toUpperCase(), this);
59            if (cold)
60                symbol.setBuiltInFunction(true);
61            setLambdaName(symbol);
62        }
63    }
64
65    public Function(Symbol symbol, String arglist)
66    {
67  this();
68        symbol.setSymbolFunction(this);
69        if (cold)
70            symbol.setBuiltInFunction(true);
71        setLambdaName(symbol);
72        setLambdaList(new SimpleString(arglist));
73    }
74
75    public Function(Symbol symbol, String arglist, String docstring)
76    {
77  this();
78        symbol.setSymbolFunction(this);
79        if (cold)
80            symbol.setBuiltInFunction(true);
81        setLambdaName(symbol);
82        setLambdaList(new SimpleString(arglist));
83        if (docstring != null) {
84            symbol.setDocumentation(Symbol.FUNCTION,
85                                    new SimpleString(docstring));
86        }
87    }
88
89    public Function(String name, String arglist)
90    {
91        this(name);
92        setLambdaList(new SimpleString(arglist));
93    }
94
95    public Function(String name, Package pkg)
96    {
97        this(name, pkg, false);
98    }
99
100    public Function(String name, Package pkg, boolean exported)
101    {
102        this(name, pkg, exported, null, null);
103    }
104
105    public Function(String name, Package pkg, boolean exported,
106                    String arglist)
107    {
108        this(name, pkg, exported, arglist, null);
109    }
110
111    public Function(String name, Package pkg, boolean exported,
112                    String arglist, String docstring)
113    {
114  this();
115        if (arglist instanceof String)
116            setLambdaList(new SimpleString(arglist));
117        if (name != null) {
118            Symbol symbol;
119            if (exported)
120                symbol = pkg.internAndExport(name.toUpperCase());
121            else
122                symbol = pkg.intern(name.toUpperCase());
123            symbol.setSymbolFunction(this);
124            if (cold)
125                symbol.setBuiltInFunction(true);
126            setLambdaName(symbol);
127            if (docstring != null)
128                symbol.setDocumentation(Symbol.FUNCTION,
129                                        new SimpleString(docstring));
130        }
131    }
132
133    public Function(LispObject name)
134    {
135  this();
136        setLambdaName(name);
137    }
138
139    public Function(LispObject name, LispObject lambdaList)
140    {
141  this();
142        setLambdaName(name);
143        setLambdaList(lambdaList);
144    }
145
146    @Override
147    public LispObject typeOf()
148    {
149        return Symbol.FUNCTION;
150    }
151
152    @Override
153    public LispObject classOf()
154    {
155        return BuiltInClass.FUNCTION;
156    }
157
158    @Override
159    public LispObject typep(LispObject typeSpecifier)
160    {
161        if (typeSpecifier == Symbol.FUNCTION)
162            return T;
163        if (typeSpecifier == Symbol.COMPILED_FUNCTION)
164            return T;
165        if (typeSpecifier == BuiltInClass.FUNCTION)
166            return T;
167        return super.typep(typeSpecifier);
168    }
169
170    @Override
171    public final LispObject getPropertyList()
172    {
173        if (propertyList == null)
174            propertyList = NIL;
175        return propertyList;
176    }
177
178    @Override
179    public final void setPropertyList(LispObject obj)
180    {
181        if (obj == null)
182            throw new NullPointerException();
183        propertyList = obj;
184    }
185
186    public final void setClassBytes(byte[] bytes)
187    {
188        propertyList = putf(propertyList, Symbol.CLASS_BYTES,
189                            new JavaObject(bytes));
190    }
191
192    public final LispObject getClassBytes() {
193  LispObject o = getf(propertyList, Symbol.CLASS_BYTES, NIL);
194  if(o != NIL) {
195      return o;
196  } else {
197      ClassLoader c = getClass().getClassLoader();
198      if(c instanceof FaslClassLoader) {
199    final LispThread thread = LispThread.currentThread(); 
200    SpecialBindingsMark mark = thread.markSpecialBindings(); 
201    try { 
202        thread.bindSpecial(Symbol.LOAD_TRUENAME, loadedFrom); 
203        return new JavaObject(((FaslClassLoader) c).getFunctionClassBytes(this));
204    } catch(Throwable t) {
205        //This is because unfortunately getFunctionClassBytes uses
206        //Debug.assertTrue(false) to signal errors
207        if(t instanceof ControlTransfer) {
208      throw (ControlTransfer) t;
209        } else {
210      return NIL;
211        }
212    } finally { 
213        thread.resetSpecialBindings(mark); 
214    }   
215      } else {
216    return NIL;
217      }
218  }
219    }
220
221    public static final Primitive FUNCTION_CLASS_BYTES = new pf_function_class_bytes();
222    public static final class pf_function_class_bytes extends Primitive {
223  public pf_function_class_bytes() {
224      super("function-class-bytes", PACKAGE_SYS, false, "function");
225        }
226        @Override
227        public LispObject execute(LispObject arg) {
228            if (arg instanceof Function) {
229                return ((Function) arg).getClassBytes();
230      }
231            return type_error(arg, Symbol.FUNCTION);
232        }
233    }
234
235    @Override
236    public LispObject execute()
237    {
238        return error(new WrongNumberOfArgumentsException(this, 0));
239    }
240
241    @Override
242    public LispObject execute(LispObject arg)
243    {
244        return error(new WrongNumberOfArgumentsException(this, 1));
245    }
246
247    @Override
248    public LispObject execute(LispObject first, LispObject second)
249
250    {
251        return error(new WrongNumberOfArgumentsException(this, 2));
252    }
253
254    @Override
255    public LispObject execute(LispObject first, LispObject second,
256                              LispObject third)
257
258    {
259        return error(new WrongNumberOfArgumentsException(this, 3));
260    }
261
262    @Override
263    public LispObject execute(LispObject first, LispObject second,
264                              LispObject third, LispObject fourth)
265
266    {
267        return error(new WrongNumberOfArgumentsException(this, 4));
268    }
269
270    @Override
271    public LispObject execute(LispObject first, LispObject second,
272                              LispObject third, LispObject fourth,
273                              LispObject fifth)
274
275    {
276        return error(new WrongNumberOfArgumentsException(this, 5));
277    }
278
279    @Override
280    public LispObject execute(LispObject first, LispObject second,
281                              LispObject third, LispObject fourth,
282                              LispObject fifth, LispObject sixth)
283
284    {
285        return error(new WrongNumberOfArgumentsException(this, 6));
286    }
287
288    @Override
289    public LispObject execute(LispObject first, LispObject second,
290                              LispObject third, LispObject fourth,
291                              LispObject fifth, LispObject sixth,
292                              LispObject seventh)
293
294    {
295        return error(new WrongNumberOfArgumentsException(this, 7));
296    }
297
298    @Override
299    public LispObject execute(LispObject first, LispObject second,
300                              LispObject third, LispObject fourth,
301                              LispObject fifth, LispObject sixth,
302                              LispObject seventh, LispObject eighth)
303
304    {
305        return error(new WrongNumberOfArgumentsException(this, 8));
306    }
307
308    @Override
309    public LispObject execute(LispObject[] args)
310    {
311        return error(new WrongNumberOfArgumentsException(this));
312    }
313
314    @Override
315    public String writeToString()
316    {
317        LispObject name = getLambdaName();
318        if (name != null && name != NIL) {
319            StringBuffer sb = new StringBuffer("#<FUNCTION ");
320            sb.append(name.writeToString());
321            sb.append(" {");
322            sb.append(Integer.toHexString(System.identityHashCode(this)).toUpperCase());
323            sb.append("}>");
324            return sb.toString();
325        }
326        // No name.
327        LispObject lambdaList = getLambdaList();
328        if (lambdaList != null) {
329            StringBuffer sb = new StringBuffer("#<FUNCTION ");
330            sb.append("(LAMBDA ");
331            if (lambdaList == NIL) {
332                sb.append("()");
333            } else {
334                final LispThread thread = LispThread.currentThread();
335                final SpecialBindingsMark mark = thread.markSpecialBindings();
336                thread.bindSpecial(Symbol.PRINT_LENGTH, Fixnum.THREE);
337                try {
338                    sb.append(lambdaList.writeToString());
339                }
340                finally {
341                    thread.resetSpecialBindings(mark);
342                }
343            }
344            sb.append(")");
345            sb.append(" {");
346            sb.append(Integer.toHexString(System.identityHashCode(this)).toUpperCase());
347            sb.append("}>");
348            return sb.toString();
349        }
350        return unreadableString("FUNCTION");
351    }
352
353    // Used by the JVM compiler.
354    public final void argCountError()
355    {
356        error(new WrongNumberOfArgumentsException(this));
357    }
358
359    // Profiling.
360    @Override
361    public final int getCallCount()
362    {
363        return callCount;
364    }
365
366    @Override
367    public void setCallCount(int n)
368    {
369        callCount = n;
370    }
371
372    @Override
373    public final void incrementCallCount()
374    {
375        ++callCount;
376    }
377
378    @Override
379    public final int getHotCount()
380    {
381        return hotCount;
382    }
383
384    @Override
385    public void setHotCount(int n)
386    {
387        hotCount = n;
388    }
389
390    @Override
391    public final void incrementHotCount()
392    {
393        ++hotCount;
394    }
395}
Note: See TracBrowser for help on using the repository browser.