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