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

Contents of /slime/swank-abcl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.48 - (hide annotations)
Fri Feb 22 14:38:39 2008 UTC (6 years, 1 month ago) by heller
Branch: MAIN
Changes since 1.47: +15 -16 lines
* swank-abcl.lisp (getpid): Return '0' in case of error.
Apparently needed bacause $PPID isn't not a standard feature.
"[T]his is not a disentanglement from, but a progressive knotting
into."

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

  ViewVC Help
Powered by ViewVC 1.1.5