aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/world/blob.jvm.lux
blob: 0027a4750ff92c02e2fcbdcbdc1cbde0a43c1596 (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
(.module:
  lux
  (lux (control [monad #+ do]
                ["ex" exception #+ exception:]
                [eq])
       (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
  Nat
  (|> +1 (bit.shift-left +8) n/dec))

(def: byte-to-nat
  (-> (primitive "java.lang.Byte") Nat)
  (|>> host.byte-to-long (:! Nat) (bit.and byte-mask)))

(def: #export (create size)
  (-> Nat Blob)
  (host.array byte size))

(def: #export (read-8 idx blob)
  (-> Nat Blob (e.Error Nat))
  (if (n/< (host.array-length blob) idx)
    (|> (host.array-read idx blob) byte-to-nat #e.Success)
    (ex.throw index-out-of-bounds (%n idx))))

(def: #export (read-16 idx blob)
  (-> Nat Blob (e.Error Nat))
  (if (n/< (host.array-length blob) (n/+ +1 idx))
    (#e.Success ($_ bit.or
                    (bit.shift-left +8 (byte-to-nat (host.array-read idx blob)))
                    (byte-to-nat (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 Nat))
  (if (n/< (host.array-length blob) (n/+ +3 idx))
    (#e.Success ($_ bit.or
                    (bit.shift-left +24 (byte-to-nat (host.array-read idx blob)))
                    (bit.shift-left +16 (byte-to-nat (host.array-read (n/+ +1 idx) blob)))
                    (bit.shift-left +8 (byte-to-nat (host.array-read (n/+ +2 idx) blob)))
                    (byte-to-nat (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 Nat))
  (if (n/< (host.array-length blob) (n/+ +7 idx))
    (#e.Success ($_ bit.or
                    (bit.shift-left +56 (byte-to-nat (host.array-read idx blob)))
                    (bit.shift-left +48 (byte-to-nat (host.array-read (n/+ +1 idx) blob)))
                    (bit.shift-left +40 (byte-to-nat (host.array-read (n/+ +2 idx) blob)))
                    (bit.shift-left +32 (byte-to-nat (host.array-read (n/+ +3 idx) blob)))
                    (bit.shift-left +24 (byte-to-nat (host.array-read (n/+ +4 idx) blob)))
                    (bit.shift-left +16 (byte-to-nat (host.array-read (n/+ +5 idx) blob)))
                    (bit.shift-left +8 (byte-to-nat (host.array-read (n/+ +6 idx) blob)))
                    (byte-to-nat (host.array-read (n/+ +7 idx) blob))))
    (ex.throw index-out-of-bounds (%n idx))))

(def: #export (write-8 idx value blob)
  (-> Nat Nat Blob (e.Error Top))
  (if (n/< (host.array-length blob) idx)
    (exec (|> blob
              (host.array-write idx (host.long-to-byte (:! Int value))))
      (#e.Success []))
    (ex.throw index-out-of-bounds (%n idx))))

(def: #export (write-16 idx value blob)
  (-> Nat Nat Blob (e.Error Top))
  (if (n/< (host.array-length blob) (n/+ +1 idx))
    (exec (|> blob
              (host.array-write idx (host.long-to-byte (:! Int (bit.shift-right +8 value))))
              (host.array-write (n/+ +1 idx) (host.long-to-byte (:! Int value))))
      (#e.Success []))
    (ex.throw index-out-of-bounds (%n idx))))

(def: #export (write-32 idx value blob)
  (-> Nat Nat Blob (e.Error Top))
  (if (n/< (host.array-length blob) (n/+ +3 idx))
    (exec (|> blob
              (host.array-write idx (host.long-to-byte (:! Int (bit.shift-right +24 value))))
              (host.array-write (n/+ +1 idx) (host.long-to-byte (:! Int (bit.shift-right +16 value))))
              (host.array-write (n/+ +2 idx) (host.long-to-byte (:! Int (bit.shift-right +8 value))))
              (host.array-write (n/+ +3 idx) (host.long-to-byte (:! Int value))))
      (#e.Success []))
    (ex.throw index-out-of-bounds (%n idx))))

(def: #export (write-64 idx value blob)
  (-> Nat Nat Blob (e.Error Top))
  (if (n/< (host.array-length blob) (n/+ +7 idx))
    (exec (|> blob
              (host.array-write idx (host.long-to-byte (:! Int (bit.shift-right +56 value))))
              (host.array-write (n/+ +1 idx) (host.long-to-byte (:! Int (bit.shift-right +48 value))))
              (host.array-write (n/+ +2 idx) (host.long-to-byte (:! Int (bit.shift-right +40 value))))
              (host.array-write (n/+ +3 idx) (host.long-to-byte (:! Int (bit.shift-right +32 value))))
              (host.array-write (n/+ +4 idx) (host.long-to-byte (:! Int (bit.shift-right +24 value))))
              (host.array-write (n/+ +5 idx) (host.long-to-byte (:! Int (bit.shift-right +16 value))))
              (host.array-write (n/+ +6 idx) (host.long-to-byte (:! Int (bit.shift-right +8 value))))
              (host.array-write (n/+ +7 idx) (host.long-to-byte (:! Int value))))
      (#e.Success []))
    (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 (n/inc to))]))))))

(def: #export (slice' from blob)
  (-> Nat Blob (e.Error Blob))
  (slice from (n/dec (host.array-length blob)) blob))

(struct: #export _ (eq.Eq Blob)
  (def: (= reference sample)
    (Arrays::equals [reference sample])))