]> gitweb.factorcode.org Git - factor.git/commitdiff
new vocab tools.annotations.assertions
authorJoe Groff <arcata@gmail.com>
Tue, 18 Oct 2011 22:13:02 +0000 (15:13 -0700)
committerJoe Groff <arcata@gmail.com>
Tue, 18 Oct 2011 22:13:34 +0000 (15:13 -0700)
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.

basis/tools/annotations/assertions/assertions.factor [new file with mode: 0644]

diff --git a/basis/tools/annotations/assertions/assertions.factor b/basis/tools/annotations/assertions/assertions.factor
new file mode 100644 (file)
index 0000000..0e3c5cb
--- /dev/null
@@ -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 ;