blob: 88b1913e91e8f3dab71eecbd1bb8868fa44f9a4f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
(.module:
[lux #- i64]
(lux (control [monad #+ do]
["ex" exception #+ exception:]
["eq" equality])
(data [bit]
[maybe]
["e" error]
text/format)
[host]))
(exception: #export (index-out-of-bounds {description Text})
description)
(exception: #export (inverted-range {description Text})
description)
(type: #export Blob (host.type (Array byte)))
(host.import java/util/Arrays
(#static copyOfRange [(Array byte) int int] (Array byte))
(#static equals [(Array byte) (Array byte)] boolean))
(def: byte-mask
I64
(|> +1 (bit.left-shift +8) dec .i64))
(def: i64
(-> (primitive "java.lang.Byte") I64)
(|>> host.byte-to-long (:! I64) (bit.and byte-mask)))
(def: byte
(-> (I64 Top) (primitive "java.lang.Byte"))
(|>> .int host.long-to-byte))
(def: #export (create size)
(-> Nat Blob)
(host.array byte size))
(def: #export (read-8 idx blob)
(-> Nat Blob (e.Error I64))
(if (n/< (host.array-length blob) idx)
(|> (host.array-read idx blob) ..i64 #e.Success)
(ex.throw index-out-of-bounds (%n idx))))
(def: #export (read-16 idx blob)
(-> Nat Blob (e.Error I64))
(if (n/< (host.array-length blob) (n/+ +1 idx))
(#e.Success ($_ bit.or
(bit.left-shift +8 (..i64 (host.array-read idx blob)))
(..i64 (host.array-read (n/+ +1 idx) blob))))
(ex.throw index-out-of-bounds (%n idx))))
(def: #export (read-32 idx blob)
(-> Nat Blob (e.Error I64))
(if (n/< (host.array-length blob) (n/+ +3 idx))
(#e.Success ($_ bit.or
(bit.left-shift +24 (..i64 (host.array-read idx blob)))
(bit.left-shift +16 (..i64 (host.array-read (n/+ +1 idx) blob)))
(bit.left-shift +8 (..i64 (host.array-read (n/+ +2 idx) blob)))
(..i64 (host.array-read (n/+ +3 idx) blob))))
(ex.throw index-out-of-bounds (%n idx))))
(def: #export (read-64 idx blob)
(-> Nat Blob (e.Error I64))
(if (n/< (host.array-length blob) (n/+ +7 idx))
(#e.Success ($_ bit.or
(bit.left-shift +56 (..i64 (host.array-read idx blob)))
(bit.left-shift +48 (..i64 (host.array-read (n/+ +1 idx) blob)))
(bit.left-shift +40 (..i64 (host.array-read (n/+ +2 idx) blob)))
(bit.left-shift +32 (..i64 (host.array-read (n/+ +3 idx) blob)))
(bit.left-shift +24 (..i64 (host.array-read (n/+ +4 idx) blob)))
(bit.left-shift +16 (..i64 (host.array-read (n/+ +5 idx) blob)))
(bit.left-shift +8 (..i64 (host.array-read (n/+ +6 idx) blob)))
(..i64 (host.array-read (n/+ +7 idx) blob))))
(ex.throw index-out-of-bounds (%n idx))))
(def: #export (write-8 idx value blob)
(-> Nat (I64 Top) Blob (e.Error Blob))
(if (n/< (host.array-length blob) idx)
(exec (|> blob
(host.array-write idx (..byte value)))
(#e.Success blob))
(ex.throw index-out-of-bounds (%n idx))))
(def: #export (write-16 idx value blob)
(-> Nat (I64 Top) Blob (e.Error Blob))
(if (n/< (host.array-length blob) (n/+ +1 idx))
(exec (|> blob
(host.array-write idx (..byte (bit.logical-right-shift +8 value)))
(host.array-write (n/+ +1 idx) (..byte value)))
(#e.Success blob))
(ex.throw index-out-of-bounds (%n idx))))
(def: #export (write-32 idx value blob)
(-> Nat (I64 Top) Blob (e.Error Blob))
(if (n/< (host.array-length blob) (n/+ +3 idx))
(exec (|> blob
(host.array-write idx (..byte (bit.logical-right-shift +24 value)))
(host.array-write (n/+ +1 idx) (..byte (bit.logical-right-shift +16 value)))
(host.array-write (n/+ +2 idx) (..byte (bit.logical-right-shift +8 value)))
(host.array-write (n/+ +3 idx) (..byte value)))
(#e.Success blob))
(ex.throw index-out-of-bounds (%n idx))))
(def: #export (write-64 idx value blob)
(-> Nat (I64 Top) Blob (e.Error Blob))
(if (n/< (host.array-length blob) (n/+ +7 idx))
(exec (|> blob
(host.array-write idx (..byte (bit.logical-right-shift +56 value)))
(host.array-write (n/+ +1 idx) (..byte (bit.logical-right-shift +48 value)))
(host.array-write (n/+ +2 idx) (..byte (bit.logical-right-shift +40 value)))
(host.array-write (n/+ +3 idx) (..byte (bit.logical-right-shift +32 value)))
(host.array-write (n/+ +4 idx) (..byte (bit.logical-right-shift +24 value)))
(host.array-write (n/+ +5 idx) (..byte (bit.logical-right-shift +16 value)))
(host.array-write (n/+ +6 idx) (..byte (bit.logical-right-shift +8 value)))
(host.array-write (n/+ +7 idx) (..byte value)))
(#e.Success blob))
(ex.throw index-out-of-bounds (%n idx))))
(def: #export (size blob)
(-> Blob Nat)
(host.array-length blob))
(def: #export (slice from to blob)
(-> Nat Nat Blob (e.Error Blob))
(with-expansions [<description> (as-is (format "from = " (%n from) " | " "to = " (%n to)))]
(let [size (host.array-length blob)]
(cond (not (n/<= to from))
(ex.throw inverted-range <description>)
(not (and (n/< size from)
(n/< size to)))
(ex.throw index-out-of-bounds <description>)
## else
(#e.Success (Arrays::copyOfRange [blob (:! Int from) (:! Int (inc to))]))))))
(def: #export (slice' from blob)
(-> Nat Blob (e.Error Blob))
(slice from (dec (host.array-length blob)) blob))
(struct: #export _ (eq.Eq Blob)
(def: (= reference sample)
(Arrays::equals [reference sample])))
|