1 ! Copyright (C) 2004, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays definitions generic hashtables inspector io kernel
4 math namespaces prettyprint prettyprint.config sequences assocs
5 sequences.private strings io.styles vectors words system
6 splitting math.parser classes.tuple continuations
7 continuations.private combinators generic.math
8 classes.builtin classes compiler.units generic.standard vocabs
9 threads threads.private init kernel.private libc io.encodings
10 mirrors accessors math.order ;
13 GENERIC: error. ( error -- )
14 GENERIC: error-help ( error -- topic )
17 M: object error-help drop f ;
19 M: tuple error. describe ;
20 M: tuple error-help class ;
22 M: string error. print ;
25 error-continuation get continuation-data stack. ;
28 error-continuation get continuation-retain stack. ;
31 error-continuation get continuation-call callstack. ;
33 : :get ( variable -- value )
34 error-continuation get continuation-name assoc-stack ;
37 error-continuation get continuation-name namestack. ;
40 1- restarts get-global nth f restarts set-global restart ;
46 : restart. ( restart n -- )
48 1+ dup 3 <= [ ":" % # " " % ] [ # " :res " % ] if
53 restarts get dup empty? [
57 "The following restarts are available:" print
59 dup length [ restart. ] 2each
62 : print-error ( error -- )
63 [ error. flush ] curry
64 [ global [ "Error in print-error!" print drop ] bind ]
72 "Type :help for debugging help." print flush
75 ERROR: assert got expect ;
77 : assert= ( a b -- ) 2dup = [ 2drop ] [ assert ] if ;
79 : depth ( -- n ) datastack length ;
81 : trim-datastacks ( seq1 seq2 -- seq1' seq2' )
82 2dup [ length ] bi@ min tuck tail >r tail r> ;
84 ERROR: relative-underflow stack ;
86 M: relative-underflow summary
87 drop "Too many items removed from data stack" ;
89 ERROR: relative-overflow stack ;
91 M: relative-overflow summary
92 drop "Superfluous items pushed to data stack" ;
94 : assert-depth ( quot -- )
95 >r datastack r> swap slip >r datastack r>
96 2dup [ length ] compare {
97 { +lt+ [ trim-datastacks nip relative-underflow ] }
99 { +gt+ [ trim-datastacks drop relative-overflow ] }
102 : expired-error. ( obj -- )
103 "Object did not survive image save/load: " write third . ;
105 : io-error. ( error -- )
106 "I/O error: " write third print ;
108 : type-check-error. ( obj -- )
109 "Type check error" print
110 "Object: " write dup fourth short.
111 "Object type: " write dup fourth class .
112 "Expected type: " write third type>class . ;
114 : divide-by-zero-error. ( obj -- )
115 "Division by zero" print drop ;
117 : signal-error. ( obj -- )
118 "Operating system signal " write third . ;
120 : array-size-error. ( obj -- )
121 "Invalid array size: " write dup third .
122 "Maximum: " write fourth 1- . ;
124 : c-string-error. ( obj -- )
125 "Cannot convert to C string: " write third . ;
127 : ffi-error. ( obj -- )
129 dup third [ write ": " write ] when*
132 : heap-scan-error. ( obj -- )
133 "Cannot do next-object outside begin/end-scan" print drop ;
135 : undefined-symbol-error. ( obj -- )
136 "The image refers to a library or symbol that was not found"
137 " at load time" append print drop ;
139 : stack-underflow. ( obj name -- )
140 write " stack underflow" print drop ;
142 : stack-overflow. ( obj name -- )
143 write " stack overflow" print drop ;
145 : datastack-underflow. "Data" stack-underflow. ;
146 : datastack-overflow. "Data" stack-overflow. ;
147 : retainstack-underflow. "Retain" stack-underflow. ;
148 : retainstack-overflow. "Retain" stack-overflow. ;
151 "Memory protection fault at address " write third .h ;
154 "Unimplemented primitive" print drop ;
156 PREDICATE: kernel-error < array
158 { [ dup empty? ] [ drop f ] }
159 { [ dup first "kernel-error" = not ] [ drop f ] }
160 [ second 0 15 between? ]
165 { 0 [ expired-error. ] }
167 { 2 [ primitive-error. ] }
168 { 3 [ type-check-error. ] }
169 { 4 [ divide-by-zero-error. ] }
170 { 5 [ signal-error. ] }
171 { 6 [ array-size-error. ] }
172 { 7 [ c-string-error. ] }
174 { 9 [ heap-scan-error. ] }
175 { 10 [ undefined-symbol-error. ] }
176 { 11 [ datastack-underflow. ] }
177 { 12 [ datastack-overflow. ] }
178 { 13 [ retainstack-underflow. ] }
179 { 14 [ retainstack-overflow. ] }
180 { 15 [ memory-error. ] }
183 M: kernel-error error. dup kernel-errors case ;
185 M: kernel-error error-help kernel-errors at first ;
188 drop "No suitable method" ;
191 "Generic word " write
192 dup no-method-generic pprint
193 " does not define a method for the " write
194 dup no-method-object class pprint
196 "Allowed classes: " write dup no-method-generic order .
197 "Dispatching on object: " write no-method-object short. ;
199 M: no-math-method summary
200 drop "No suitable arithmetic method" ;
202 M: no-next-method summary
203 drop "Executing call-next-method from least-specific method" ;
205 M: inconsistent-next-method summary
206 drop "Executing call-next-method with inconsistent parameters" ;
208 M: check-method summary
209 drop "Invalid parameters for create-method" ;
211 M: no-tuple-class summary
212 drop "BOA constructors can only be defined for tuple classes" ;
214 M: bad-superclass summary
215 drop "Tuple classes can only inherit from other tuple classes" ;
218 drop "Fall-through in cond" ;
221 drop "Fall-through in case" ;
223 M: slice-error error.
224 "Cannot create slice because " write
225 slice-error-reason print ;
227 M: bounds-error summary drop "Sequence index out of bounds" ;
229 M: condition error. error>> error. ;
231 M: condition summary error>> summary ;
233 M: condition error-help error>> error-help ;
235 M: assert summary drop "Assertion failed" ;
238 "Assertion failed" print
239 standard-table-style [
242 [ expect>> [ [ "Expect:" write ] with-cell pprint-cell ] with-row ]
243 [ got>> [ [ "Got:" write ] with-cell pprint-cell ] with-row ] bi
246 M: immutable summary drop "Sequence is immutable" ;
248 M: redefine-error error.
249 "Re-definition of " write
250 redefine-error-def . ;
253 drop "Calling a deferred word before it has been defined" ;
255 M: no-compilation-unit error.
256 "Attempting to define " write
257 no-compilation-unit-definition pprint
258 " outside of a compilation unit" print ;
261 drop "Vocabulary does not exist" ;
264 drop "Memory allocation failed" ;
266 M: double-free summary
267 drop "Free failed since memory is not allocated" ;
269 M: realloc-error summary
270 drop "Memory reallocation failed" ;
272 : error-in-thread. ( thread -- )
273 "Error in thread " write
276 " (" % dup thread-name %
277 ", " % dup thread-quot unparse-short % ")" %
278 ] "" make swap write-object ":" print nl ;
281 M: thread error-in-thread ( error thread -- )
282 initial-thread get-global eq? [
286 error-thread get-global error-in-thread. print-error flush
290 M: encode-error summary drop "Character encoding error" ;
292 M: decode-error summary drop "Character decoding error" ;
294 M: no-such-slot summary drop "No such slot" ;
296 M: immutable-slot summary drop "Slot is immutable" ;
298 M: bad-create summary drop "Bad parameters to create" ;
302 : init-debugger ( -- )
303 V{ } clone set-catchstack
306 self error-thread set-global
307 continuation error-continuation set-global
310 ! VM adds this to kernel errors, so that user-space
312 "kernel-error" 6 setenv ;
316 [ init-debugger ] "debugger" add-init-hook