/[slime]/slime/swank-lispworks.lisp
ViewVC logotype

Contents of /slime/swank-lispworks.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.143 - (show annotations)
Sun Nov 6 17:05:05 2011 UTC (2 years, 5 months ago) by heller
Branch: MAIN
Changes since 1.142: +8 -0 lines
* swank-lispworks.lisp (string-to-utf8, string-to-utf8): Implemented.
1 ;;; -*- indent-tabs-mode: nil -*-
2 ;;;
3 ;;; swank-lispworks.lisp --- LispWorks specific code for SLIME.
4 ;;;
5 ;;; Created 2003, Helmut Eller
6 ;;;
7 ;;; This code has been placed in the Public Domain. All warranties
8 ;;; are disclaimed.
9 ;;;
10
11 (in-package :swank-backend)
12
13 (eval-when (:compile-toplevel :load-toplevel :execute)
14 (require "comm")
15 (import-from :stream *gray-stream-symbols* :swank-backend))
16
17 (import-swank-mop-symbols :clos '(:slot-definition-documentation
18 :slot-boundp-using-class
19 :slot-value-using-class
20 :slot-makunbound-using-class
21 :eql-specializer
22 :eql-specializer-object
23 :compute-applicable-methods-using-classes))
24
25 (defun swank-mop:slot-definition-documentation (slot)
26 (documentation slot t))
27
28 (defun swank-mop:slot-boundp-using-class (class object slotd)
29 (clos:slot-boundp-using-class class object
30 (clos:slot-definition-name slotd)))
31
32 (defun swank-mop:slot-value-using-class (class object slotd)
33 (clos:slot-value-using-class class object
34 (clos:slot-definition-name slotd)))
35
36 (defun (setf swank-mop:slot-value-using-class) (value class object slotd)
37 (setf (clos:slot-value-using-class class object
38 (clos:slot-definition-name slotd))
39 value))
40
41 (defun swank-mop:slot-makunbound-using-class (class object slotd)
42 (clos:slot-makunbound-using-class class object
43 (clos:slot-definition-name slotd)))
44
45 (defun swank-mop:compute-applicable-methods-using-classes (gf classes)
46 (clos::compute-applicable-methods-from-classes gf classes))
47
48 ;; lispworks doesn't have the eql-specializer class, it represents
49 ;; them as a list of `(EQL ,OBJECT)
50 (deftype swank-mop:eql-specializer () 'cons)
51
52 (defun swank-mop:eql-specializer-object (eql-spec)
53 (second eql-spec))
54
55 (eval-when (:compile-toplevel :execute :load-toplevel)
56 (defvar *original-defimplementation* (macro-function 'defimplementation))
57 (defmacro defimplementation (&whole whole name args &body body
58 &environment env)
59 (declare (ignore args body))
60 `(progn
61 (dspec:record-definition '(defun ,name) (dspec:location)
62 :check-redefinition-p nil)
63 ,(funcall *original-defimplementation* whole env))))
64
65 ;;; UTF8
66
67 (defimplementation string-to-utf8 (string)
68 (ef:encode-lisp-string string :utf-8))
69
70 (defimplementation utf8-to-string (octets)
71 (ef:decode-external-string octets :utf-8))
72
73 ;;; TCP server
74
75 (defimplementation preferred-communication-style ()
76 :spawn)
77
78 (defun socket-fd (socket)
79 (etypecase socket
80 (fixnum socket)
81 (comm:socket-stream (comm:socket-stream-socket socket))))
82
83 (defimplementation create-socket (host port)
84 (multiple-value-bind (socket where errno)
85 #-(or lispworks4.1 (and macosx lispworks4.3))
86 (comm::create-tcp-socket-for-service port :address host)
87 #+(or lispworks4.1 (and macosx lispworks4.3))
88 (comm::create-tcp-socket-for-service port)
89 (cond (socket socket)
90 (t (error 'network-error
91 :format-control "~A failed: ~A (~D)"
92 :format-arguments (list where
93 (list #+unix (lw:get-unix-error errno))
94 errno))))))
95
96 (defimplementation local-port (socket)
97 (nth-value 1 (comm:get-socket-address (socket-fd socket))))
98
99 (defimplementation close-socket (socket)
100 (comm::close-socket (socket-fd socket)))
101
102 (defimplementation accept-connection (socket
103 &key external-format buffering timeout)
104 (declare (ignore buffering))
105 (let* ((fd (comm::get-fd-from-socket socket)))
106 (assert (/= fd -1))
107 (assert (valid-external-format-p external-format))
108 (cond ((member (first external-format) '(:latin-1 :ascii))
109 (make-instance 'comm:socket-stream
110 :socket fd
111 :direction :io
112 :read-timeout timeout
113 :element-type 'base-char))
114 (t
115 (assert (member (first external-format) '(:utf-8)))
116 (make-instance 'utf8-stream
117 :byte-stream
118 (make-instance 'comm:socket-stream
119 :socket fd
120 :direction :io
121 :read-timeout timeout
122 :element-type '(unsigned-byte 8)))))))
123
124 (defclass utf8-stream (stream:fundamental-character-input-stream
125 stream:fundamental-character-output-stream)
126 ((byte-stream :type comm:socket-stream
127 :initform nil
128 :initarg :byte-stream
129 :accessor utf8-stream-byte-stream)))
130
131 ;; Helper function. Decode the next N bytes starting from INDEX.
132 ;; Return the decoded char and the new index.
133 (defun utf8-decode-aux (buffer index limit byte0 n)
134 (declare (simple-string buffer) (fixnum index limit byte0 n))
135 (if (< (- limit index) n)
136 (values nil index)
137 (do ((i 0 (1+ i))
138 (code byte0 (let ((byte (char-code (schar buffer (+ index i)))))
139 (cond ((= (ldb (byte 2 6) byte) #b10)
140 (+ (ash code 6) (ldb (byte 6 0) byte)))
141 (t
142 (error "Invalid encoding"))))))
143 ((= i n)
144 (values (cond ((<= code #xff) (code-char code))
145 ((<= #xdc00 code #xdbff)
146 (error "Invalid Unicode code point: #x~x" code))
147 ((< code char-code-limit)
148 (code-char code))
149 (t
150 (error
151 "Can't represent code point: #x~x ~
152 (char-code-limit is #x~x)"
153 code char-code-limit)))
154 (+ index n))))))
155
156 ;; Decode one character in BUFFER starting at INDEX.
157 ;; Return 2 values: the character and the new index.
158 ;; If there aren't enough bytes between INDEX and LIMIT return nil.
159 (defun utf8-decode (buffer index limit)
160 (declare (simple-string buffer) (fixnum index limit))
161 (if (= index limit)
162 (values nil index)
163 (let ((b (char-code (schar buffer index))))
164 (if (<= b #x7f)
165 (values (code-char b) (1+ index))
166 (macrolet ((try (marker else)
167 (let* ((l (integer-length marker))
168 (n (- l 2)))
169 `(if (= (ldb (byte ,l ,(- 8 l)) b) ,marker)
170 (utf8-decode-aux buffer (1+ index) limit
171 (ldb (byte ,(- 8 l) 0) b)
172 ,n)
173 ,else))))
174 (try #b110
175 (try #b1110
176 (try #b11110
177 (try #b111110
178 (try #b1111110
179 (error "Invalid encoding")))))))))))
180
181 ;; Decode characters from BUFFER and write them to STRING.
182 ;; Return 2 values: LASTINDEX and LASTSTART where
183 ;; LASTINDEX is the last index in BUFFER that was not decoded
184 ;; and LASTSTART is the last index in STRING not written.
185 (defun utf8-decode-into (buffer index limit string start end)
186 (declare (string string) (fixnum index limit start end))
187 (loop
188 (cond ((= start end)
189 (return (values index start)))
190 (t
191 (multiple-value-bind (c i) (utf8-decode buffer index limit)
192 (cond (c
193 (setf (aref string start) c)
194 (setq index i)
195 (setq start (1+ start)))
196 (t
197 (return (values index start)))))))))
198
199 (defmacro utf8-encode-aux (code buffer start end n)
200 `(cond ((< (- ,end ,start) ,n)
201 ,start)
202 (t
203 (setf (schar ,buffer ,start)
204 (code-char
205 (dpb (ldb (byte ,(- 7 n) ,(* 6 (1- n))) ,code)
206 (byte ,(- 7 n) 0)
207 ,(dpb 0 (byte 1 (- 7 n)) #xff))))
208 ,@(loop for i from 0 upto (- n 2) collect
209 `(setf (schar ,buffer (+ ,start ,(- n 1 i)))
210 (code-char
211 (dpb (ldb (byte 6 ,(* 6 i)) ,code)
212 (byte 6 0)
213 #b10111111))))
214 (+ ,start ,n))))
215
216 (defun utf8-encode (char buffer start end)
217 (declare (fixnum start end))
218 (let ((code (char-code char)))
219 (cond ((<= code #x7f)
220 (cond ((< start end)
221 (setf (schar buffer start) char)
222 (1+ start))
223 (t start)))
224 ((<= code #x7ff) (utf8-encode-aux code buffer start end 2))
225 ((<= #xd800 code #xdfff)
226 (error "Invalid Unicode code point (surrogate): #x~x" code))
227 ((<= code #xffff) (utf8-encode-aux code buffer start end 3))
228 ((<= code #x1fffff) (utf8-encode-aux code buffer start end 4))
229 ((<= code #x3ffffff) (utf8-encode-aux code buffer start end 5))
230 ((<= code #x7fffffff) (utf8-encode-aux code buffer start end 6))
231 (t (error "Can't encode ~s (~x)" char code)))))
232
233 (defun utf8-encode-into (string start end buffer index limit)
234 (loop
235 (cond ((= start end)
236 (return (values start index)))
237 ((= index limit)
238 (return (values start index)))
239 (t
240 (let ((i2 (utf8-encode (char string start) buffer index limit)))
241 (cond ((= i2 index)
242 (return (values start index)))
243 (t
244 (setq index i2)
245 (incf start))))))))
246
247 (defun utf8-stream-read-char (stream no-hang)
248 (with-slots (byte-stream) stream
249 (loop
250 (stream:with-stream-input-buffer (b i l) byte-stream
251 (multiple-value-bind (c i2) (utf8-decode b i l)
252 (cond (c
253 (setf i i2)
254 (return c))
255 ((and no-hang
256 (not (sys:wait-for-input-streams-returning-first
257 (list byte-stream) :timeout 0)))
258 (return nil))
259 ((stream:stream-fill-buffer byte-stream)
260 #| next iteration |# )
261 (t
262 (return :eof))))))))
263
264 (defmethod stream:stream-read-char ((stream utf8-stream))
265 (utf8-stream-read-char stream nil))
266
267 (defmethod stream:stream-read-char-no-hang ((stream utf8-stream))
268 (utf8-stream-read-char stream t))
269
270 (defmethod stream:stream-read-sequence ((stream utf8-stream) (string string)
271 start end)
272 (with-slots (byte-stream) stream
273 (loop
274 (stream:with-stream-input-buffer (b i l) byte-stream
275 (multiple-value-bind (i2 s2) (utf8-decode-into b i l string start end)
276 (setq i i2)
277 (setq start s2)
278 (cond ((= start end)
279 (return start))
280 ((stream:stream-fill-buffer byte-stream)
281 #| next iteration |# )
282 (t
283 (return start))))))))
284
285 (defmethod stream:stream-unread-char ((stream utf8-stream) (c character))
286 (with-slots (byte-stream) stream
287 (stream:with-stream-input-buffer (b i l) byte-stream
288 (declare (ignorable l))
289 (let* ((bytes (ef:encode-lisp-string (string c) :utf-8))
290 (len (length bytes))
291 (i2 (- i len)))
292 (assert (equal (utf8-decode b i2 i) c))
293 (setq i i2)
294 nil))))
295
296 (defmethod stream:stream-write-char ((stream utf8-stream) (char character))
297 (with-slots (byte-stream) stream
298 (loop
299 (stream:with-stream-output-buffer (b i l) byte-stream
300 (let ((i2 (utf8-encode char b i l)))
301 (cond ((< i i2)
302 (setf i i2)
303 (return char))
304 ((stream:stream-flush-buffer byte-stream)
305 )
306 (t
307 (error "Can't flush buffer"))))))))
308
309 (defmethod stream:stream-write-string ((stream utf8-stream)
310 (string string)
311 &optional (start 0)
312 (end (length string)))
313 (with-slots (byte-stream) stream
314 (loop
315 (stream:with-stream-output-buffer (b i l) byte-stream
316 (multiple-value-bind (s2 i2) (utf8-encode-into string start end
317 b i l)
318 (setf i i2)
319 (setf start s2)
320 (cond ((= start end)
321 (return string))
322 ((stream:stream-flush-buffer byte-stream)
323 )
324 (t
325 (error "Can't flush buffer"))))))))
326
327 (defmethod stream:stream-write-sequence ((stream utf8-stream)
328 seq start end)
329 (stream:stream-write-string seq start end))
330
331 (defmethod stream:stream-force-output ((stream utf8-stream))
332 (with-slots (byte-stream) stream (force-output byte-stream)))
333
334 (defmethod stream:stream-finish-output ((stream utf8-stream))
335 (with-slots (byte-stream) stream (finish-output byte-stream)))
336
337 ;;; Coding Systems
338
339 (defun valid-external-format-p (external-format)
340 (member external-format *external-format-to-coding-system*
341 :test #'equal :key #'car))
342
343 (defvar *external-format-to-coding-system*
344 '(((:latin-1 :eol-style :lf)
345 "latin-1-unix" "iso-latin-1-unix" "iso-8859-1-unix")
346 ;;((:latin-1) "latin-1" "iso-latin-1" "iso-8859-1")
347 ;;((:utf-8) "utf-8")
348 ((:utf-8 :eol-style :lf) "utf-8-unix")
349 ;;((:euc-jp) "euc-jp")
350 ((:euc-jp :eol-style :lf) "euc-jp-unix")
351 ;;((:ascii) "us-ascii")
352 ((:ascii :eol-style :lf) "us-ascii-unix")))
353
354 (defimplementation find-external-format (coding-system)
355 (car (rassoc-if (lambda (x) (member coding-system x :test #'equal))
356 *external-format-to-coding-system*)))
357
358 ;;; Unix signals
359
360 (defun sigint-handler ()
361 (with-simple-restart (continue "Continue from SIGINT handler.")
362 (invoke-debugger "SIGINT")))
363
364 (defun make-sigint-handler (process)
365 (lambda (&rest args)
366 (declare (ignore args))
367 (mp:process-interrupt process #'sigint-handler)))
368
369 (defun set-sigint-handler ()
370 ;; Set SIGINT handler on Swank request handler thread.
371 #-win32
372 (sys::set-signal-handler +sigint+
373 (make-sigint-handler mp:*current-process*)))
374
375 #-win32
376 (defimplementation install-sigint-handler (handler)
377 (sys::set-signal-handler +sigint+
378 (let ((self mp:*current-process*))
379 (lambda (&rest args)
380 (declare (ignore args))
381 (mp:process-interrupt self handler)))))
382
383 (defimplementation getpid ()
384 #+win32 (win32:get-current-process-id)
385 #-win32 (system::getpid))
386
387 (defimplementation lisp-implementation-type-name ()
388 "lispworks")
389
390 (defimplementation set-default-directory (directory)
391 (namestring (hcl:change-directory directory)))
392
393 ;;;; Documentation
394
395 (defun map-list (function list)
396 "Map over proper and not proper lists."
397 (loop for (car . cdr) on list
398 collect (funcall function car) into result
399 when (null cdr) return result
400 when (atom cdr) return (nconc result (funcall function cdr))))
401
402 (defun replace-strings-with-symbols (tree)
403 (map-list
404 (lambda (x)
405 (typecase x
406 (list
407 (replace-strings-with-symbols x))
408 (symbol
409 x)
410 (string
411 (intern x))
412 (t
413 (intern (write-to-string x)))))
414 tree))
415
416 (defimplementation arglist (symbol-or-function)
417 (let ((arglist (lw:function-lambda-list symbol-or-function)))
418 (etypecase arglist
419 ((member :dont-know)
420 :not-available)
421 (list
422 (replace-strings-with-symbols arglist)))))
423
424 (defimplementation function-name (function)
425 (nth-value 2 (function-lambda-expression function)))
426
427 (defimplementation macroexpand-all (form)
428 (walker:walk-form form))
429
430 (defun generic-function-p (object)
431 (typep object 'generic-function))
432
433 (defimplementation describe-symbol-for-emacs (symbol)
434 "Return a plist describing SYMBOL.
435 Return NIL if the symbol is unbound."
436 (let ((result '()))
437 (labels ((first-line (string)
438 (let ((pos (position #\newline string)))
439 (if (null pos) string (subseq string 0 pos))))
440 (doc (kind &optional (sym symbol))
441 (let ((string (or (documentation sym kind))))
442 (if string
443 (first-line string)
444 :not-documented)))
445 (maybe-push (property value)
446 (when value
447 (setf result (list* property value result)))))
448 (maybe-push
449 :variable (when (boundp symbol)
450 (doc 'variable)))
451 (maybe-push
452 :generic-function (if (and (fboundp symbol)
453 (generic-function-p (fdefinition symbol)))
454 (doc 'function)))
455 (maybe-push
456 :function (if (and (fboundp symbol)
457 (not (generic-function-p (fdefinition symbol))))
458 (doc 'function)))
459 (maybe-push
460 :setf (let ((setf-name (sys:underlying-setf-name `(setf ,symbol))))
461 (if (fboundp setf-name)
462 (doc 'setf))))
463 (maybe-push
464 :class (if (find-class symbol nil)
465 (doc 'class)))
466 result)))
467
468 (defimplementation describe-definition (symbol type)
469 (ecase type
470 (:variable (describe-symbol symbol))
471 (:class (describe (find-class symbol)))
472 ((:function :generic-function) (describe-function symbol))
473 (:setf (describe-function (sys:underlying-setf-name `(setf ,symbol))))))
474
475 (defun describe-function (symbol)
476 (cond ((fboundp symbol)
477 (format t "(~A ~/pprint-fill/)~%~%~:[(not documented)~;~:*~A~]~%"
478 symbol
479 (lispworks:function-lambda-list symbol)
480 (documentation symbol 'function))
481 (describe (fdefinition symbol)))
482 (t (format t "~S is not fbound" symbol))))
483
484 (defun describe-symbol (sym)
485 (format t "~A is a symbol in package ~A." sym (symbol-package sym))
486 (when (boundp sym)
487 (format t "~%~%Value: ~A" (symbol-value sym)))
488 (let ((doc (documentation sym 'variable)))
489 (when doc
490 (format t "~%~%Variable documentation:~%~A" doc)))
491 (when (fboundp sym)
492 (describe-function sym)))
493
494 ;;; Debugging
495
496 (defclass slime-env (env:environment)
497 ((debugger-hook :initarg :debugger-hoook)))
498
499 (defun slime-env (hook io-bindings)
500 (make-instance 'slime-env :name "SLIME Environment"
501 :io-bindings io-bindings
502 :debugger-hoook hook))
503
504 (defmethod env-internals:environment-display-notifier
505 ((env slime-env) &key restarts condition)
506 (declare (ignore restarts condition))
507 (funcall (swank-sym :swank-debugger-hook) condition *debugger-hook*)
508 ;; nil
509 )
510
511 (defmethod env-internals:environment-display-debugger ((env slime-env))
512 *debug-io*)
513
514 (defmethod env-internals:confirm-p ((e slime-env) &optional msg &rest args)
515 (apply (swank-sym :y-or-n-p-in-emacs) msg args))
516
517 (defimplementation call-with-debugger-hook (hook fun)
518 (let ((*debugger-hook* hook))
519 (env:with-environment ((slime-env hook '()))
520 (funcall fun))))
521
522 (defimplementation install-debugger-globally (function)
523 (setq *debugger-hook* function)
524 (setf (env:environment) (slime-env function '())))
525
526 (defvar *sldb-top-frame*)
527
528 (defun interesting-frame-p (frame)
529 (cond ((or (dbg::call-frame-p frame)
530 (dbg::derived-call-frame-p frame)
531 (dbg::foreign-frame-p frame)
532 (dbg::interpreted-call-frame-p frame))
533 t)
534 ((dbg::catch-frame-p frame) dbg:*print-catch-frames*)
535 ((dbg::binding-frame-p frame) dbg:*print-binding-frames*)
536 ((dbg::handler-frame-p frame) dbg:*print-handler-frames*)
537 ((dbg::restart-frame-p frame) dbg:*print-restart-frames*)
538 (t nil)))
539
540 (defun nth-next-frame (frame n)
541 "Unwind FRAME N times."
542 (do ((frame frame (dbg::frame-next frame))
543 (i n (if (interesting-frame-p frame) (1- i) i)))
544 ((or (not frame)
545 (and (interesting-frame-p frame) (zerop i)))
546 frame)))
547
548 (defun nth-frame (index)
549 (nth-next-frame *sldb-top-frame* index))
550
551 (defun find-top-frame ()
552 "Return the most suitable top-frame for the debugger."
553 (or (do ((frame (dbg::debugger-stack-current-frame dbg::*debugger-stack*)
554 (nth-next-frame frame 1)))
555 ((or (null frame) ; no frame found!
556 (and (dbg::call-frame-p frame)
557 (eq (dbg::call-frame-function-name frame)
558 'invoke-debugger)))
559 (nth-next-frame frame 1)))
560 ;; if we can't find a invoke-debugger frame, take any old frame at the top
561 (dbg::debugger-stack-current-frame dbg::*debugger-stack*)))
562
563 (defimplementation call-with-debugging-environment (fn)
564 (dbg::with-debugger-stack ()
565 (let ((*sldb-top-frame* (find-top-frame)))
566 (funcall fn))))
567
568 (defimplementation compute-backtrace (start end)
569 (let ((end (or end most-positive-fixnum))
570 (backtrace '()))
571 (do ((frame (nth-frame start) (dbg::frame-next frame))
572 (i start))
573 ((or (not frame) (= i end)) (nreverse backtrace))
574 (when (interesting-frame-p frame)
575 (incf i)
576 (push frame backtrace)))))
577
578 (defun frame-actual-args (frame)
579 (let ((*break-on-signals* nil)
580 (kind nil))
581 (loop for arg in (dbg::call-frame-arglist frame)
582 if (eq kind '&rest)
583 nconc (handler-case
584 (dbg::dbg-eval arg frame)
585 (error (e) (list (format nil "<~A>" arg))))
586 and do (loop-finish)
587 else
588 if (member arg '(&rest &optional &key))
589 do (setq kind arg)
590 else
591 nconc
592 (handler-case
593 (nconc (and (eq kind '&key)
594 (list (cond ((symbolp arg)
595 (intern (symbol-name arg) :keyword))
596 ((and (consp arg) (symbolp (car arg)))
597 (intern (symbol-name (car arg)) :keyword))
598 (t (caar arg)))))
599 (list (dbg::dbg-eval
600 (cond ((symbolp arg) arg)
601 ((and (consp arg) (symbolp (car arg)))
602 (car arg))
603 (t (cadar arg)))
604 frame)))
605 (error (e) (list (format nil "<~A>" arg)))))))
606
607 (defimplementation print-frame (frame stream)
608 (cond ((dbg::call-frame-p frame)
609 (prin1 (cons (dbg::call-frame-function-name frame)
610 (frame-actual-args frame))
611 stream))
612 (t (princ frame stream))))
613
614 (defun frame-vars (frame)
615 (first (dbg::frame-locals-format-list frame #'list 75 0)))
616
617 (defimplementation frame-locals (n)
618 (let ((frame (nth-frame n)))
619 (if (dbg::call-frame-p frame)
620 (mapcar (lambda (var)
621 (destructuring-bind (name value symbol location) var
622 (declare (ignore name location))
623 (list :name symbol :id 0
624 :value value)))
625 (frame-vars frame)))))
626
627 (defimplementation frame-var-value (frame var)
628 (let ((frame (nth-frame frame)))
629 (destructuring-bind (_n value _s _l) (nth var (frame-vars frame))
630 (declare (ignore _n _s _l))
631 value)))
632
633 (defimplementation frame-source-location (frame)
634 (let ((frame (nth-frame frame))
635 (callee (if (plusp frame) (nth-frame (1- frame)))))
636 (if (dbg::call-frame-p frame)
637 (let ((dspec (dbg::call-frame-function-name frame))
638 (cname (and (dbg::call-frame-p callee)
639 (dbg::call-frame-function-name callee)))
640 (path (and (dbg::call-frame-p frame)
641 (dbg::call-frame-edit-path frame))))
642 (if dspec
643 (frame-location dspec cname path))))))
644
645 (defimplementation eval-in-frame (form frame-number)
646 (let ((frame (nth-frame frame-number)))
647 (dbg::dbg-eval form frame)))
648
649 (defimplementation return-from-frame (frame-number form)
650 (let* ((frame (nth-frame frame-number))
651 (return-frame (dbg::find-frame-for-return frame)))
652 (dbg::dbg-return-from-call-frame frame form return-frame
653 dbg::*debugger-stack*)))
654
655 (defimplementation restart-frame (frame-number)
656 (let ((frame (nth-frame frame-number)))
657 (dbg::restart-frame frame :same-args t)))
658
659 (defimplementation disassemble-frame (frame-number)
660 (let* ((frame (nth-frame frame-number)))
661 (when (dbg::call-frame-p frame)
662 (let ((function (dbg::get-call-frame-function frame)))
663 (disassemble function)))))
664
665 ;;; Definition finding
666
667 (defun frame-location (dspec callee-name edit-path)
668 (let ((infos (dspec:find-dspec-locations dspec)))
669 (cond (infos
670 (destructuring-bind ((rdspec location) &rest _) infos
671 (declare (ignore _))
672 (let ((name (and callee-name (symbolp callee-name)
673 (string callee-name)))
674 (path (edit-path-to-cmucl-source-path edit-path)))
675 (make-dspec-location rdspec location
676 `(:call-site ,name :edit-path ,path)))))
677 (t
678 (list :error (format nil "Source location not available for: ~S"
679 dspec))))))
680
681 ;; dbg::call-frame-edit-path is not documented but lets assume the
682 ;; binary representation of the integer EDIT-PATH should be
683 ;; interpreted as a sequence of CAR or CDR. #b1111010 is roughly the
684 ;; same as cadadddr. Something is odd with the highest bit.
685 (defun edit-path-to-cmucl-source-path (edit-path)
686 (and edit-path
687 (cons 0
688 (let ((n -1))
689 (loop for i from (1- (integer-length edit-path)) downto 0
690 if (logbitp i edit-path) do (incf n)
691 else collect (prog1 n (setq n 0)))))))
692
693 ;; (edit-path-to-cmucl-source-path #b1111010) => (0 3 1)
694
695 (defimplementation find-definitions (name)
696 (let ((locations (dspec:find-name-locations dspec:*dspec-classes* name)))
697 (loop for (dspec location) in locations
698 collect (list dspec (make-dspec-location dspec location)))))
699
700
701 ;;; Compilation
702
703 (defmacro with-swank-compilation-unit ((location &rest options) &body body)
704 (lw:rebinding (location)
705 `(let ((compiler::*error-database* '()))
706 (with-compilation-unit ,options
707 (multiple-value-prog1 (progn ,@body)
708 (signal-error-data-base compiler::*error-database*
709 ,location)
710 (signal-undefined-functions compiler::*unknown-functions*
711 ,location))))))
712
713 (defimplementation swank-compile-file (input-file output-file
714 load-p external-format
715 &key policy)
716 (declare (ignore policy))
717 (with-swank-compilation-unit (input-file)
718 (compile-file input-file
719 :output-file output-file
720 :load load-p
721 :external-format external-format)))
722
723 (defvar *within-call-with-compilation-hooks* nil
724 "Whether COMPILE-FILE was called from within CALL-WITH-COMPILATION-HOOKS.")
725
726 (defvar *undefined-functions-hash* nil
727 "Hash table to map info about undefined functions to pathnames.")
728
729 (lw:defadvice (compile-file compile-file-and-collect-notes :around)
730 (pathname &rest rest)
731 (multiple-value-prog1 (apply #'lw:call-next-advice pathname rest)
732 (when *within-call-with-compilation-hooks*
733 (maphash (lambda (unfun dspecs)
734 (dolist (dspec dspecs)
735 (let ((unfun-info (list unfun dspec)))
736 (unless (gethash unfun-info *undefined-functions-hash*)
737 (setf (gethash unfun-info *undefined-functions-hash*)
738 pathname)))))
739 compiler::*unknown-functions*))))
740
741 (defimplementation call-with-compilation-hooks (function)
742 (let ((compiler::*error-database* '())
743 (*undefined-functions-hash* (make-hash-table :test 'equal))
744 (*within-call-with-compilation-hooks* t))
745 (with-compilation-unit ()
746 (prog1 (funcall function)
747 (signal-error-data-base compiler::*error-database*)
748 (signal-undefined-functions compiler::*unknown-functions*)))))
749
750 (defun map-error-database (database fn)
751 (loop for (filename . defs) in database do
752 (loop for (dspec . conditions) in defs do
753 (dolist (c conditions)
754 (multiple-value-bind (condition path)
755 (if (consp c) (values (car c) (cdr c)) (values c nil))
756 (funcall fn filename dspec condition path))))))
757
758 (defun lispworks-severity (condition)
759 (cond ((not condition) :warning)
760 (t (etypecase condition
761 (error :error)
762 (style-warning :warning)
763 (warning :warning)))))
764
765 (defun signal-compiler-condition (message location condition)
766 (check-type message string)
767 (signal
768 (make-instance 'compiler-condition :message message
769 :severity (lispworks-severity condition)
770 :location location
771 :original-condition condition)))
772
773 (defvar *temp-file-format* '(:utf-8 :eol-style :lf))
774
775 (defun compile-from-temp-file (string filename)
776 (unwind-protect
777 (progn
778 (with-open-file (s filename :direction :output
779 :if-exists :supersede
780 :external-format *temp-file-format*)
781
782 (write-string string s)
783 (finish-output s))
784 (multiple-value-bind (binary-filename warnings? failure?)
785 (compile-file filename :load t
786 :external-format *temp-file-format*)
787 (declare (ignore warnings?))
788 (when binary-filename
789 (delete-file binary-filename))
790 (not failure?)))
791 (delete-file filename)))
792
793 (defun dspec-function-name-position (dspec fallback)
794 (etypecase dspec
795 (cons (let ((name (dspec:dspec-primary-name dspec)))
796 (typecase name
797 ((or symbol string)
798 (list :function-name (string name)))
799 (t fallback))))
800 (null fallback)
801 (symbol (list :function-name (string dspec)))))
802
803 (defmacro with-fairly-standard-io-syntax (&body body)
804 "Like WITH-STANDARD-IO-SYNTAX but preserve *PACKAGE* and *READTABLE*."
805 (let ((package (gensym))
806 (readtable (gensym)))
807 `(let ((,package *package*)
808 (,readtable *readtable*))
809 (with-standard-io-syntax
810 (let ((*package* ,package)
811 (*readtable* ,readtable))
812 ,@body)))))
813
814 (defun skip-comments (stream)
815 (let ((pos0 (file-position stream)))
816 (cond ((equal (ignore-errors (list (read-delimited-list #\( stream)))
817 '(()))
818 (file-position stream (1- (file-position stream))))
819 (t (file-position stream pos0)))))
820
821 #-(or lispworks4.1 lispworks4.2) ; no dspec:parse-form-dspec prior to 4.3
822 (defun dspec-stream-position (stream dspec)
823 (with-fairly-standard-io-syntax
824 (loop (let* ((pos (progn (skip-comments stream) (file-position stream)))
825 (form (read stream nil '#1=#:eof)))
826 (when (eq form '#1#)
827 (return nil))
828 (labels ((check-dspec (form)
829 (when (consp form)
830 (let ((operator (car form)))
831 (case operator
832 ((progn)
833 (mapcar #'check-dspec
834 (cdr form)))
835 ((eval-when locally macrolet symbol-macrolet)
836 (mapcar #'check-dspec
837 (cddr form)))
838 ((in-package)
839 (let ((package (find-package (second form))))
840 (when package
841 (setq *package* package))))
842 (otherwise
843 (let ((form-dspec (dspec:parse-form-dspec form)))
844 (when (dspec:dspec-equal dspec form-dspec)
845 (return pos)))))))))
846 (check-dspec form))))))
847
848 (defun dspec-file-position (file dspec)
849 (let* ((*compile-file-pathname* (pathname file))
850 (*compile-file-truename* (truename *compile-file-pathname*))
851 (*load-pathname* *compile-file-pathname*)
852 (*load-truename* *compile-file-truename*))
853 (with-open-file (stream file)
854 (let ((pos
855 #-(or lispworks4.1 lispworks4.2)
856 (dspec-stream-position stream dspec)))
857 (if pos
858 (list :position (1+ pos))
859 (dspec-function-name-position dspec `(:position 1)))))))
860
861 (defun emacs-buffer-location-p (location)
862 (and (consp location)
863 (eq (car location) :emacs-buffer)))
864
865 (defun make-dspec-location (dspec location &optional hints)
866 (etypecase location
867 ((or pathname string)
868 (multiple-value-bind (file err)
869 (ignore-errors (namestring (truename location)))
870 (if err
871 (list :error (princ-to-string err))
872 (make-location `(:file ,file)
873 (dspec-file-position file dspec)
874 hints))))
875 (symbol
876 `(:error ,(format nil "Cannot resolve location: ~S" location)))
877 ((satisfies emacs-buffer-location-p)
878 (destructuring-bind (_ buffer offset string) location
879 (declare (ignore _ string))
880 (make-location `(:buffer ,buffer)
881 (dspec-function-name-position dspec `(:offset ,offset 0))
882 hints)))))
883
884 (defun make-dspec-progenitor-location (dspec location edit-path)
885 (let ((canon-dspec (dspec:canonicalize-dspec dspec)))
886 (make-dspec-location
887 (if canon-dspec
888 (if (dspec:local-dspec-p canon-dspec)
889 (dspec:dspec-progenitor canon-dspec)
890 canon-dspec)
891 nil)
892 location
893 (if edit-path
894 (list :edit-path (edit-path-to-cmucl-source-path edit-path))))))
895
896 (defun signal-error-data-base (database &optional location)
897 (map-error-database
898 database
899 (lambda (filename dspec condition edit-path)
900 (signal-compiler-condition
901 (format nil "~A" condition)
902 (make-dspec-progenitor-location dspec (or location filename) edit-path)
903 condition))))
904
905 (defun unmangle-unfun (symbol)
906 "Converts symbols like 'SETF::|\"CL-USER\" \"GET\"| to
907 function names like \(SETF GET)."
908 (cond ((sys::setf-symbol-p symbol)
909 (sys::setf-pair-from-underlying-name symbol))
910 (t symbol)))
911
912 (defun signal-undefined-functions (htab &optional filename)
913 (maphash (lambda (unfun dspecs)
914 (dolist (dspec dspecs)
915 (signal-compiler-condition
916 (format nil "Undefined function ~A" (unmangle-unfun unfun))
917 (make-dspec-progenitor-location
918 dspec
919 (or filename
920 (gethash (list unfun dspec) *undefined-functions-hash*))
921 nil)
922 nil)))
923 htab))
924
925 (defimplementation swank-compile-string (string &key buffer position filename
926 policy)
927 (declare (ignore filename policy))
928 (assert buffer)
929 (assert position)
930 (let* ((location (list :emacs-buffer buffer position string))
931 (tmpname (hcl:make-temp-file nil "lisp")))
932 (with-swank-compilation-unit (location)
933 (compile-from-temp-file
934 (with-output-to-string (s)
935 (let ((*print-radix* t))
936 (print `(eval-when (:compile-toplevel)
937 (setq dspec::*location* (list ,@location)))
938 s))
939 (write-string string s))
940 tmpname))))
941
942 ;;; xref
943
944 (defmacro defxref (name function)
945 `(defimplementation ,name (name)
946 (xref-results (,function name))))
947
948 (defxref who-calls hcl:who-calls)
949 (defxref who-macroexpands hcl:who-calls) ; macros are in the calls table too
950 (defxref calls-who hcl:calls-who)
951 (defxref list-callers list-callers-internal)
952 (defxref list-callees list-callees-internal)
953
954 (defun list-callers-internal (name)
955 (let ((callers (make-array 100
956 :fill-pointer 0
957 :adjustable t)))
958 (hcl:sweep-all-objects
959 #'(lambda (object)
960 (when (and #+Harlequin-PC-Lisp (low:compiled-code-p object)
961 #+Harlequin-Unix-Lisp (sys:callablep object)
962 #-(or Harlequin-PC-Lisp Harlequin-Unix-Lisp) (sys:compiled-code-p object)
963 (system::find-constant$funcallable name object))
964 (vector-push-extend object callers))))
965 ;; Delay dspec:object-dspec until after sweep-all-objects
966 ;; to reduce allocation problems.
967 (loop for object across callers
968 collect (if (symbolp object)
969 (list 'function object)
970 (or (dspec:object-dspec object) object)))))
971
972 (defun list-callees-internal (name)
973 (let ((callees '()))
974 (system::find-constant$funcallable
975 'junk name
976 :test #'(lambda (junk constant)
977 (declare (ignore junk))
978 (when (and (symbolp constant)
979 (fboundp constant))
980 (pushnew (list 'function constant) callees :test 'equal))
981 ;; Return nil so we iterate over all constants.
982 nil))
983 callees))
984
985 ;; only for lispworks 4.2 and above
986 #-lispworks4.1
987 (progn
988 (defxref who-references hcl:who-references)
989 (defxref who-binds hcl:who-binds)
990 (defxref who-sets hcl:who-sets))
991
992 (defimplementation who-specializes (classname)
993 (let ((methods (clos:class-direct-methods (find-class classname))))
994 (xref-results (mapcar #'dspec:object-dspec methods))))
995
996 (defun xref-results (dspecs)
997 (flet ((frob-locs (dspec locs)
998 (cond (locs
999 (loop for (name loc) in locs
1000 collect (list name (make-dspec-location name loc))))
1001 (t `((,dspec (:error "Source location not available")))))))
1002 (loop for dspec in dspecs
1003 append (frob-locs dspec (dspec:dspec-definition-locations dspec)))))
1004
1005 ;;; Inspector
1006
1007 (defmethod emacs-inspect ((o t))
1008 (lispworks-inspect o))
1009
1010 (defmethod emacs-inspect ((o function))
1011 (lispworks-inspect o))
1012
1013 ;; FIXME: slot-boundp-using-class in LW works with names so we can't
1014 ;; use our method in swank.lisp.
1015 (defmethod emacs-inspect ((o standard-object))
1016 (lispworks-inspect o))
1017
1018 (defun lispworks-inspect (o)
1019 (multiple-value-bind (names values _getter _setter type)
1020 (lw:get-inspector-values o nil)
1021 (declare (ignore _getter _setter))
1022 (append
1023 (label-value-line "Type" type)
1024 (loop for name in names
1025 for value in values
1026 append (label-value-line name value)))))
1027
1028 ;;; Miscellaneous
1029
1030 (defimplementation quit-lisp ()
1031 (lispworks:quit))
1032
1033 ;;; Tracing
1034
1035 (defun parse-fspec (fspec)
1036 "Return a dspec for FSPEC."
1037 (ecase (car fspec)
1038 ((:defmethod) `(method ,(cdr fspec)))))
1039
1040 (defun tracedp (dspec)
1041 (member dspec (eval '(trace)) :test #'equal))
1042
1043 (defun toggle-trace-aux (dspec)
1044 (cond ((tracedp dspec)
1045 (eval `(untrace ,dspec))
1046 (format nil "~S is now untraced." dspec))
1047 (t
1048 (eval `(trace (,dspec)))
1049 (format nil "~S is now traced." dspec))))
1050
1051 (defimplementation toggle-trace (fspec)
1052 (toggle-trace-aux (parse-fspec fspec)))
1053
1054 ;;; Multithreading
1055
1056 (defimplementation initialize-multiprocessing (continuation)
1057 (cond ((not mp::*multiprocessing*)
1058 (push (list "Initialize SLIME" '() continuation)
1059 mp:*initial-processes*)
1060 (mp:initialize-multiprocessing))
1061 (t (funcall continuation))))
1062
1063 (defimplementation spawn (fn &key name)
1064 (mp:process-run-function name () fn))
1065
1066 (defvar *id-lock* (mp:make-lock))
1067 (defvar *thread-id-counter* 0)
1068
1069 (defimplementation thread-id (thread)
1070 (mp:with-lock (*id-lock*)
1071 (or (getf (mp:process-plist thread) 'id)
1072 (setf (getf (mp:process-plist thread) 'id)
1073 (incf *thread-id-counter*)))))
1074
1075 (defimplementation find-thread (id)
1076 (find id (mp:list-all-processes)
1077 :key (lambda (p) (getf (mp:process-plist p) 'id))))
1078
1079 (defimplementation thread-name (thread)
1080 (mp:process-name thread))
1081
1082 (defimplementation thread-status (thread)
1083 (format nil "~A ~D"
1084 (mp:process-whostate thread)
1085 (mp:process-priority thread)))
1086
1087 (defimplementation make-lock (&key name)
1088 (mp:make-lock :name name))
1089
1090 (defimplementation call-with-lock-held (lock function)
1091 (mp:with-lock (lock) (funcall function)))
1092
1093 (defimplementation current-thread ()
1094 mp:*current-process*)
1095
1096 (defimplementation all-threads ()
1097 (mp:list-all-processes))
1098
1099 (defimplementation interrupt-thread (thread fn)
1100 (mp:process-interrupt thread fn))
1101
1102 (defimplementation kill-thread (thread)
1103 (mp:process-kill thread))
1104
1105 (defimplementation thread-alive-p (thread)
1106 (mp:process-alive-p thread))
1107
1108 (defstruct (mailbox (:conc-name mailbox.))
1109 (mutex (mp:make-lock :name "thread mailbox"))
1110 (queue '() :type list))
1111
1112 (defvar *mailbox-lock* (mp:make-lock))
1113
1114 (defun mailbox (thread)
1115 (mp:with-lock (*mailbox-lock*)
1116 (or (getf (mp:process-plist thread) 'mailbox)
1117 (setf (getf (mp:process-plist thread) 'mailbox)
1118 (make-mailbox)))))
1119
1120 (defimplementation receive-if (test &optional timeout)
1121 (let* ((mbox (mailbox mp:*current-process*))
1122 (lock (mailbox.mutex mbox)))
1123 (assert (or (not timeout) (eq timeout t)))
1124 (loop
1125 (check-slime-interrupts)
1126 (mp:with-lock (lock "receive-if/try")
1127 (let* ((q (mailbox.queue mbox))
1128 (tail (member-if test q)))
1129 (when tail
1130 (setf (mailbox.queue mbox) (nconc (ldiff q tail) (cdr tail)))
1131 (return (car tail)))))
1132 (when (eq timeout t) (return (values nil t)))
1133 (mp:process-wait-with-timeout
1134 "receive-if" 0.3 (lambda () (some test (mailbox.queue mbox)))))))
1135
1136 (defimplementation send (thread message)
1137 (let ((mbox (mailbox thread)))
1138 (mp:with-lock ((mailbox.mutex mbox))
1139 (setf (mailbox.queue mbox)
1140 (nconc (mailbox.queue mbox) (list message))))))
1141
1142 (defimplementation set-default-initial-binding (var form)
1143 (setq mp:*process-initial-bindings*
1144 (acons var `(eval (quote ,form))
1145 mp:*process-initial-bindings* )))
1146
1147 (defimplementation thread-attributes (thread)
1148 (list :priority (mp:process-priority thread)
1149 :idle (mp:process-idle-time thread)))
1150
1151 ;;; Some intergration with the lispworks environment
1152
1153 (defun swank-sym (name) (find-symbol (string name) :swank))
1154
1155
1156 ;;;; Weak hashtables
1157
1158 (defimplementation make-weak-key-hash-table (&rest args)
1159 (apply #'make-hash-table :weak-kind :key args))
1160
1161 (defimplementation make-weak-value-hash-table (&rest args)
1162 (apply #'make-hash-table :weak-kind :value args))

  ViewVC Help
Powered by ViewVC 1.1.5