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 sequences assocs sequences.private
5 strings io.styles vectors words system splitting math.parser
6 classes.tuple continuations continuations.private combinators
7 generic.math io.streams.duplex classes.builtin classes
8 compiler.units generic.standard vocabs threads threads.private
9 init kernel.private libc io.encodings mirrors accessors ;
12 GENERIC: error. ( error -- )
13 GENERIC: error-help ( error -- topic )
16 M: object error-help drop f ;
18 M: tuple error. describe ;
19 M: tuple error-help class ;
21 M: string error. print ;
24 error-continuation get continuation-data stack. ;
27 error-continuation get continuation-retain stack. ;
30 error-continuation get continuation-call callstack. ;
32 : :get ( variable -- value )
33 error-continuation get continuation-name assoc-stack ;
36 error-continuation get continuation-name namestack. ;
39 1- restarts get-global nth f restarts set-global restart ;
45 : restart. ( restart n -- )
47 1+ dup 3 <= [ ":" % # " " % ] [ # " :res " % ] if
52 restarts get dup empty? [
56 "The following restarts are available:" print
58 dup length [ restart. ] 2each
61 : print-error ( error -- )
62 [ error. flush ] curry
63 [ global [ "Error in print-error!" print drop ] bind ]
72 "Type :help for debugging help." print flush
73 ] error-hook set-global
76 [ error-hook get call ] recover ;
78 ERROR: assert got expect ;
80 : assert= ( a b -- ) 2dup = [ 2drop ] [ assert ] if ;
82 : depth ( -- n ) datastack length ;
84 : trim-datastacks ( seq1 seq2 -- seq1' seq2' )
85 2dup [ length ] bi@ min tuck tail >r tail r> ;
87 ERROR: relative-underflow stack ;
89 M: relative-underflow summary
90 drop "Too many items removed from data stack" ;
92 ERROR: relative-overflow stack ;
94 M: relative-overflow summary
95 drop "Superfluous items pushed to data stack" ;
97 : assert-depth ( quot -- )
98 >r datastack r> swap slip >r datastack r>
99 2dup [ length ] compare sgn {
100 { -1 [ trim-datastacks nip relative-underflow ] }
102 { 1 [ trim-datastacks drop relative-overflow ] }
105 : expired-error. ( obj -- )
106 "Object did not survive image save/load: " write third . ;
108 : io-error. ( error -- )
109 "I/O error: " write third print ;
111 : type-check-error. ( obj -- )
112 "Type check error" print
113 "Object: " write dup fourth short.
114 "Object type: " write dup fourth class .
115 "Expected type: " write third type>class . ;
117 : divide-by-zero-error. ( obj -- )
118 "Division by zero" print drop ;
120 : signal-error. ( obj -- )
121 "Operating system signal " write third . ;
123 : array-size-error. ( obj -- )
124 "Invalid array size: " write dup third .
125 "Maximum: " write fourth 1- . ;
127 : c-string-error. ( obj -- )
128 "Cannot convert to C string: " write third . ;
130 : ffi-error. ( obj -- )
132 dup third [ write ": " write ] when*
135 : heap-scan-error. ( obj -- )
136 "Cannot do next-object outside begin/end-scan" print drop ;
138 : undefined-symbol-error. ( obj -- )
139 "The image refers to a library or symbol that was not found"
140 " at load time" append print drop ;
142 : stack-underflow. ( obj name -- )
143 write " stack underflow" print drop ;
145 : stack-overflow. ( obj name -- )
146 write " stack overflow" print drop ;
148 : datastack-underflow. "Data" stack-underflow. ;
149 : datastack-overflow. "Data" stack-overflow. ;
150 : retainstack-underflow. "Retain" stack-underflow. ;
151 : retainstack-overflow. "Retain" stack-overflow. ;
154 "Memory protection fault at address " write third .h ;
157 "Unimplemented primitive" print drop ;
159 PREDICATE: kernel-error < array
161 { [ dup empty? ] [ drop f ] }
162 { [ dup first "kernel-error" = not ] [ drop f ] }
163 [ second 0 15 between? ]
168 { 0 [ expired-error. ] }
170 { 2 [ primitive-error. ] }
171 { 3 [ type-check-error. ] }
172 { 4 [ divide-by-zero-error. ] }
173 { 5 [ signal-error. ] }
174 { 6 [ array-size-error. ] }
175 { 7 [ c-string-error. ] }
177 { 9 [ heap-scan-error. ] }
178 { 10 [ undefined-symbol-error. ] }
179 { 11 [ datastack-underflow. ] }
180 { 12 [ datastack-overflow. ] }
181 { 13 [ retainstack-underflow. ] }
182 { 14 [ retainstack-overflow. ] }
183 { 15 [ memory-error. ] }
186 M: kernel-error error. dup kernel-errors case ;
188 M: kernel-error error-help kernel-errors at first ;
191 drop "No suitable method" ;
194 "Generic word " write
195 dup no-method-generic pprint
196 " does not define a method for the " write
197 dup no-method-object class pprint
199 "Allowed classes: " write dup no-method-generic order .
200 "Dispatching on object: " write no-method-object short. ;
202 M: no-math-method summary
203 drop "No suitable arithmetic method" ;
205 M: no-next-method summary
206 drop "Executing call-next-method from least-specific method" ;
208 M: inconsistent-next-method summary
209 drop "Executing call-next-method with inconsistent parameters" ;
211 M: stream-closed-twice summary
212 drop "Attempt to perform I/O on closed stream" ;
214 M: check-method summary
215 drop "Invalid parameters for create-method" ;
217 M: no-tuple-class summary
218 drop "BOA constructors can only be defined for tuple classes" ;
220 M: bad-superclass summary
221 drop "Tuple classes can only inherit from other tuple classes" ;
224 drop "Fall-through in cond" ;
227 drop "Fall-through in case" ;
229 M: slice-error error.
230 "Cannot create slice because " write
231 slice-error-reason print ;
233 M: bounds-error summary drop "Sequence index out of bounds" ;
235 M: condition error. error>> error. ;
237 M: condition summary error>> summary ;
239 M: condition error-help error>> error-help ;
241 M: assert summary drop "Assertion failed" ;
243 M: immutable summary drop "Sequence is immutable" ;
245 M: redefine-error error.
246 "Re-definition of " write
247 redefine-error-def . ;
250 drop "Calling a deferred word before it has been defined" ;
252 M: no-compilation-unit error.
253 "Attempting to define " write
254 no-compilation-unit-definition pprint
255 " outside of a compilation unit" print ;
258 drop "Vocabulary does not exist" ;
261 drop "Memory allocation failed" ;
263 M: double-free summary
264 drop "Free failed since memory is not allocated" ;
266 M: realloc-error summary
267 drop "Memory reallocation failed" ;
269 : error-in-thread. ( -- )
270 error-thread get-global
271 "Error in thread " write
274 " (" % dup thread-name %
275 ", " % dup thread-quot unparse-short % ")" %
276 ] "" make swap write-object ":" print nl ;
279 M: thread error-in-thread ( error thread -- )
280 initial-thread get-global eq? [
284 error-in-thread. print-error flush
288 M: encode-error summary drop "Character encoding error" ;
290 M: decode-error summary drop "Character decoding error" ;
292 M: no-such-slot summary drop "No such slot" ;
294 M: immutable-slot summary drop "Slot is immutable" ;
296 M: bad-create summary drop "Bad parameters to create" ;
300 : init-debugger ( -- )
301 V{ } clone set-catchstack
304 self error-thread set-global
305 continuation error-continuation set-global
308 ! VM adds this to kernel errors, so that user-space
310 "kernel-error" 6 setenv ;
314 [ init-debugger ] "debugger" add-init-hook