From 300a0256c8a5e469559a6ae6516d19e9dd3a1630 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Tue, 18 Oct 2011 15:13:02 -0700 Subject: [PATCH] new vocab tools.annotations.assertions Annotates unsafe words with assertions that their inputs and outputs are valid. Provide annotations for stream-read(-partial)-unsafe and (set-)nth-unsafe to start with. --- .../annotations/assertions/assertions.factor | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 basis/tools/annotations/assertions/assertions.factor diff --git a/basis/tools/annotations/assertions/assertions.factor b/basis/tools/annotations/assertions/assertions.factor new file mode 100644 index 0000000000..0e3c5cbb84 --- /dev/null +++ b/basis/tools/annotations/assertions/assertions.factor @@ -0,0 +1,48 @@ +USING: alien fry generalizations io io.ports kernel locals math +sequences sequences.private tools.annotations ; +IN: tools.annotations.assertions + +ERROR: invalid-nth-unsafe n seq word ; + +: check-nth-unsafe ( n seq word -- n seq ) + 2over length >= [ invalid-nth-unsafe ] [ drop ] if ; inline + +: (assert-nth-unsafe) ( word -- ) + dup [ swap '[ _ check-nth-unsafe @ ] ] curry annotate ; + +: assert-nth-unsafe ( -- ) + \ nth-unsafe (assert-nth-unsafe) + \ set-nth-unsafe (assert-nth-unsafe) ; + +: reset-nth-unsafe ( -- ) + \ nth-unsafe reset + \ set-nth-unsafe reset ; + +ERROR: invalid-stream-read-unsafe len buf port word ; +ERROR: invalid-stream-read-unsafe-return out-len in-len buf port word ; + +:: check-stream-read-unsafe-before ( n buf stream word -- n buf stream ) + buf alien? [ n buf port ] [ + n buf byte-length > + [ n buf stream word invalid-stream-read-unsafe ] + [ n buf stream ] if + ] if ; inline + +:: check-stream-read-unsafe-after ( count n buf stream word -- count ) + count n > + [ count n buf stream word invalid-stream-read-unsafe-return ] + [ count ] if ; + +: (assert-stream-read-unsafe) ( word -- ) + dup [ swap '[ _ + [ check-stream-read-unsafe-before @ ] + [ check-stream-read-unsafe-after ] 4 nbi + ] ] curry annotate ; + +: assert-stream-read-unsafe ( -- ) + \ stream-read-unsafe (assert-stream-read-unsafe) + \ stream-read-partial-unsafe (assert-stream-read-unsafe) ; + +: reset-stream-read-unsafe ( -- ) + \ stream-read-unsafe reset + \ stream-read-partial-unsafe reset ;