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

Contents of /slime/swank-abcl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.50 - (show annotations)
Fri Aug 8 13:43:33 2008 UTC (5 years, 8 months ago) by heller
Branch: MAIN
Changes since 1.49: +0 -24 lines
Spawn the auto-flush thread in the front end.
This removes some copy&paste code in various backends.

* swank.lisp (auto-flush-loop): New function.
(open-streams): Use it.
* swank-backend.lisp (make-stream-interactive): Deleted.
1 ;;;; -*- 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 (eval-when (:compile-toplevel :load-toplevel :execute)
14 (require :collect) ;just so that it doesn't spoil the flying letters
15 (require :pprint))
16
17 (defun sys::break (&optional (format-control "BREAK called")
18 &rest format-arguments)
19 (let ((*saved-backtrace* (backtrace-as-list-ignoring-swank-calls)))
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 (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
32 (defimplementation call-with-compilation-hooks (function)
33 (funcall function))
34
35 ;;; swank-mop
36
37 ;;dummies and definition
38
39 (defclass standard-slot-definition ()())
40
41 ;(defun class-finalized-p (class) t)
42
43 (defun slot-definition-documentation (slot) #+nil (documentation slot 't))
44 (defun slot-definition-type (slot) t)
45 (defun class-prototype (class))
46 (defun generic-function-declarations (gf))
47 (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 (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
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 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 mop::class-finalized-p
81 cl:class-name
82 mop::class-precedence-list
83 class-prototype ;;dummy
84 class-slots
85 specializer-direct-methods
86 ;; eql-specializer accessors
87 mop::eql-specializer-object
88 ;; generic function readers
89 mop::generic-function-argument-precedence-order
90 generic-function-declarations ;;dummy
91 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 ;; method readers
97 method-generic-function
98 method-function
99 mop::method-lambda-list
100 mop::method-specializers
101 mop::method-qualifiers
102 ;; slot readers
103 mop::slot-definition-allocation
104 slot-definition-documentation ;;dummy
105 mop::slot-definition-initargs
106 mop::slot-definition-initform
107 mop::slot-definition-initfunction
108 slot-definition-name
109 slot-definition-type ;;dummy
110 mop::slot-definition-readers
111 mop::slot-definition-writers
112 slot-boundp-using-class
113 slot-value-using-class
114 ))
115
116 ;;;; 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 (defimplementation accept-connection (socket
136 &key external-format buffering timeout)
137 (declare (ignore buffering timeout external-format))
138 (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 (handler-case
147 (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 ;; java.lang.String), so we have to use the actual
156 ;; parameter instance to get java.lang.Class
157 (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 (java:jcall (java:jmethod "java.lang.Process" "getInputStream")
165 process)))
166 (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
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 (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
195 (defimplementation function-name (function)
196 (nth-value 2 (function-lambda-expression function)))
197
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 (defun backtrace-as-list-ignoring-swank-calls ()
244 (let ((list (ext:backtrace-as-list)))
245 (subseq list (1+ (or (position (intern "SWANK-DEBUGGER-HOOK" 'swank) list :key 'car) -1)))))
246
247 (defimplementation call-with-debugging-environment (debugger-loop-fn)
248 (let ((*sldb-topframe* (car (backtrace-as-list-ignoring-swank-calls)) #+nil (excl::int-newest-frame)))
249 (funcall debugger-loop-fn)))
250
251 (defun nth-frame (index)
252 (nth index (backtrace-as-list-ignoring-swank-calls)))
253
254 (defimplementation compute-backtrace (start end)
255 (let ((end (or end most-positive-fixnum)))
256 (subseq (backtrace-as-list-ignoring-swank-calls) start end)))
257
258 (defimplementation print-frame (frame stream)
259 (write-string (string-trim '(#\space #\newline)
260 (prin1-to-string frame))
261 stream))
262
263 (defimplementation frame-locals (index)
264 `(,(list :name "??" :id 0 :value "??")))
265
266
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 (in-package :swank-backend)
307
308 (defun handle-compiler-warning (condition)
309 (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 (make-location
328 (list :file *compile-filename*)
329 (list :position 1)))))))))
330
331 (defvar *abcl-signaled-conditions*)
332
333 (defimplementation swank-compile-file (filename load-p external-format)
334 (declare (ignore external-format))
335 (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
344 (defimplementation swank-compile-string (string &key buffer position directory
345 debug)
346 (declare (ignore directory debug))
347 (let ((jvm::*resignal-compiler-warnings* t)
348 (*abcl-signaled-conditions* nil))
349 (handler-bind ((warning #'handle-compiler-warning))
350 (let ((*buffer-name* buffer)
351 (*buffer-start-position* position)
352 (*buffer-string* string))
353 (funcall (compile nil (read-from-string
354 (format nil "(~S () ~A)" 'lambda string))))))))
355
356 #|
357 ;;;; Definition Finding
358
359 (defun find-fspec-location (fspec type)
360 (let ((file (excl::fspec-pathname fspec type)))
361 (etypecase file
362 (pathname
363 (let ((start (scm:find-definition-in-file fspec type file)))
364 (make-location (list :file (namestring (truename file)))
365 (if start
366 (list :position (1+ start))
367 (list :function-name (string fspec))))))
368 ((member :top-level)
369 (list :error (format nil "Defined at toplevel: ~A" fspec)))
370 (null
371 (list :error (format nil "Unkown source location for ~A" fspec))))))
372
373 (defun fspec-definition-locations (fspec)
374 (let ((defs (excl::find-multiple-definitions fspec)))
375 (loop for (fspec type) in defs
376 collect (list fspec (find-fspec-location fspec type)))))
377
378 (defimplementation find-definitions (symbol)
379 (fspec-definition-locations symbol))
380
381 |#
382
383 (defun source-location (symbol)
384 (when (pathnamep (ext:source-pathname symbol))
385 `(((,symbol)
386 (:location
387 (:file ,(namestring (ext:source-pathname symbol)))
388 (:position ,(or (ext:source-file-position symbol) 0) t)
389 (:snippet nil))))))
390
391
392 (defimplementation find-definitions (symbol)
393 (source-location symbol))
394
395 #|
396 Uncomment this if you have patched xref.lisp, as in
397 http://article.gmane.org/gmane.lisp.slime.devel/2425
398 Also, make sure that xref.lisp is loaded by modifying the armedbear
399 part of *sysdep-pathnames* in swank.loader.lisp.
400
401 ;;;; XREF
402 (setq pxref:*handle-package-forms* '(cl:in-package))
403
404 (defmacro defxref (name function)
405 `(defimplementation ,name (name)
406 (xref-results (,function name))))
407
408 (defxref who-calls pxref:list-callers)
409 (defxref who-references pxref:list-readers)
410 (defxref who-binds pxref:list-setters)
411 (defxref who-sets pxref:list-setters)
412 (defxref list-callers pxref:list-callers)
413 (defxref list-callees pxref:list-callees)
414
415 (defun xref-results (symbols)
416 (let ((xrefs '()))
417 (dolist (symbol symbols)
418 (push (list symbol (cadar (source-location symbol))) xrefs))
419 xrefs))
420 |#
421
422 ;;;; Inspecting
423
424 (defmethod emacs-inspect ((slot mop::slot-definition))
425 `("Name: " (:value ,(mop::%slot-definition-name slot))
426 (:newline)
427 "Documentation:" (:newline)
428 ,@(when (slot-definition-documentation slot)
429 `((:value ,(slot-definition-documentation slot)) (:newline)))
430 "Initialization:" (:newline)
431 " Args: " (:value ,(mop::%slot-definition-initargs slot)) (:newline)
432 " Form: " ,(if (mop::%slot-definition-initfunction slot)
433 `(:value ,(mop::%slot-definition-initform slot))
434 "#<unspecified>") (:newline)
435 " Function: " (:value ,(mop::%slot-definition-initfunction slot))
436 (:newline)))
437
438 (defmethod emacs-inspect ((f function))
439 `(,@(when (function-name f)
440 `("Name: "
441 ,(princ-to-string (function-name f)) (:newline)))
442 ,@(multiple-value-bind (args present)
443 (sys::arglist f)
444 (when present `("Argument list: " ,(princ-to-string args) (:newline))))
445 (:newline)
446 #+nil,@(when (documentation f t)
447 `("Documentation:" (:newline) ,(documentation f t) (:newline)))
448 ,@(when (function-lambda-expression f)
449 `("Lambda expression:"
450 (:newline) ,(princ-to-string (function-lambda-expression f)) (:newline)))))
451
452 #|
453
454 (defmethod emacs-inspect ((o t))
455 (let* ((class (class-of o))
456 (slots (mop::class-slots class)))
457 (mapcar (lambda (slot)
458 (let ((name (mop::slot-definition-name slot)))
459 (cons (princ-to-string name)
460 (slot-value o name))))
461 slots)))
462 |#
463
464 ;;;; Multithreading
465
466 (defimplementation startup-multiprocessing ()
467 #+nil(mp:start-scheduler))
468
469 (defimplementation spawn (fn &key name)
470 (ext:make-thread (lambda () (funcall fn)) :name name))
471
472 (defvar *thread-props-lock* (ext:make-thread-lock))
473
474 (defvar *thread-props* (make-hash-table) ; should be a weak table
475 "A hashtable mapping threads to a plist.")
476
477 (defvar *thread-id-counter* 0)
478
479 (defimplementation thread-id (thread)
480 (ext:with-thread-lock (*thread-props-lock*)
481 (or (getf (gethash thread *thread-props*) 'id)
482 (setf (getf (gethash thread *thread-props*) 'id)
483 (incf *thread-id-counter*)))))
484
485 (defimplementation find-thread (id)
486 (find id (all-threads)
487 :key (lambda (thread)
488 (getf (gethash thread *thread-props*) 'id))))
489
490 (defimplementation thread-name (thread)
491 (ext:thread-name thread))
492
493 (defimplementation thread-status (thread)
494 (format nil "Thread is ~:[dead~;alive~]" (ext:thread-alive-p thread)))
495
496 (defimplementation make-lock (&key name)
497 (ext:make-thread-lock))
498
499 (defimplementation call-with-lock-held (lock function)
500 (ext:with-thread-lock (lock) (funcall function)))
501
502 (defimplementation current-thread ()
503 (ext:current-thread))
504
505 (defimplementation all-threads ()
506 (copy-list (ext:mapcar-threads #'identity)))
507
508 (defimplementation interrupt-thread (thread fn)
509 (ext:interrupt-thread thread fn))
510
511 (defimplementation kill-thread (thread)
512 (ext:destroy-thread thread))
513
514 (defun mailbox (thread)
515 "Return THREAD's mailbox."
516 (ext:with-thread-lock (*thread-props-lock*)
517 (or (getf (gethash thread *thread-props*) 'mailbox)
518 (setf (getf (gethash thread *thread-props*) 'mailbox)
519 (ext:make-mailbox)))))
520
521 (defimplementation send (thread object)
522 (ext:mailbox-send (mailbox thread) object))
523
524 (defimplementation receive ()
525 (ext:mailbox-read (mailbox (ext:current-thread))))
526
527 (defimplementation quit-lisp ()
528 (ext:exit))
529
530 ;; WORKAROUND: call/initialize accessors at load time
531 (let ((c (make-condition 'compiler-condition
532 :original-condition nil
533 :severity ':note :message "" :location nil))
534 (slots `(severity message short-message references location)))
535 (dolist (slot slots)
536 (funcall slot c)))

  ViewVC Help
Powered by ViewVC 1.1.5