]> gitweb.factorcode.org Git - factor.git/blob - basis/debugger/debugger.factor
VM: bignum_to_fixnum_strict and an accompanying vm error in case the conversion fails
[factor.git] / basis / debugger / debugger.factor
1 ! Copyright (C) 2004, 2011 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.strings arrays assocs classes
4 classes.builtin classes.mixin classes.tuple classes.tuple.parser
5 combinators combinators.short-circuit compiler.units
6 continuations definitions destructors effects.parser generic
7 generic.math generic.parser generic.single grouping io
8 io.encodings io.styles kernel kernel.private lexer make math
9 math.order math.parser namespaces parser prettyprint sequences
10 sequences.private slots source-files.errors strings
11 strings.parser summary system vocabs vocabs.loader
12 vocabs.parser words ;
13 FROM: namespaces => change-global ;
14 IN: debugger
15
16 GENERIC: error-help ( error -- topic )
17
18 M: object error-help drop f ;
19
20 M: tuple error-help class-of ;
21
22 M: source-file-error error-help error>> error-help ;
23
24 GENERIC: error. ( error -- )
25
26 M: object error. short. ;
27
28 M: string error. print ;
29
30 : traceback-link. ( continuation -- )
31     "[" write [ "Traceback" ] dip write-object "]" print ;
32
33 : :s ( -- )
34     error-continuation get data>> stack. ;
35
36 : :r ( -- )
37     error-continuation get retain>> stack. ;
38
39 : :c ( -- )
40     error-continuation get call>> callstack. ;
41
42 : :get ( variable -- value )
43     error-continuation get name>> assoc-stack ;
44
45 : :res ( n -- * )
46     1 - restarts [ nth f ] change-global continue-restart ;
47
48 : :1 ( -- * ) 1 :res ;
49 : :2 ( -- * ) 2 :res ;
50 : :3 ( -- * ) 3 :res ;
51
52 : restart. ( restart n -- )
53     [
54         1 + dup 3 <= [ ":" % # "      " % ] [ # " :res  " % ] if
55         name>> %
56     ] "" make print ;
57
58 : restarts. ( -- )
59     restarts get [
60         nl
61         "The following restarts are available:" print
62         nl
63         [ restart. ] each-index
64     ] unless-empty ;
65
66 : print-error ( error -- )
67     [ error. flush ] curry
68     [ [ "Error in print-error!" print drop ] with-global ]
69     recover ;
70
71 : :error ( -- )
72     error get print-error ;
73
74 : print-error-and-restarts ( error -- )
75     print-error
76     restarts.
77     nl
78     "Type :help for debugging help." print flush ;
79
80 : try ( quot -- )
81     [ print-error-and-restarts ] recover ; inline
82
83 : expired-error. ( obj -- )
84     "Object did not survive image save/load: " write third . ;
85
86 : io-error. ( error -- )
87     "I/O error #" write third . ;
88
89 : type-check-error. ( obj -- )
90     "Type check error" print
91     "Object: " write dup fourth short.
92     "Object type: " write dup fourth class-of .
93     "Expected type: " write third type>class . ;
94
95 : divide-by-zero-error. ( obj -- )
96     "Division by zero" print drop ;
97
98 HOOK: signal-error. os ( obj -- )
99
100 : array-size-error. ( obj -- )
101     "Invalid array size: " write dup third .
102     "Maximum: " write fourth 1 - . ;
103
104 : fixnum-range-error. ( obj -- )
105     "Cannot convert to fixnum: " write third . ;
106
107 : c-string-error. ( obj -- )
108     "Cannot convert to C string: " write third . ;
109
110 : ffi-error. ( obj -- )
111     "FFI error" print drop ;
112
113 : undefined-symbol-error. ( obj -- )
114     "Cannot resolve C library function" print
115     "Symbol: " write dup third symbol>string print
116     "Library: " write fourth .
117     "You are probably missing a library or the library path is wrong." print
118     "See http://concatenative.org/wiki/view/Factor/Requirements" print ;
119
120 : stack-underflow. ( obj name -- )
121     write " stack underflow" print drop ;
122
123 : stack-overflow. ( obj name -- )
124     write " stack overflow" print drop ;
125
126 : datastack-underflow. ( obj -- ) "Data" stack-underflow. ;
127 : datastack-overflow. ( obj -- ) "Data" stack-overflow. ;
128 : retainstack-underflow. ( obj -- ) "Retain" stack-underflow. ;
129 : retainstack-overflow. ( obj -- ) "Retain" stack-overflow. ;
130 : callstack-underflow. ( obj -- ) "Call" stack-underflow. ;
131 : callstack-overflow. ( obj -- ) "Call" stack-overflow. ;
132
133 : memory-error. ( error -- )
134     "Memory protection fault at address " write third .h ;
135
136 : primitive-error. ( error -- )
137     "Unimplemented primitive" print drop ;
138
139 : fp-trap-error. ( error -- )
140     "Floating point trap" print drop ;
141
142 : interrupt-error. ( error -- )
143     "Interrupt" print drop ;
144
145 PREDICATE: vm-error < array
146     dup length 2 < [ drop f ] [
147         {
148             [ first-unsafe "kernel-error" = ]
149             [ second-unsafe 0 kernel-error-count 1 - between? ]
150         } 1&&
151     ] if ;
152
153 : vm-errors ( error -- n errors )
154     second {
155         [ expired-error.          ]
156         [ io-error.               ]
157         [ primitive-error.        ]
158         [ type-check-error.       ]
159         [ divide-by-zero-error.   ]
160         [ signal-error.           ]
161         [ array-size-error.       ]
162         [ fixnum-range-error.     ]
163         [ c-string-error.         ]
164         [ ffi-error.              ]
165         [ undefined-symbol-error. ]
166         [ datastack-underflow.    ]
167         [ datastack-overflow.     ]
168         [ retainstack-underflow.  ]
169         [ retainstack-overflow.   ]
170         [ callstack-underflow.    ]
171         [ callstack-overflow.     ]
172         [ memory-error.           ]
173         [ fp-trap-error.          ]
174         [ interrupt-error.        ]
175     } ; inline
176
177 M: vm-error summary drop "VM error" ;
178
179 M: vm-error error. dup vm-errors dispatch ;
180
181 M: vm-error error-help vm-errors nth first ;
182
183 M: no-method summary
184     drop "No suitable method" ;
185
186 M: no-method error.
187     "Generic word " write
188     dup generic>> pprint
189     " does not define a method for the " write
190     dup object>> class-of pprint
191     " class." print
192     "Dispatching on object: " write object>> short. ;
193
194 M: bad-slot-value summary drop "Bad store to specialized slot" ;
195
196 M: bad-slot-name summary drop "Bad slot name in object literal" ;
197
198 M: no-math-method summary
199     drop "No suitable arithmetic method" ;
200
201 M: no-next-method summary
202     drop "Executing call-next-method from least-specific method" ;
203
204 M: inconsistent-next-method summary
205     drop "Executing call-next-method with inconsistent parameters" ;
206
207 M: check-method-error summary
208     drop "Invalid parameters for create-method" ;
209
210 M: not-a-tuple summary
211     drop "Not a tuple" ;
212
213 M: bad-superclass summary
214     drop "Tuple classes can only inherit from non-final tuple classes" ;
215
216 M: bad-initial-value summary
217     drop "Incompatible initial value" ;
218
219 M: no-cond summary
220     drop "Fall-through in cond" ;
221
222 M: no-case summary
223     drop "Fall-through in case" ;
224
225 M: slice-error summary
226     drop "Cannot create slice" ;
227
228 M: bounds-error summary drop "Sequence index out of bounds" ;
229
230 M: groups-error summary drop "Non positive group size" ;
231
232 M: condition error. error>> error. ;
233
234 M: condition summary error>> summary ;
235
236 M: condition error-help error>> error-help ;
237
238 M: assert summary drop "Assertion failed" ;
239
240 M: assert-sequence summary drop "Assertion failed" ;
241
242 M: assert-sequence error.
243     standard-table-style [
244         [ "=== Expected:" print expected>> stack. ]
245         [ "=== Got:" print got>> stack. ] bi
246     ] tabular-output ;
247
248 M: immutable summary drop "Sequence is immutable" ;
249
250 M: redefine-error error.
251     "Re-definition of " write
252     def>> . ;
253
254 M: undefined-word summary
255     word>> undefined-word?
256     "Cannot execute a deferred word before it has been defined"
257     "Cannot execute a word before it has been compiled"
258     ? ;
259
260 M: no-compilation-unit error.
261     "Attempting to define " write
262     definition>> pprint
263     " outside of a compilation unit" print ;
264
265 M: no-vocab summary
266     drop "Vocabulary does not exist" ;
267
268 M: encode-error summary drop "Character encoding error" ;
269
270 M: decode-error summary drop "Character decoding error" ;
271
272 M: bad-create summary drop "Bad parameters to create" ;
273
274 M: cannot-be-inline summary drop "This type of word cannot be inlined" ;
275
276 M: attempt-all-error summary drop "Nothing to attempt" ;
277
278 M: already-disposed summary drop "Attempting to operate on disposed object" ;
279
280 M: no-current-vocab-error summary
281     drop "Not in a vocabulary; IN: form required" ;
282
283 M: no-word-error summary
284     name>>
285     "No word named “"
286     "” found in current vocabulary search path" surround ;
287
288 M: no-word-error error. summary print ;
289
290 M: no-word-in-vocab summary
291     [ vocab>> ] [ word>> ] bi
292     [ "No word named “" % % "” found in “" % % "” vocabulary" % ] "" make ;
293
294 M: no-word-in-vocab error. summary print ;
295
296 M: ambiguous-use-error summary
297     words>> first name>>
298     "More than one vocabulary defines a word named “" "”" surround ;
299
300 M: ambiguous-use-error error. summary print ;
301
302 M: staging-violation summary
303     drop
304     "A parsing word cannot be used in the same file it is defined in." ;
305
306 M: bad-number summary
307     drop "Bad number literal" ;
308
309 M: duplicate-slot-names summary
310     drop "Duplicate slot names" ;
311
312 M: invalid-slot-name summary
313     drop "Invalid slot name" ;
314
315 M: bad-inheritance summary
316     drop "Circularity in inheritance chain" ;
317
318 M: not-in-a-method-error summary
319     drop "call-next-method can only be called in a method definition" ;
320
321 M: version-control-merge-conflict summary
322     drop "Version control merge conflict in source code" ;
323
324 GENERIC: expected>string ( obj -- str )
325
326 M: f expected>string drop "end of input" ;
327 M: word expected>string name>> ;
328 M: string expected>string ;
329
330 M: unexpected error.
331     "Expected " write
332     dup want>> expected>string write
333     " but got " write
334     got>> expected>string print ;
335
336 M: lexer-error error.
337     [ lexer-dump ] [ error>> error. ] bi ;
338
339 M: lexer-error summary
340     error>> summary ;
341
342 M: lexer-error compute-restarts
343     error>> compute-restarts ;
344
345 M: lexer-error error-help
346     error>> error-help ;
347
348 M: bad-effect summary
349     drop "Bad stack effect declaration" ;
350
351 M: invalid-row-variable summary
352     drop "Stack effect row variables can only occur as the first input or output" ;
353
354 M: row-variable-can't-have-type summary
355     drop "Stack effect row variables cannot have a declared type" ;
356
357 M: bad-escape error.
358     "Bad escape code: \\" write char>> write nl ;
359
360 M: bad-literal-tuple summary drop "Bad literal tuple" ;
361
362 M: check-mixin-class-error summary drop "Not a mixin class" ;
363
364 M: not-found-in-roots summary
365     path>> "Cannot resolve vocab: " prepend ;
366
367 M: wrong-values summary drop "Quotation's stack effect does not match call site" ;
368
369 M: stack-effect-omits-dashes summary drop "Stack effect must contain “--”" ;
370
371 { "threads" "debugger" } "debugger.threads" require-when