aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/data/binary.lux
blob: 40a7b3fc71f3c7af3daff21a6cef0ca8cd2ccc88 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
(.module:
  [lux (#- i64)
   ["@" target]
   ["." host]
   [abstract
    [monad (#+ do)]
    [equivalence (#+ Equivalence)]
    [monoid (#+ Monoid)]]
   [control
    ["." try (#+ Try)]
    ["." exception (#+ exception:)]]
   [data
    ["." maybe]
    [text
     ["%" format (#+ format)]]
    [collection
     ["." array]]]
   [math
    [number (#+ hex)
     ["n" nat]
     ["f" frac]
     ["." i64]]]])

(exception: #export (index_out_of_bounds {size Nat} {index Nat})
  (exception.report
   ["Size" (%.nat size)]
   ["Index" (%.nat index)]))

(template [<name>]
  [(exception: #export (<name> {size Nat} {from Nat} {to Nat})
     (exception.report
      ["Size" (%.nat size)]
      ["From" (%.nat from)]
      ["To" (%.nat to)]))]

  [slice_out_of_bounds]
  [inverted_slice]
  )

(with_expansions [<jvm> (as_is (type: #export Binary (host.type [byte]))

                               (host.import: java/lang/Object)
                               
                               (host.import: java/lang/System
                                 ["#::."
                                  (#static arraycopy [java/lang/Object int java/lang/Object int int] #try void)])

                               (host.import: java/util/Arrays
                                 ["#::."
                                  (#static copyOfRange [[byte] int int] [byte])
                                  (#static equals [[byte] [byte]] boolean)])

                               (def: byte_mask
                                 I64
                                 (|> i64.bits_per_byte i64.mask .i64))

                               (def: i64
                                 (-> (primitive "java.lang.Byte") I64)
                                 (|>> host.byte_to_long (:coerce I64) (i64.and ..byte_mask)))

                               (def: byte
                                 (-> (I64 Any) (primitive "java.lang.Byte"))
                                 (for {@.old
                                       (|>> .int host.long_to_byte)

                                       @.jvm
                                       (|>> .int (:coerce (primitive "java.lang.Long")) host.long_to_byte)})))]
  (for {@.old (as_is <jvm>)
        @.jvm (as_is <jvm>)

        @.js
        (as_is (host.import: ArrayBuffer
                 (new [host.Number]))
               
               (host.import: Uint8Array
                 (new [ArrayBuffer])
                 (length host.Number))
               
               (type: #export Binary
                 Uint8Array))

        @.python
        (type: #export Binary
          (primitive "bytearray"))

        @.scheme
        (as_is (type: #export Binary
                 (primitive "bytevector"))
               
               (host.import: (make-bytevector [Nat] Binary))
               (host.import: (bytevector-u8-ref [Binary Nat] I64))
               (host.import: (bytevector-u8-set! [Binary Nat (I64 Any)] Any))
               (host.import: (bytevector-length [Binary] Nat)))}

       ## Default
       (type: #export Binary
         (array.Array (I64 Any)))))

(template: (!size binary)
  (for {@.old (host.array_length binary)
        @.jvm (host.array_length binary)

        @.js
        (|> binary
            Uint8Array::length
            f.nat)

        @.python
        (|> binary
            (:coerce (array.Array (I64 Any)))
            "python array length")

        @.scheme
        (..bytevector-length [binary])}

       ## Default
       (array.size binary)))

(template: (!read idx binary)
  (for {@.old (..i64 (host.array_read idx binary))
        @.jvm (..i64 (host.array_read idx binary))

        @.js
        (|> binary
            (: ..Binary)
            (:coerce (array.Array .Frac))
            ("js array read" idx)
            f.nat
            .i64)

        @.python
        (|> binary
            (:coerce (array.Array .I64))
            ("python array read" idx))

        @.scheme
        (..bytevector-u8-ref [binary idx])}

       ## Default
       (|> binary
           (array.read idx)
           (maybe.default (: (I64 Any) 0))
           (:coerce I64))))

(template: (!!write <byte_type> <post> <write> idx value binary)
  (|> binary
      (: ..Binary)
      (:coerce (array.Array <byte_type>))
      (<write> idx (|> value .nat (n.% (hex "100")) <post>))
      (:coerce ..Binary)))

(template: (!write idx value binary)
  (for {@.old (host.array_write idx (..byte value) binary)
        @.jvm (host.array_write idx (..byte value) binary)

        @.js (!!write .Frac n.frac "js array write" idx value binary)
        @.python (!!write (I64 Any) (:coerce (I64 Any)) "python array write" idx value binary)
        @.scheme (exec (..bytevector-u8-set! [binary idx value])
                   binary)}

       ## Default
       (array.write! idx (|> value .nat (n.% (hex "100"))) binary)))

(def: #export size
  (-> Binary Nat)
  (|>> !size))

(def: #export create
  (-> Nat Binary)
  (for {@.old (|>> (host.array byte))
        @.jvm (|>> (host.array byte))

        @.js
        (|>> n.frac ArrayBuffer::new Uint8Array::new)

        @.python
        (|>> ("python apply" (:coerce host.Function ("python constant" "bytearray")))
             (:coerce Binary))

        @.scheme
        (|>> ..make-bytevector)}

       ## Default
       array.new))

(def: #export (fold f init binary)
  (All [a] (-> (-> I64 a a) a Binary a))
  (let [size (..!size binary)]
    (loop [idx 0
           output init]
      (if (n.< size idx)
        (recur (inc idx) (f (!read idx binary) output))
        output))))

(def: #export (read/8 idx binary)
  (-> Nat Binary (Try I64))
  (if (n.< (..!size binary) idx)
    (#try.Success (!read idx binary))
    (exception.throw ..index_out_of_bounds [(..!size binary) idx])))

(def: #export (read/16 idx binary)
  (-> Nat Binary (Try I64))
  (if (n.< (..!size binary) (n.+ 1 idx))
    (#try.Success ($_ i64.or
                      (i64.left_shift 8 (!read idx binary))
                      (!read (n.+ 1 idx) binary)))
    (exception.throw ..index_out_of_bounds [(..!size binary) idx])))

(def: #export (read/32 idx binary)
  (-> Nat Binary (Try I64))
  (if (n.< (..!size binary) (n.+ 3 idx))
    (#try.Success ($_ i64.or
                      (i64.left_shift 24 (!read idx binary))
                      (i64.left_shift 16 (!read (n.+ 1 idx) binary))
                      (i64.left_shift 8 (!read (n.+ 2 idx) binary))
                      (!read (n.+ 3 idx) binary)))
    (exception.throw ..index_out_of_bounds [(..!size binary) idx])))

(def: #export (read/64 idx binary)
  (-> Nat Binary (Try I64))
  (if (n.< (..!size binary) (n.+ 7 idx))
    (#try.Success ($_ i64.or
                      (i64.left_shift 56 (!read idx binary))
                      (i64.left_shift 48 (!read (n.+ 1 idx) binary))
                      (i64.left_shift 40 (!read (n.+ 2 idx) binary))
                      (i64.left_shift 32 (!read (n.+ 3 idx) binary))
                      (i64.left_shift 24 (!read (n.+ 4 idx) binary))
                      (i64.left_shift 16 (!read (n.+ 5 idx) binary))
                      (i64.left_shift 8 (!read (n.+ 6 idx) binary))
                      (!read (n.+ 7 idx) binary)))
    (exception.throw ..index_out_of_bounds [(..!size binary) idx])))

(def: #export (write/8 idx value binary)
  (-> Nat (I64 Any) Binary (Try Binary))
  (if (n.< (..!size binary) idx)
    (#try.Success (|> binary
                      (!write idx value)))
    (exception.throw ..index_out_of_bounds [(..!size binary) idx])))

(def: #export (write/16 idx value binary)
  (-> Nat (I64 Any) Binary (Try Binary))
  (if (n.< (..!size binary) (n.+ 1 idx))
    (#try.Success (|> binary
                      (!write idx (i64.right_shift 8 value))
                      (!write (n.+ 1 idx) value)))
    (exception.throw ..index_out_of_bounds [(..!size binary) idx])))

(def: #export (write/32 idx value binary)
  (-> Nat (I64 Any) Binary (Try Binary))
  (if (n.< (..!size binary) (n.+ 3 idx))
    (#try.Success (|> binary
                      (!write idx (i64.right_shift 24 value))
                      (!write (n.+ 1 idx) (i64.right_shift 16 value))
                      (!write (n.+ 2 idx) (i64.right_shift 8 value))
                      (!write (n.+ 3 idx) value)))
    (exception.throw ..index_out_of_bounds [(..!size binary) idx])))

(def: #export (write/64 idx value binary)
  (-> Nat (I64 Any) Binary (Try Binary))
  (if (n.< (..!size binary) (n.+ 7 idx))
    (for {@.scheme (let [write_high (|>> (!write idx (i64.right_shift 56 value))
                                         (!write (n.+ 1 idx) (i64.right_shift 48 value))
                                         (!write (n.+ 2 idx) (i64.right_shift 40 value))
                                         (!write (n.+ 3 idx) (i64.right_shift 32 value)))
                         write_low (|>> (!write (n.+ 4 idx) (i64.right_shift 24 value))
                                        (!write (n.+ 5 idx) (i64.right_shift 16 value))
                                        (!write (n.+ 6 idx) (i64.right_shift 8 value))
                                        (!write (n.+ 7 idx) value))]
                     (|> binary write_high write_low #try.Success))}
         (#try.Success (|> binary
                           (!write idx (i64.right_shift 56 value))
                           (!write (n.+ 1 idx) (i64.right_shift 48 value))
                           (!write (n.+ 2 idx) (i64.right_shift 40 value))
                           (!write (n.+ 3 idx) (i64.right_shift 32 value))
                           (!write (n.+ 4 idx) (i64.right_shift 24 value))
                           (!write (n.+ 5 idx) (i64.right_shift 16 value))
                           (!write (n.+ 6 idx) (i64.right_shift 8 value))
                           (!write (n.+ 7 idx) value))))
    (exception.throw ..index_out_of_bounds [(..!size binary) idx])))

(structure: #export equivalence
  (Equivalence Binary)
  
  (def: (= reference sample)
    (with_expansions [<jvm> (java/util/Arrays::equals reference sample)]
     (for {@.old <jvm>
           @.jvm <jvm>}
          (let [limit (!size reference)]
            (and (n.= limit
                      (!size sample))
                 (loop [idx 0]
                   (if (n.< limit idx)
                     (and (n.= (!read idx reference)
                               (!read idx sample))
                          (recur (inc idx)))
                     true))))))))

(for {@.old (as_is)
      @.jvm (as_is)}

     ## Default
     (exception: #export (cannot_copy_bytes {bytes Nat}
                                            {source_input Nat}
                                            {target_output Nat})
       (exception.report
        ["Bytes" (%.nat bytes)]
        ["Source input space" (%.nat source_input)]
        ["Target output space" (%.nat target_output)])))

(def: #export (copy bytes source_offset source target_offset target)
  (-> Nat Nat Binary Nat Binary (Try Binary))
  (with_expansions [<jvm> (as_is (do try.monad
                                   [_ (java/lang/System::arraycopy source (.int source_offset) target (.int target_offset) (.int bytes))]
                                   (wrap target)))]
    (for {@.old <jvm>
          @.jvm <jvm>}
         
         ## Default
         (let [source_input (n.- source_offset (!size source))
               target_output (n.- target_offset (!size target))]
           (if (n.<= source_input bytes)
             (loop [idx 0]
               (if (n.< bytes idx)
                 (exec (!write (n.+ target_offset idx)
                               (!read (n.+ source_offset idx) source)
                               target)
                   (recur (inc idx)))
                 (#try.Success target)))
             (exception.throw ..cannot_copy_bytes [bytes source_input target_output]))))))

(def: #export (slice from to binary)
  (-> Nat Nat Binary (Try Binary))
  (let [size (..!size binary)]
    (if (n.<= to from)
      (if (and (n.< size from)
               (n.< size to))
        (with_expansions [<jvm> (as_is (#try.Success (java/util/Arrays::copyOfRange binary (.int from) (.int (inc to)))))]
          (for {@.old <jvm>
                @.jvm <jvm>}
               
               ## Default
               (let [how_many (inc (n.- from to))]
                 (..copy how_many from binary 0 (..create how_many)))))
        (exception.throw ..slice_out_of_bounds [size from to]))
      (exception.throw ..inverted_slice [size from to]))))

(def: #export (drop from binary)
  (-> Nat Binary Binary)
  (case from
    0 binary
    _ (case (..slice from (dec (..!size binary)) binary)
        (#try.Success slice)
        slice
        
        (#try.Failure _)
        (..create 0))))

(structure: #export monoid
  (Monoid Binary)

  (def: identity
    (..create 0))

  (def: (compose left right)
    (let [sizeL (!size left)
          sizeR (!size right)
          output (..create (n.+ sizeL sizeR))]
      (exec
        (..copy sizeL 0 left 0 output)
        (..copy sizeR 0 right sizeL output)
        output))))