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

Contents of /slime/swank-abcl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.57 - (hide annotations)
Thu Oct 16 21:16:01 2008 UTC (5 years, 6 months ago) by heller
Branch: MAIN
Changes since 1.56: +2 -1 lines
* swank-lispworks.lisp (with-swank-compilation-unit): Return the
values of BODY.
(compile-from-temp-file)
* swank-allegro.lisp (compile-from-temp-file)
* swank-clisp.lisp (swank-compile-string)
* swank-abcl.lisp (swank-compile-string): Return T on success.
* swank.lisp (collect-notes): Check return type of
FUNCTION.
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    
131     (defimplementation close-socket (socket)
132     (ext:server-socket-close socket))
133    
134 heller 1.23 (defimplementation accept-connection (socket
135 heller 1.40 &key external-format buffering timeout)
136 heller 1.41 (declare (ignore buffering timeout external-format))
137 heller 1.1 (ext:get-socket-stream (ext:socket-accept socket)))
138    
139     ;;;; Unix signals
140    
141     (defimplementation call-without-interrupts (fn)
142     (funcall fn))
143    
144     (defimplementation getpid ()
145 heller 1.48 (handler-case
146 trittweiler 1.44 (let* ((runtime
147     (java:jstatic "getRuntime" "java.lang.Runtime"))
148     (command
149     (java:jnew-array-from-array
150     "java.lang.String" #("sh" "-c" "echo $PPID")))
151     (runtime-exec-jmethod
152     ;; Complicated because java.lang.Runtime.exec() is
153     ;; overloaded on a non-primitive type (array of
154 heller 1.48 ;; java.lang.String), so we have to use the actual
155     ;; parameter instance to get java.lang.Class
156 trittweiler 1.44 (java:jmethod "java.lang.Runtime" "exec"
157     (java:jcall
158     (java:jmethod "java.lang.Object" "getClass")
159     command)))
160     (process
161     (java:jcall runtime-exec-jmethod runtime command))
162     (output
163 heller 1.48 (java:jcall (java:jmethod "java.lang.Process" "getInputStream")
164 trittweiler 1.44 process)))
165 heller 1.48 (java:jcall (java:jmethod "java.lang.Process" "waitFor")
166     process)
167     (loop :with b :do
168     (setq b
169     (java:jcall (java:jmethod "java.io.InputStream" "read")
170     output))
171     :until (member b '(-1 #x0a)) ; Either EOF or LF
172     :collecting (code-char b) :into result
173     :finally (return
174     (parse-integer (coerce result 'string)))))
175     (t () 0)))
176 heller 1.1
177     (defimplementation lisp-implementation-type-name ()
178     "armedbear")
179    
180     (defimplementation set-default-directory (directory)
181     (let ((dir (sys::probe-directory directory)))
182     (when dir (setf *default-pathname-defaults* dir))
183     (namestring dir)))
184    
185    
186     ;;;; Misc
187    
188 heller 1.40 (defimplementation arglist (fun)
189     (cond ((symbolp fun)
190     (multiple-value-bind (arglist present) (sys::arglist fun)
191     (if present arglist :not-available)))
192     (t :not-available)))
193 asimon 1.13
194     (defimplementation function-name (function)
195     (nth-value 2 (function-lambda-expression function)))
196 heller 1.1
197     (defimplementation macroexpand-all (form)
198     (macroexpand form))
199    
200     (defimplementation describe-symbol-for-emacs (symbol)
201     (let ((result '()))
202     (flet ((doc (kind &optional (sym symbol))
203     (or (documentation sym kind) :not-documented))
204     (maybe-push (property value)
205     (when value
206     (setf result (list* property value result)))))
207     (maybe-push
208     :variable (when (boundp symbol)
209     (doc 'variable)))
210     (maybe-push
211     :function (if (fboundp symbol)
212     (doc 'function)))
213     (maybe-push
214     :class (if (find-class symbol nil)
215     (doc 'class)))
216     result)))
217    
218    
219     (defimplementation describe-definition (symbol namespace)
220     (ecase namespace
221     (:variable
222     (describe symbol))
223     ((:function :generic-function)
224     (describe (symbol-function symbol)))
225     (:class
226     (describe (find-class symbol)))))
227    
228     (defimplementation describe-definition (symbol namespace)
229     (ecase namespace
230     (:variable
231     (describe symbol))
232     ((:function :generic-function)
233     (describe (symbol-function symbol)))
234     (:class
235     (describe (find-class symbol)))))
236    
237    
238     ;;;; Debugger
239    
240     (defvar *sldb-topframe*)
241    
242 aruttenberg 1.31 (defun backtrace-as-list-ignoring-swank-calls ()
243     (let ((list (ext:backtrace-as-list)))
244 aruttenberg 1.33 (subseq list (1+ (or (position (intern "SWANK-DEBUGGER-HOOK" 'swank) list :key 'car) -1)))))
245 aruttenberg 1.31
246 heller 1.1 (defimplementation call-with-debugging-environment (debugger-loop-fn)
247 aruttenberg 1.31 (let ((*sldb-topframe* (car (backtrace-as-list-ignoring-swank-calls)) #+nil (excl::int-newest-frame)))
248 heller 1.1 (funcall debugger-loop-fn)))
249    
250     (defun nth-frame (index)
251 aruttenberg 1.31 (nth index (backtrace-as-list-ignoring-swank-calls)))
252 heller 1.1
253     (defimplementation compute-backtrace (start end)
254     (let ((end (or end most-positive-fixnum)))
255 trittweiler 1.54 (loop for f in (subseq (backtrace-as-list-ignoring-swank-calls) start end)
256     collect (make-swank-frame :%frame f :restartable :unknown))))
257 heller 1.1
258 trittweiler 1.54 (defimplementation print-swank-frame (frame stream)
259 heller 1.25 (write-string (string-trim '(#\space #\newline)
260 trittweiler 1.54 (prin1-to-string (swank-frame.%frame frame)))
261 heller 1.25 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 heller 1.55 (list :offset *buffer-start-position* 0)))
321 aruttenberg 1.38 (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 heller 1.56 (values fn warn
342     (or fail
343     (and load-p
344     (not (load fn))))))))))
345 heller 1.1
346 heller 1.49 (defimplementation swank-compile-string (string &key buffer position directory
347     debug)
348     (declare (ignore directory debug))
349 aruttenberg 1.38 (let ((jvm::*resignal-compiler-warnings* t)
350     (*abcl-signaled-conditions* nil))
351     (handler-bind ((warning #'handle-compiler-warning))
352     (let ((*buffer-name* buffer)
353     (*buffer-start-position* position)
354     (*buffer-string* string))
355     (funcall (compile nil (read-from-string
356 heller 1.57 (format nil "(~S () ~A)" 'lambda string))))
357     t))))
358 heller 1.1
359     #|
360     ;;;; Definition Finding
361    
362     (defun find-fspec-location (fspec type)
363     (let ((file (excl::fspec-pathname fspec type)))
364     (etypecase file
365     (pathname
366     (let ((start (scm:find-definition-in-file fspec type file)))
367     (make-location (list :file (namestring (truename file)))
368     (if start
369     (list :position (1+ start))
370     (list :function-name (string fspec))))))
371     ((member :top-level)
372     (list :error (format nil "Defined at toplevel: ~A" fspec)))
373     (null
374     (list :error (format nil "Unkown source location for ~A" fspec))))))
375    
376     (defun fspec-definition-locations (fspec)
377     (let ((defs (excl::find-multiple-definitions fspec)))
378     (loop for (fspec type) in defs
379     collect (list fspec (find-fspec-location fspec type)))))
380    
381     (defimplementation find-definitions (symbol)
382     (fspec-definition-locations symbol))
383    
384     |#
385    
386 asimon 1.2 (defun source-location (symbol)
387 asimon 1.24 (when (pathnamep (ext:source-pathname symbol))
388 asimon 1.2 `(((,symbol)
389     (:location
390     (:file ,(namestring (ext:source-pathname symbol)))
391 heller 1.55 (:position ,(or (ext:source-file-position symbol) 1))
392     (:align t))))))
393 asimon 1.24
394 asimon 1.2
395     (defimplementation find-definitions (symbol)
396     (source-location symbol))
397    
398 asimon 1.17 #|
399     Uncomment this if you have patched xref.lisp, as in
400     http://article.gmane.org/gmane.lisp.slime.devel/2425
401     Also, make sure that xref.lisp is loaded by modifying the armedbear
402     part of *sysdep-pathnames* in swank.loader.lisp.
403 asimon 1.2
404 heller 1.1 ;;;; XREF
405 asimon 1.17 (setq pxref:*handle-package-forms* '(cl:in-package))
406 heller 1.1
407     (defmacro defxref (name function)
408     `(defimplementation ,name (name)
409     (xref-results (,function name))))
410    
411     (defxref who-calls pxref:list-callers)
412     (defxref who-references pxref:list-readers)
413     (defxref who-binds pxref:list-setters)
414     (defxref who-sets pxref:list-setters)
415     (defxref list-callers pxref:list-callers)
416     (defxref list-callees pxref:list-callees)
417    
418     (defun xref-results (symbols)
419     (let ((xrefs '()))
420     (dolist (symbol symbols)
421 asimon 1.17 (push (list symbol (cadar (source-location symbol))) xrefs))
422 heller 1.1 xrefs))
423     |#
424    
425 asimon 1.15 ;;;; Inspecting
426    
427 heller 1.46 (defmethod emacs-inspect ((slot mop::slot-definition))
428 asimon 1.28 `("Name: " (:value ,(mop::%slot-definition-name slot))
429 asimon 1.15 (:newline)
430     "Documentation:" (:newline)
431     ,@(when (slot-definition-documentation slot)
432     `((:value ,(slot-definition-documentation slot)) (:newline)))
433     "Initialization:" (:newline)
434 asimon 1.28 " Args: " (:value ,(mop::%slot-definition-initargs slot)) (:newline)
435     " Form: " ,(if (mop::%slot-definition-initfunction slot)
436     `(:value ,(mop::%slot-definition-initform slot))
437 asimon 1.15 "#<unspecified>") (:newline)
438 asimon 1.28 " Function: " (:value ,(mop::%slot-definition-initfunction slot))
439 heller 1.47 (:newline)))
440 asimon 1.16
441 heller 1.46 (defmethod emacs-inspect ((f function))
442 asimon 1.17 `(,@(when (function-name f)
443     `("Name: "
444     ,(princ-to-string (function-name f)) (:newline)))
445     ,@(multiple-value-bind (args present)
446     (sys::arglist f)
447     (when present `("Argument list: " ,(princ-to-string args) (:newline))))
448 asimon 1.16 (:newline)
449     #+nil,@(when (documentation f t)
450     `("Documentation:" (:newline) ,(documentation f t) (:newline)))
451     ,@(when (function-lambda-expression f)
452     `("Lambda expression:"
453 heller 1.47 (:newline) ,(princ-to-string (function-lambda-expression f)) (:newline)))))
454 asimon 1.15
455 heller 1.1 #|
456    
457 heller 1.46 (defmethod emacs-inspect ((o t))
458 heller 1.1 (let* ((class (class-of o))
459 asimon 1.26 (slots (mop::class-slots class)))
460 heller 1.1 (mapcar (lambda (slot)
461 asimon 1.26 (let ((name (mop::slot-definition-name slot)))
462 heller 1.1 (cons (princ-to-string name)
463     (slot-value o name))))
464 heller 1.47 slots)))
465 heller 1.1 |#
466 asimon 1.15
467 heller 1.1 ;;;; Multithreading
468    
469     (defimplementation startup-multiprocessing ()
470     #+nil(mp:start-scheduler))
471    
472     (defimplementation spawn (fn &key name)
473 asimon 1.8 (ext:make-thread (lambda () (funcall fn)) :name name))
474 heller 1.1
475 heller 1.6 (defvar *thread-props-lock* (ext:make-thread-lock))
476    
477     (defvar *thread-props* (make-hash-table) ; should be a weak table
478     "A hashtable mapping threads to a plist.")
479    
480     (defvar *thread-id-counter* 0)
481    
482     (defimplementation thread-id (thread)
483     (ext:with-thread-lock (*thread-props-lock*)
484     (or (getf (gethash thread *thread-props*) 'id)
485     (setf (getf (gethash thread *thread-props*) 'id)
486     (incf *thread-id-counter*)))))
487    
488     (defimplementation find-thread (id)
489     (find id (all-threads)
490 asimon 1.7 :key (lambda (thread)
491 heller 1.6 (getf (gethash thread *thread-props*) 'id))))
492    
493 heller 1.1 (defimplementation thread-name (thread)
494 asimon 1.8 (ext:thread-name thread))
495 heller 1.1
496     (defimplementation thread-status (thread)
497 asimon 1.4 (format nil "Thread is ~:[dead~;alive~]" (ext:thread-alive-p thread)))
498 heller 1.1
499     (defimplementation make-lock (&key name)
500     (ext:make-thread-lock))
501    
502     (defimplementation call-with-lock-held (lock function)
503     (ext:with-thread-lock (lock) (funcall function)))
504    
505     (defimplementation current-thread ()
506     (ext:current-thread))
507    
508     (defimplementation all-threads ()
509     (copy-list (ext:mapcar-threads #'identity)))
510    
511     (defimplementation interrupt-thread (thread fn)
512     (ext:interrupt-thread thread fn))
513    
514     (defimplementation kill-thread (thread)
515     (ext:destroy-thread thread))
516    
517 heller 1.51 (defstruct mailbox
518     (mutex (ext:make-mutex))
519     (queue '()))
520    
521 heller 1.1 (defun mailbox (thread)
522     "Return THREAD's mailbox."
523 heller 1.6 (ext:with-thread-lock (*thread-props-lock*)
524     (or (getf (gethash thread *thread-props*) 'mailbox)
525     (setf (getf (gethash thread *thread-props*) 'mailbox)
526 heller 1.51 (make-mailbox)))))
527 heller 1.1
528 asimon 1.5 (defimplementation send (thread object)
529 heller 1.51 (let ((mbox (mailbox thread)))
530     (ext:with-mutex ((mailbox-mutex mbox))
531     (setf (mailbox-queue mbox)
532     (nconc (mailbox-queue mbox) (list message))))))
533    
534     #+(or)
535     (defimplementation receive-if (thread &optional timeout)
536     (let* ((mbox (mailbox (current-thread))))
537     (assert (or (not timeout) (eq timeout t)))
538     (loop
539     (check-slime-interrupts)
540     (ext:with-mutex ((mailbox-mutex mbox))
541     (let* ((q (mailbox-queue mbox))
542     (tail (member-if test q)))
543     (when tail
544     (setf (mailbox-queue mbox) (nconc (ldiff q tail) (cdr tail)))
545     (return (car tail))))
546     (when (eq timeout t) (return (values nil t)))
547     ;;(java:jcall (java:jmethod "java.lang.Object" "wait")
548     ;; (mailbox-mutex mbox) 1000)
549     ))))
550 heller 1.1
551     (defimplementation quit-lisp ()
552     (ext:exit))
553 asimon 1.12
554 heller 1.43 ;; WORKAROUND: call/initialize accessors at load time
555     (let ((c (make-condition 'compiler-condition
556     :original-condition nil
557     :severity ':note :message "" :location nil))
558     (slots `(severity message short-message references location)))
559     (dolist (slot slots)
560     (funcall slot c)))

  ViewVC Help
Powered by ViewVC 1.1.5