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

Contents of /slime/swank-abcl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.65 - (hide annotations)
Sun Jun 21 07:22:56 2009 UTC (4 years, 9 months ago) by heller
Branch: MAIN
Changes since 1.64: +1 -1 lines
* swank-backend.lisp (frame-source-location): Renamed from
frame-source-location-for-emacs.  Update callers accordingly.
1 heller 1.41 ;;;; -*- indent-tabs-mode: nil; outline-regexp: ";;;;;*"; -*-
2 heller 1.1 ;;;
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     (eval-when (:compile-toplevel :load-toplevel :execute)
14     (require :collect) ;just so that it doesn't spoil the flying letters
15 asimon 1.17 (require :pprint))
16    
17 heller 1.25 (defun sys::break (&optional (format-control "BREAK called")
18     &rest format-arguments)
19 aruttenberg 1.31 (let ((*saved-backtrace* (backtrace-as-list-ignoring-swank-calls)))
20 asimon 1.20 (with-simple-restart (continue "Return from BREAK.")
21     (invoke-debugger
22     (sys::%make-condition 'simple-condition
23     (list :format-control format-control
24     :format-arguments format-arguments))))
25     nil))
26    
27 heller 1.52 (defimplementation make-output-stream (write-string)
28     (ext:make-slime-output-stream write-string))
29    
30     (defimplementation make-input-stream (read-string)
31 heller 1.53 (ext:make-slime-input-stream read-string
32     (make-synonym-stream '*standard-output*)))
33 heller 1.1
34 aruttenberg 1.34 (defimplementation call-with-compilation-hooks (function)
35     (funcall function))
36    
37 asimon 1.12 ;;; swank-mop
38 asimon 1.13
39 aruttenberg 1.36 ;;dummies and definition
40 asimon 1.15
41 asimon 1.12 (defclass standard-slot-definition ()())
42 asimon 1.15
43 aruttenberg 1.36 ;(defun class-finalized-p (class) t)
44 asimon 1.15
45     (defun slot-definition-documentation (slot) #+nil (documentation slot 't))
46 asimon 1.13 (defun slot-definition-type (slot) t)
47 asimon 1.14 (defun class-prototype (class))
48     (defun generic-function-declarations (gf))
49 aruttenberg 1.36 (defun specializer-direct-methods (spec) (mop::class-direct-methods spec))
50    
51     (defun slot-definition-name (slot)
52     (mop::%slot-definition-name slot))
53    
54     (defun class-slots (class)
55     (mop::%class-slots class))
56    
57     (defun method-generic-function (method)
58     (mop::%method-generic-function method))
59    
60     (defun method-function (method)
61     (mop::%method-function method))
62    
63 aruttenberg 1.37 (defun slot-boundp-using-class (class object slotdef)
64     (system::slot-boundp object (slot-definition-name slotdef)))
65    
66     (defun slot-value-using-class (class object slotdef)
67     (system::slot-value object (slot-definition-name slotdef)))
68 asimon 1.12
69     (import-to-swank-mop
70     '( ;; classes
71     cl:standard-generic-function
72     standard-slot-definition ;;dummy
73     cl:method
74     cl:standard-class
75     ;; standard-class readers
76 asimon 1.26 mop::class-default-initargs
77     mop::class-direct-default-initargs
78     mop::class-direct-slots
79     mop::class-direct-subclasses
80     mop::class-direct-superclasses
81     mop::eql-specializer
82 aruttenberg 1.36 mop::class-finalized-p
83 asimon 1.12 cl:class-name
84 asimon 1.26 mop::class-precedence-list
85 asimon 1.14 class-prototype ;;dummy
86 aruttenberg 1.36 class-slots
87     specializer-direct-methods
88 asimon 1.17 ;; eql-specializer accessors
89 asimon 1.26 mop::eql-specializer-object
90 asimon 1.12 ;; generic function readers
91 asimon 1.26 mop::generic-function-argument-precedence-order
92 asimon 1.14 generic-function-declarations ;;dummy
93 asimon 1.26 mop::generic-function-lambda-list
94     mop::generic-function-methods
95     mop::generic-function-method-class
96     mop::generic-function-method-combination
97     mop::generic-function-name
98 asimon 1.12 ;; method readers
99 aruttenberg 1.36 method-generic-function
100     method-function
101 asimon 1.26 mop::method-lambda-list
102     mop::method-specializers
103     mop::method-qualifiers
104 asimon 1.12 ;; slot readers
105 asimon 1.26 mop::slot-definition-allocation
106 asimon 1.13 slot-definition-documentation ;;dummy
107 asimon 1.26 mop::slot-definition-initargs
108     mop::slot-definition-initform
109     mop::slot-definition-initfunction
110 aruttenberg 1.36 slot-definition-name
111 asimon 1.13 slot-definition-type ;;dummy
112 asimon 1.26 mop::slot-definition-readers
113 aruttenberg 1.37 mop::slot-definition-writers
114     slot-boundp-using-class
115     slot-value-using-class
116     ))
117 asimon 1.12
118 heller 1.1 ;;;; TCP Server
119    
120    
121     (defimplementation preferred-communication-style ()
122 heller 1.51 nil)
123 heller 1.1
124     (defimplementation create-socket (host port)
125     (ext:make-server-socket port))
126    
127     (defimplementation local-port (socket)
128     (java:jcall (java:jmethod "java.net.ServerSocket" "getLocalPort") socket))
129    
130     (defimplementation close-socket (socket)
131     (ext:server-socket-close socket))
132    
133 heller 1.23 (defimplementation accept-connection (socket
134 heller 1.40 &key external-format buffering timeout)
135 trittweiler 1.64 (declare (ignore buffering timeout))
136     (ext:get-socket-stream (ext:socket-accept socket)
137     :external-format external-format))
138    
139     ;;;; External formats
140    
141     (defvar *external-format-to-coding-system*
142     '((:iso-8859-1 "latin-1" "iso-latin-1" "iso-8859-1")
143     ((:iso-8859-1 :eol-style :lf) "latin-1-unix" "iso-latin-1-unix" "iso-8859-1-unix")
144     (:utf-8 "utf-8")
145     ((:utf-8 :eol-style :lf) "utf-8-unix")
146     (:euc-jp "euc-jp")
147     ((:euc-jp :eol-style :lf) "euc-jp-unix")
148     (:us-ascii "us-ascii")
149     ((:us-ascii :eol-style :lf) "us-ascii-unix")))
150    
151     (defimplementation find-external-format (coding-system)
152     (car (rassoc-if (lambda (x)
153     (member coding-system x :test #'equal))
154     *external-format-to-coding-system*)))
155 heller 1.1
156     ;;;; Unix signals
157    
158     (defimplementation call-without-interrupts (fn)
159     (funcall fn))
160    
161     (defimplementation getpid ()
162 heller 1.48 (handler-case
163 trittweiler 1.44 (let* ((runtime
164     (java:jstatic "getRuntime" "java.lang.Runtime"))
165     (command
166     (java:jnew-array-from-array
167     "java.lang.String" #("sh" "-c" "echo $PPID")))
168     (runtime-exec-jmethod
169     ;; Complicated because java.lang.Runtime.exec() is
170     ;; overloaded on a non-primitive type (array of
171 heller 1.48 ;; java.lang.String), so we have to use the actual
172     ;; parameter instance to get java.lang.Class
173 trittweiler 1.44 (java:jmethod "java.lang.Runtime" "exec"
174     (java:jcall
175     (java:jmethod "java.lang.Object" "getClass")
176     command)))
177     (process
178     (java:jcall runtime-exec-jmethod runtime command))
179     (output
180 heller 1.48 (java:jcall (java:jmethod "java.lang.Process" "getInputStream")
181 trittweiler 1.44 process)))
182 heller 1.48 (java:jcall (java:jmethod "java.lang.Process" "waitFor")
183     process)
184     (loop :with b :do
185     (setq b
186     (java:jcall (java:jmethod "java.io.InputStream" "read")
187     output))
188     :until (member b '(-1 #x0a)) ; Either EOF or LF
189     :collecting (code-char b) :into result
190     :finally (return
191     (parse-integer (coerce result 'string)))))
192     (t () 0)))
193 heller 1.1
194     (defimplementation lisp-implementation-type-name ()
195     "armedbear")
196    
197     (defimplementation set-default-directory (directory)
198     (let ((dir (sys::probe-directory directory)))
199     (when dir (setf *default-pathname-defaults* dir))
200     (namestring dir)))
201    
202    
203     ;;;; Misc
204    
205 heller 1.40 (defimplementation arglist (fun)
206     (cond ((symbolp fun)
207     (multiple-value-bind (arglist present) (sys::arglist fun)
208     (if present arglist :not-available)))
209     (t :not-available)))
210 asimon 1.13
211     (defimplementation function-name (function)
212     (nth-value 2 (function-lambda-expression function)))
213 heller 1.1
214     (defimplementation macroexpand-all (form)
215     (macroexpand form))
216    
217     (defimplementation describe-symbol-for-emacs (symbol)
218     (let ((result '()))
219     (flet ((doc (kind &optional (sym symbol))
220     (or (documentation sym kind) :not-documented))
221     (maybe-push (property value)
222     (when value
223     (setf result (list* property value result)))))
224     (maybe-push
225     :variable (when (boundp symbol)
226     (doc 'variable)))
227     (maybe-push
228     :function (if (fboundp symbol)
229     (doc 'function)))
230     (maybe-push
231     :class (if (find-class symbol nil)
232     (doc 'class)))
233     result)))
234    
235    
236     (defimplementation describe-definition (symbol namespace)
237     (ecase namespace
238     (:variable
239     (describe symbol))
240     ((:function :generic-function)
241     (describe (symbol-function symbol)))
242     (:class
243     (describe (find-class symbol)))))
244    
245     (defimplementation describe-definition (symbol namespace)
246     (ecase namespace
247     (:variable
248     (describe symbol))
249     ((:function :generic-function)
250     (describe (symbol-function symbol)))
251     (:class
252     (describe (find-class symbol)))))
253    
254    
255     ;;;; Debugger
256    
257     (defvar *sldb-topframe*)
258    
259 aruttenberg 1.31 (defun backtrace-as-list-ignoring-swank-calls ()
260     (let ((list (ext:backtrace-as-list)))
261 aruttenberg 1.33 (subseq list (1+ (or (position (intern "SWANK-DEBUGGER-HOOK" 'swank) list :key 'car) -1)))))
262 aruttenberg 1.31
263 heller 1.1 (defimplementation call-with-debugging-environment (debugger-loop-fn)
264 aruttenberg 1.31 (let ((*sldb-topframe* (car (backtrace-as-list-ignoring-swank-calls)) #+nil (excl::int-newest-frame)))
265 heller 1.1 (funcall debugger-loop-fn)))
266    
267     (defun nth-frame (index)
268 aruttenberg 1.31 (nth index (backtrace-as-list-ignoring-swank-calls)))
269 heller 1.1
270     (defimplementation compute-backtrace (start end)
271     (let ((end (or end most-positive-fixnum)))
272 trittweiler 1.54 (loop for f in (subseq (backtrace-as-list-ignoring-swank-calls) start end)
273 heller 1.58 collect f)))
274 heller 1.1
275 heller 1.58 (defimplementation print-frame (frame stream)
276 heller 1.25 (write-string (string-trim '(#\space #\newline)
277 heller 1.58 (prin1-to-string frame))
278 heller 1.25 stream))
279 heller 1.1
280     (defimplementation frame-locals (index)
281 aruttenberg 1.31 `(,(list :name "??" :id 0 :value "??")))
282 heller 1.25
283 heller 1.1 #+nil
284     (defimplementation disassemble-frame (index)
285     (disassemble (debugger:frame-function (nth-frame index))))
286    
287 heller 1.65 (defimplementation frame-source-location (index)
288 heller 1.1 (list :error (format nil "Cannot find source for frame: ~A"
289     (nth-frame index))))
290    
291     #+nil
292     (defimplementation eval-in-frame (form frame-number)
293     (debugger:eval-form-in-context
294     form
295     (debugger:environment-of-frame (nth-frame frame-number))))
296    
297     #+nil
298     (defimplementation return-from-frame (frame-number form)
299     (let ((frame (nth-frame frame-number)))
300     (multiple-value-call #'debugger:frame-return
301     frame (debugger:eval-form-in-context
302     form
303     (debugger:environment-of-frame frame)))))
304    
305     ;;; XXX doesn't work for frames with arguments
306     #+nil
307     (defimplementation restart-frame (frame-number)
308     (let ((frame (nth-frame frame-number)))
309     (debugger:frame-retry frame (debugger:frame-function frame))))
310    
311     ;;;; Compiler hooks
312    
313     (defvar *buffer-name* nil)
314     (defvar *buffer-start-position*)
315     (defvar *buffer-string*)
316     (defvar *compile-filename*)
317    
318 aruttenberg 1.38 (in-package :swank-backend)
319    
320 heller 1.1 (defun handle-compiler-warning (condition)
321 heller 1.59 (let ((loc (when (and jvm::*compile-file-pathname*
322     system::*source-position*)
323     (cons jvm::*compile-file-pathname* system::*source-position*))))
324     ;; filter condition signaled more than once.
325     (unless (member condition *abcl-signaled-conditions*)
326 aruttenberg 1.38 (push condition *abcl-signaled-conditions*)
327     (signal (make-condition
328     'compiler-condition
329     :original-condition condition
330     :severity :warning
331     :message (format nil "~A" condition)
332     :location (cond (*buffer-name*
333     (make-location
334     (list :buffer *buffer-name*)
335 heller 1.55 (list :offset *buffer-start-position* 0)))
336 aruttenberg 1.38 (loc
337     (destructuring-bind (file . pos) loc
338     (make-location
339     (list :file (namestring (truename file)))
340     (list :position (1+ pos)))))
341     (t
342 heller 1.1 (make-location
343 heller 1.59 (list :file (namestring *compile-filename*))
344 aruttenberg 1.38 (list :position 1)))))))))
345    
346     (defvar *abcl-signaled-conditions*)
347 heller 1.1
348 heller 1.63 (defimplementation swank-compile-file (input-file output-file
349     load-p external-format)
350 heller 1.27 (declare (ignore external-format))
351 aruttenberg 1.38 (let ((jvm::*resignal-compiler-warnings* t)
352     (*abcl-signaled-conditions* nil))
353     (handler-bind ((warning #'handle-compiler-warning))
354     (let ((*buffer-name* nil)
355 heller 1.63 (*compile-filename* input-file))
356     (multiple-value-bind (fn warn fail)
357     (compile-file input-file :output-file output-file)
358 heller 1.56 (values fn warn
359     (or fail
360     (and load-p
361     (not (load fn))))))))))
362 heller 1.1
363 heller 1.62 (defimplementation swank-compile-string (string &key buffer position filename
364     policy)
365     (declare (ignore filename policy))
366 aruttenberg 1.38 (let ((jvm::*resignal-compiler-warnings* t)
367     (*abcl-signaled-conditions* nil))
368     (handler-bind ((warning #'handle-compiler-warning))
369     (let ((*buffer-name* buffer)
370     (*buffer-start-position* position)
371     (*buffer-string* string))
372     (funcall (compile nil (read-from-string
373 heller 1.57 (format nil "(~S () ~A)" 'lambda string))))
374     t))))
375 heller 1.1
376     #|
377     ;;;; Definition Finding
378    
379     (defun find-fspec-location (fspec type)
380     (let ((file (excl::fspec-pathname fspec type)))
381     (etypecase file
382     (pathname
383     (let ((start (scm:find-definition-in-file fspec type file)))
384     (make-location (list :file (namestring (truename file)))
385     (if start
386     (list :position (1+ start))
387     (list :function-name (string fspec))))))
388     ((member :top-level)
389     (list :error (format nil "Defined at toplevel: ~A" fspec)))
390     (null
391     (list :error (format nil "Unkown source location for ~A" fspec))))))
392    
393     (defun fspec-definition-locations (fspec)
394     (let ((defs (excl::find-multiple-definitions fspec)))
395     (loop for (fspec type) in defs
396     collect (list fspec (find-fspec-location fspec type)))))
397    
398     (defimplementation find-definitions (symbol)
399     (fspec-definition-locations symbol))
400    
401     |#
402    
403 asimon 1.2 (defun source-location (symbol)
404 asimon 1.24 (when (pathnamep (ext:source-pathname symbol))
405 asimon 1.2 `(((,symbol)
406     (:location
407     (:file ,(namestring (ext:source-pathname symbol)))
408 heller 1.55 (:position ,(or (ext:source-file-position symbol) 1))
409     (:align t))))))
410 asimon 1.24
411 asimon 1.2
412     (defimplementation find-definitions (symbol)
413     (source-location symbol))
414    
415 asimon 1.17 #|
416     Uncomment this if you have patched xref.lisp, as in
417     http://article.gmane.org/gmane.lisp.slime.devel/2425
418     Also, make sure that xref.lisp is loaded by modifying the armedbear
419     part of *sysdep-pathnames* in swank.loader.lisp.
420 asimon 1.2
421 heller 1.1 ;;;; XREF
422 asimon 1.17 (setq pxref:*handle-package-forms* '(cl:in-package))
423 heller 1.1
424     (defmacro defxref (name function)
425     `(defimplementation ,name (name)
426     (xref-results (,function name))))
427    
428     (defxref who-calls pxref:list-callers)
429     (defxref who-references pxref:list-readers)
430     (defxref who-binds pxref:list-setters)
431     (defxref who-sets pxref:list-setters)
432     (defxref list-callers pxref:list-callers)
433     (defxref list-callees pxref:list-callees)
434    
435     (defun xref-results (symbols)
436     (let ((xrefs '()))
437     (dolist (symbol symbols)
438 asimon 1.17 (push (list symbol (cadar (source-location symbol))) xrefs))
439 heller 1.1 xrefs))
440     |#
441    
442 asimon 1.15 ;;;; Inspecting
443    
444 heller 1.46 (defmethod emacs-inspect ((slot mop::slot-definition))
445 asimon 1.28 `("Name: " (:value ,(mop::%slot-definition-name slot))
446 asimon 1.15 (:newline)
447     "Documentation:" (:newline)
448     ,@(when (slot-definition-documentation slot)
449     `((:value ,(slot-definition-documentation slot)) (:newline)))
450     "Initialization:" (:newline)
451 asimon 1.28 " Args: " (:value ,(mop::%slot-definition-initargs slot)) (:newline)
452     " Form: " ,(if (mop::%slot-definition-initfunction slot)
453     `(:value ,(mop::%slot-definition-initform slot))
454 asimon 1.15 "#<unspecified>") (:newline)
455 asimon 1.28 " Function: " (:value ,(mop::%slot-definition-initfunction slot))
456 heller 1.47 (:newline)))
457 asimon 1.16
458 heller 1.46 (defmethod emacs-inspect ((f function))
459 asimon 1.17 `(,@(when (function-name f)
460     `("Name: "
461     ,(princ-to-string (function-name f)) (:newline)))
462     ,@(multiple-value-bind (args present)
463     (sys::arglist f)
464     (when present `("Argument list: " ,(princ-to-string args) (:newline))))
465 asimon 1.16 (:newline)
466     #+nil,@(when (documentation f t)
467     `("Documentation:" (:newline) ,(documentation f t) (:newline)))
468     ,@(when (function-lambda-expression f)
469     `("Lambda expression:"
470 heller 1.47 (:newline) ,(princ-to-string (function-lambda-expression f)) (:newline)))))
471 asimon 1.15
472 heller 1.1 #|
473    
474 heller 1.46 (defmethod emacs-inspect ((o t))
475 heller 1.1 (let* ((class (class-of o))
476 asimon 1.26 (slots (mop::class-slots class)))
477 heller 1.1 (mapcar (lambda (slot)
478 asimon 1.26 (let ((name (mop::slot-definition-name slot)))
479 heller 1.1 (cons (princ-to-string name)
480     (slot-value o name))))
481 heller 1.47 slots)))
482 heller 1.1 |#
483 asimon 1.15
484 heller 1.1 ;;;; Multithreading
485    
486     (defimplementation startup-multiprocessing ()
487     #+nil(mp:start-scheduler))
488    
489     (defimplementation spawn (fn &key name)
490 asimon 1.8 (ext:make-thread (lambda () (funcall fn)) :name name))
491 heller 1.1
492 heller 1.6 (defvar *thread-props-lock* (ext:make-thread-lock))
493    
494     (defvar *thread-props* (make-hash-table) ; should be a weak table
495     "A hashtable mapping threads to a plist.")
496    
497     (defvar *thread-id-counter* 0)
498    
499     (defimplementation thread-id (thread)
500     (ext:with-thread-lock (*thread-props-lock*)
501     (or (getf (gethash thread *thread-props*) 'id)
502     (setf (getf (gethash thread *thread-props*) 'id)
503     (incf *thread-id-counter*)))))
504    
505     (defimplementation find-thread (id)
506     (find id (all-threads)
507 asimon 1.7 :key (lambda (thread)
508 heller 1.6 (getf (gethash thread *thread-props*) 'id))))
509    
510 heller 1.1 (defimplementation thread-name (thread)
511 asimon 1.8 (ext:thread-name thread))
512 heller 1.1
513     (defimplementation thread-status (thread)
514 asimon 1.4 (format nil "Thread is ~:[dead~;alive~]" (ext:thread-alive-p thread)))
515 heller 1.1
516     (defimplementation make-lock (&key name)
517     (ext:make-thread-lock))
518    
519     (defimplementation call-with-lock-held (lock function)
520     (ext:with-thread-lock (lock) (funcall function)))
521    
522     (defimplementation current-thread ()
523     (ext:current-thread))
524    
525     (defimplementation all-threads ()
526     (copy-list (ext:mapcar-threads #'identity)))
527    
528     (defimplementation interrupt-thread (thread fn)
529     (ext:interrupt-thread thread fn))
530    
531     (defimplementation kill-thread (thread)
532     (ext:destroy-thread thread))
533    
534 heller 1.51 (defstruct mailbox
535     (mutex (ext:make-mutex))
536     (queue '()))
537    
538 heller 1.1 (defun mailbox (thread)
539     "Return THREAD's mailbox."
540 heller 1.6 (ext:with-thread-lock (*thread-props-lock*)
541     (or (getf (gethash thread *thread-props*) 'mailbox)
542     (setf (getf (gethash thread *thread-props*) 'mailbox)
543 heller 1.51 (make-mailbox)))))
544 heller 1.1
545 asimon 1.5 (defimplementation send (thread object)
546 heller 1.51 (let ((mbox (mailbox thread)))
547     (ext:with-mutex ((mailbox-mutex mbox))
548     (setf (mailbox-queue mbox)
549     (nconc (mailbox-queue mbox) (list message))))))
550    
551     #+(or)
552     (defimplementation receive-if (thread &optional timeout)
553     (let* ((mbox (mailbox (current-thread))))
554     (assert (or (not timeout) (eq timeout t)))
555     (loop
556     (check-slime-interrupts)
557     (ext:with-mutex ((mailbox-mutex mbox))
558     (let* ((q (mailbox-queue mbox))
559     (tail (member-if test q)))
560     (when tail
561     (setf (mailbox-queue mbox) (nconc (ldiff q tail) (cdr tail)))
562     (return (car tail))))
563     (when (eq timeout t) (return (values nil t)))
564     ;;(java:jcall (java:jmethod "java.lang.Object" "wait")
565     ;; (mailbox-mutex mbox) 1000)
566     ))))
567 heller 1.1
568     (defimplementation quit-lisp ()
569     (ext:exit))
570 asimon 1.12
571 heller 1.43 ;; WORKAROUND: call/initialize accessors at load time
572     (let ((c (make-condition 'compiler-condition
573     :original-condition nil
574     :severity ':note :message "" :location nil))
575     (slots `(severity message short-message references location)))
576     (dolist (slot slots)
577     (funcall slot c)))

  ViewVC Help
Powered by ViewVC 1.1.5