]> gitweb.factorcode.org Git - factor.git/blob - basis/tools/annotations/assertions/assertions.factor
basis: ERROR: changes.
[factor.git] / basis / tools / annotations / assertions / assertions.factor
1 USING: alien fry generalizations io io.ports kernel locals math
2 sequences sequences.private tools.annotations ;
3 IN: tools.annotations.assertions
4
5 ERROR: invalid-nth-unsafe n seq word ;
6
7 : check-nth-unsafe ( n seq word -- n seq )
8     2over length >= [ invalid-nth-unsafe ] [ drop ] if ; inline
9
10 : (assert-nth-unsafe) ( word -- )
11     dup [ swap '[ _ check-nth-unsafe @ ] ] curry annotate ;
12
13 : assert-nth-unsafe ( -- )
14     \ nth-unsafe (assert-nth-unsafe)
15     \ set-nth-unsafe (assert-nth-unsafe) ;
16
17 : reset-nth-unsafe ( -- )
18     \ nth-unsafe reset
19     \ set-nth-unsafe reset ;
20
21 ERROR: invalid-stream-read-unsafe len buf port word ;
22 ERROR: invalid-stream-read-unsafe-return out-len in-len buf port word ;
23
24 :: check-stream-read-unsafe-before ( n buf stream word -- n buf stream )
25     buf alien? [ n buf port ] [
26         n buf byte-length >
27         [ n buf stream word throw-invalid-stream-read-unsafe ]
28         [ n buf stream ] if
29     ] if ; inline
30
31 :: check-stream-read-unsafe-after ( count n buf stream word -- count )
32     count n >
33     [ count n buf stream word throw-invalid-stream-read-unsafe-return ]
34     [ count ] if ;
35
36 : (assert-stream-read-unsafe) ( word -- )
37     dup [ swap '[ _
38         [ check-stream-read-unsafe-before @ ]
39         [ check-stream-read-unsafe-after ] 4 nbi
40     ] ] curry annotate ;
41
42 : assert-stream-read-unsafe ( -- )
43     \ stream-read-unsafe (assert-stream-read-unsafe)
44     \ stream-read-partial-unsafe (assert-stream-read-unsafe) ;
45
46 : reset-stream-read-unsafe ( -- )
47     \ stream-read-unsafe reset
48     \ stream-read-partial-unsafe reset ;