source: trunk/abcl/src/org/armedbear/lisp/TypeError.java

Last change on this file was 15519, checked in by Mark Evenson, 3 years ago

Fix and standardize all error reader functions

Fixes #388, replaces #389. The reader function stream-error-stream
signals a type-error if it's applied to conditions that's typep to
stream-error, but whose class is a subtype to stream-error:

`lisp
(define-condition broken-error (stream-error) ())

BROKEN-ERROR

(stream-error-stream (make-condition 'stream-error :stream (make-string-input-stream "foo")))

#S(SYSTEM::STRING-INPUT-STREAM)

(stream-error-stream (make-condition 'broken-error :stream (make-string-input-stream "foo")))

#<THREAD "interpreter" {2075326}>: Debugger invoked on condition of type TYPE-ERROR

The value #<BROKEN-ERROR {75A83798}> is not of type STREAM-ERROR.

`

This fault exists within these reader functions:

  • file-error-pathname (almost -- instead of a type-error, the function returns NIL instead.)
  • package-error-package
  • arithmetic-error-operation
  • arithmetic-error-operands

The fault exists because the Java code checks if its argument is
instanceof its respective Java class. So, the method will fail on any
conditions defined in CL that subclass these errors.

Furthermore, some reader functions only check if they're instanceof
StandardObject?, which allows any CLOS object to succeed as long as the
slot the primitive method is looking for exists:

  • cell-error-name
  • type-error-datum
  • type-error-expected-type

Finally, some reader functions exist in standalone Java files, while
others reside in its condition's class file, adding to some
organizational clutter.

This change fills two main jobs:

  • Standardize all reader functions to the same behavior: If the argument is typep to the right condition, return the right slot value. Otherwise, signal a type error.
  • Move standalone reader Java code to the files of errors they apply to.

As an aside, this change would make ABCL's reader functions conformant
to a wave of proposals for WSCL:

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.4 KB
Line 
1/*
2 * TypeError.java
3 *
4 * Copyright (C) 2002-2005 Peter Graves
5 * $Id: TypeError.java 15519 2021-09-19 08:54:57Z 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
36import static org.armedbear.lisp.Lisp.*;
37
38public class TypeError extends LispError
39{
40    public TypeError()
41    {
42        super(StandardClass.TYPE_ERROR);
43    }
44
45    protected TypeError(LispClass cls)
46    {
47        super(cls);
48    }
49
50    public TypeError(LispObject datum, LispObject expectedType)
51
52    {
53        super(StandardClass.TYPE_ERROR);
54        setDatum(datum);
55        setExpectedType(expectedType);
56    }
57
58    public TypeError(LispObject initArgs)
59    {
60        super(StandardClass.TYPE_ERROR);
61        initialize(initArgs);
62    }
63
64    @Override
65    protected void initialize(LispObject initArgs)
66    {
67        super.initialize(initArgs);
68        LispObject datum = null;
69        LispObject expectedType = null;
70        LispObject first, second;
71        while (initArgs != NIL) {
72            first = initArgs.car();
73            initArgs = initArgs.cdr();
74            second = initArgs.car();
75            initArgs = initArgs.cdr();
76            if (first == Keyword.DATUM) {
77                if (datum == null)
78                    datum = second;
79            } else if (first == Keyword.EXPECTED_TYPE) {
80                if (expectedType == null)
81                    expectedType = second;
82            }
83        }
84        if (datum != null)
85            setDatum(datum);
86        if (expectedType != null)
87            setExpectedType(expectedType);
88    }
89
90    public TypeError(String message)
91    {
92        super(StandardClass.TYPE_ERROR);
93        setFormatControl(message);
94        setDatum(NIL);
95        setExpectedType(NIL);
96    }
97
98    public TypeError(String message, LispObject datum, LispObject expectedType)
99
100    {
101        super(StandardClass.TYPE_ERROR);
102        setFormatControl(message);
103        setDatum(datum);
104        setExpectedType(expectedType);
105    }
106
107    @Override
108    public LispObject typeOf()
109    {
110        return Symbol.TYPE_ERROR;
111    }
112
113    @Override
114    public LispObject classOf()
115    {
116        return StandardClass.TYPE_ERROR;
117    }
118
119    @Override
120    public LispObject typep(LispObject type)
121    {
122        if (type == Symbol.TYPE_ERROR)
123            return T;
124        if (type == StandardClass.TYPE_ERROR)
125            return T;
126        return super.typep(type);
127    }
128
129    @Override
130    public String getMessage()
131    {
132        final LispThread thread = LispThread.currentThread();
133        final SpecialBindingsMark mark = thread.markSpecialBindings();
134        thread.bindSpecial(Symbol.PRINT_ESCAPE, T);
135        try {
136            String s = super.getMessage();
137            if (s != null)
138                return s;
139            final LispObject datum = getDatum();
140            final LispObject expectedType = getExpectedType();
141            StringBuilder sb = new StringBuilder();
142            String name = datum != null ? datum.princToString() : null;
143            String type = null;
144            if (expectedType != null)
145                type = expectedType.princToString();
146            if (type != null) {
147                if (name != null) {
148                    sb.append("The value ");
149                    sb.append(name);
150                } else
151                    sb.append("Value");
152                sb.append(" is not of type ");
153                sb.append(type);
154            } else if (name != null) {
155                sb.append("Wrong type: ");
156                sb.append(name);
157            }
158            sb.append('.');
159            return sb.toString();
160        }
161        finally {
162            thread.resetSpecialBindings(mark);
163        }
164    }
165
166    public final LispObject getDatum()
167    {
168        return getInstanceSlotValue(Symbol.DATUM);
169    }
170
171    private final void setDatum(LispObject datum)
172    {
173        setInstanceSlotValue(Symbol.DATUM, datum);
174    }
175
176    public final LispObject getExpectedType()
177    {
178        return getInstanceSlotValue(Symbol.EXPECTED_TYPE);
179    }
180
181    private final void setExpectedType(LispObject expectedType)
182
183    {
184        setInstanceSlotValue(Symbol.EXPECTED_TYPE, expectedType);
185    }
186
187    // ### type-error-datum
188    private static final Primitive TYPE_ERROR_DATUM =
189        new Primitive(Symbol.TYPE_ERROR_DATUM, "condition")
190    {
191        @Override
192        public LispObject execute(LispObject arg)
193        {
194            if (arg.typep(Symbol.TYPE_ERROR) == NIL) {
195                return type_error(arg, Symbol.TYPE_ERROR);
196            }
197
198            final StandardObject obj = (StandardObject) arg;
199            return obj.getInstanceSlotValue(Symbol.DATUM);
200        }
201    };
202
203    // ### type-error-expected-type
204    private static final Primitive TYPE_ERROR_EXPECTED_TYPE =
205        new Primitive(Symbol.TYPE_ERROR_EXPECTED_TYPE, "condition")
206    {
207        @Override
208        public LispObject execute(LispObject arg)
209        {
210            if (arg.typep(Symbol.TYPE_ERROR) == NIL) {
211                return type_error(arg, Symbol.TYPE_ERROR);
212            }
213
214            final StandardObject obj = (StandardObject) arg;
215            return obj.getInstanceSlotValue(Symbol.EXPECTED_TYPE);
216        }
217    };
218}
Note: See TracBrowser for help on using the repository browser.