1 ! Copyright (C) 2004, 2007 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 tuples continuations continuations.private combinators
7 generic.math io.streams.duplex classes
11 GENERIC: error. ( error -- )
12 GENERIC: error-help ( error -- topic )
15 M: object error-help drop f ;
17 M: tuple error. describe ;
18 M: tuple error-help class ;
20 M: string error. print ;
23 error-continuation get continuation-data stack. ;
26 error-continuation get continuation-retain stack. ;
29 error-continuation get continuation-call callstack. ;
31 : :get ( variable -- value )
32 error-continuation get continuation-name assoc-stack ;
35 1- restarts get-global nth f restarts set-global restart ;
41 : restart. ( restart n -- )
43 1+ dup 3 <= [ ":" % # " " % ] [ # " :res " % ] if
48 restarts get dup empty? [
52 "The following restarts are available:" print
54 dup length [ restart. ] 2each
59 "Debugger commands:" print
61 ":help - documentation for this error" print
62 ":s - data stack at exception time" print
63 ":r - retain stack at exception time" print
64 ":c - call stack at exception time" print
65 ":edit - jump to source location (parse errors only)" print
67 ":get ( var -- value ) accesses variables at time of the error" print
70 : print-error ( error -- )
71 [ error. flush ] curry
72 [ global [ "Error in print-error!" print drop ] bind ]
77 [ print-error restarts. debug-help ] error-hook set-global
80 [ error-hook get call ] recover ;
82 TUPLE: assert got expect ;
84 : assert ( got expect -- * ) \ assert construct-boa throw ;
86 : assert= ( a b -- ) 2dup = [ 2drop ] [ assert ] if ;
88 : depth ( -- n ) datastack length ;
90 : assert-depth ( quot -- ) depth slip depth swap assert= ;
92 : expired-error. ( obj -- )
93 "Object did not survive image save/load: " write third . ;
95 : undefined-word-error. ( obj -- )
96 "Undefined word: " write third . ;
98 : io-error. ( error -- )
99 "I/O error: " write third print ;
101 : type-check-error. ( obj -- )
102 "Type check error" print
103 "Object: " write dup fourth short.
104 "Object type: " write dup fourth class .
105 "Expected type: " write third type>class . ;
107 : divide-by-zero-error. ( obj -- )
108 "Division by zero" print drop ;
110 : signal-error. ( obj -- )
111 "Operating system signal " write third . ;
113 : array-size-error. ( obj -- )
114 "Invalid array size: " write dup third .
115 "Maximum: " write fourth 1- . ;
117 : c-string-error. ( obj -- )
118 "Cannot convert to C string: " write third . ;
120 : ffi-error. ( obj -- )
122 dup third [ write ": " write ] when*
125 : heap-scan-error. ( obj -- )
126 "Cannot do next-object outside begin/end-scan" print drop ;
128 : undefined-symbol-error. ( obj -- )
129 "The image refers to a library or symbol that was not found"
130 " at load time" append print drop ;
132 : stack-underflow. ( obj name -- )
133 write " stack underflow" print drop ;
135 : stack-overflow. ( obj name -- )
136 write " stack overflow" print drop ;
138 : datastack-underflow. "Data" stack-underflow. ;
139 : datastack-overflow. "Data" stack-overflow. ;
140 : retainstack-underflow. "Retain" stack-underflow. ;
141 : retainstack-overflow. "Retain" stack-overflow. ;
144 "Memory protection fault at address " write third .h ;
147 "Unimplemented primitive" print drop ;
149 PREDICATE: array kernel-error ( obj -- ? )
151 { [ dup empty? ] [ drop f ] }
152 { [ dup first "kernel-error" = not ] [ drop f ] }
153 { [ t ] [ second 0 16 between? ] }
158 { 0 [ expired-error. ] }
160 { 2 [ undefined-word-error. ] }
161 { 3 [ type-check-error. ] }
162 { 4 [ divide-by-zero-error. ] }
163 { 5 [ signal-error. ] }
164 { 6 [ array-size-error. ] }
165 { 7 [ c-string-error. ] }
167 { 9 [ heap-scan-error. ] }
168 { 10 [ undefined-symbol-error. ] }
169 { 11 [ datastack-underflow. ] }
170 { 12 [ datastack-overflow. ] }
171 { 13 [ retainstack-underflow. ] }
172 { 14 [ retainstack-overflow. ] }
173 { 15 [ memory-error. ] }
174 { 16 [ primitive-error. ] }
177 M: kernel-error error. dup kernel-errors case ;
179 M: kernel-error error-help kernel-errors at first ;
182 drop "No suitable method" ;
185 "Generic word " write
186 dup no-method-generic pprint
187 " does not define a method for the " write
188 dup no-method-object class pprint
190 "Allowed classes: " write dup no-method-generic order .
191 "Dispatching on object: " write no-method-object short. ;
193 M: no-math-method summary
194 drop "No suitable arithmetic method" ;
196 M: check-closed summary
197 drop "Attempt to perform I/O on closed stream" ;
199 M: check-method summary
200 drop "Invalid parameters for define-method" ;
202 M: check-tuple summary
203 drop "Invalid class for define-constructor" ;
206 drop "Fall-through in cond" ;
209 drop "Fall-through in case" ;
211 M: slice-error error.
212 "Cannot create slice because " write
213 slice-error-reason print ;
215 M: bounds-error summary drop "Sequence index out of bounds" ;
217 M: condition error. delegate error. ;
219 M: condition error-help drop f ;
221 M: assert summary drop "Assertion failed" ;
223 M: immutable summary drop "Sequence is immutable" ;