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

Contents of /slime/swank-abcl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5