@grain/stdlib 0.4.0 → 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bytes.md ADDED
@@ -0,0 +1,621 @@
1
+ ---
2
+ title: Bytes
3
+ ---
4
+
5
+ Utilities for working with byte sequences.
6
+
7
+ <details disabled>
8
+ <summary tabindex="-1">Added in <code>0.3.2</code></summary>
9
+ No other changes yet.
10
+ </details>
11
+
12
+ ```grain
13
+ import Bytes from "bytes"
14
+ ```
15
+
16
+ ## Values
17
+
18
+ Functions for working with the Bytes data type.
19
+
20
+ ### Bytes.**make**
21
+
22
+ <details disabled>
23
+ <summary tabindex="-1">Added in <code>0.3.2</code></summary>
24
+ No other changes yet.
25
+ </details>
26
+
27
+ ```grain
28
+ make : Number -> Bytes
29
+ ```
30
+
31
+ Creates a new byte sequence of the input size.
32
+
33
+ Parameters:
34
+
35
+ |param|type|description|
36
+ |-----|----|-----------|
37
+ |`size`|`Number`|The number of bytes to store|
38
+
39
+ Returns:
40
+
41
+ |type|description|
42
+ |----|-----------|
43
+ |`Bytes`|The new byte sequence|
44
+
45
+ ### Bytes.**empty**
46
+
47
+ <details disabled>
48
+ <summary tabindex="-1">Added in <code>0.3.2</code></summary>
49
+ No other changes yet.
50
+ </details>
51
+
52
+ ```grain
53
+ empty : Bytes
54
+ ```
55
+
56
+ An empty byte sequence.
57
+
58
+ ### Bytes.**fromString**
59
+
60
+ <details disabled>
61
+ <summary tabindex="-1">Added in <code>0.3.2</code></summary>
62
+ No other changes yet.
63
+ </details>
64
+
65
+ ```grain
66
+ fromString : String -> Bytes
67
+ ```
68
+
69
+ Creates a new byte sequence from the input string.
70
+
71
+ Parameters:
72
+
73
+ |param|type|description|
74
+ |-----|----|-----------|
75
+ |`string`|`String`|The string to copy into a byte sequence|
76
+
77
+ Returns:
78
+
79
+ |type|description|
80
+ |----|-----------|
81
+ |`Bytes`|The new byte sequence|
82
+
83
+ ### Bytes.**toString**
84
+
85
+ <details disabled>
86
+ <summary tabindex="-1">Added in <code>0.3.2</code></summary>
87
+ No other changes yet.
88
+ </details>
89
+
90
+ ```grain
91
+ toString : Bytes -> String
92
+ ```
93
+
94
+ Creates a new string from the input bytes.
95
+
96
+ Parameters:
97
+
98
+ |param|type|description|
99
+ |-----|----|-----------|
100
+ |`bytes`|`Bytes`|The source byte sequence|
101
+
102
+ Returns:
103
+
104
+ |type|description|
105
+ |----|-----------|
106
+ |`String`|The string representation of the bytes|
107
+
108
+ ### Bytes.**length**
109
+
110
+ <details disabled>
111
+ <summary tabindex="-1">Added in <code>0.3.2</code></summary>
112
+ No other changes yet.
113
+ </details>
114
+
115
+ ```grain
116
+ length : Bytes -> Number
117
+ ```
118
+
119
+ Returns the length of a byte sequence.
120
+
121
+ Parameters:
122
+
123
+ |param|type|description|
124
+ |-----|----|-----------|
125
+ |`bytes`|`Bytes`|The byte sequence to inspect|
126
+
127
+ Returns:
128
+
129
+ |type|description|
130
+ |----|-----------|
131
+ |`Number`|The number of bytes|
132
+
133
+ ### Bytes.**copy**
134
+
135
+ <details disabled>
136
+ <summary tabindex="-1">Added in <code>0.3.2</code></summary>
137
+ No other changes yet.
138
+ </details>
139
+
140
+ ```grain
141
+ copy : Bytes -> Bytes
142
+ ```
143
+
144
+ Creates a new byte sequence that contains the same bytes as the input byte sequence.
145
+
146
+ Parameters:
147
+
148
+ |param|type|description|
149
+ |-----|----|-----------|
150
+ |`bytes`|`Bytes`|The byte sequence to copy|
151
+
152
+ Returns:
153
+
154
+ |type|description|
155
+ |----|-----------|
156
+ |`Bytes`|The new byte sequence|
157
+
158
+ ### Bytes.**slice**
159
+
160
+ <details disabled>
161
+ <summary tabindex="-1">Added in <code>0.3.2</code></summary>
162
+ No other changes yet.
163
+ </details>
164
+
165
+ ```grain
166
+ slice : (Number, Number, Bytes) -> Bytes
167
+ ```
168
+
169
+ Returns a copy of a subset of the input byte sequence.
170
+
171
+ Parameters:
172
+
173
+ |param|type|description|
174
+ |-----|----|-----------|
175
+ |`start`|`Number`|The start index|
176
+ |`length`|`Number`|The number of bytes to include after the starting index|
177
+ |`bytes`|`Bytes`|The byte sequence to copy from|
178
+
179
+ Returns:
180
+
181
+ |type|description|
182
+ |----|-----------|
183
+ |`Bytes`|A byte sequence with of the copied bytes|
184
+
185
+ ### Bytes.**resize**
186
+
187
+ <details disabled>
188
+ <summary tabindex="-1">Added in <code>0.3.2</code></summary>
189
+ No other changes yet.
190
+ </details>
191
+
192
+ ```grain
193
+ resize : (Number, Number, Bytes) -> Bytes
194
+ ```
195
+
196
+ Returns a copy of a byte sequence with bytes added or removed from the beginning and/or end.
197
+
198
+ A positive number represents bytes to add, while a negative number represents bytes to remove.
199
+
200
+ Parameters:
201
+
202
+ |param|type|description|
203
+ |-----|----|-----------|
204
+ |`left`|`Number`|The number of uninitialized bytes to prepend|
205
+ |`right`|`Number`|The number of uninitialized bytes to append|
206
+ |`bytes`|`Bytes`|The byte sequence get a subset of bytes from|
207
+
208
+ Returns:
209
+
210
+ |type|description|
211
+ |----|-----------|
212
+ |`Bytes`|A resized byte sequence|
213
+
214
+ ### Bytes.**move**
215
+
216
+ <details disabled>
217
+ <summary tabindex="-1">Added in <code>0.3.2</code></summary>
218
+ No other changes yet.
219
+ </details>
220
+
221
+ ```grain
222
+ move : (Number, Number, Number, Bytes, Bytes) -> Void
223
+ ```
224
+
225
+ Copies a range of bytes from a source byte sequence to a given location
226
+ in a destination byte sequence.
227
+
228
+ Parameters:
229
+
230
+ |param|type|description|
231
+ |-----|----|-----------|
232
+ |`srcIndex`|`Number`|The starting index to copy bytes from|
233
+ |`dstIndex`|`Number`|The starting index to copy bytes into|
234
+ |`length`|`Number`|The amount of bytes to copy from the source buffer|
235
+ |`src`|`Bytes`|The source byte sequence|
236
+ |`dst`|`Bytes`|The destination byte sequence|
237
+
238
+ ### Bytes.**concat**
239
+
240
+ <details disabled>
241
+ <summary tabindex="-1">Added in <code>0.3.2</code></summary>
242
+ No other changes yet.
243
+ </details>
244
+
245
+ ```grain
246
+ concat : (Bytes, Bytes) -> Bytes
247
+ ```
248
+
249
+ Creates a new byte sequence that contains the bytes of both byte sequences.
250
+
251
+ Parameters:
252
+
253
+ |param|type|description|
254
+ |-----|----|-----------|
255
+ |`bytes1`|`Bytes`|The beginning byte sequence|
256
+ |`bytes2`|`Bytes`|The ending byte sequence|
257
+
258
+ Returns:
259
+
260
+ |type|description|
261
+ |----|-----------|
262
+ |`Bytes`|The new byte sequence|
263
+
264
+ ### Bytes.**fill**
265
+
266
+ <details disabled>
267
+ <summary tabindex="-1">Added in <code>0.3.2</code></summary>
268
+ No other changes yet.
269
+ </details>
270
+
271
+ ```grain
272
+ fill : (Int32, Bytes) -> Void
273
+ ```
274
+
275
+ Replaces all bytes in a byte sequnce with the new value provided.
276
+
277
+ Parameters:
278
+
279
+ |param|type|description|
280
+ |-----|----|-----------|
281
+ |`value`|`Int32`|The value replacing each byte|
282
+ |`bytes`|`Bytes`|The byte sequence to update|
283
+
284
+ ## Binary operations on integers
285
+
286
+ Functions for encoding and decoding integers stored in a byte sequence.
287
+
288
+ ### Bytes.**getInt8S**
289
+
290
+ <details disabled>
291
+ <summary tabindex="-1">Added in <code>0.3.2</code></summary>
292
+ No other changes yet.
293
+ </details>
294
+
295
+ ```grain
296
+ getInt8S : (Number, Bytes) -> Int32
297
+ ```
298
+
299
+ Gets a signed 8-bit integer starting at the given byte index.
300
+
301
+ Parameters:
302
+
303
+ |param|type|description|
304
+ |-----|----|-----------|
305
+ |`index`|`Number`|The byte index to access|
306
+ |`bytes`|`Bytes`|The byte sequence to access|
307
+
308
+ Returns:
309
+
310
+ |type|description|
311
+ |----|-----------|
312
+ |`Int32`|A 32-bit integer representing a signed 8-bit integer that starts at the given index|
313
+
314
+ ### Bytes.**getInt8U**
315
+
316
+ <details disabled>
317
+ <summary tabindex="-1">Added in <code>0.3.2</code></summary>
318
+ No other changes yet.
319
+ </details>
320
+
321
+ ```grain
322
+ getInt8U : (Number, Bytes) -> Int32
323
+ ```
324
+
325
+ Gets an unsigned 8-bit integer starting at the given byte index.
326
+
327
+ Parameters:
328
+
329
+ |param|type|description|
330
+ |-----|----|-----------|
331
+ |`index`|`Number`|The byte index to access|
332
+ |`bytes`|`Bytes`|The byte sequence to access|
333
+
334
+ Returns:
335
+
336
+ |type|description|
337
+ |----|-----------|
338
+ |`Int32`|A 32-bit integer representing an unsigned 8-bit integer that starts at the given index|
339
+
340
+ ### Bytes.**setInt8**
341
+
342
+ <details disabled>
343
+ <summary tabindex="-1">Added in <code>0.3.2</code></summary>
344
+ No other changes yet.
345
+ </details>
346
+
347
+ ```grain
348
+ setInt8 : (Number, Int32, Bytes) -> Void
349
+ ```
350
+
351
+ Sets a signed 8-bit integer starting at the given byte index.
352
+
353
+ Parameters:
354
+
355
+ |param|type|description|
356
+ |-----|----|-----------|
357
+ |`index`|`Number`|The byte index to update|
358
+ |`value`|`Int32`|The value to set|
359
+ |`bytes`|`Bytes`|The byte sequence to mutate|
360
+
361
+ ### Bytes.**getInt16S**
362
+
363
+ <details disabled>
364
+ <summary tabindex="-1">Added in <code>0.3.2</code></summary>
365
+ No other changes yet.
366
+ </details>
367
+
368
+ ```grain
369
+ getInt16S : (Number, Bytes) -> Int32
370
+ ```
371
+
372
+ Gets a signed 16-bit integer starting at the given byte index.
373
+
374
+ Parameters:
375
+
376
+ |param|type|description|
377
+ |-----|----|-----------|
378
+ |`index`|`Number`|The byte index to access|
379
+ |`bytes`|`Bytes`|The byte sequence to access|
380
+
381
+ Returns:
382
+
383
+ |type|description|
384
+ |----|-----------|
385
+ |`Int32`|A 32-bit integer representing a signed 16-bit integer that starts at the given index|
386
+
387
+ ### Bytes.**getInt16U**
388
+
389
+ <details disabled>
390
+ <summary tabindex="-1">Added in <code>0.3.2</code></summary>
391
+ No other changes yet.
392
+ </details>
393
+
394
+ ```grain
395
+ getInt16U : (Number, Bytes) -> Int32
396
+ ```
397
+
398
+ Gets an unsigned 16-bit integer starting at the given byte index.
399
+
400
+ Parameters:
401
+
402
+ |param|type|description|
403
+ |-----|----|-----------|
404
+ |`index`|`Number`|The byte index to access|
405
+ |`bytes`|`Bytes`|The byte sequence to access|
406
+
407
+ Returns:
408
+
409
+ |type|description|
410
+ |----|-----------|
411
+ |`Int32`|A 32-bit integer representing an unsigned 16-bit integer that starts at the given index|
412
+
413
+ ### Bytes.**setInt16**
414
+
415
+ <details disabled>
416
+ <summary tabindex="-1">Added in <code>0.3.2</code></summary>
417
+ No other changes yet.
418
+ </details>
419
+
420
+ ```grain
421
+ setInt16 : (Number, Int32, Bytes) -> Void
422
+ ```
423
+
424
+ Sets a signed 16-bit integer starting at the given byte index.
425
+
426
+ Parameters:
427
+
428
+ |param|type|description|
429
+ |-----|----|-----------|
430
+ |`index`|`Number`|The byte index to update|
431
+ |`value`|`Int32`|The value to set|
432
+ |`bytes`|`Bytes`|The byte sequence to mutate|
433
+
434
+ ### Bytes.**getInt32**
435
+
436
+ <details disabled>
437
+ <summary tabindex="-1">Added in <code>0.3.2</code></summary>
438
+ No other changes yet.
439
+ </details>
440
+
441
+ ```grain
442
+ getInt32 : (Number, Bytes) -> Int32
443
+ ```
444
+
445
+ Gets a signed 32-bit integer starting at the given byte index.
446
+
447
+ Parameters:
448
+
449
+ |param|type|description|
450
+ |-----|----|-----------|
451
+ |`index`|`Number`|The byte index to access|
452
+ |`bytes`|`Bytes`|The byte sequence to access|
453
+
454
+ Returns:
455
+
456
+ |type|description|
457
+ |----|-----------|
458
+ |`Int32`|A signed 32-bit integer that starts at the given index|
459
+
460
+ ### Bytes.**setInt32**
461
+
462
+ <details disabled>
463
+ <summary tabindex="-1">Added in <code>0.3.2</code></summary>
464
+ No other changes yet.
465
+ </details>
466
+
467
+ ```grain
468
+ setInt32 : (Number, Int32, Bytes) -> Void
469
+ ```
470
+
471
+ Sets a signed 32-bit integer starting at the given byte index.
472
+
473
+ Parameters:
474
+
475
+ |param|type|description|
476
+ |-----|----|-----------|
477
+ |`index`|`Number`|The byte index to update|
478
+ |`value`|`Int32`|The value to set|
479
+ |`bytes`|`Bytes`|The byte sequence to mutate|
480
+
481
+ ### Bytes.**getFloat32**
482
+
483
+ <details disabled>
484
+ <summary tabindex="-1">Added in <code>0.3.2</code></summary>
485
+ No other changes yet.
486
+ </details>
487
+
488
+ ```grain
489
+ getFloat32 : (Number, Bytes) -> Float32
490
+ ```
491
+
492
+ Gets a 32-bit float starting at the given byte index.
493
+
494
+ Parameters:
495
+
496
+ |param|type|description|
497
+ |-----|----|-----------|
498
+ |`index`|`Number`|The byte index to access|
499
+ |`bytes`|`Bytes`|The byte sequence to access|
500
+
501
+ Returns:
502
+
503
+ |type|description|
504
+ |----|-----------|
505
+ |`Float32`|A 32-bit float that starts at the given index|
506
+
507
+ ### Bytes.**setFloat32**
508
+
509
+ <details disabled>
510
+ <summary tabindex="-1">Added in <code>0.3.2</code></summary>
511
+ No other changes yet.
512
+ </details>
513
+
514
+ ```grain
515
+ setFloat32 : (Number, Float32, Bytes) -> Void
516
+ ```
517
+
518
+ Sets a 32-bit float starting at the given byte index.
519
+
520
+ Parameters:
521
+
522
+ |param|type|description|
523
+ |-----|----|-----------|
524
+ |`index`|`Number`|The byte index to update|
525
+ |`value`|`Float32`|The value to set|
526
+ |`bytes`|`Bytes`|The byte sequence to mutate|
527
+
528
+ ### Bytes.**getInt64**
529
+
530
+ <details disabled>
531
+ <summary tabindex="-1">Added in <code>0.3.2</code></summary>
532
+ No other changes yet.
533
+ </details>
534
+
535
+ ```grain
536
+ getInt64 : (Number, Bytes) -> Int64
537
+ ```
538
+
539
+ Gets a signed 64-bit integer starting at the given byte index.
540
+
541
+ Parameters:
542
+
543
+ |param|type|description|
544
+ |-----|----|-----------|
545
+ |`index`|`Number`|The byte index to access|
546
+ |`bytes`|`Bytes`|The byte sequence to access|
547
+
548
+ Returns:
549
+
550
+ |type|description|
551
+ |----|-----------|
552
+ |`Int64`|A signed 64-bit integer that starts at the given index|
553
+
554
+ ### Bytes.**setInt64**
555
+
556
+ <details disabled>
557
+ <summary tabindex="-1">Added in <code>0.3.2</code></summary>
558
+ No other changes yet.
559
+ </details>
560
+
561
+ ```grain
562
+ setInt64 : (Number, Int64, Bytes) -> Void
563
+ ```
564
+
565
+ Sets a signed 64-bit integer starting at the given byte index.
566
+
567
+ Parameters:
568
+
569
+ |param|type|description|
570
+ |-----|----|-----------|
571
+ |`index`|`Number`|The byte index to update|
572
+ |`value`|`Int64`|The value to set|
573
+ |`bytes`|`Bytes`|The byte sequence to mutate|
574
+
575
+ ### Bytes.**getFloat64**
576
+
577
+ <details disabled>
578
+ <summary tabindex="-1">Added in <code>0.3.2</code></summary>
579
+ No other changes yet.
580
+ </details>
581
+
582
+ ```grain
583
+ getFloat64 : (Number, Bytes) -> Float64
584
+ ```
585
+
586
+ Gets a 64-bit float starting at the given byte index.
587
+
588
+ Parameters:
589
+
590
+ |param|type|description|
591
+ |-----|----|-----------|
592
+ |`index`|`Number`|The byte index to access|
593
+ |`bytes`|`Bytes`|The byte sequence to access|
594
+
595
+ Returns:
596
+
597
+ |type|description|
598
+ |----|-----------|
599
+ |`Float64`|A 64-bit float that starts at the given index|
600
+
601
+ ### Bytes.**setFloat64**
602
+
603
+ <details disabled>
604
+ <summary tabindex="-1">Added in <code>0.3.2</code></summary>
605
+ No other changes yet.
606
+ </details>
607
+
608
+ ```grain
609
+ setFloat64 : (Number, Float64, Bytes) -> Void
610
+ ```
611
+
612
+ Sets a 64-bit float starting at the given byte index.
613
+
614
+ Parameters:
615
+
616
+ |param|type|description|
617
+ |-----|----|-----------|
618
+ |`index`|`Number`|The byte index to update|
619
+ |`value`|`Float64`|The value to set|
620
+ |`bytes`|`Bytes`|The byte sequence to mutate|
621
+
package/char.gr CHANGED
@@ -26,7 +26,7 @@ let isValid = (n) => {
26
26
  // @param char: Char - the input character
27
27
  // @returns Number
28
28
  @disableGC
29
- let code = (c: Char) => {
29
+ let rec code = (c: Char) => {
30
30
  // Algorithm from https://encoding.spec.whatwg.org/#utf-8-decoder
31
31
 
32
32
  let c = WasmI32.fromGrain(c)
@@ -86,6 +86,9 @@ let code = (c: Char) => {
86
86
  break
87
87
  }
88
88
  }
89
+
90
+ Memory.decRef(c)
91
+ Memory.decRef(WasmI32.fromGrain(code))
89
92
  tagSimpleNumber(result)
90
93
  }
91
94
 
@@ -93,7 +96,7 @@ let code = (c: Char) => {
93
96
  // @param codePoint: Number - the Unicode code point
94
97
  // @returns Char
95
98
  @disableGC
96
- let fromCode = (code: Number) => {
99
+ let rec fromCode = (code: Number) => {
97
100
  // Algorithm from https://encoding.spec.whatwg.org/#utf-8-encoder
98
101
 
99
102
  let (+) = WasmI32.add
@@ -113,7 +116,7 @@ let fromCode = (code: Number) => {
113
116
  }
114
117
 
115
118
  let code = code >>> 1n
116
- if (code < 0x80n) {
119
+ let result = if (code < 0x80n) {
117
120
  let char = allocateChar()
118
121
  WasmI32.store8(char, code, 4n)
119
122
  WasmI32.toGrain(char): Char
@@ -143,6 +146,11 @@ let fromCode = (code: Number) => {
143
146
 
144
147
  WasmI32.toGrain(char): Char
145
148
  }
149
+
150
+ // We've asserted that the original `code` was a stack allocated number so
151
+ // no need to decRef it
152
+ Memory.decRef(WasmI32.fromGrain(fromCode))
153
+ result
146
154
  }
147
155
 
148
156
  // Returns the next valid Unicode character by code point. Fails if the input character is U+10FFFF.
package/hash.gr CHANGED
@@ -1,5 +1,12 @@
1
1
  /* grainc-flags --no-gc */
2
2
 
3
+ /**
4
+ * @module Hash: Utilities for hashing any value.
5
+ * @example import Hash from "hash"
6
+ *
7
+ * @since v0.1.0
8
+ */
9
+
3
10
  /**
4
11
  This module implements MurmurHash3 for Grain data types.
5
12
  https://en.wikipedia.org/wiki/MurmurHash
@@ -208,14 +215,30 @@ let rec hashOne = (val, depth) => {
208
215
  hash32(val)
209
216
  }
210
217
  }
218
+ /**
219
+ * @section Values: Functions for hashing.
220
+ */
211
221
 
212
- export let hash = (a) => {
222
+ /**
223
+ * A generic hash function that produces an integer from any value. If `a == b` then `Hash.hash(a) == Hash.hash(b)`.
224
+ *
225
+ * @param anything: The value to hash
226
+ * @returns A hash for the given value
227
+ *
228
+ * @since v0.1.0
229
+ */
230
+ export let rec hash = (anything) => {
213
231
  h = seed
214
232
 
215
- hashOne(WasmI32.fromGrain(a), 0n)
233
+ hashOne(WasmI32.fromGrain(anything), 0n)
216
234
  finalize(0n)
217
235
 
218
236
  // Tag the number on the way out.
219
237
  // Since Grain has proper modulus, negative numbers are okay.
220
- tagSimpleNumber(h)
238
+ let result = tagSimpleNumber(h)
239
+
240
+ Memory.decRef(WasmI32.fromGrain(hash))
241
+ Memory.decRef(WasmI32.fromGrain(anything))
242
+
243
+ result
221
244
  }