Skip to content

Commit

Permalink
Add type checks to default methods to prevent recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
yitzchak authored and easye committed Jan 23, 2024
1 parent bba779e commit 1d19fa6
Showing 1 changed file with 44 additions and 11 deletions.
55 changes: 44 additions & 11 deletions src/org/armedbear/lisp/gray-streams.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,15 @@
(defgeneric gray-pathname (pathspec))
(defgeneric gray-truename (filespec))

(defun assert-stream (stream)
(if (gray-streamp stream)
t
(error 'type-error :datum stream :expected-type 'stream)))

(defun bug-or-error (stream fun)
(assert-stream stream)
(error "The stream ~S has no suitable method for ~S." stream fun))

(defmethod gray-close ((stream fundamental-stream) &key abort)
(declare (ignore abort))
(setf (stream-open-p stream) nil)
Expand Down Expand Up @@ -260,10 +269,6 @@

(defclass fundamental-binary-stream (fundamental-stream) ())

(defmethod gray-stream-element-type ((s fundamental-binary-stream))
(declare (ignore s))
'(unsigned-byte 8))

(defgeneric stream-read-byte (stream))
(defgeneric stream-write-byte (stream integer))

Expand Down Expand Up @@ -599,33 +604,61 @@
80)))

(defmethod gray-stream-element-type (stream)
(funcall *ansi-stream-element-type* stream))
(if (ansi-streamp stream)
(funcall *ansi-stream-element-type* stream)
(bug-or-error stream 'gray-stream-element-type)))

(defmethod gray-close (stream &key abort)
(funcall *ansi-close* stream :abort abort))
(if (ansi-streamp stream)
(funcall *ansi-close* stream :abort abort)
(bug-or-error stream 'gray-close)))

(defmethod gray-input-stream-p (stream)
(funcall *ansi-input-stream-p* stream))
(cond ((ansi-streamp stream)
(funcall *ansi-input-stream-p* stream))
(t
(assert-stream stream)
nil)))

(defmethod gray-input-character-stream-p (stream)
(funcall *ansi-input-character-stream-p* stream))
(cond ((ansi-streamp stream)
(funcall *ansi-input-character-stream-p* stream))
(t
(assert-stream stream)
nil)))

(defmethod gray-output-stream-p (stream)
(funcall *ansi-output-stream-p* stream))
(cond ((ansi-streamp stream)
(funcall *ansi-output-stream-p* stream))
(t
(assert-stream stream)
nil)))

(defmethod gray-interactive-stream-p (stream)
(funcall *ansi-interactive-stream-p* stream))
(cond ((ansi-streamp stream)
(funcall *ansi-interactive-stream-p* stream))
(t
(assert-stream stream)
nil)))

(defmethod gray-open-stream-p (stream)
(funcall *ansi-open-stream-p* stream))
(cond ((ansi-streamp stream)
(funcall *ansi-open-stream-p* stream))
(t
(assert-stream stream)
nil)))

(defmethod gray-streamp (stream)
(funcall *ansi-streamp* stream))

(defmethod gray-pathname (pathspec)
(unless (typep pathspec '(or string pathname stream))
(error 'type-error :datum pathspec :expected-type '(or string pathname stream)))
(funcall *ansi-pathname* pathspec))

(defmethod gray-truename (pathspec)
(unless (typep pathspec '(or string pathname stream))
(error 'type-error :datum pathspec :expected-type '(or string pathname stream)))
(funcall *ansi-truename* pathspec))

(defun gray-write-sequence (sequence stream &key (start 0) end)
Expand Down

0 comments on commit 1d19fa6

Please sign in to comment.