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

Contents of /slime/swank-abcl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5