source: trunk/abcl/src/org/armedbear/lisp/LispStackFrame.java @ 12105

Last change on this file since 12105 was 12105, checked in by Mark Evenson, 16 years ago

Split StackFrame? abstraction into Java and Lisp stack frames.

From the original patch/idea from Tobias Rittweiler this introduces
more information of primary interest to ABCL implemnters such as when
a form like (make-thread #'(lambda ())) is evaluated

All users of EXT:BACKTRACE-AS-LIST should now use SYS:BACKTRACE, the
results of which is a list of the new builtin classes JAVA_STACK_FRAME
or LISP_STACK_FRAME. The methods SYS:FRAME-TO-STRING and
SYS:FRAME-TO-LIST are defined to break these new objects into
inspectable parts. As a convenience, there is a SYS:BACKTRACE-AS-LIST
which calls SYS:FRAME-TO-LIST to each element of the computed
backtrace.

Refactorings have occurred on the Java side: the misnamed
LispThread?.backtrace() is now LispThread?.printBacktrace().
LispThread?.backtraceAsList() is now LispThread?.backtrace() as it is
a shorter name, and more to the point.

Java stack frames only appear after a call through Lisp.error(), which
has only the top level as a restart as an option.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.0 KB
Line 
1/*
2 * LispStackFrame.java
3 *
4 * Copyright (C) 2009 Mark Evenson
5 * $Id: LispStackFrame.java 12105 2009-08-19 14:51:56Z mevenson $
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
36public class LispStackFrame 
37  extends StackFrame
38{
39  public final LispObject operator;
40  private final LispObject first;
41  private final LispObject second;
42  private final LispObject third;
43  private final LispObject[] args;
44
45  public LispStackFrame(LispObject operator)
46  {
47    this.operator = operator;
48    first = null;
49    second = null;
50    third = null;
51    args = null;
52  }
53
54  public LispStackFrame(LispObject operator, LispObject arg)
55  {
56    this.operator = operator;
57    first = arg;
58    second = null;
59    third = null;
60    args = null;
61  }
62
63  public LispStackFrame(LispObject operator, LispObject first,
64      LispObject second)
65  {
66    this.operator = operator;
67    this.first = first;
68    this.second = second;
69    third = null;
70    args = null;
71  }
72
73  public LispStackFrame(LispObject operator, LispObject first,
74      LispObject second, LispObject third)
75
76  {
77    this.operator = operator;
78    this.first = first;
79    this.second = second;
80    this.third = third;
81    args = null;
82  }
83
84  public LispStackFrame(LispObject operator, LispObject... args)
85  {
86    this.operator = operator;
87    first = null;
88    second = null;
89    third = null;
90    final int length = args.length;
91    this.args = new LispObject[length];
92    System.arraycopy(args, 0, this.args, 0, length);
93  }
94
95   @Override
96   public LispObject typeOf() { 
97     return Symbol.LISP_STACK_FRAME; 
98   }
99 
100   @Override
101   public LispObject classOf() { 
102     return BuiltInClass.LISP_STACK_FRAME; 
103   }
104
105   @Override
106   public String writeToString() 
107   { 
108     String result = "";
109     final String LISP_STACK_FRAME = "LISP-STACK-FRAME";
110     try {
111       result =  unreadableString(LISP_STACK_FRAME + " " 
112          + toLispString().getStringValue());
113     } catch (ConditionThrowable t) {
114       Debug.trace("Implementation error: ");
115       Debug.trace(t);
116       result = unreadableString(LISP_STACK_FRAME);
117     }
118     return result;
119   }
120
121  @Override
122  public LispObject typep(LispObject typeSpecifier) 
123    throws ConditionThrowable
124  {
125    if (typeSpecifier == Symbol.LISP_STACK_FRAME)
126      return T;
127    if (typeSpecifier == BuiltInClass.LISP_STACK_FRAME)
128      return T;
129    return super.typep(typeSpecifier);
130   }
131
132  public LispObject toLispList() 
133    throws ConditionThrowable
134  {
135    LispObject result = argsToLispList();
136    if (operator instanceof Operator) {
137      LispObject lambdaName = ((Operator)operator).getLambdaName();
138      if (lambdaName != null && lambdaName != Lisp.NIL)
139  return result.push(lambdaName);
140    }
141    return result.push(operator);
142  }
143
144  private LispObject argsToLispList() 
145    throws ConditionThrowable
146  {
147    LispObject result = Lisp.NIL;
148    if (args != null) {
149      for (int i = 0; i < args.length; i++)
150  result = result.push(args[i]);
151    } else {
152      do {
153  if (first != null)
154    result = result.push(first);
155  else
156    break;
157  if (second != null)
158    result = result.push(second);
159  else
160    break;
161  if (third != null)
162    result = result.push(third);
163  else
164    break;
165      } while (false);
166    }
167    return result.nreverse();
168  }
169
170  public SimpleString toLispString() 
171    throws ConditionThrowable
172  {
173    return new SimpleString(toLispList().writeToString());
174  }
175
176  public LispObject getOperator() {
177    return operator;
178  }
179
180  @Override 
181  public LispObject getParts() 
182    throws ConditionThrowable
183  {
184    LispObject result = NIL;
185    result = result.push(new Cons("OPERATOR", getOperator()));
186    LispObject args = argsToLispList();
187    if (args != NIL) {
188      result = result.push(new Cons("ARGS", args));
189    }
190       
191    return result.nreverse();
192  }
193}
Note: See TracBrowser for help on using the repository browser.