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

Contents of /slime/swank-lispworks.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.109 - (show annotations)
Sat Aug 9 19:56:52 2008 UTC (5 years, 8 months ago) by heller
Branch: MAIN
Changes since 1.108: +10 -4 lines
* swank-lispworks.lisp (disassemble-frame): 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 :eql-specializer
19 :eql-specializer-object
20 :compute-applicable-methods-using-classes))
21
22 (defun swank-mop:slot-definition-documentation (slot)
23 (documentation slot t))
24
25 (defun swank-mop:compute-applicable-methods-using-classes (gf classes)
26 (clos::compute-applicable-methods-from-classes gf classes))
27
28 ;; lispworks doesn't have the eql-specializer class, it represents
29 ;; them as a list of `(EQL ,OBJECT)
30 (deftype swank-mop:eql-specializer () 'cons)
31
32 (defun swank-mop:eql-specializer-object (eql-spec)
33 (second eql-spec))
34
35 (when (fboundp 'dspec::define-dspec-alias)
36 (dspec::define-dspec-alias defimplementation (name args &rest body)
37 `(defun ,name ,args ,@body)))
38
39 ;;; TCP server
40
41 (defimplementation preferred-communication-style ()
42 :spawn)
43
44 (defun socket-fd (socket)
45 (etypecase socket
46 (fixnum socket)
47 (comm:socket-stream (comm:socket-stream-socket socket))))
48
49 (defimplementation create-socket (host port)
50 (multiple-value-bind (socket where errno)
51 #-(or lispworks4.1 (and macosx lispworks4.3))
52 (comm::create-tcp-socket-for-service port :address host)
53 #+(or lispworks4.1 (and macosx lispworks4.3))
54 (comm::create-tcp-socket-for-service port)
55 (cond (socket socket)
56 (t (error 'network-error
57 :format-control "~A failed: ~A (~D)"
58 :format-arguments (list where
59 (list #+unix (lw:get-unix-error errno))
60 errno))))))
61
62 (defimplementation local-port (socket)
63 (nth-value 1 (comm:get-socket-address (socket-fd socket))))
64
65 (defimplementation close-socket (socket)
66 (comm::close-socket (socket-fd socket)))
67
68 (defimplementation accept-connection (socket
69 &key external-format buffering timeout)
70 (declare (ignore buffering timeout external-format))
71 (let* ((fd (comm::get-fd-from-socket socket)))
72 (assert (/= fd -1))
73 (make-instance 'comm:socket-stream :socket fd :direction :io
74 :element-type 'base-char)))
75
76 (defun set-sigint-handler ()
77 ;; Set SIGINT handler on Swank request handler thread.
78 #-win32
79 (sys::set-signal-handler +sigint+
80 (make-sigint-handler mp:*current-process*)))
81
82 ;;; Coding Systems
83
84 (defvar *external-format-to-coding-system*
85 '(((:latin-1 :eol-style :lf)
86 "latin-1-unix" "iso-latin-1-unix" "iso-8859-1-unix")
87 ((:latin-1)
88 "latin-1" "iso-latin-1" "iso-8859-1")
89 ((:utf-8) "utf-8")
90 ((:utf-8 :eol-style :lf) "utf-8-unix")
91 ((:euc-jp) "euc-jp")
92 ((:euc-jp :eol-style :lf) "euc-jp-unix")
93 ((:ascii) "us-ascii")
94 ((:ascii :eol-style :lf) "us-ascii-unix")))
95
96 (defimplementation find-external-format (coding-system)
97 (car (rassoc-if (lambda (x) (member coding-system x :test #'equal))
98 *external-format-to-coding-system*)))
99
100 ;;; Unix signals
101
102 (defun sigint-handler ()
103 (with-simple-restart (continue "Continue from SIGINT handler.")
104 (invoke-debugger "SIGINT")))
105
106 (defun make-sigint-handler (process)
107 (lambda (&rest args)
108 (declare (ignore args))
109 (mp:process-interrupt process #'sigint-handler)))
110
111 (defimplementation call-without-interrupts (fn)
112 (lw:without-interrupts (funcall fn)))
113
114 (defimplementation getpid ()
115 #+win32 (win32:get-current-process-id)
116 #-win32 (system::getpid))
117
118 (defimplementation lisp-implementation-type-name ()
119 "lispworks")
120
121 (defimplementation set-default-directory (directory)
122 (namestring (hcl:change-directory directory)))
123
124 ;;;; Documentation
125
126 (defimplementation arglist (symbol-or-function)
127 (let ((arglist (lw:function-lambda-list symbol-or-function)))
128 (etypecase arglist
129 ((member :dont-know)
130 :not-available)
131 (list
132 arglist))))
133
134 (defimplementation function-name (function)
135 (nth-value 2 (function-lambda-expression function)))
136
137 (defimplementation macroexpand-all (form)
138 (walker:walk-form form))
139
140 (defun generic-function-p (object)
141 (typep object 'generic-function))
142
143 (defimplementation describe-symbol-for-emacs (symbol)
144 "Return a plist describing SYMBOL.
145 Return NIL if the symbol is unbound."
146 (let ((result '()))
147 (labels ((first-line (string)
148 (let ((pos (position #\newline string)))
149 (if (null pos) string (subseq string 0 pos))))
150 (doc (kind &optional (sym symbol))
151 (let ((string (documentation sym kind)))
152 (if string
153 (first-line string)
154 :not-documented)))
155 (maybe-push (property value)
156 (when value
157 (setf result (list* property value result)))))
158 (maybe-push
159 :variable (when (boundp symbol)
160 (doc 'variable)))
161 (maybe-push
162 :generic-function (if (and (fboundp symbol)
163 (generic-function-p (fdefinition symbol)))
164 (doc 'function)))
165 (maybe-push
166 :function (if (and (fboundp symbol)
167 (not (generic-function-p (fdefinition symbol))))
168 (doc 'function)))
169 (maybe-push
170 :setf (let ((setf-name (sys:underlying-setf-name `(setf ,symbol))))
171 (if (fboundp setf-name)
172 (doc 'setf))))
173 (maybe-push
174 :class (if (find-class symbol nil)
175 (doc 'class)))
176 result)))
177
178 (defimplementation describe-definition (symbol type)
179 (ecase type
180 (:variable (describe-symbol symbol))
181 (:class (describe (find-class symbol)))
182 ((:function :generic-function) (describe-function symbol))
183 (:setf (describe-function (sys:underlying-setf-name `(setf ,symbol))))))
184
185 (defun describe-function (symbol)
186 (cond ((fboundp symbol)
187 (format t "~%(~A~{ ~A~})~%~%~:[(not documented)~;~:*~A~]~%"
188 (string-downcase symbol)
189 (mapcar #'string-upcase
190 (lispworks:function-lambda-list symbol))
191 (documentation symbol 'function))
192 (describe (fdefinition symbol)))
193 (t (format t "~S is not fbound" symbol))))
194
195 (defun describe-symbol (sym)
196 (format t "~A is a symbol in package ~A." sym (symbol-package sym))
197 (when (boundp sym)
198 (format t "~%~%Value: ~A" (symbol-value sym)))
199 (let ((doc (documentation sym 'variable)))
200 (when doc
201 (format t "~%~%Variable documentation:~%~A" doc)))
202 (when (fboundp sym)
203 (describe-function sym)))
204
205 ;;; Debugging
206
207 (defclass slime-env (env:environment)
208 ((debugger-hook :initarg :debugger-hoook)))
209
210 (defun slime-env (hook io-bindings)
211 (make-instance 'slime-env :name "SLIME Environment"
212 :io-bindings io-bindings
213 :debugger-hoook hook))
214
215 (defmethod env-internals:environment-display-notifier
216 ((env slime-env) &key restarts condition)
217 (declare (ignore restarts))
218 (funcall (slot-value env 'debugger-hook) condition *debugger-hook*))
219
220 (defmethod env-internals:environment-display-debugger ((env slime-env))
221 *debug-io*)
222
223 ;;(defimplementation call-with-debugger-hook (hook fun)
224 ;; (let ((*debugger-hook* hook))
225 ;; (env:with-environment ((slime-env hook '()))
226 ;; (funcall fun))))
227
228 (defimplementation install-debugger-globally (function)
229 (setq *debugger-hook* function)
230 (setf (env:environment) (slime-env function '())))
231
232 (defvar *sldb-top-frame*)
233
234 (defun interesting-frame-p (frame)
235 (cond ((or (dbg::call-frame-p frame)
236 (dbg::derived-call-frame-p frame)
237 (dbg::foreign-frame-p frame)
238 (dbg::interpreted-call-frame-p frame))
239 t)
240 ((dbg::catch-frame-p frame) dbg:*print-catch-frames*)
241 ((dbg::binding-frame-p frame) dbg:*print-binding-frames*)
242 ((dbg::handler-frame-p frame) dbg:*print-handler-frames*)
243 ((dbg::restart-frame-p frame) dbg:*print-restart-frames*)
244 ((dbg::open-frame-p frame) dbg:*print-open-frames*)
245 (t nil)))
246
247 (defun nth-next-frame (frame n)
248 "Unwind FRAME N times."
249 (do ((frame frame (dbg::frame-next frame))
250 (i n (if (interesting-frame-p frame) (1- i) i)))
251 ((or (not frame)
252 (and (interesting-frame-p frame) (zerop i)))
253 frame)))
254
255 (defun nth-frame (index)
256 (nth-next-frame *sldb-top-frame* index))
257
258 (defun find-top-frame ()
259 "Return the most suitable top-frame for the debugger."
260 (or (do ((frame (dbg::debugger-stack-current-frame dbg::*debugger-stack*)
261 (nth-next-frame frame 1)))
262 ((or (null frame) ; no frame found!
263 (and (dbg::call-frame-p frame)
264 (eq (dbg::call-frame-function-name frame)
265 'invoke-debugger)))
266 (nth-next-frame frame 1)))
267 ;; if we can't find a invoke-debugger frame, take any old frame at the top
268 (dbg::debugger-stack-current-frame dbg::*debugger-stack*)))
269
270 (defimplementation call-with-debugging-environment (fn)
271 (dbg::with-debugger-stack ()
272 (let ((*sldb-top-frame* (find-top-frame)))
273 (funcall fn))))
274
275 (defimplementation compute-backtrace (start end)
276 (let ((end (or end most-positive-fixnum))
277 (backtrace '()))
278 (do ((frame (nth-frame start) (dbg::frame-next frame))
279 (i start))
280 ((or (not frame) (= i end)) (nreverse backtrace))
281 (when (interesting-frame-p frame)
282 (incf i)
283 (push frame backtrace)))))
284
285 (defun frame-actual-args (frame)
286 (let ((*break-on-signals* nil))
287 (mapcar (lambda (arg)
288 (case arg
289 ((&rest &optional &key) arg)
290 (t
291 (handler-case (dbg::dbg-eval arg frame)
292 (error (e) (format nil "<~A>" arg))))))
293 (dbg::call-frame-arglist frame))))
294
295 (defimplementation print-frame (frame stream)
296 (cond ((dbg::call-frame-p frame)
297 (format stream "~S ~S"
298 (dbg::call-frame-function-name frame)
299 (frame-actual-args frame)))
300 (t (princ frame stream))))
301
302 (defun frame-vars (frame)
303 (first (dbg::frame-locals-format-list frame #'list 75 0)))
304
305 (defimplementation frame-locals (n)
306 (let ((frame (nth-frame n)))
307 (if (dbg::call-frame-p frame)
308 (mapcar (lambda (var)
309 (destructuring-bind (name value symbol location) var
310 (declare (ignore name location))
311 (list :name symbol :id 0
312 :value value)))
313 (frame-vars frame)))))
314
315 (defimplementation frame-var-value (frame var)
316 (let ((frame (nth-frame frame)))
317 (destructuring-bind (_n value _s _l) (nth var (frame-vars frame))
318 (declare (ignore _n _s _l))
319 value)))
320
321 (defimplementation frame-catch-tags (index)
322 (declare (ignore index))
323 nil)
324
325 (defimplementation frame-source-location-for-emacs (frame)
326 (let ((frame (nth-frame frame))
327 (callee (if (plusp frame) (nth-frame (1- frame)))))
328 (if (dbg::call-frame-p frame)
329 (let ((dspec (dbg::call-frame-function-name frame))
330 (cname (and (dbg::call-frame-p callee)
331 (dbg::call-frame-function-name callee))))
332 (if dspec
333 (frame-location dspec cname))))))
334
335 (defimplementation eval-in-frame (form frame-number)
336 (let ((frame (nth-frame frame-number)))
337 (dbg::dbg-eval form frame)))
338
339 (defimplementation return-from-frame (frame-number form)
340 (let* ((frame (nth-frame frame-number))
341 (return-frame (dbg::find-frame-for-return frame)))
342 (dbg::dbg-return-from-call-frame frame form return-frame
343 dbg::*debugger-stack*)))
344
345 (defimplementation restart-frame (frame-number)
346 (let ((frame (nth-frame frame-number)))
347 (dbg::restart-frame frame :same-args t)))
348
349 (defimplementation disassemble-frame (frame-number)
350 (let* ((frame (nth-frame frame-number)))
351 (when (dbg::call-frame-p frame)
352 (let ((function (dbg::get-call-frame-function frame)))
353 (disassemble function)))))
354
355 ;;; Definition finding
356
357 (defun frame-location (dspec callee-name)
358 (let ((infos (dspec:find-dspec-locations dspec)))
359 (cond (infos
360 (destructuring-bind ((rdspec location) &rest _) infos
361 (declare (ignore _))
362 (let ((name (and callee-name (symbolp callee-name)
363 (string callee-name))))
364 (make-dspec-location rdspec location
365 `(:call-site ,name)))))
366 (t
367 (list :error (format nil "Source location not available for: ~S"
368 dspec))))))
369
370 (defimplementation find-definitions (name)
371 (let ((locations (dspec:find-name-locations dspec:*dspec-classes* name)))
372 (loop for (dspec location) in locations
373 collect (list dspec (make-dspec-location dspec location)))))
374
375
376 ;;; Compilation
377
378 (defmacro with-swank-compilation-unit ((location &rest options) &body body)
379 (lw:rebinding (location)
380 `(let ((compiler::*error-database* '()))
381 (with-compilation-unit ,options
382 ,@body
383 (signal-error-data-base compiler::*error-database* ,location)
384 (signal-undefined-functions compiler::*unknown-functions* ,location)))))
385
386 (defimplementation swank-compile-file (filename load-p external-format)
387 (with-swank-compilation-unit (filename)
388 (compile-file filename :load load-p :external-format external-format)))
389
390 (defvar *within-call-with-compilation-hooks* nil
391 "Whether COMPILE-FILE was called from within CALL-WITH-COMPILATION-HOOKS.")
392
393 (defvar *undefined-functions-hash* nil
394 "Hash table to map info about undefined functions to pathnames.")
395
396 (lw:defadvice (compile-file compile-file-and-collect-notes :around)
397 (pathname &rest rest)
398 (multiple-value-prog1 (apply #'lw:call-next-advice pathname rest)
399 (when *within-call-with-compilation-hooks*
400 (maphash (lambda (unfun dspecs)
401 (dolist (dspec dspecs)
402 (let ((unfun-info (list unfun dspec)))
403 (unless (gethash unfun-info *undefined-functions-hash*)
404 (setf (gethash unfun-info *undefined-functions-hash*)
405 pathname)))))
406 compiler::*unknown-functions*))))
407
408 (defimplementation call-with-compilation-hooks (function)
409 (let ((compiler::*error-database* '())
410 (*undefined-functions-hash* (make-hash-table :test 'equal))
411 (*within-call-with-compilation-hooks* t))
412 (with-compilation-unit ()
413 (prog1 (funcall function)
414 (signal-error-data-base compiler::*error-database*)
415 (signal-undefined-functions compiler::*unknown-functions*)))))
416
417 (defun map-error-database (database fn)
418 (loop for (filename . defs) in database do
419 (loop for (dspec . conditions) in defs do
420 (dolist (c conditions)
421 (funcall fn filename dspec (if (consp c) (car c) c))))))
422
423 (defun lispworks-severity (condition)
424 (cond ((not condition) :warning)
425 (t (etypecase condition
426 (error :error)
427 (style-warning :warning)
428 (warning :warning)))))
429
430 (defun signal-compiler-condition (message location condition)
431 (check-type message string)
432 (signal
433 (make-instance 'compiler-condition :message message
434 :severity (lispworks-severity condition)
435 :location location
436 :original-condition condition)))
437
438 (defun compile-from-temp-file (string filename)
439 (unwind-protect
440 (progn
441 (with-open-file (s filename :direction :output :if-exists :supersede)
442 (write-string string s)
443 (finish-output s))
444 (let ((binary-filename (compile-file filename :load t)))
445 (when binary-filename
446 (delete-file binary-filename))))
447 (delete-file filename)))
448
449 (defun dspec-buffer-position (dspec offset)
450 (etypecase dspec
451 (cons (let ((name (dspec:dspec-primary-name dspec)))
452 (typecase name
453 ((or symbol string)
454 (list :function-name (string name)))
455 (t (list :position offset)))))
456 (null (list :position offset))
457 (symbol (list :function-name (string dspec)))))
458
459 (defmacro with-fairly-standard-io-syntax (&body body)
460 "Like WITH-STANDARD-IO-SYNTAX but preserve *PACKAGE* and *READTABLE*."
461 (let ((package (gensym))
462 (readtable (gensym)))
463 `(let ((,package *package*)
464 (,readtable *readtable*))
465 (with-standard-io-syntax
466 (let ((*package* ,package)
467 (*readtable* ,readtable))
468 ,@body)))))
469
470 #-(or lispworks4.1 lispworks4.2) ; no dspec:parse-form-dspec prior to 4.3
471 (defun dspec-stream-position (stream dspec)
472 (with-fairly-standard-io-syntax
473 (loop (let* ((pos (file-position stream))
474 (form (read stream nil '#1=#:eof)))
475 (when (eq form '#1#)
476 (return nil))
477 (labels ((check-dspec (form)
478 (when (consp form)
479 (let ((operator (car form)))
480 (case operator
481 ((progn)
482 (mapcar #'check-dspec
483 (cdr form)))
484 ((eval-when locally macrolet symbol-macrolet)
485 (mapcar #'check-dspec
486 (cddr form)))
487 ((in-package)
488 (let ((package (find-package (second form))))
489 (when package
490 (setq *package* package))))
491 (otherwise
492 (let ((form-dspec (dspec:parse-form-dspec form)))
493 (when (dspec:dspec-equal dspec form-dspec)
494 (return pos)))))))))
495 (check-dspec form))))))
496
497 (defun dspec-file-position (file dspec)
498 (let* ((*compile-file-pathname* (pathname file))
499 (*compile-file-truename* (truename *compile-file-pathname*))
500 (*load-pathname* *compile-file-pathname*)
501 (*load-truename* *compile-file-truename*))
502 (with-open-file (stream file)
503 (let ((pos
504 #-(or lispworks4.1 lispworks4.2)
505 (dspec-stream-position stream dspec)))
506 (if pos
507 (list :position (1+ pos) t)
508 (dspec-buffer-position dspec 1))))))
509
510 (defun emacs-buffer-location-p (location)
511 (and (consp location)
512 (eq (car location) :emacs-buffer)))
513
514 (defun make-dspec-location (dspec location &optional hints)
515 (etypecase location
516 ((or pathname string)
517 (multiple-value-bind (file err)
518 (ignore-errors (namestring (truename location)))
519 (if err
520 (list :error (princ-to-string err))
521 (make-location `(:file ,file)
522 (dspec-file-position file dspec)
523 hints))))
524 (symbol
525 `(:error ,(format nil "Cannot resolve location: ~S" location)))
526 ((satisfies emacs-buffer-location-p)
527 (destructuring-bind (_ buffer offset string) location
528 (declare (ignore _ string))
529 (make-location `(:buffer ,buffer)
530 (dspec-buffer-position dspec offset)
531 hints)))))
532
533 (defun make-dspec-progenitor-location (dspec location)
534 (let ((canon-dspec (dspec:canonicalize-dspec dspec)))
535 (make-dspec-location
536 (if canon-dspec
537 (if (dspec:local-dspec-p canon-dspec)
538 (dspec:dspec-progenitor canon-dspec)
539 canon-dspec)
540 nil)
541 location)))
542
543 (defun signal-error-data-base (database &optional location)
544 (map-error-database
545 database
546 (lambda (filename dspec condition)
547 (signal-compiler-condition
548 (format nil "~A" condition)
549 (make-dspec-progenitor-location dspec (or location filename))
550 condition))))
551
552 (defun unmangle-unfun (symbol)
553 "Converts symbols like 'SETF::|\"CL-USER\" \"GET\"| to
554 function names like \(SETF GET)."
555 (cond ((sys::setf-symbol-p symbol)
556 (sys::setf-pair-from-underlying-name symbol))
557 (t symbol)))
558
559 (defun signal-undefined-functions (htab &optional filename)
560 (maphash (lambda (unfun dspecs)
561 (dolist (dspec dspecs)
562 (signal-compiler-condition
563 (format nil "Undefined function ~A" (unmangle-unfun unfun))
564 (make-dspec-progenitor-location dspec
565 (or filename
566 (gethash (list unfun dspec)
567 *undefined-functions-hash*)))
568 nil)))
569 htab))
570
571 (defimplementation swank-compile-string (string &key buffer position directory
572 debug)
573 (declare (ignore directory debug))
574 (assert buffer)
575 (assert position)
576 (let* ((location (list :emacs-buffer buffer position string))
577 (tmpname (hcl:make-temp-file nil "lisp")))
578 (with-swank-compilation-unit (location)
579 (compile-from-temp-file
580 (with-output-to-string (s)
581 (let ((*print-radix* t))
582 (print `(eval-when (:compile-toplevel)
583 (setq dspec::*location* (list ,@location)))
584 s))
585 (write-string string s))
586 tmpname))))
587
588 ;;; xref
589
590 (defmacro defxref (name function)
591 `(defimplementation ,name (name)
592 (xref-results (,function name))))
593
594 (defxref who-calls hcl:who-calls)
595 (defxref who-macroexpands hcl:who-calls) ; macros are in the calls table too
596 (defxref calls-who hcl:calls-who)
597 (defxref list-callers list-callers-internal)
598 ;; (defxref list-callees list-callees-internal)
599
600 (defun list-callers-internal (name)
601 (let ((callers (make-array 100
602 :fill-pointer 0
603 :adjustable t)))
604 (hcl:sweep-all-objects
605 #'(lambda (object)
606 (when (and #+Harlequin-PC-Lisp (low:compiled-code-p object)
607 #-Harlequin-PC-Lisp (sys::callablep object)
608 (system::find-constant$funcallable name object))
609 (vector-push-extend object callers))))
610 ;; Delay dspec:object-dspec until after sweep-all-objects
611 ;; to reduce allocation problems.
612 (loop for object across callers
613 collect (if (symbolp object)
614 (list 'function object)
615 (or (dspec:object-dspec object) object)))))
616
617 ;; only for lispworks 4.2 and above
618 #-lispworks4.1
619 (progn
620 (defxref who-references hcl:who-references)
621 (defxref who-binds hcl:who-binds)
622 (defxref who-sets hcl:who-sets))
623
624 (defimplementation who-specializes (classname)
625 (let ((methods (clos:class-direct-methods (find-class classname))))
626 (xref-results (mapcar #'dspec:object-dspec methods))))
627
628 (defun xref-results (dspecs)
629 (flet ((frob-locs (dspec locs)
630 (cond (locs
631 (loop for (name loc) in locs
632 collect (list name (make-dspec-location name loc))))
633 (t `((,dspec (:error "Source location not available")))))))
634 (loop for dspec in dspecs
635 append (frob-locs dspec (dspec:dspec-definition-locations dspec)))))
636
637 ;;; Inspector
638
639 (defmethod emacs-inspect ((o t))
640 (lispworks-inspect o))
641
642 (defmethod emacs-inspect ((o function))
643 (lispworks-inspect o))
644
645 ;; FIXME: slot-boundp-using-class in LW works with names so we can't
646 ;; use our method in swank.lisp.
647 (defmethod emacs-inspect ((o standard-object))
648 (lispworks-inspect o))
649
650 (defun lispworks-inspect (o)
651 (multiple-value-bind (names values _getter _setter type)
652 (lw:get-inspector-values o nil)
653 (declare (ignore _getter _setter))
654 (append
655 (label-value-line "Type" type)
656 (loop for name in names
657 for value in values
658 append (label-value-line name value)))))
659
660 ;;; Miscellaneous
661
662 (defimplementation quit-lisp ()
663 (lispworks:quit))
664
665 ;;; Tracing
666
667 (defun parse-fspec (fspec)
668 "Return a dspec for FSPEC."
669 (ecase (car fspec)
670 ((:defmethod) `(method ,(cdr fspec)))))
671
672 (defun tracedp (dspec)
673 (member dspec (eval '(trace)) :test #'equal))
674
675 (defun toggle-trace-aux (dspec)
676 (cond ((tracedp dspec)
677 (eval `(untrace ,dspec))
678 (format nil "~S is now untraced." dspec))
679 (t
680 (eval `(trace (,dspec)))
681 (format nil "~S is now traced." dspec))))
682
683 (defimplementation toggle-trace (fspec)
684 (toggle-trace-aux (parse-fspec fspec)))
685
686 ;;; Multithreading
687
688 (defimplementation initialize-multiprocessing (continuation)
689 (cond ((not mp::*multiprocessing*)
690 (push (list "Initialize SLIME" '() continuation)
691 mp:*initial-processes*)
692 (mp:initialize-multiprocessing))
693 (t (funcall continuation))))
694
695 (defimplementation spawn (fn &key name)
696 (let ((mp:*process-initial-bindings*
697 (remove (find-package :cl)
698 mp:*process-initial-bindings*
699 :key (lambda (x) (symbol-package (car x))))))
700 (mp:process-run-function name () fn)))
701
702 (defvar *id-lock* (mp:make-lock))
703 (defvar *thread-id-counter* 0)
704
705 (defimplementation thread-id (thread)
706 (mp:with-lock (*id-lock*)
707 (or (getf (mp:process-plist thread) 'id)
708 (setf (getf (mp:process-plist thread) 'id)
709 (incf *thread-id-counter*)))))
710
711 (defimplementation find-thread (id)
712 (find id (mp:list-all-processes)
713 :key (lambda (p) (getf (mp:process-plist p) 'id))))
714
715 (defimplementation thread-name (thread)
716 (mp:process-name thread))
717
718 (defimplementation thread-status (thread)
719 (format nil "~A ~D"
720 (mp:process-whostate thread)
721 (mp:process-priority thread)))
722
723 (defimplementation make-lock (&key name)
724 (mp:make-lock :name name))
725
726 (defimplementation call-with-lock-held (lock function)
727 (mp:with-lock (lock) (funcall function)))
728
729 (defimplementation current-thread ()
730 mp:*current-process*)
731
732 (defimplementation all-threads ()
733 (mp:list-all-processes))
734
735 (defimplementation interrupt-thread (thread fn)
736 (mp:process-interrupt thread fn))
737
738 (defimplementation kill-thread (thread)
739 (mp:process-kill thread))
740
741 (defimplementation thread-alive-p (thread)
742 (mp:process-alive-p thread))
743
744 (defstruct (mailbox (:conc-name mailbox.))
745 (mutex (mp:make-lock :name "thread mailbox"))
746 (queue '() :type list))
747
748 (defvar *mailbox-lock* (mp:make-lock))
749
750 (defun mailbox (thread)
751 (mp:with-lock (*mailbox-lock*)
752 (or (getf (mp:process-plist thread) 'mailbox)
753 (setf (getf (mp:process-plist thread) 'mailbox)
754 (make-mailbox)))))
755
756 (defimplementation receive ()
757 (receive-if (constantly t)))
758
759 (defimplementation receive-if (test)
760 (let* ((mbox (mailbox mp:*current-process*))
761 (lock (mailbox.mutex mbox)))
762 (loop
763 (check-slime-interrupts)
764 (mp:with-lock (lock "receive-if/try")
765 (let* ((q (mailbox.queue mbox))
766 (tail (member-if test q)))
767 (when tail
768 (setf (mailbox.queue mbox) (nconc (ldiff q tail) (cdr tail)))
769 (return (car tail)))))
770 (mp:process-wait-with-timeout
771 "receive-if" 0.2 (lambda () (some test (mailbox.queue mbox)))))))
772
773 (defimplementation send (thread message)
774 (let ((mbox (mailbox thread)))
775 (mp:with-lock ((mailbox.mutex mbox))
776 (setf (mailbox.queue mbox)
777 (nconc (mailbox.queue mbox) (list message))))))
778
779 ;;; Some intergration with the lispworks environment
780
781 (defun swank-sym (name) (find-symbol (string name) :swank))
782
783 (defimplementation emacs-connected ()
784 (when (eq (eval (swank-sym :*communication-style*))
785 nil)
786 (set-sigint-handler))
787 ;; pop up the slime debugger by default
788 (let ((lw:*handle-warn-on-redefinition* :warn))
789 (defmethod env-internals:environment-display-notifier
790 (env &key restarts condition)
791 (declare (ignore restarts))
792 (funcall (swank-sym :swank-debugger-hook) condition *debugger-hook*))
793 (defmethod env-internals:environment-display-debugger (env)
794 *debug-io*)))
795
796
797 (defmethod env-internals:confirm-p ((e slime-env) &optional msg &rest args)
798 (apply (swank-sym :y-or-n-p-in-emacs) msg args))
799
800
801 ;;;; Weak hashtables
802
803 (defimplementation make-weak-key-hash-table (&rest args)
804 (apply #'make-hash-table :weak-kind :key args))
805
806 (defimplementation make-weak-value-hash-table (&rest args)
807 (apply #'make-hash-table :weak-kind :value args))

  ViewVC Help
Powered by ViewVC 1.1.5