aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/unsafe/lux/data/binary.lux
blob: 08af20a1d121f0ea5b2622465a0f7675f679fe82 (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
(.using
 [library
  [lux (.full)
   ["@" target]
   ["[0]" ffi]
   [control
    [function
     [inline (.only inline:)]]]
   [data
    [collection
     ["[0]" array "_"
      ["[1]" \\unsafe]]]]
   [math
    [number (.only hex)
     ["[0]" i64]]]]])

(with_expansions [<jvm> (these (type: .public Binary
                                 (ffi.type [byte]))

                               (ffi.import: java/lang/Object
                                 "[1]::[0]")
                               
                               (ffi.import: java/lang/System
                                 "[1]::[0]"
                                 ("static" arraycopy [java/lang/Object int java/lang/Object int int] void))

                               (ffi.import: java/util/Arrays
                                 "[1]::[0]"
                                 ("static" copyOfRange [[byte] int int] [byte])
                                 ("static" equals [[byte] [byte]] boolean)))]
  (for @.old (these <jvm>)
       @.jvm (these <jvm>)

       @.js
       (these (ffi.import: ArrayBuffer
                "[1]::[0]")
              (ffi.import: Uint8Array
                "[1]::[0]")
              
              (type: .public Binary
                Uint8Array))

       @.python
       (type: .public Binary
         (Primitive "bytearray"))

       @.scheme
       (these (type: .public Binary
                (Primitive "bytevector"))
              
              (ffi.import: (make-bytevector [Nat] Binary))
              (ffi.import: (bytevector-u8-ref [Binary Nat] I64))
              (ffi.import: (bytevector-u8-set! [Binary Nat (I64 Any)] Any))
              (ffi.import: (bytevector-length [Binary] Nat)))

       ... Default
       (type: .public Binary
         (array.Array (I64 Any)))))

(`` (with_expansions [<size> (.is .Nat size)
                      <jvm> (ffi.array byte <size>)
                      <jvm> (.is ..Binary <jvm>)]
      (template: .public (empty size)
        [(is ..Binary
             (for (~~ (.static @.old)) <jvm>
                  (~~ (.static @.jvm)) <jvm>

                  (~~ (.static @.js))
                  (.|> <size>
                       .int
                       "lux i64 f64"
                       []
                       ("js object new" ("js constant" "ArrayBuffer"))
                       []
                       ("js object new" ("js constant" "Uint8Array"))
                       (.as ..Binary))

                  (~~ (.static @.python))
                  (.|> <size>
                       []
                       ("python apply" (.as ffi.Function ("python constant" "bytearray")))
                       (.as ..Binary))

                  (~~ (.static @.scheme))
                  (..make-bytevector <size>)

                  ... Default
                  (array.empty <size>)))])))

(`` (with_expansions [<it> (.is ..Binary it)
                      <jvm> (ffi.length <it>)]
      (template: .public (size it)
        [(.is .Nat
              (.for (~~ (.static @.old)) <jvm>
                    (~~ (.static @.jvm)) <jvm>

                    (~~ (.static @.js))
                    (.|> <it>
                         ("js object get" "length")
                         (.as .Frac)
                         "lux f64 i64"
                         .nat)

                    (~~ (.static @.python))
                    (.|> <it>
                         (.as (array.Array (.I64 .Any)))
                         "python array length")

                    (~~ (.static @.scheme))
                    (..bytevector-length [<it>])

                    ... Default
                    (array.size <it>)))])))

(def: byte_mask
  Nat
  (i64.mask i64.bits_per_byte))

(with_expansions [<byte_mask> (.static ..byte_mask)
                  <it> (.is ..Binary it)
                  <index> (.is .Nat index)
                  <jvm> (ffi.read! <index> <it>)
                  <jvm> (ffi.byte_to_long <jvm>)
                  <jvm> (.|> <jvm>
                             (.as .I64)
                             ("lux i64 and" <byte_mask>))]
  (template: .public (bits_8 index it)
    [(.<| (.as .I64)
          (.is (.I64 .Any))
          (`` (.for (~~ (.static @.old)) (~~ <jvm>)
                    (~~ (.static @.jvm)) (~~ <jvm>)

                    (~~ (.static @.js))
                    (.|> <it>
                         (.as (array.Array .Frac))
                         ("js array read" <index>)
                         (.as .Frac)
                         "lux f64 i64"
                         .i64)

                    (~~ (.static @.python))
                    (.|> <it>
                         (.as (array.Array .I64))
                         ("python array read" <index>))

                    (~~ (.static @.scheme))
                    (..bytevector-u8-ref [<it> <index>])

                    ... Default
                    (.if (array.lacks? <index> <it>)
                      (.i64 0)
                      (array.item <index> <it>)))))]))

(template: .public (bits_16 index' it')
  [(.<| (.let [index (.is Nat index')
               it (.is ..Binary it')])
        (.as .I64)
        (.is (.I64 .Any))
        (.all "lux i64 or"
              ("lux i64 left-shift" 8 (..bits_8 index it))
              (..bits_8 ("lux i64 +" 1 index) it)))])

(template: .public (bits_32 index' it')
  [(.<| (.let [index (.is Nat index')
               it (.is ..Binary it')])
        (.as .I64)
        (.is (.I64 .Any))
        (.all "lux i64 or"
              ("lux i64 left-shift" 24 (..bits_8 index it))
              ("lux i64 left-shift" 16 (..bits_8 ("lux i64 +" 1 index) it))
              ("lux i64 left-shift" 8 (..bits_8 ("lux i64 +" 2 index) it))
              (..bits_8 ("lux i64 +" 3 index) it)))])

(template: .public (bits_64 index' it')
  [(.<| (.let [index (.is Nat index')
               it (.is ..Binary it')])
        (.as .I64)
        (.is (.I64 .Any))
        (.all "lux i64 or"
              ("lux i64 left-shift" 56 (..bits_8 index it))
              ("lux i64 left-shift" 48 (..bits_8 ("lux i64 +" 1 index) it))
              ("lux i64 left-shift" 40 (..bits_8 ("lux i64 +" 2 index) it))
              ("lux i64 left-shift" 32 (..bits_8 ("lux i64 +" 3 index) it))
              ("lux i64 left-shift" 24 (..bits_8 ("lux i64 +" 4 index) it))
              ("lux i64 left-shift" 16 (..bits_8 ("lux i64 +" 5 index) it))
              ("lux i64 left-shift" 8 (..bits_8 ("lux i64 +" 6 index) it))
              (..bits_8 ("lux i64 +" 7 index) it)))])

(with_expansions [<byte> (hex "FF")
                  <it> (.is ..Binary it)
                  <index> (.is .Nat index)
                  <value> (.is (.I64 .Any) value)
                  <jvm_value> (`` (.for (~~ (.static @.old))
                                        (.as .Int <value>)

                                        (~~ (.static @.jvm))
                                        (.as (.Primitive "java.lang.Long") <value>)
                                        <value>))
                  <jvm_value> <jvm_value>
                  <jvm_value> (ffi.long_to_byte <jvm_value>)
                  <jvm> (ffi.write! <index> <jvm_value> <it>)]
  (`` (template: .public (has_8! index value it)
        [(.is ..Binary
              (.for (~~ (.static @.old)) <jvm>
                    (~~ (.static @.jvm)) <jvm>

                    (~~ (.static @.js))
                    (.|> <it>
                         (.is ..Binary)
                         (.as (array.Array .Frac))
                         ("js array write" <index>
                          (.|> <value>
                               .int
                               ("lux i64 and" (.int <byte>))
                               "lux i64 f64"
                               .as_expected))
                         (.as ..Binary))
                    
                    (~~ (.static @.python))
                    (.|> <it>
                         (.is ..Binary)
                         (.as (array.Array (.I64 .Any)))
                         ("python array write" <index> (.|> <value> ("lux i64 and" <byte>) (.is (.I64 .Any))))
                         (.as ..Binary))
                    
                    (~~ (.static @.scheme))
                    (.let [it' <it>]
                      (.exec
                        (..bytevector-u8-set! [it' <index> <value>])
                        it'))

                    ... Default
                    (array.has! <index> (.|> <value> .int ("lux i64 and" (.int <byte>))) <it>)))])))

(template: .public (has_16! index' value' it)
  [(.let [index (.is .Nat index')
          value (.is (.I64 .Any) value')]
     (.|> it
          (..has_8! index ("lux i64 right-shift" 8 value))
          (..has_8! ("lux i64 +" 1 index) value)))])

(template: .public (has_32! index' value' it)
  [(.let [index (.is .Nat index')
          value (.is (.I64 .Any) value')]
     (.|> it
          (..has_8! index ("lux i64 right-shift" 24 value))
          (..has_8! ("lux i64 +" 1 index) ("lux i64 right-shift" 16 value))
          (..has_8! ("lux i64 +" 2 index) ("lux i64 right-shift" 8 value))
          (..has_8! ("lux i64 +" 3 index) value)))])

(`` (template: .public (has_64! index' value' it)
      [(.let [index (.is .Nat index')
              value (.is (.I64 .Any) value')]
         (.for (~~ (.static @.scheme)) (.let [write_high (.is (.-> ..Binary ..Binary)
                                                              (.|>> (..has_8! index ("lux i64 right-shift" 56 value))
                                                                    (..has_8! ("lux i64 +" 1 index) ("lux i64 right-shift" 48 value))
                                                                    (..has_8! ("lux i64 +" 2 index) ("lux i64 right-shift" 40 value))
                                                                    (..has_8! ("lux i64 +" 3 index) ("lux i64 right-shift" 32 value))))
                                              write_low (.is (.-> ..Binary ..Binary)
                                                             (.|>> (..has_8! ("lux i64 +" 4 index) ("lux i64 right-shift" 24 value))
                                                                   (..has_8! ("lux i64 +" 5 index) ("lux i64 right-shift" 16 value))
                                                                   (..has_8! ("lux i64 +" 6 index) ("lux i64 right-shift" 8 value))
                                                                   (..has_8! ("lux i64 +" 7 index) value)))]
                                         (.|> it
                                              write_high
                                              write_low))
               (.|> it
                    (..has_8! index ("lux i64 right-shift" 56 value))
                    (..has_8! ("lux i64 +" 1 index) ("lux i64 right-shift" 48 value))
                    (..has_8! ("lux i64 +" 2 index) ("lux i64 right-shift" 40 value))
                    (..has_8! ("lux i64 +" 3 index) ("lux i64 right-shift" 32 value))
                    (..has_8! ("lux i64 +" 4 index) ("lux i64 right-shift" 24 value))
                    (..has_8! ("lux i64 +" 5 index) ("lux i64 right-shift" 16 value))
                    (..has_8! ("lux i64 +" 6 index) ("lux i64 right-shift" 8 value))
                    (..has_8! ("lux i64 +" 7 index) value))))]))

(with_expansions [<reference> (.is ..Binary reference')
                  <sample> (.is ..Binary sample')
                  <jvm> (java/util/Arrays::equals <reference> <sample>)
                  <jvm> (ffi.of_boolean <jvm>)]
  (`` (template: .public (= reference' sample')
        [(.for (~~ (.static @.old)) <jvm>
               (~~ (.static @.jvm)) <jvm>
               (.let [reference <reference>
                      sample <sample>
                      limit (..size reference)]
                 (.and ("lux i64 =" limit (..size sample))
                       (.loop (again [index 0])
                         (.if ("lux i64 =" limit index)
                           .true
                           (.and ("lux i64 ="
                                  (..bits_8 index reference)
                                  (..bits_8 index sample))
                                 (again ("lux i64 +" 1 index))))))))])))

... TODO: Turn into a template ASAP.
(`` (inline: .public (copy! bytes source_offset source target_offset target)
      (-> .Nat .Nat ..Binary Nat ..Binary ..Binary)
      (with_expansions [<jvm> (java/lang/System::arraycopy source (ffi.as_int (.int source_offset))
                                                           target (ffi.as_int (.int target_offset))
                                                           (ffi.as_int (.int bytes)))
                        <jvm> (.exec
                                <jvm>
                                target)]
        (.for (~~ (.static @.old)) <jvm>
              (~~ (.static @.jvm)) <jvm>
              
              ... Default
              (.loop (again [index 0])
                (.if ("lux i64 <" (.int bytes) (.int index))
                  (.exec
                    (..has_8! ("lux i64 +" target_offset index)
                              (..bits_8 ("lux i64 +" source_offset index) source)
                              target)
                    (again ("lux i64 +" 1 index)))
                  target))))))

... TODO: Turn into a template ASAP.
(`` (with_expansions [<jvm> (java/util/Arrays::copyOfRange binary
                                                           (ffi.as_int (.int offset))
                                                           (ffi.as_int (.int limit)))
                      <jvm> (.let [limit ("lux i64 +" size offset)]
                              <jvm>)]
      (inline: .public (slice offset size binary)
        (-> .Nat .Nat ..Binary ..Binary)
        (.for (~~ (.static @.old)) <jvm>
              (~~ (.static @.jvm)) <jvm>
              
              ... Default
              (..copy! size offset binary 0 (..empty size))))))