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

Contents of /slime/swank-allegro.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.38 - (show annotations)
Tue Jun 22 06:24:48 2004 UTC (9 years, 9 months ago) by lgorrie
Branch: MAIN
Changes since 1.37: +3 -1 lines
Use `unbound-slot-filler' for unbound inspector slots.
1 ;;;; -*- Mode: lisp; indent-tabs-mode: nil; outline-regexp: ";;;;;*"; -*-
2 ;;;
3 ;;; swank-allegro.lisp --- Allegro CL 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. This code was written for "Allegro CL Trial
9 ;;; Edition "5.0 [Linux/X86] (8/29/98 10:57)".
10 ;;;
11
12 (in-package :swank-backend)
13
14 (eval-when (:compile-toplevel :load-toplevel :execute)
15 (require :sock)
16 (require :process)
17
18 (import
19 '(excl:fundamental-character-output-stream
20 excl:stream-write-char
21 excl:stream-force-output
22 excl:fundamental-character-input-stream
23 excl:stream-read-char
24 excl:stream-listen
25 excl:stream-unread-char
26 excl:stream-clear-input
27 excl:stream-line-column
28 excl:stream-read-char-no-hang)))
29
30 ;;;; TCP Server
31
32 (defimplementation preferred-communication-style ()
33 :spawn)
34
35 (defimplementation create-socket (host port)
36 (socket:make-socket :connect :passive :local-port port
37 :local-host host :reuse-address t))
38
39 (defimplementation local-port (socket)
40 (socket:local-port socket))
41
42 (defimplementation close-socket (socket)
43 (close socket))
44
45 (defimplementation accept-connection (socket)
46 (socket:accept-connection socket :wait t))
47
48 ;; The following defitinions are workarounds for the buggy
49 ;; no-applicable-method function in Allegro 5. We have to provide an
50 ;; implementation.
51 (defimplementation emacs-connected (stream)
52 (declare (ignore stream)))
53
54 (defimplementation format-sldb-condition (c)
55 (princ-to-string c))
56
57 (defimplementation condition-references (c)
58 (declare (ignore))
59 '())
60
61 ;;;; Unix signals
62
63 (defimplementation call-without-interrupts (fn)
64 (excl:without-interrupts (funcall fn)))
65
66 (defimplementation getpid ()
67 (excl::getpid))
68
69 (defimplementation lisp-implementation-type-name ()
70 "allegro")
71
72 (defimplementation set-default-directory (directory)
73 (excl:chdir directory)
74 (namestring (setf *default-pathname-defaults*
75 (truename (merge-pathnames directory)))))
76
77 (defimplementation default-directory ()
78 (excl:chdir))
79
80 (defimplementation call-with-syntax-hooks (fn)
81 (funcall fn))
82
83 ;;;; Misc
84
85 (defimplementation arglist (symbol)
86 (handler-case (excl:arglist symbol)
87 (simple-error () :not-available)))
88
89 (defimplementation macroexpand-all (form)
90 (excl::walk form))
91
92 (defimplementation describe-symbol-for-emacs (symbol)
93 (let ((result '()))
94 (flet ((doc (kind &optional (sym symbol))
95 (or (documentation sym kind) :not-documented))
96 (maybe-push (property value)
97 (when value
98 (setf result (list* property value result)))))
99 (maybe-push
100 :variable (when (boundp symbol)
101 (doc 'variable)))
102 (maybe-push
103 :function (if (fboundp symbol)
104 (doc 'function)))
105 (maybe-push
106 :class (if (find-class symbol nil)
107 (doc 'class)))
108 result)))
109
110 (defimplementation describe-definition (symbol namespace)
111 (ecase namespace
112 (:variable
113 (describe symbol))
114 ((:function :generic-function)
115 (describe (symbol-function symbol)))
116 (:class
117 (describe (find-class symbol)))))
118
119 ;;;; Debugger
120
121 (defvar *sldb-topframe*)
122
123 (defimplementation call-with-debugging-environment (debugger-loop-fn)
124 (let ((*sldb-topframe* (excl::int-newest-frame))
125 (excl::*break-hook* nil))
126 (funcall debugger-loop-fn)))
127
128 (defun nth-frame (index)
129 (do ((frame *sldb-topframe* (excl::int-next-older-frame frame))
130 (i index (1- i)))
131 ((zerop i) frame)))
132
133 (defimplementation compute-backtrace (start end)
134 (let ((end (or end most-positive-fixnum)))
135 (loop for f = (nth-frame start) then (excl::int-next-older-frame f)
136 for i from start below end
137 while f
138 collect f)))
139
140 (defimplementation print-frame (frame stream)
141 (debugger:output-frame stream frame :moderate))
142
143 (defimplementation frame-locals (index)
144 (let ((frame (nth-frame index)))
145 (loop for i from 0 below (debugger:frame-number-vars frame)
146 collect (list :name (debugger:frame-var-name frame i)
147 :id 0
148 :value (debugger:frame-var-value frame i)))))
149
150 (defimplementation frame-catch-tags (index)
151 (declare (ignore index))
152 nil)
153
154 (defimplementation disassemble-frame (index)
155 (disassemble (debugger:frame-function (nth-frame index))))
156
157 (defimplementation frame-source-location-for-emacs (index)
158 (let* ((frame (nth-frame index))
159 (expr (debugger:frame-expression frame))
160 (fspec (first expr)))
161 (second (first (fspec-definition-locations fspec)))))
162
163 (defimplementation eval-in-frame (form frame-number)
164 (debugger:eval-form-in-context
165 form
166 (debugger:environment-of-frame (nth-frame frame-number))))
167
168 (defimplementation return-from-frame (frame-number form)
169 (let ((frame (nth-frame frame-number)))
170 (multiple-value-call #'debugger:frame-return
171 frame (debugger:eval-form-in-context
172 form
173 (debugger:environment-of-frame frame)))))
174
175 ;;; XXX doesn't work for frames with arguments
176 (defimplementation restart-frame (frame-number)
177 (let ((frame (nth-frame frame-number)))
178 (debugger:frame-retry frame (debugger:frame-function frame))))
179
180 ;;;; Compiler hooks
181
182 (defvar *buffer-name* nil)
183 (defvar *buffer-start-position*)
184 (defvar *buffer-string*)
185 (defvar *compile-filename* nil)
186
187 (defun handle-compiler-warning (condition)
188 (let ((loc (getf (slot-value condition 'excl::plist) :loc)))
189 (signal (make-condition
190 'compiler-condition
191 :original-condition condition
192 :severity :warning
193 :message (format nil "~A" condition)
194 :location (cond (*buffer-name*
195 (make-location
196 (list :buffer *buffer-name*)
197 (list :position *buffer-start-position*)))
198 (loc
199 (destructuring-bind (file . pos) loc
200 (make-location
201 (list :file (namestring (truename file)))
202 (list :position (1+ pos)))))
203 (*compile-filename*
204 (make-location
205 (list :file *compile-filename*)
206 (list :position 1)))
207 (t
208 (list :error "No error location available.")))))))
209
210 (defimplementation call-with-compilation-hooks (function)
211 (handler-bind ((warning #'handle-compiler-warning))
212 (funcall function)))
213
214 (defimplementation swank-compile-file (*compile-filename* load-p)
215 (with-compilation-hooks ()
216 (let ((*buffer-name* nil))
217 (compile-file *compile-filename* :load-after-compile load-p))))
218
219 (defimplementation swank-compile-string (string &key buffer position)
220 (with-compilation-hooks ()
221 (let ((*buffer-name* buffer)
222 (*buffer-start-position* position)
223 (*buffer-string* string))
224 (funcall (compile nil (read-from-string
225 (format nil "(~S () ~A)" 'lambda string)))))))
226
227 ;;;; Definition Finding
228
229 (defun fspec-primary-name (fspec)
230 (etypecase fspec
231 (symbol (string fspec))
232 (list (string (second fspec)))))
233
234 (defun find-fspec-location (fspec type)
235 (let ((file (excl:source-file fspec)))
236 (etypecase file
237 (pathname
238 (let* ((start (scm:find-definition-in-file fspec type file))
239 (pos (if start
240 (list :position (1+ start))
241 (list :function-name (fspec-primary-name fspec)))))
242 (make-location (list :file (namestring (truename file)))
243 pos)))
244 ((member :top-level)
245 (list :error (format nil "Defined at toplevel: ~A" fspec)))
246 (null
247 (list :error (format nil "Unknown source location for ~A" fspec))))))
248
249 (defun fspec-definition-locations (fspec)
250 (let ((defs (excl::find-multiple-definitions fspec)))
251 (loop for (fspec type) in defs
252 collect (list fspec (find-fspec-location fspec type)))))
253
254 (defimplementation find-definitions (symbol)
255 (fspec-definition-locations symbol))
256
257 ;;;; XREF
258
259 (defmacro defxref (name relation name1 name2)
260 `(defimplementation ,name (x)
261 (xref-result (xref:get-relation ,relation ,name1 ,name2))))
262
263 (defxref who-calls :calls :wild x)
264 (defxref who-references :uses :wild x)
265 (defxref who-binds :binds :wild x)
266 (defxref who-macroexpands :macro-calls :wild x)
267 (defxref who-sets :sets :wild x)
268 (defxref list-callees :calls x :wild)
269
270 (defun xref-result (fspecs)
271 (loop for fspec in fspecs
272 append (fspec-definition-locations fspec)))
273
274 ;;;; Inspecting
275
276 (defmethod inspected-parts (o)
277 (let* ((class (class-of o))
278 (slots (clos:class-slots class)))
279 (values (format nil "~A~% is a ~A" o class)
280 (mapcar (lambda (slot)
281 (let ((name (clos:slot-definition-name slot)))
282 (cons (princ-to-string name)
283 (if (slot-boundp o name)
284 (slot-value o name)
285 (make-unbound-slot-filler)))))
286 slots))))
287
288 ;;;; Multithreading
289
290 (defimplementation startup-multiprocessing ()
291 (mp:start-scheduler))
292
293 (defimplementation spawn (fn &key name)
294 (mp:process-run-function name fn))
295
296 (defimplementation thread-name (thread)
297 (mp:process-name thread))
298
299 (defimplementation thread-status (thread)
300 (format nil "~A ~D" (mp:process-whostate thread)
301 (mp:process-priority thread)))
302
303 (defimplementation make-lock (&key name)
304 (mp:make-process-lock :name name))
305
306 (defimplementation call-with-lock-held (lock function)
307 (mp:with-process-lock (lock) (funcall function)))
308
309 (defimplementation current-thread ()
310 mp:*current-process*)
311
312 (defimplementation all-threads ()
313 (copy-list mp:*all-processes*))
314
315 (defimplementation interrupt-thread (thread fn)
316 (mp:process-interrupt thread fn))
317
318 (defimplementation kill-thread (thread)
319 (mp:process-kill thread))
320
321 (defvar *mailbox-lock* (mp:make-process-lock :name "mailbox lock"))
322
323 (defstruct (mailbox (:conc-name mailbox.))
324 (mutex (mp:make-process-lock :name "process mailbox"))
325 (queue '() :type list))
326
327 (defun mailbox (thread)
328 "Return THREAD's mailbox."
329 (mp:with-process-lock (*mailbox-lock*)
330 (or (getf (mp:process-property-list thread) 'mailbox)
331 (setf (getf (mp:process-property-list thread) 'mailbox)
332 (make-mailbox)))))
333
334 (defimplementation send (thread message)
335 (let* ((mbox (mailbox thread))
336 (mutex (mailbox.mutex mbox)))
337 (mp:process-wait-with-timeout
338 "yielding before sending" 0.1
339 (lambda ()
340 (mp:with-process-lock (mutex)
341 (< (length (mailbox.queue mbox)) 10))))
342 (mp:with-process-lock (mutex)
343 (setf (mailbox.queue mbox)
344 (nconc (mailbox.queue mbox) (list message))))))
345
346 (defimplementation receive ()
347 (let* ((mbox (mailbox mp:*current-process*))
348 (mutex (mailbox.mutex mbox)))
349 (mp:process-wait "receive" #'mailbox.queue mbox)
350 (mp:with-process-lock (mutex)
351 (pop (mailbox.queue mbox)))))
352
353 (defimplementation quit-lisp ()
354 (excl:exit 0 :quiet t))

  ViewVC Help
Powered by ViewVC 1.1.5