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

Contents of /slime/swank-abcl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.44 - (show annotations)
Mon Oct 22 08:36:32 2007 UTC (6 years, 6 months ago) by trittweiler
Branch: MAIN
Changes since 1.43: +32 -2 lines
* swank-abcl.lisp (getpid): Implemented.
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 (if (not (find :unix *features*))
147 0
148 (let* ((runtime
149 (java:jstatic "getRuntime" "java.lang.Runtime"))
150 (command
151 (java:jnew-array-from-array
152 "java.lang.String" #("sh" "-c" "echo $PPID")))
153 (runtime-exec-jmethod
154 ;; Complicated because java.lang.Runtime.exec() is
155 ;; overloaded on a non-primitive type (array of
156 ;; java.lang.String), so we have to use the actual parameter
157 ;; instance to get java.lang.Class
158 (java:jmethod "java.lang.Runtime" "exec"
159 (java:jcall
160 (java:jmethod "java.lang.Object" "getClass")
161 command)))
162 (process
163 (java:jcall runtime-exec-jmethod runtime command))
164 (output
165 (java:jcall (java:jmethod "java.lang.Process" "getInputStream")
166 process)))
167 (java:jcall (java:jmethod "java.lang.Process" "waitFor") process)
168 (loop
169 :with b
170 :do (setq b
171 (java:jcall (java:jmethod "java.io.InputStream" "read")
172 output))
173 :until (member b '(-1 #x0a)) ; Either EOF or LF
174 :collecting (code-char b) :into result
175 :finally (return
176 (values
177 (parse-integer (coerce result 'string))))))))
178
179 (defimplementation lisp-implementation-type-name ()
180 "armedbear")
181
182 (defimplementation set-default-directory (directory)
183 (let ((dir (sys::probe-directory directory)))
184 (when dir (setf *default-pathname-defaults* dir))
185 (namestring dir)))
186
187
188 ;;;; Misc
189
190 (defimplementation arglist (fun)
191 (cond ((symbolp fun)
192 (multiple-value-bind (arglist present) (sys::arglist fun)
193 (if present arglist :not-available)))
194 (t :not-available)))
195
196 (defimplementation function-name (function)
197 (nth-value 2 (function-lambda-expression function)))
198
199 (defimplementation macroexpand-all (form)
200 (macroexpand form))
201
202 (defimplementation describe-symbol-for-emacs (symbol)
203 (let ((result '()))
204 (flet ((doc (kind &optional (sym symbol))
205 (or (documentation sym kind) :not-documented))
206 (maybe-push (property value)
207 (when value
208 (setf result (list* property value result)))))
209 (maybe-push
210 :variable (when (boundp symbol)
211 (doc 'variable)))
212 (maybe-push
213 :function (if (fboundp symbol)
214 (doc 'function)))
215 (maybe-push
216 :class (if (find-class symbol nil)
217 (doc 'class)))
218 result)))
219
220
221 (defimplementation describe-definition (symbol namespace)
222 (ecase namespace
223 (:variable
224 (describe symbol))
225 ((:function :generic-function)
226 (describe (symbol-function symbol)))
227 (:class
228 (describe (find-class symbol)))))
229
230 (defimplementation describe-definition (symbol namespace)
231 (ecase namespace
232 (:variable
233 (describe symbol))
234 ((:function :generic-function)
235 (describe (symbol-function symbol)))
236 (:class
237 (describe (find-class symbol)))))
238
239
240 ;;;; Debugger
241
242 (defvar *sldb-topframe*)
243
244 (defun backtrace-as-list-ignoring-swank-calls ()
245 (let ((list (ext:backtrace-as-list)))
246 (subseq list (1+ (or (position (intern "SWANK-DEBUGGER-HOOK" 'swank) list :key 'car) -1)))))
247
248 (defimplementation call-with-debugging-environment (debugger-loop-fn)
249 (let ((*sldb-topframe* (car (backtrace-as-list-ignoring-swank-calls)) #+nil (excl::int-newest-frame)))
250 (funcall debugger-loop-fn)))
251
252 (defun nth-frame (index)
253 (nth index (backtrace-as-list-ignoring-swank-calls)))
254
255 (defimplementation compute-backtrace (start end)
256 (let ((end (or end most-positive-fixnum)))
257 (subseq (backtrace-as-list-ignoring-swank-calls) start end)))
258
259 (defimplementation print-frame (frame stream)
260 (write-string (string-trim '(#\space #\newline)
261 (prin1-to-string frame))
262 stream))
263
264 (defimplementation frame-locals (index)
265 `(,(list :name "??" :id 0 :value "??")))
266
267
268 (defimplementation frame-catch-tags (index)
269 (declare (ignore index))
270 nil)
271
272 #+nil
273 (defimplementation disassemble-frame (index)
274 (disassemble (debugger:frame-function (nth-frame index))))
275
276 (defimplementation frame-source-location-for-emacs (index)
277 (list :error (format nil "Cannot find source for frame: ~A"
278 (nth-frame index))))
279
280 #+nil
281 (defimplementation eval-in-frame (form frame-number)
282 (debugger:eval-form-in-context
283 form
284 (debugger:environment-of-frame (nth-frame frame-number))))
285
286 #+nil
287 (defimplementation return-from-frame (frame-number form)
288 (let ((frame (nth-frame frame-number)))
289 (multiple-value-call #'debugger:frame-return
290 frame (debugger:eval-form-in-context
291 form
292 (debugger:environment-of-frame frame)))))
293
294 ;;; XXX doesn't work for frames with arguments
295 #+nil
296 (defimplementation restart-frame (frame-number)
297 (let ((frame (nth-frame frame-number)))
298 (debugger:frame-retry frame (debugger:frame-function frame))))
299
300 ;;;; Compiler hooks
301
302 (defvar *buffer-name* nil)
303 (defvar *buffer-start-position*)
304 (defvar *buffer-string*)
305 (defvar *compile-filename*)
306
307 (in-package :swank-backend)
308
309 (defun handle-compiler-warning (condition)
310 (let ((loc nil));(getf (slot-value condition 'excl::plist) :loc)))
311 (unless (member condition *abcl-signaled-conditions*) ; filter condition signaled more than once.
312 (push condition *abcl-signaled-conditions*)
313 (signal (make-condition
314 'compiler-condition
315 :original-condition condition
316 :severity :warning
317 :message (format nil "~A" condition)
318 :location (cond (*buffer-name*
319 (make-location
320 (list :buffer *buffer-name*)
321 (list :position *buffer-start-position*)))
322 (loc
323 (destructuring-bind (file . pos) loc
324 (make-location
325 (list :file (namestring (truename file)))
326 (list :position (1+ pos)))))
327 (t
328 (make-location
329 (list :file *compile-filename*)
330 (list :position 1)))))))))
331
332 (defvar *abcl-signaled-conditions*)
333
334 (defimplementation swank-compile-file (filename load-p external-format)
335 (declare (ignore external-format))
336 (let ((jvm::*resignal-compiler-warnings* t)
337 (*abcl-signaled-conditions* nil))
338 (handler-bind ((warning #'handle-compiler-warning))
339 (let ((*buffer-name* nil)
340 (*compile-filename* filename))
341 (multiple-value-bind (fn warn fail) (compile-file filename)
342 (when (and load-p (not fail))
343 (load fn)))))))
344
345 (defimplementation swank-compile-string (string &key buffer position directory)
346 (declare (ignore directory))
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 (defclass abcl-inspector (backend-inspector) ())
425
426 (defimplementation make-default-inspector ()
427 (make-instance 'abcl-inspector))
428
429 (defmethod inspect-for-emacs ((slot mop::slot-definition)
430 (inspector backend-inspector))
431 (declare (ignore inspector))
432 (values "A slot."
433 `("Name: " (:value ,(mop::%slot-definition-name slot))
434 (:newline)
435 "Documentation:" (:newline)
436 ,@(when (slot-definition-documentation slot)
437 `((:value ,(slot-definition-documentation slot)) (:newline)))
438 "Initialization:" (:newline)
439 " Args: " (:value ,(mop::%slot-definition-initargs slot)) (:newline)
440 " Form: " ,(if (mop::%slot-definition-initfunction slot)
441 `(:value ,(mop::%slot-definition-initform slot))
442 "#<unspecified>") (:newline)
443 " Function: " (:value ,(mop::%slot-definition-initfunction slot))
444 (:newline))))
445
446 (defmethod inspect-for-emacs ((f function) (inspector backend-inspector))
447 (declare (ignore inspector))
448 (values "A function."
449 `(,@(when (function-name f)
450 `("Name: "
451 ,(princ-to-string (function-name f)) (:newline)))
452 ,@(multiple-value-bind (args present)
453 (sys::arglist f)
454 (when present `("Argument list: " ,(princ-to-string args) (:newline))))
455 (:newline)
456 #+nil,@(when (documentation f t)
457 `("Documentation:" (:newline) ,(documentation f t) (:newline)))
458 ,@(when (function-lambda-expression f)
459 `("Lambda expression:"
460 (:newline) ,(princ-to-string (function-lambda-expression f)) (:newline))))))
461
462 #|
463
464 (defmethod inspect-for-emacs ((o t) (inspector backend-inspector))
465 (let* ((class (class-of o))
466 (slots (mop::class-slots class)))
467 (values (format nil "~A~% is a ~A" o class)
468 (mapcar (lambda (slot)
469 (let ((name (mop::slot-definition-name slot)))
470 (cons (princ-to-string name)
471 (slot-value o name))))
472 slots))))
473 |#
474
475 ;;;; Multithreading
476
477 (defimplementation startup-multiprocessing ()
478 #+nil(mp:start-scheduler))
479
480 (defimplementation spawn (fn &key name)
481 (ext:make-thread (lambda () (funcall fn)) :name name))
482
483 (defvar *thread-props-lock* (ext:make-thread-lock))
484
485 (defvar *thread-props* (make-hash-table) ; should be a weak table
486 "A hashtable mapping threads to a plist.")
487
488 (defvar *thread-id-counter* 0)
489
490 (defimplementation thread-id (thread)
491 (ext:with-thread-lock (*thread-props-lock*)
492 (or (getf (gethash thread *thread-props*) 'id)
493 (setf (getf (gethash thread *thread-props*) 'id)
494 (incf *thread-id-counter*)))))
495
496 (defimplementation find-thread (id)
497 (find id (all-threads)
498 :key (lambda (thread)
499 (getf (gethash thread *thread-props*) 'id))))
500
501 (defimplementation thread-name (thread)
502 (ext:thread-name thread))
503
504 (defimplementation thread-status (thread)
505 (format nil "Thread is ~:[dead~;alive~]" (ext:thread-alive-p thread)))
506
507 (defimplementation make-lock (&key name)
508 (ext:make-thread-lock))
509
510 (defimplementation call-with-lock-held (lock function)
511 (ext:with-thread-lock (lock) (funcall function)))
512
513 (defimplementation current-thread ()
514 (ext:current-thread))
515
516 (defimplementation all-threads ()
517 (copy-list (ext:mapcar-threads #'identity)))
518
519 (defimplementation interrupt-thread (thread fn)
520 (ext:interrupt-thread thread fn))
521
522 (defimplementation kill-thread (thread)
523 (ext:destroy-thread thread))
524
525 (defun mailbox (thread)
526 "Return THREAD's mailbox."
527 (ext:with-thread-lock (*thread-props-lock*)
528 (or (getf (gethash thread *thread-props*) 'mailbox)
529 (setf (getf (gethash thread *thread-props*) 'mailbox)
530 (ext:make-mailbox)))))
531
532 (defimplementation send (thread object)
533 (ext:mailbox-send (mailbox thread) object))
534
535 (defimplementation receive ()
536 (ext:mailbox-read (mailbox (ext:current-thread))))
537
538 ;;; Auto-flush streams
539
540 ;; XXX race conditions
541 (defvar *auto-flush-streams* '())
542
543 (defvar *auto-flush-thread* nil)
544
545 (defimplementation make-stream-interactive (stream)
546 (setq *auto-flush-streams* (adjoin stream *auto-flush-streams*))
547 (unless *auto-flush-thread*
548 (setq *auto-flush-thread*
549 (ext:make-thread #'flush-streams
550 :name "auto-flush-thread"))))
551
552 (defun flush-streams ()
553 (loop
554 (setq *auto-flush-streams*
555 (remove-if (lambda (x)
556 (not (and (open-stream-p x)
557 (output-stream-p x))))
558 *auto-flush-streams*))
559 (mapc #'finish-output *auto-flush-streams*)
560 (sleep 0.15)))
561
562 (defimplementation quit-lisp ()
563 (ext:exit))
564
565 ;; WORKAROUND: call/initialize accessors at load time
566 (let ((c (make-condition 'compiler-condition
567 :original-condition nil
568 :severity ':note :message "" :location nil))
569 (slots `(severity message short-message references location)))
570 (dolist (slot slots)
571 (funcall slot c)))

  ViewVC Help
Powered by ViewVC 1.1.5