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

Contents of /slime/swank-lispworks.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.102 - (show annotations)
Mon Aug 4 09:13:06 2008 UTC (5 years, 8 months ago) by heller
Branch: MAIN
Changes since 1.101: +32 -16 lines
* swank-lispworks.lisp (receive-if): Handle interrupts.
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 `(defmethod ,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 ;;; Definition finding
350
351 (defun frame-location (dspec callee-name)
352 (let ((infos (dspec:find-dspec-locations dspec)))
353 (cond (infos
354 (destructuring-bind ((rdspec location) &rest _) infos
355 (declare (ignore _))
356 (let ((name (and callee-name (symbolp callee-name)
357 (string callee-name))))
358 (make-dspec-location rdspec location
359 `(:call-site ,name)))))
360 (t
361 (list :error (format nil "Source location not available for: ~S"
362 dspec))))))
363
364 (defimplementation find-definitions (name)
365 (let ((locations (dspec:find-name-locations dspec:*dspec-classes* name)))
366 (loop for (dspec location) in locations
367 collect (list dspec (make-dspec-location dspec location)))))
368
369
370 ;;; Compilation
371
372 (defmacro with-swank-compilation-unit ((location &rest options) &body body)
373 (lw:rebinding (location)
374 `(let ((compiler::*error-database* '()))
375 (with-compilation-unit ,options
376 ,@body
377 (signal-error-data-base compiler::*error-database* ,location)
378 (signal-undefined-functions compiler::*unknown-functions* ,location)))))
379
380 (defimplementation swank-compile-file (filename load-p external-format)
381 (with-swank-compilation-unit (filename)
382 (compile-file filename :load load-p :external-format external-format)))
383
384 (defvar *within-call-with-compilation-hooks* nil
385 "Whether COMPILE-FILE was called from within CALL-WITH-COMPILATION-HOOKS.")
386
387 (defvar *undefined-functions-hash* nil
388 "Hash table to map info about undefined functions to pathnames.")
389
390 (lw:defadvice (compile-file compile-file-and-collect-notes :around)
391 (pathname &rest rest)
392 (multiple-value-prog1 (apply #'lw:call-next-advice pathname rest)
393 (when *within-call-with-compilation-hooks*
394 (maphash (lambda (unfun dspecs)
395 (dolist (dspec dspecs)
396 (let ((unfun-info (list unfun dspec)))
397 (unless (gethash unfun-info *undefined-functions-hash*)
398 (setf (gethash unfun-info *undefined-functions-hash*)
399 pathname)))))
400 compiler::*unknown-functions*))))
401
402 (defimplementation call-with-compilation-hooks (function)
403 (let ((compiler::*error-database* '())
404 (*undefined-functions-hash* (make-hash-table :test 'equal))
405 (*within-call-with-compilation-hooks* t))
406 (with-compilation-unit ()
407 (prog1 (funcall function)
408 (signal-error-data-base compiler::*error-database*)
409 (signal-undefined-functions compiler::*unknown-functions*)))))
410
411 (defun map-error-database (database fn)
412 (loop for (filename . defs) in database do
413 (loop for (dspec . conditions) in defs do
414 (dolist (c conditions)
415 (funcall fn filename dspec (if (consp c) (car c) c))))))
416
417 (defun lispworks-severity (condition)
418 (cond ((not condition) :warning)
419 (t (etypecase condition
420 (error :error)
421 (style-warning :warning)
422 (warning :warning)))))
423
424 (defun signal-compiler-condition (message location condition)
425 (check-type message string)
426 (signal
427 (make-instance 'compiler-condition :message message
428 :severity (lispworks-severity condition)
429 :location location
430 :original-condition condition)))
431
432 (defun compile-from-temp-file (string filename)
433 (unwind-protect
434 (progn
435 (with-open-file (s filename :direction :output :if-exists :supersede)
436 (write-string string s)
437 (finish-output s))
438 (let ((binary-filename (compile-file filename :load t)))
439 (when binary-filename
440 (delete-file binary-filename))))
441 (delete-file filename)))
442
443 (defun dspec-buffer-position (dspec offset)
444 (etypecase dspec
445 (cons (let ((name (dspec:dspec-primary-name dspec)))
446 (typecase name
447 ((or symbol string)
448 (list :function-name (string name)))
449 (t (list :position offset)))))
450 (null (list :position offset))
451 (symbol (list :function-name (string dspec)))))
452
453 (defmacro with-fairly-standard-io-syntax (&body body)
454 "Like WITH-STANDARD-IO-SYNTAX but preserve *PACKAGE* and *READTABLE*."
455 (let ((package (gensym))
456 (readtable (gensym)))
457 `(let ((,package *package*)
458 (,readtable *readtable*))
459 (with-standard-io-syntax
460 (let ((*package* ,package)
461 (*readtable* ,readtable))
462 ,@body)))))
463
464 #-(or lispworks4.1 lispworks4.2) ; no dspec:parse-form-dspec prior to 4.3
465 (defun dspec-stream-position (stream dspec)
466 (with-fairly-standard-io-syntax
467 (loop (let* ((pos (file-position stream))
468 (form (read stream nil '#1=#:eof)))
469 (when (eq form '#1#)
470 (return nil))
471 (labels ((check-dspec (form)
472 (when (consp form)
473 (let ((operator (car form)))
474 (case operator
475 ((progn)
476 (mapcar #'check-dspec
477 (cdr form)))
478 ((eval-when locally macrolet symbol-macrolet)
479 (mapcar #'check-dspec
480 (cddr form)))
481 ((in-package)
482 (let ((package (find-package (second form))))
483 (when package
484 (setq *package* package))))
485 (otherwise
486 (let ((form-dspec (dspec:parse-form-dspec form)))
487 (when (dspec:dspec-equal dspec form-dspec)
488 (return pos)))))))))
489 (check-dspec form))))))
490
491 (defun dspec-file-position (file dspec)
492 (let* ((*compile-file-pathname* (pathname file))
493 (*compile-file-truename* (truename *compile-file-pathname*))
494 (*load-pathname* *compile-file-pathname*)
495 (*load-truename* *compile-file-truename*))
496 (with-open-file (stream file)
497 (let ((pos
498 #-(or lispworks4.1 lispworks4.2)
499 (dspec-stream-position stream dspec)))
500 (if pos
501 (list :position (1+ pos) t)
502 (dspec-buffer-position dspec 1))))))
503
504 (defun emacs-buffer-location-p (location)
505 (and (consp location)
506 (eq (car location) :emacs-buffer)))
507
508 (defun make-dspec-location (dspec location &optional hints)
509 (etypecase location
510 ((or pathname string)
511 (multiple-value-bind (file err)
512 (ignore-errors (namestring (truename location)))
513 (if err
514 (list :error (princ-to-string err))
515 (make-location `(:file ,file)
516 (dspec-file-position file dspec)
517 hints))))
518 (symbol
519 `(:error ,(format nil "Cannot resolve location: ~S" location)))
520 ((satisfies emacs-buffer-location-p)
521 (destructuring-bind (_ buffer offset string) location
522 (declare (ignore _ string))
523 (make-location `(:buffer ,buffer)
524 (dspec-buffer-position dspec offset)
525 hints)))))
526
527 (defun make-dspec-progenitor-location (dspec location)
528 (let ((canon-dspec (dspec:canonicalize-dspec dspec)))
529 (make-dspec-location
530 (if canon-dspec
531 (if (dspec:local-dspec-p canon-dspec)
532 (dspec:dspec-progenitor canon-dspec)
533 canon-dspec)
534 nil)
535 location)))
536
537 (defun signal-error-data-base (database &optional location)
538 (map-error-database
539 database
540 (lambda (filename dspec condition)
541 (signal-compiler-condition
542 (format nil "~A" condition)
543 (make-dspec-progenitor-location dspec (or location filename))
544 condition))))
545
546 (defun unmangle-unfun (symbol)
547 "Converts symbols like 'SETF::|\"CL-USER\" \"GET\"| to
548 function names like \(SETF GET)."
549 (cond ((sys::setf-symbol-p symbol)
550 (sys::setf-pair-from-underlying-name symbol))
551 (t symbol)))
552
553 (defun signal-undefined-functions (htab &optional filename)
554 (maphash (lambda (unfun dspecs)
555 (dolist (dspec dspecs)
556 (signal-compiler-condition
557 (format nil "Undefined function ~A" (unmangle-unfun unfun))
558 (make-dspec-progenitor-location dspec
559 (or filename
560 (gethash (list unfun dspec)
561 *undefined-functions-hash*)))
562 nil)))
563 htab))
564
565 (defimplementation swank-compile-string (string &key buffer position directory
566 debug)
567 (declare (ignore directory debug))
568 (assert buffer)
569 (assert position)
570 (let* ((location (list :emacs-buffer buffer position string))
571 (tmpname (hcl:make-temp-file nil "lisp")))
572 (with-swank-compilation-unit (location)
573 (compile-from-temp-file
574 (with-output-to-string (s)
575 (let ((*print-radix* t))
576 (print `(eval-when (:compile-toplevel)
577 (setq dspec::*location* (list ,@location)))
578 s))
579 (write-string string s))
580 tmpname))))
581
582 ;;; xref
583
584 (defmacro defxref (name function)
585 `(defimplementation ,name (name)
586 (xref-results (,function name))))
587
588 (defxref who-calls hcl:who-calls)
589 (defxref who-macroexpands hcl:who-calls) ; macros are in the calls table too
590 (defxref calls-who hcl:calls-who)
591 (defxref list-callers list-callers-internal)
592 ;; (defxref list-callees list-callees-internal)
593
594 (defun list-callers-internal (name)
595 (let ((callers (make-array 100
596 :fill-pointer 0
597 :adjustable t)))
598 (hcl:sweep-all-objects
599 #'(lambda (object)
600 (when (and #+Harlequin-PC-Lisp (low:compiled-code-p object)
601 #-Harlequin-PC-Lisp (sys::callablep object)
602 (system::find-constant$funcallable name object))
603 (vector-push-extend object callers))))
604 ;; Delay dspec:object-dspec until after sweep-all-objects
605 ;; to reduce allocation problems.
606 (loop for object across callers
607 collect (if (symbolp object)
608 (list 'function object)
609 (or (dspec:object-dspec object) object)))))
610
611 ;; only for lispworks 4.2 and above
612 #-lispworks4.1
613 (progn
614 (defxref who-references hcl:who-references)
615 (defxref who-binds hcl:who-binds)
616 (defxref who-sets hcl:who-sets))
617
618 (defimplementation who-specializes (classname)
619 (let ((methods (clos:class-direct-methods (find-class classname))))
620 (xref-results (mapcar #'dspec:object-dspec methods))))
621
622 (defun xref-results (dspecs)
623 (flet ((frob-locs (dspec locs)
624 (cond (locs
625 (loop for (name loc) in locs
626 collect (list name (make-dspec-location name loc))))
627 (t `((,dspec (:error "Source location not available")))))))
628 (loop for dspec in dspecs
629 append (frob-locs dspec (dspec:dspec-definition-locations dspec)))))
630
631 ;;; Inspector
632
633 (defmethod emacs-inspect ((o t))
634 (lispworks-inspect o))
635
636 (defmethod emacs-inspect ((o function))
637 (lispworks-inspect o))
638
639 ;; FIXME: slot-boundp-using-class in LW works with names so we can't
640 ;; use our method in swank.lisp.
641 (defmethod emacs-inspect ((o standard-object))
642 (lispworks-inspect o))
643
644 (defun lispworks-inspect (o)
645 (multiple-value-bind (names values _getter _setter type)
646 (lw:get-inspector-values o nil)
647 (declare (ignore _getter _setter))
648 (append
649 (label-value-line "Type" type)
650 (loop for name in names
651 for value in values
652 append (label-value-line name value)))))
653
654 ;;; Miscellaneous
655
656 (defimplementation quit-lisp ()
657 (lispworks:quit))
658
659 ;;; Tracing
660
661 (defun parse-fspec (fspec)
662 "Return a dspec for FSPEC."
663 (ecase (car fspec)
664 ((:defmethod) `(method ,(cdr fspec)))))
665
666 (defun tracedp (dspec)
667 (member dspec (eval '(trace)) :test #'equal))
668
669 (defun toggle-trace-aux (dspec)
670 (cond ((tracedp dspec)
671 (eval `(untrace ,dspec))
672 (format nil "~S is now untraced." dspec))
673 (t
674 (eval `(trace (,dspec)))
675 (format nil "~S is now traced." dspec))))
676
677 (defimplementation toggle-trace (fspec)
678 (toggle-trace-aux (parse-fspec fspec)))
679
680 ;;; Multithreading
681
682 (defimplementation initialize-multiprocessing (continuation)
683 (cond ((not mp::*multiprocessing*)
684 (push (list "Initialize SLIME" '() continuation)
685 mp:*initial-processes*)
686 (mp:initialize-multiprocessing))
687 (t (funcall continuation))))
688
689 (defimplementation spawn (fn &key name)
690 (let ((mp:*process-initial-bindings*
691 (remove (find-package :cl)
692 mp:*process-initial-bindings*
693 :key (lambda (x) (symbol-package (car x))))))
694 (mp:process-run-function name () fn)))
695
696 (defvar *id-lock* (mp:make-lock))
697 (defvar *thread-id-counter* 0)
698
699 (defimplementation thread-id (thread)
700 (mp:with-lock (*id-lock*)
701 (or (getf (mp:process-plist thread) 'id)
702 (setf (getf (mp:process-plist thread) 'id)
703 (incf *thread-id-counter*)))))
704
705 (defimplementation find-thread (id)
706 (find id (mp:list-all-processes)
707 :key (lambda (p) (getf (mp:process-plist p) 'id))))
708
709 (defimplementation thread-name (thread)
710 (mp:process-name thread))
711
712 (defimplementation thread-status (thread)
713 (format nil "~A ~D"
714 (mp:process-whostate thread)
715 (mp:process-priority thread)))
716
717 (defimplementation make-lock (&key name)
718 (mp:make-lock :name name))
719
720 (defimplementation call-with-lock-held (lock function)
721 (mp:with-lock (lock) (funcall function)))
722
723 (defimplementation current-thread ()
724 mp:*current-process*)
725
726 (defimplementation all-threads ()
727 (mp:list-all-processes))
728
729 (defimplementation interrupt-thread (thread fn)
730 (mp:process-interrupt thread fn))
731
732 (defimplementation kill-thread (thread)
733 (mp:process-kill thread))
734
735 (defimplementation thread-alive-p (thread)
736 (mp:process-alive-p thread))
737
738 (defstruct (mailbox (:conc-name mailbox.))
739 (mutex (mp:make-lock :name "thread mailbox"))
740 (queue '() :type list))
741
742 (defvar *mailbox-lock* (mp:make-lock))
743
744 (defun mailbox (thread)
745 (mp:with-lock (*mailbox-lock*)
746 (or (getf (mp:process-plist thread) 'mailbox)
747 (setf (getf (mp:process-plist thread) 'mailbox)
748 (make-mailbox)))))
749
750 (defimplementation receive ()
751 (let* ((mbox (mailbox mp:*current-process*))
752 (lock (mailbox.mutex mbox)))
753 (loop
754 (mp:process-wait "receive" #'mailbox.queue mbox)
755 (mp:without-interrupts
756 (mp:with-lock (lock "receive/try" 0.1)
757 (when (mailbox.queue mbox)
758 (return (pop (mailbox.queue mbox)))))))))
759
760 (defimplementation receive-if (test)
761 (let* ((mbox (mailbox mp:*current-process*))
762 (lock (mailbox.mutex mbox)))
763 (loop
764 (mp:process-wait "receive-if"
765 (lambda () (some test (mailbox.queue mbox))))
766 (mp:without-interrupts
767 (mp:with-lock (lock "receive-if/try" 0.1)
768 (let* ((q (mailbox.queue mbox))
769 (tail (member-if test q)))
770 (when tail
771 (setf (mailbox.queue mbox) (nconc (ldiff q tail) (cdr tail)))
772 (return (car tail)))))))))
773
774 (defimplementation send (thread message)
775 (let ((mbox (mailbox thread)))
776 (mp:without-interrupts
777 (mp:with-lock ((mailbox.mutex mbox))
778 (setf (mailbox.queue mbox)
779 (nconc (mailbox.queue mbox) (list message)))))))
780
781 ;;; Some intergration with the lispworks environment
782
783 (defun swank-sym (name) (find-symbol (string name) :swank))
784
785 (defimplementation emacs-connected ()
786 (when (eq (eval (swank-sym :*communication-style*))
787 nil)
788 (set-sigint-handler))
789 ;; pop up the slime debugger by default
790 (let ((lw:*handle-warn-on-redefinition* :warn))
791 (defmethod env-internals:environment-display-notifier
792 (env &key restarts condition)
793 (declare (ignore restarts))
794 (funcall (swank-sym :swank-debugger-hook) condition *debugger-hook*))
795 (defmethod env-internals:environment-display-debugger (env)
796 *debug-io*)))
797
798 (defimplementation make-stream-interactive (stream)
799 (unless (find-method #'stream:stream-soft-force-output nil `((eql ,stream))
800 nil)
801 (let ((lw:*handle-warn-on-redefinition* :warn))
802 (defmethod stream:stream-soft-force-output ((o (eql stream)))
803 (force-output o)))))
804
805 (defmethod env-internals:confirm-p ((e slime-env) &optional msg &rest args)
806 (apply (swank-sym :y-or-n-p-in-emacs) msg args))
807
808
809 ;;;; Weak hashtables
810
811 (defimplementation make-weak-key-hash-table (&rest args)
812 (apply #'make-hash-table :weak-kind :key args))
813
814 (defimplementation make-weak-value-hash-table (&rest args)
815 (apply #'make-hash-table :weak-kind :value args))

  ViewVC Help
Powered by ViewVC 1.1.5