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

Contents of /slime/swank-lispworks.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5