disclosers.scm 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. ; Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. ; Authors: Richard Kelsey, Jonathan Rees, Mike Sperber
  3. ; --------------------
  4. ; DISCLOSE methods
  5. (define-method &disclose ((obj <closure>))
  6. (let ((id (template-id (closure-template obj)))
  7. (name (template-print-name (closure-template obj))))
  8. (if name
  9. (list 'procedure
  10. id
  11. ;; A heuristic that sometimes loses.
  12. ; (if (and (pair? name)
  13. ; (eq? (car name) '#t) ;Curried
  14. ; (vector? (closure-env obj)))
  15. ; (error-form
  16. ; (if (null? (cdddr name))
  17. ; (caddr name)
  18. ; (cdddr name))
  19. ; (reverse (cdr (vector->list (closure-env obj)))))
  20. ; name)
  21. name)
  22. (list 'procedure id))))
  23. (define-method &disclose ((obj <template>))
  24. (let ((id (template-id obj))
  25. (name (template-print-name obj)))
  26. (if name
  27. (list 'template id name)
  28. (list 'template id))))
  29. (define-method &disclose ((obj <location>))
  30. (cons 'location
  31. (cons (location-id obj)
  32. (let ((name (location-name obj)))
  33. (if (and name (not (eq? name (location-id obj))))
  34. (list name (location-package-name obj))
  35. '())))))
  36. (define-method &disclose ((obj <cell>))
  37. (if (cell-unassigned? obj)
  38. (list 'uninitialized-cell)
  39. (list 'cell (cell-ref obj))))
  40. ;; this overwrites the method defined in rts/continuation.scm
  41. (define-method &disclose ((obj <continuation>))
  42. (list (if (vm-exception-continuation? obj)
  43. 'vm-exception-continuation
  44. 'continuation)
  45. (list 'pc (continuation-pc obj))
  46. (let ((tem (continuation-template obj)))
  47. (if tem
  48. (or (template-print-name tem) ; <-- the original method doesn't have this
  49. (template-id tem))
  50. '?))))
  51. (define-method &disclose ((obj <code-vector>))
  52. (cons 'byte-vector
  53. (let ((z (code-vector-length obj)))
  54. (do ((i (- z 1) (- i 1))
  55. (l '() (cons (code-vector-ref obj i) l)))
  56. ((< i 0) l))))
  57. )
  58. (define-method &disclose ((obj <channel>))
  59. (let ((status (channel-status obj)))
  60. (list (cond ((= status (enum channel-status-option closed))
  61. 'closed-channel)
  62. ((or (= status (enum channel-status-option input))
  63. (= status (enum channel-status-option special-input)))
  64. 'input-channel)
  65. ((or (= status (enum channel-status-option output))
  66. (= status (enum channel-status-option special-output)))
  67. 'output-channel)
  68. (else ; shouldn't happen unless we get out of sync
  69. 'unknown-channel))
  70. (channel-id obj)
  71. (channel-os-index obj))))
  72. (define-method &disclose ((obj <port>))
  73. (disclose-port obj))
  74. (define (template-print-name tem)
  75. (make-print-name (template-names tem)))
  76. (define (make-print-name names)
  77. (if (null? names)
  78. #f
  79. (let ((name (car names))
  80. (parent-name (make-print-name (cdr names))))
  81. (cond (parent-name
  82. `(,(if name name 'unnamed)
  83. in
  84. ,@(if (pair? parent-name) parent-name (list parent-name))))
  85. ((string? name) #f) ;File name
  86. (else name)))))
  87. (define (template-file-name tem)
  88. (let loop ((names (template-names tem)))
  89. (if (null? names)
  90. #f
  91. (if (string? (car names))
  92. (car names)
  93. (loop (cdr names))))))
  94. ; --------------------
  95. ; Location names
  96. (define (location-info loc)
  97. (let ((id (location-id loc)))
  98. (if (integer? id)
  99. (table-ref location-info-table id)
  100. #f)))
  101. (define (location-name loc)
  102. (let ((probe (location-info loc)))
  103. (if probe
  104. (car probe)
  105. #f)))
  106. (define (location-package-name loc)
  107. (let ((probe (location-info loc)))
  108. (if probe
  109. (table-ref package-name-table (cdr probe))
  110. #f)))
  111. ; --------------------
  112. ; Associating names with templates
  113. (define (template-debug-data tem)
  114. (get-debug-data (template-info tem)))
  115. (define (template-id tem)
  116. (let ((info (template-info tem)))
  117. (if (debug-data? info)
  118. (debug-data-uid info)
  119. info)))
  120. (define (template-name tem)
  121. (let ((probe (template-debug-data tem)))
  122. (if probe
  123. (debug-data-name probe)
  124. '?)))
  125. (define (template-names tem)
  126. (debug-data-names (template-info tem)))
  127. ; We can follow parent links to get a full description of procedure
  128. ; nesting: "foo in bar in unnamed in baz"
  129. (define (debug-data-names info)
  130. (let ((dd (get-debug-data info)))
  131. (if (debug-data? dd) ;paranoid
  132. (cons (debug-data-name dd)
  133. (debug-data-names (debug-data-parent dd)))
  134. '())))
  135. ; --------------------
  136. ; Utilities
  137. (define (error-form proc args)
  138. (cons proc (map value->expression args)))
  139. ; Print non-self-evaluating value X as 'X.
  140. (define (value->expression obj) ;mumble
  141. (if (or (symbol? obj)
  142. (pair? obj)
  143. (null? obj)
  144. (vector? obj))
  145. `',obj
  146. obj))