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

Contents of /slime/swank-abcl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.33 - (show annotations)
Tue Jan 3 04:46:52 2006 UTC (8 years, 3 months ago) by aruttenberg
Branch: MAIN
Changes since 1.32: +1 -1 lines
*** empty log message ***
1 ;;;; -*- Mode: lisp; indent-tabs-mode: nil; outline-regexp: ";;;;;*"; -*-
2 ;;;
3 ;;; swank-abcl.lisp --- Armedbear CL specific code for SLIME.
4 ;;;
5 ;;; Adapted from swank-acl.lisp, Andras Simon, 2004
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
14 (eval-when (:compile-toplevel :load-toplevel :execute)
15 (require :collect) ;just so that it doesn't spoil the flying letters
16 (require :pprint))
17
18 (defun sys::break (&optional (format-control "BREAK called")
19 &rest format-arguments)
20 (let ((*saved-backtrace* (backtrace-as-list-ignoring-swank-calls)))
21 (with-simple-restart (continue "Return from BREAK.")
22 (invoke-debugger
23 (sys::%make-condition 'simple-condition
24 (list :format-control format-control
25 :format-arguments format-arguments))))
26 nil))
27
28 (defimplementation make-fn-streams (input-fn output-fn)
29 (let* ((output (ext:make-slime-output-stream output-fn))
30 (input (ext:make-slime-input-stream input-fn output)))
31 (values input output)))
32
33 ;;; swank-mop
34
35 ;;dummies:
36
37 (defclass standard-slot-definition ()())
38
39 (defun class-finalized-p (class) t)
40
41 (defun slot-definition-documentation (slot) #+nil (documentation slot 't))
42 (defun slot-definition-type (slot) t)
43 (defun class-prototype (class))
44 (defun generic-function-declarations (gf))
45 (defun specializer-direct-methods (spec) nil)
46
47 (import-to-swank-mop
48 '( ;; classes
49 cl:standard-generic-function
50 standard-slot-definition ;;dummy
51 cl:method
52 cl:standard-class
53 ;; standard-class readers
54 mop::class-default-initargs
55 mop::class-direct-default-initargs
56 mop::class-direct-slots
57 mop::class-direct-subclasses
58 mop::class-direct-superclasses
59 mop::eql-specializer
60 class-finalized-p ;;dummy
61 cl:class-name
62 mop::class-precedence-list
63 class-prototype ;;dummy
64 mop::class-slots
65 specializer-direct-methods ;;dummy
66 ;; eql-specializer accessors
67 mop::eql-specializer-object
68 ;; generic function readers
69 mop::generic-function-argument-precedence-order
70 generic-function-declarations ;;dummy
71 mop::generic-function-lambda-list
72 mop::generic-function-methods
73 mop::generic-function-method-class
74 mop::generic-function-method-combination
75 mop::generic-function-name
76 ;; method readers
77 mop::method-generic-function
78 mop::method-function
79 mop::method-lambda-list
80 mop::method-specializers
81 mop::method-qualifiers
82 ;; slot readers
83 mop::slot-definition-allocation
84 slot-definition-documentation ;;dummy
85 mop::slot-definition-initargs
86 mop::slot-definition-initform
87 mop::slot-definition-initfunction
88 mop::slot-definition-name
89 slot-definition-type ;;dummy
90 mop::slot-definition-readers
91 mop::slot-definition-writers))
92
93 ;;;; TCP Server
94
95
96 (defimplementation preferred-communication-style ()
97 :spawn)
98
99
100
101 (defimplementation create-socket (host port)
102 (ext:make-server-socket port))
103
104
105 (defimplementation local-port (socket)
106 (java:jcall (java:jmethod "java.net.ServerSocket" "getLocalPort") socket))
107
108
109 (defimplementation close-socket (socket)
110 (ext:server-socket-close socket))
111
112 (defimplementation accept-connection (socket
113 &key (external-format :iso-latin-1-unix) buffering)
114 (declare (ignore buffering))
115 (assert (eq external-format :iso-latin-1-unix))
116 (ext:get-socket-stream (ext:socket-accept socket)))
117
118 ;;;; Unix signals
119
120 (defimplementation call-without-interrupts (fn)
121 (funcall fn))
122
123 ;;there are too many to count
124 (defimplementation getpid ()
125 0)
126
127 (defimplementation lisp-implementation-type-name ()
128 "armedbear")
129
130 (defimplementation set-default-directory (directory)
131 (let ((dir (sys::probe-directory directory)))
132 (when dir (setf *default-pathname-defaults* dir))
133 (namestring dir)))
134
135
136 ;;;; Misc
137
138
139 (defimplementation arglist ((symbol t))
140 (multiple-value-bind (arglist present)
141 (sys::arglist symbol)
142 (if present arglist :not-available)))
143
144
145 (defimplementation function-name (function)
146 (nth-value 2 (function-lambda-expression function)))
147
148 (defimplementation macroexpand-all (form)
149 (macroexpand form))
150
151 (defimplementation describe-symbol-for-emacs (symbol)
152 (let ((result '()))
153 (flet ((doc (kind &optional (sym symbol))
154 (or (documentation sym kind) :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 :function (if (fboundp symbol)
163 (doc 'function)))
164 (maybe-push
165 :class (if (find-class symbol nil)
166 (doc 'class)))
167 result)))
168
169
170 (defimplementation describe-definition (symbol namespace)
171 (ecase namespace
172 (:variable
173 (describe symbol))
174 ((:function :generic-function)
175 (describe (symbol-function symbol)))
176 (:class
177 (describe (find-class symbol)))))
178
179 (defimplementation describe-definition (symbol namespace)
180 (ecase namespace
181 (:variable
182 (describe symbol))
183 ((:function :generic-function)
184 (describe (symbol-function symbol)))
185 (:class
186 (describe (find-class symbol)))))
187
188
189 ;;;; Debugger
190
191 (defvar *sldb-topframe*)
192
193 (defun backtrace-as-list-ignoring-swank-calls ()
194 (let ((list (ext:backtrace-as-list)))
195 (subseq list (1+ (or (position (intern "SWANK-DEBUGGER-HOOK" 'swank) list :key 'car) -1)))))
196
197 (defimplementation call-with-debugging-environment (debugger-loop-fn)
198 (let ((*sldb-topframe* (car (backtrace-as-list-ignoring-swank-calls)) #+nil (excl::int-newest-frame)))
199 (funcall debugger-loop-fn)))
200
201 (defun nth-frame (index)
202 (nth index (backtrace-as-list-ignoring-swank-calls)))
203
204 (defimplementation compute-backtrace (start end)
205 (let ((end (or end most-positive-fixnum)))
206 (subseq (backtrace-as-list-ignoring-swank-calls) start end)))
207
208 (defimplementation print-frame (frame stream)
209 (write-string (string-trim '(#\space #\newline)
210 (prin1-to-string frame))
211 stream))
212
213 (defimplementation frame-locals (index)
214 `(,(list :name "??" :id 0 :value "??")))
215
216
217 (defimplementation frame-catch-tags (index)
218 (declare (ignore index))
219 nil)
220
221 #+nil
222 (defimplementation disassemble-frame (index)
223 (disassemble (debugger:frame-function (nth-frame index))))
224
225 (defimplementation frame-source-location-for-emacs (index)
226 (list :error (format nil "Cannot find source for frame: ~A"
227 (nth-frame index))))
228
229 #+nil
230 (defimplementation eval-in-frame (form frame-number)
231 (debugger:eval-form-in-context
232 form
233 (debugger:environment-of-frame (nth-frame frame-number))))
234
235 #+nil
236 (defimplementation return-from-frame (frame-number form)
237 (let ((frame (nth-frame frame-number)))
238 (multiple-value-call #'debugger:frame-return
239 frame (debugger:eval-form-in-context
240 form
241 (debugger:environment-of-frame frame)))))
242
243 ;;; XXX doesn't work for frames with arguments
244 #+nil
245 (defimplementation restart-frame (frame-number)
246 (let ((frame (nth-frame frame-number)))
247 (debugger:frame-retry frame (debugger:frame-function frame))))
248
249 ;;;; Compiler hooks
250
251 (defvar *buffer-name* nil)
252 (defvar *buffer-start-position*)
253 (defvar *buffer-string*)
254 (defvar *compile-filename*)
255
256 (defun handle-compiler-warning (condition)
257 #+nil
258 (let ((loc (getf (slot-value condition 'excl::plist) :loc)))
259 (signal (make-condition
260 'compiler-condition
261 :original-condition condition
262 :severity :warning
263 :message (format nil "~A" condition)
264 :location (cond (*buffer-name*
265 (make-location
266 (list :buffer *buffer-name*)
267 (list :position *buffer-start-position*)))
268 (loc
269 (destructuring-bind (file . pos) loc
270 (make-location
271 (list :file (namestring (truename file)))
272 (list :position (1+ pos)))))
273 (t
274 (make-location
275 (list :file *compile-filename*)
276 (list :position 1))))))))
277
278 (defimplementation swank-compile-file (filename load-p
279 &optional external-format)
280 (declare (ignore external-format))
281 (handler-bind ((warning #'handle-compiler-warning))
282 (let ((*buffer-name* nil)
283 (*compile-filename* filename))
284 (multiple-value-bind (fn warn fail) (compile-file filename)
285 (when (and load-p (not fail))
286 (load fn))))))
287
288 (defimplementation swank-compile-string (string &key buffer position directory)
289 (declare (ignore directory))
290 (handler-bind ((warning #'handle-compiler-warning))
291 (let ((*buffer-name* buffer)
292 (*buffer-start-position* position)
293 (*buffer-string* string))
294 (funcall (compile nil (read-from-string
295 (format nil "(~S () ~A)" 'lambda string)))))))
296
297 #|
298 ;;;; Definition Finding
299
300 (defun find-fspec-location (fspec type)
301 (let ((file (excl::fspec-pathname fspec type)))
302 (etypecase file
303 (pathname
304 (let ((start (scm:find-definition-in-file fspec type file)))
305 (make-location (list :file (namestring (truename file)))
306 (if start
307 (list :position (1+ start))
308 (list :function-name (string fspec))))))
309 ((member :top-level)
310 (list :error (format nil "Defined at toplevel: ~A" fspec)))
311 (null
312 (list :error (format nil "Unkown source location for ~A" fspec))))))
313
314 (defun fspec-definition-locations (fspec)
315 (let ((defs (excl::find-multiple-definitions fspec)))
316 (loop for (fspec type) in defs
317 collect (list fspec (find-fspec-location fspec type)))))
318
319 (defimplementation find-definitions (symbol)
320 (fspec-definition-locations symbol))
321
322 |#
323
324 (defun source-location (symbol)
325 (when (pathnamep (ext:source-pathname symbol))
326 `(((,symbol)
327 (:location
328 (:file ,(namestring (ext:source-pathname symbol)))
329 (:position ,(or (ext:source-file-position symbol) 0) t)
330 (:snippet nil))))))
331
332
333 (defimplementation find-definitions (symbol)
334 (source-location symbol))
335
336 #|
337 Uncomment this if you have patched xref.lisp, as in
338 http://article.gmane.org/gmane.lisp.slime.devel/2425
339 Also, make sure that xref.lisp is loaded by modifying the armedbear
340 part of *sysdep-pathnames* in swank.loader.lisp.
341
342 ;;;; XREF
343 (setq pxref:*handle-package-forms* '(cl:in-package))
344
345 (defmacro defxref (name function)
346 `(defimplementation ,name (name)
347 (xref-results (,function name))))
348
349 (defxref who-calls pxref:list-callers)
350 (defxref who-references pxref:list-readers)
351 (defxref who-binds pxref:list-setters)
352 (defxref who-sets pxref:list-setters)
353 (defxref list-callers pxref:list-callers)
354 (defxref list-callees pxref:list-callees)
355
356 (defun xref-results (symbols)
357 (let ((xrefs '()))
358 (dolist (symbol symbols)
359 (push (list symbol (cadar (source-location symbol))) xrefs))
360 xrefs))
361 |#
362
363 ;;;; Inspecting
364
365 (defclass abcl-inspector (inspector)
366 ())
367
368 (defimplementation make-default-inspector ()
369 (make-instance 'abcl-inspector))
370
371 (defmethod inspect-for-emacs ((slot mop::slot-definition) (inspector abcl-inspector))
372 (declare (ignore inspector))
373 (values "A slot."
374 `("Name: " (:value ,(mop::%slot-definition-name slot))
375 (:newline)
376 "Documentation:" (:newline)
377 ,@(when (slot-definition-documentation slot)
378 `((:value ,(slot-definition-documentation slot)) (:newline)))
379 "Initialization:" (:newline)
380 " Args: " (:value ,(mop::%slot-definition-initargs slot)) (:newline)
381 " Form: " ,(if (mop::%slot-definition-initfunction slot)
382 `(:value ,(mop::%slot-definition-initform slot))
383 "#<unspecified>") (:newline)
384 " Function: " (:value ,(mop::%slot-definition-initfunction slot))
385 (:newline))))
386
387 (defmethod inspect-for-emacs ((f function) (inspector abcl-inspector))
388 (declare (ignore inspector))
389 (values "A function."
390 `(,@(when (function-name f)
391 `("Name: "
392 ,(princ-to-string (function-name f)) (:newline)))
393 ,@(multiple-value-bind (args present)
394 (sys::arglist f)
395 (when present `("Argument list: " ,(princ-to-string args) (:newline))))
396 (:newline)
397 #+nil,@(when (documentation f t)
398 `("Documentation:" (:newline) ,(documentation f t) (:newline)))
399 ,@(when (function-lambda-expression f)
400 `("Lambda expression:"
401 (:newline) ,(princ-to-string (function-lambda-expression f)) (:newline))))))
402
403 #|
404
405 (defimplementation inspect-for-emacs ((o t) (inspector abcl-inspector))
406 (let* ((class (class-of o))
407 (slots (mop::class-slots class)))
408 (values (format nil "~A~% is a ~A" o class)
409 (mapcar (lambda (slot)
410 (let ((name (mop::slot-definition-name slot)))
411 (cons (princ-to-string name)
412 (slot-value o name))))
413 slots))))
414 |#
415
416 ;;;; Multithreading
417
418 (defimplementation startup-multiprocessing ()
419 #+nil(mp:start-scheduler))
420
421 (defimplementation spawn (fn &key name)
422 (ext:make-thread (lambda () (funcall fn)) :name name))
423
424 (defvar *thread-props-lock* (ext:make-thread-lock))
425
426 (defvar *thread-props* (make-hash-table) ; should be a weak table
427 "A hashtable mapping threads to a plist.")
428
429 (defvar *thread-id-counter* 0)
430
431 (defimplementation thread-id (thread)
432 (ext:with-thread-lock (*thread-props-lock*)
433 (or (getf (gethash thread *thread-props*) 'id)
434 (setf (getf (gethash thread *thread-props*) 'id)
435 (incf *thread-id-counter*)))))
436
437 (defimplementation find-thread (id)
438 (find id (all-threads)
439 :key (lambda (thread)
440 (getf (gethash thread *thread-props*) 'id))))
441
442 (defimplementation thread-name (thread)
443 (ext:thread-name thread))
444
445 (defimplementation thread-status (thread)
446 (format nil "Thread is ~:[dead~;alive~]" (ext:thread-alive-p thread)))
447
448 (defimplementation make-lock (&key name)
449 (ext:make-thread-lock))
450
451 (defimplementation call-with-lock-held (lock function)
452 (ext:with-thread-lock (lock) (funcall function)))
453
454 (defimplementation current-thread ()
455 (ext:current-thread))
456
457 (defimplementation all-threads ()
458 (copy-list (ext:mapcar-threads #'identity)))
459
460 (defimplementation interrupt-thread (thread fn)
461 (ext:interrupt-thread thread fn))
462
463 (defimplementation kill-thread (thread)
464 (ext:destroy-thread thread))
465
466 (defun mailbox (thread)
467 "Return THREAD's mailbox."
468 (ext:with-thread-lock (*thread-props-lock*)
469 (or (getf (gethash thread *thread-props*) 'mailbox)
470 (setf (getf (gethash thread *thread-props*) 'mailbox)
471 (ext:make-mailbox)))))
472
473 (defimplementation send (thread object)
474 (ext:mailbox-send (mailbox thread) object))
475
476 (defimplementation receive ()
477 (ext:mailbox-read (mailbox (ext:current-thread))))
478
479 ;;; Auto-flush streams
480
481 ;; XXX race conditions
482 (defvar *auto-flush-streams* '())
483
484 (defvar *auto-flush-thread* nil)
485
486 (defimplementation make-stream-interactive (stream)
487 (setq *auto-flush-streams* (adjoin stream *auto-flush-streams*))
488 (unless *auto-flush-thread*
489 (setq *auto-flush-thread*
490 (ext:make-thread #'flush-streams
491 :name "auto-flush-thread"))))
492
493 (defun flush-streams ()
494 (loop
495 (setq *auto-flush-streams*
496 (remove-if (lambda (x)
497 (not (and (open-stream-p x)
498 (output-stream-p x))))
499 *auto-flush-streams*))
500 (mapc #'finish-output *auto-flush-streams*)
501 (sleep 0.15)))
502
503 (defimplementation quit-lisp ()
504 (ext:exit))
505

  ViewVC Help
Powered by ViewVC 1.1.5