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

Contents of /slime/swank-abcl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.53 - (show annotations)
Sun Aug 31 11:58:01 2008 UTC (5 years, 7 months ago) by heller
Branch: MAIN
Changes since 1.52: +2 -6 lines
* swank-backend.lisp (make-input-stream): Remove make-fn-streams.
Update callers accordingly.
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-output-stream (write-string)
28 (ext:make-slime-output-stream write-string))
29
30 (defimplementation make-input-stream (read-string)
31 (ext:make-slime-input-stream read-string
32 (make-synonym-stream '*standard-output*)))
33
34 (defimplementation call-with-compilation-hooks (function)
35 (funcall function))
36
37 ;;; swank-mop
38
39 ;;dummies and definition
40
41 (defclass standard-slot-definition ()())
42
43 ;(defun class-finalized-p (class) t)
44
45 (defun slot-definition-documentation (slot) #+nil (documentation slot 't))
46 (defun slot-definition-type (slot) t)
47 (defun class-prototype (class))
48 (defun generic-function-declarations (gf))
49 (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 (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
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 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 mop::class-finalized-p
83 cl:class-name
84 mop::class-precedence-list
85 class-prototype ;;dummy
86 class-slots
87 specializer-direct-methods
88 ;; eql-specializer accessors
89 mop::eql-specializer-object
90 ;; generic function readers
91 mop::generic-function-argument-precedence-order
92 generic-function-declarations ;;dummy
93 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 ;; method readers
99 method-generic-function
100 method-function
101 mop::method-lambda-list
102 mop::method-specializers
103 mop::method-qualifiers
104 ;; slot readers
105 mop::slot-definition-allocation
106 slot-definition-documentation ;;dummy
107 mop::slot-definition-initargs
108 mop::slot-definition-initform
109 mop::slot-definition-initfunction
110 slot-definition-name
111 slot-definition-type ;;dummy
112 mop::slot-definition-readers
113 mop::slot-definition-writers
114 slot-boundp-using-class
115 slot-value-using-class
116 ))
117
118 ;;;; TCP Server
119
120
121 (defimplementation preferred-communication-style ()
122 nil)
123
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 (defimplementation accept-connection (socket
135 &key external-format buffering timeout)
136 (declare (ignore buffering timeout external-format))
137 (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 (handler-case
146 (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 ;; java.lang.String), so we have to use the actual
155 ;; parameter instance to get java.lang.Class
156 (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 (java:jcall (java:jmethod "java.lang.Process" "getInputStream")
164 process)))
165 (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
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 (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
194 (defimplementation function-name (function)
195 (nth-value 2 (function-lambda-expression function)))
196
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 (defun backtrace-as-list-ignoring-swank-calls ()
243 (let ((list (ext:backtrace-as-list)))
244 (subseq list (1+ (or (position (intern "SWANK-DEBUGGER-HOOK" 'swank) list :key 'car) -1)))))
245
246 (defimplementation call-with-debugging-environment (debugger-loop-fn)
247 (let ((*sldb-topframe* (car (backtrace-as-list-ignoring-swank-calls)) #+nil (excl::int-newest-frame)))
248 (funcall debugger-loop-fn)))
249
250 (defun nth-frame (index)
251 (nth index (backtrace-as-list-ignoring-swank-calls)))
252
253 (defimplementation compute-backtrace (start end)
254 (let ((end (or end most-positive-fixnum)))
255 (subseq (backtrace-as-list-ignoring-swank-calls) start end)))
256
257 (defimplementation print-frame (frame stream)
258 (write-string (string-trim '(#\space #\newline)
259 (prin1-to-string frame))
260 stream))
261
262 (defimplementation frame-locals (index)
263 `(,(list :name "??" :id 0 :value "??")))
264
265
266 (defimplementation frame-catch-tags (index)
267 (declare (ignore index))
268 nil)
269
270 #+nil
271 (defimplementation disassemble-frame (index)
272 (disassemble (debugger:frame-function (nth-frame index))))
273
274 (defimplementation frame-source-location-for-emacs (index)
275 (list :error (format nil "Cannot find source for frame: ~A"
276 (nth-frame index))))
277
278 #+nil
279 (defimplementation eval-in-frame (form frame-number)
280 (debugger:eval-form-in-context
281 form
282 (debugger:environment-of-frame (nth-frame frame-number))))
283
284 #+nil
285 (defimplementation return-from-frame (frame-number form)
286 (let ((frame (nth-frame frame-number)))
287 (multiple-value-call #'debugger:frame-return
288 frame (debugger:eval-form-in-context
289 form
290 (debugger:environment-of-frame frame)))))
291
292 ;;; XXX doesn't work for frames with arguments
293 #+nil
294 (defimplementation restart-frame (frame-number)
295 (let ((frame (nth-frame frame-number)))
296 (debugger:frame-retry frame (debugger:frame-function frame))))
297
298 ;;;; Compiler hooks
299
300 (defvar *buffer-name* nil)
301 (defvar *buffer-start-position*)
302 (defvar *buffer-string*)
303 (defvar *compile-filename*)
304
305 (in-package :swank-backend)
306
307 (defun handle-compiler-warning (condition)
308 (let ((loc nil));(getf (slot-value condition 'excl::plist) :loc)))
309 (unless (member condition *abcl-signaled-conditions*) ; filter condition signaled more than once.
310 (push condition *abcl-signaled-conditions*)
311 (signal (make-condition
312 'compiler-condition
313 :original-condition condition
314 :severity :warning
315 :message (format nil "~A" condition)
316 :location (cond (*buffer-name*
317 (make-location
318 (list :buffer *buffer-name*)
319 (list :position *buffer-start-position*)))
320 (loc
321 (destructuring-bind (file . pos) loc
322 (make-location
323 (list :file (namestring (truename file)))
324 (list :position (1+ pos)))))
325 (t
326 (make-location
327 (list :file *compile-filename*)
328 (list :position 1)))))))))
329
330 (defvar *abcl-signaled-conditions*)
331
332 (defimplementation swank-compile-file (filename load-p external-format)
333 (declare (ignore external-format))
334 (let ((jvm::*resignal-compiler-warnings* t)
335 (*abcl-signaled-conditions* nil))
336 (handler-bind ((warning #'handle-compiler-warning))
337 (let ((*buffer-name* nil)
338 (*compile-filename* filename))
339 (multiple-value-bind (fn warn fail) (compile-file filename)
340 (when (and load-p (not fail))
341 (load fn)))))))
342
343 (defimplementation swank-compile-string (string &key buffer position directory
344 debug)
345 (declare (ignore directory debug))
346 (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
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 (defun source-location (symbol)
383 (when (pathnamep (ext:source-pathname symbol))
384 `(((,symbol)
385 (:location
386 (:file ,(namestring (ext:source-pathname symbol)))
387 (:position ,(or (ext:source-file-position symbol) 0) t)
388 (:snippet nil))))))
389
390
391 (defimplementation find-definitions (symbol)
392 (source-location symbol))
393
394 #|
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
400 ;;;; XREF
401 (setq pxref:*handle-package-forms* '(cl:in-package))
402
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 (push (list symbol (cadar (source-location symbol))) xrefs))
418 xrefs))
419 |#
420
421 ;;;; Inspecting
422
423 (defmethod emacs-inspect ((slot mop::slot-definition))
424 `("Name: " (:value ,(mop::%slot-definition-name slot))
425 (:newline)
426 "Documentation:" (:newline)
427 ,@(when (slot-definition-documentation slot)
428 `((:value ,(slot-definition-documentation slot)) (:newline)))
429 "Initialization:" (:newline)
430 " Args: " (:value ,(mop::%slot-definition-initargs slot)) (:newline)
431 " Form: " ,(if (mop::%slot-definition-initfunction slot)
432 `(:value ,(mop::%slot-definition-initform slot))
433 "#<unspecified>") (:newline)
434 " Function: " (:value ,(mop::%slot-definition-initfunction slot))
435 (:newline)))
436
437 (defmethod emacs-inspect ((f function))
438 `(,@(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 (:newline)
445 #+nil,@(when (documentation f t)
446 `("Documentation:" (:newline) ,(documentation f t) (:newline)))
447 ,@(when (function-lambda-expression f)
448 `("Lambda expression:"
449 (:newline) ,(princ-to-string (function-lambda-expression f)) (:newline)))))
450
451 #|
452
453 (defmethod emacs-inspect ((o t))
454 (let* ((class (class-of o))
455 (slots (mop::class-slots class)))
456 (mapcar (lambda (slot)
457 (let ((name (mop::slot-definition-name slot)))
458 (cons (princ-to-string name)
459 (slot-value o name))))
460 slots)))
461 |#
462
463 ;;;; Multithreading
464
465 (defimplementation startup-multiprocessing ()
466 #+nil(mp:start-scheduler))
467
468 (defimplementation spawn (fn &key name)
469 (ext:make-thread (lambda () (funcall fn)) :name name))
470
471 (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 :key (lambda (thread)
487 (getf (gethash thread *thread-props*) 'id))))
488
489 (defimplementation thread-name (thread)
490 (ext:thread-name thread))
491
492 (defimplementation thread-status (thread)
493 (format nil "Thread is ~:[dead~;alive~]" (ext:thread-alive-p thread)))
494
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 (defstruct mailbox
514 (mutex (ext:make-mutex))
515 (queue '()))
516
517 (defun mailbox (thread)
518 "Return THREAD's mailbox."
519 (ext:with-thread-lock (*thread-props-lock*)
520 (or (getf (gethash thread *thread-props*) 'mailbox)
521 (setf (getf (gethash thread *thread-props*) 'mailbox)
522 (make-mailbox)))))
523
524 (defimplementation send (thread object)
525 (let ((mbox (mailbox thread)))
526 (ext:with-mutex ((mailbox-mutex mbox))
527 (setf (mailbox-queue mbox)
528 (nconc (mailbox-queue mbox) (list message))))))
529
530 #+(or)
531 (defimplementation receive-if (thread &optional timeout)
532 (let* ((mbox (mailbox (current-thread))))
533 (assert (or (not timeout) (eq timeout t)))
534 (loop
535 (check-slime-interrupts)
536 (ext:with-mutex ((mailbox-mutex mbox))
537 (let* ((q (mailbox-queue mbox))
538 (tail (member-if test q)))
539 (when tail
540 (setf (mailbox-queue mbox) (nconc (ldiff q tail) (cdr tail)))
541 (return (car tail))))
542 (when (eq timeout t) (return (values nil t)))
543 ;;(java:jcall (java:jmethod "java.lang.Object" "wait")
544 ;; (mailbox-mutex mbox) 1000)
545 ))))
546
547 (defimplementation quit-lisp ()
548 (ext:exit))
549
550 ;; WORKAROUND: call/initialize accessors at load time
551 (let ((c (make-condition 'compiler-condition
552 :original-condition nil
553 :severity ':note :message "" :location nil))
554 (slots `(severity message short-message references location)))
555 (dolist (slot slots)
556 (funcall slot c)))

  ViewVC Help
Powered by ViewVC 1.1.5