@grain/stdlib 0.6.6 → 0.7.1

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.
Files changed (137) hide show
  1. package/CHANGELOG.md +67 -0
  2. package/LICENSE +1 -1
  3. package/README.md +2 -2
  4. package/array.gr +55 -7
  5. package/array.md +606 -560
  6. package/bigint.md +228 -228
  7. package/buffer.gr +85 -53
  8. package/buffer.md +442 -319
  9. package/bytes.gr +112 -35
  10. package/bytes.md +299 -219
  11. package/char.gr +201 -99
  12. package/char.md +447 -120
  13. package/exception.gr +11 -11
  14. package/exception.md +29 -4
  15. package/float32.gr +327 -3
  16. package/float32.md +698 -111
  17. package/float64.gr +320 -3
  18. package/float64.md +698 -111
  19. package/fs.gr +1082 -0
  20. package/fs.md +630 -0
  21. package/hash.gr +142 -88
  22. package/hash.md +105 -17
  23. package/int16.md +178 -178
  24. package/int32.gr +26 -5
  25. package/int32.md +266 -231
  26. package/int64.gr +27 -2
  27. package/int64.md +266 -231
  28. package/int8.md +178 -178
  29. package/json.gr +366 -51
  30. package/json.md +431 -15
  31. package/list.gr +328 -31
  32. package/list.md +759 -336
  33. package/map.gr +20 -12
  34. package/map.md +266 -260
  35. package/marshal.gr +41 -40
  36. package/marshal.md +14 -14
  37. package/number.gr +278 -35
  38. package/number.md +688 -269
  39. package/option.md +162 -162
  40. package/package.json +5 -3
  41. package/path.gr +48 -0
  42. package/path.md +180 -89
  43. package/pervasives.gr +2 -2
  44. package/pervasives.md +275 -275
  45. package/priorityqueue.gr +7 -7
  46. package/priorityqueue.md +131 -131
  47. package/queue.gr +183 -29
  48. package/queue.md +404 -148
  49. package/random.md +43 -43
  50. package/range.gr +4 -4
  51. package/range.md +42 -42
  52. package/rational.md +123 -123
  53. package/regex.gr +52 -51
  54. package/regex.md +102 -102
  55. package/result.md +118 -118
  56. package/runtime/atof/common.md +39 -39
  57. package/runtime/atof/decimal.gr +6 -6
  58. package/runtime/atof/decimal.md +14 -14
  59. package/runtime/atof/lemire.gr +5 -5
  60. package/runtime/atof/lemire.md +1 -1
  61. package/runtime/atof/parse.gr +16 -16
  62. package/runtime/atof/parse.md +2 -2
  63. package/runtime/atof/slow.md +1 -1
  64. package/runtime/atof/table.md +2 -2
  65. package/runtime/atoi/parse.gr +3 -3
  66. package/runtime/atoi/parse.md +1 -1
  67. package/runtime/bigint.gr +15 -47
  68. package/runtime/bigint.md +54 -60
  69. package/runtime/compare.gr +2 -2
  70. package/runtime/compare.md +8 -8
  71. package/runtime/dataStructures.md +211 -211
  72. package/runtime/debugPrint.gr +4 -1
  73. package/runtime/debugPrint.md +9 -9
  74. package/runtime/equal.gr +99 -77
  75. package/runtime/equal.md +8 -8
  76. package/runtime/exception.gr +62 -82
  77. package/runtime/exception.md +62 -11
  78. package/runtime/gc.gr +39 -45
  79. package/runtime/gc.md +4 -4
  80. package/runtime/malloc.gr +7 -7
  81. package/runtime/malloc.md +13 -13
  82. package/runtime/math/kernel/cos.gr +70 -0
  83. package/runtime/math/kernel/cos.md +14 -0
  84. package/runtime/math/kernel/sin.gr +65 -0
  85. package/runtime/math/kernel/sin.md +14 -0
  86. package/runtime/math/kernel/tan.gr +136 -0
  87. package/runtime/math/kernel/tan.md +14 -0
  88. package/runtime/math/rempio2.gr +244 -0
  89. package/runtime/math/rempio2.md +14 -0
  90. package/runtime/math/trig.gr +130 -0
  91. package/runtime/math/trig.md +28 -0
  92. package/runtime/math/umuldi.gr +26 -0
  93. package/runtime/math/umuldi.md +14 -0
  94. package/runtime/numberUtils.gr +29 -29
  95. package/runtime/numberUtils.md +12 -12
  96. package/runtime/numbers.gr +373 -381
  97. package/runtime/numbers.md +348 -342
  98. package/runtime/string.gr +37 -105
  99. package/runtime/string.md +20 -26
  100. package/runtime/unsafe/constants.md +24 -24
  101. package/runtime/unsafe/conv.md +19 -19
  102. package/runtime/unsafe/memory.gr +24 -20
  103. package/runtime/unsafe/memory.md +27 -7
  104. package/runtime/unsafe/offsets.gr +36 -0
  105. package/runtime/unsafe/offsets.md +88 -0
  106. package/runtime/unsafe/panic.gr +28 -0
  107. package/runtime/unsafe/panic.md +14 -0
  108. package/runtime/unsafe/tags.md +32 -32
  109. package/runtime/unsafe/wasmf32.md +28 -28
  110. package/runtime/unsafe/wasmf64.md +28 -28
  111. package/runtime/unsafe/wasmi32.md +47 -47
  112. package/runtime/unsafe/wasmi64.md +50 -50
  113. package/runtime/utf8.gr +189 -0
  114. package/runtime/utf8.md +117 -0
  115. package/runtime/wasi.gr +4 -2
  116. package/runtime/wasi.md +147 -147
  117. package/set.gr +18 -11
  118. package/set.md +253 -247
  119. package/stack.gr +171 -2
  120. package/stack.md +371 -89
  121. package/string.gr +352 -557
  122. package/string.md +298 -255
  123. package/uint16.md +170 -170
  124. package/uint32.gr +25 -4
  125. package/uint32.md +249 -214
  126. package/uint64.gr +25 -5
  127. package/uint64.md +249 -214
  128. package/uint8.md +170 -170
  129. package/uri.gr +57 -53
  130. package/uri.md +88 -89
  131. package/wasi/file.gr +67 -59
  132. package/wasi/file.md +308 -308
  133. package/wasi/process.md +26 -26
  134. package/wasi/random.md +12 -12
  135. package/wasi/time.md +16 -16
  136. package/runtime/utils/printing.gr +0 -60
  137. package/runtime/utils/printing.md +0 -26
package/buffer.gr CHANGED
@@ -16,13 +16,17 @@ from "runtime/unsafe/wasmi32" include WasmI32
16
16
  from "runtime/unsafe/conv" include Conv
17
17
  from "runtime/exception" include Exception
18
18
  from "runtime/dataStructures" include DataStructures
19
- use DataStructures.{ untagChar }
19
+ use DataStructures.{ untagChar, tagSimpleNumber }
20
20
  from "int32" include Int32
21
21
  from "bytes" include Bytes
22
22
  from "string" include String
23
23
  from "char" include Char
24
24
  from "runtime/numbers" include Numbers
25
25
  use Numbers.{ coerceNumberToWasmI32 }
26
+ from "runtime/utf8" include Utf8
27
+ use Utf8.{ usvEncodeLength, utf8ByteCount, exception MalformedUnicode }
28
+ from "runtime/unsafe/offsets" include Offsets
29
+ use Offsets.{ _BYTES_LEN_OFFSET, _BYTES_DATA_OFFSET }
26
30
 
27
31
  abstract record Buffer {
28
32
  mut len: Number,
@@ -30,12 +34,6 @@ abstract record Buffer {
30
34
  mut data: Bytes,
31
35
  }
32
36
 
33
- @unsafe
34
- let _SIZE_OFFSET = 4n
35
-
36
- @unsafe
37
- let _VALUE_OFFSET = 8n
38
-
39
37
  let _8BIT_LEN = 1
40
38
 
41
39
  let _16BIT_LEN = 2
@@ -46,7 +44,7 @@ let _64BIT_LEN = 8
46
44
 
47
45
  /* Gets the size of a Bytes via its ptr */
48
46
  @unsafe
49
- let getSize = ptr => WasmI32.load(ptr, _SIZE_OFFSET)
47
+ let getSize = ptr => WasmI32.load(ptr, _BYTES_LEN_OFFSET)
50
48
 
51
49
  /* Doubles the size of buffer's underlying byte sequence, if the given size is larger than the size of a buffer's underlying byte sequence */
52
50
  let autogrow = (len, buf) => {
@@ -75,7 +73,11 @@ let autogrow = (len, buf) => {
75
73
  @unsafe
76
74
  let appendBytes = (srcOff, dstOff, len, src, dst) => {
77
75
  use WasmI32.{ (+) }
78
- Memory.copy(dst + _VALUE_OFFSET + dstOff, src + _VALUE_OFFSET + srcOff, len)
76
+ Memory.copy(
77
+ dst + _BYTES_DATA_OFFSET + dstOff,
78
+ src + _BYTES_DATA_OFFSET + srcOff,
79
+ len
80
+ )
79
81
  }
80
82
 
81
83
  /*
@@ -237,7 +239,7 @@ provide let truncate = (length, buffer) => {
237
239
  let src = WasmI32.fromGrain(buffer.data)
238
240
  let size = getSize(src)
239
241
  let off = coerceNumberToWasmI32(length)
240
- let ret = Memory.fill(src + _VALUE_OFFSET + off, 0n, size - off)
242
+ let ret = Memory.fill(src + _BYTES_DATA_OFFSET + off, 0n, size - off)
241
243
  buffer.len = length
242
244
  }
243
245
 
@@ -374,6 +376,41 @@ provide let addString = (string, buffer) => {
374
376
  buffer.len += bytelen
375
377
  }
376
378
 
379
+ /**
380
+ * Gets the UTF-8 encoded character at the given byte index.
381
+ *
382
+ * @param index: The byte index to access
383
+ * @param buffer: The buffer to access
384
+ * @returns A character starting at the given index
385
+ *
386
+ * @throws IndexOutOfBounds: When `index` is negative
387
+ * @throws IndexOutOfBounds: When `index + 1` is greater than the buffer size
388
+ * @throws MalformedUnicode: When the bytes at the index are not a valid UTF-8 sequence
389
+ *
390
+ * @example
391
+ * let buf = Buffer.make(32)
392
+ * Buffer.addString("Hello World 🌾", buf)
393
+ * assert Buffer.getChar(12, buf) == '🌾'
394
+ *
395
+ * @since v0.7.1
396
+ */
397
+ @unsafe
398
+ provide let getChar = (index, buffer) => {
399
+ use WasmI32.{ (+), (&), (+), (==), (>) }
400
+ checkIsIndexInBounds(index, 1, buffer)
401
+ // Note: We do a raw check as we need the byte length before reading the full char
402
+ let bytes = buffer.data
403
+ let ptr = WasmI32.fromGrain(bytes)
404
+ let offset = coerceNumberToWasmI32(index)
405
+ let byte = WasmI32.load8U(ptr + offset, _BYTES_DATA_OFFSET)
406
+ let charSize = utf8ByteCount(byte)
407
+ if (offset + charSize > coerceNumberToWasmI32(buffer.len)) {
408
+ throw MalformedUnicode
409
+ }
410
+ ignore(bytes)
411
+ Bytes.getChar(index, bytes)
412
+ }
413
+
377
414
  /**
378
415
  * Appends the bytes of a character to a buffer.
379
416
  *
@@ -389,48 +426,12 @@ provide let addString = (string, buffer) => {
389
426
  */
390
427
  @unsafe
391
428
  provide let addChar = (char, buffer) => {
392
- use WasmI32.{ (-), (*), (&), (|), (>>>), ltU as (<), gtU as (>), leU as (<=) }
393
429
  let usv = untagChar(char)
394
-
395
- let bytelen = if (usv < 0x80n) {
396
- autogrow(1, buffer)
397
- use WasmI32.{ (+) }
398
- let off = coerceNumberToWasmI32(buffer.len)
399
- let dst = WasmI32.fromGrain(buffer.data) + _VALUE_OFFSET
400
- WasmI32.store8(dst, usv, off)
401
- 1
402
- } else {
403
- let mut count = 0n
404
- let mut bytelen = 0
405
- let mut offset = 0n
406
- if (usv <= 0x07FFn) {
407
- count = 1n
408
- bytelen = 2
409
- offset = 0xC0n
410
- } else if (usv <= 0xFFFFn) {
411
- count = 2n
412
- bytelen = 3
413
- offset = 0xE0n
414
- } else {
415
- count = 3n
416
- bytelen = 4
417
- offset = 0xF0n
418
- }
419
- use WasmI32.{ (+) }
420
- autogrow(bytelen, buffer)
421
- let off = coerceNumberToWasmI32(buffer.len)
422
- let dst = WasmI32.fromGrain(buffer.data) + _VALUE_OFFSET
423
- WasmI32.store8(dst, (usv >>> (6n * count)) + offset, off)
424
- let mut n = 0n
425
- while (count > 0n) {
426
- n += 1n
427
- let temp = usv >>> (6n * (count - 1n))
428
- WasmI32.store8(dst + n, 0x80n | temp & 0x3Fn, off)
429
- count -= 1n
430
- }
431
- bytelen
432
- }
433
- buffer.len += bytelen
430
+ let byteCount = tagSimpleNumber(usvEncodeLength(usv))
431
+ autogrow(byteCount, buffer)
432
+ let index = buffer.len
433
+ buffer.len += byteCount
434
+ Bytes.setChar(index, char, buffer.data)
434
435
  }
435
436
 
436
437
  /**
@@ -514,7 +515,8 @@ provide let addBytesSlice = (
514
515
  use WasmI32.{ (-), (<), (>), (>=) }
515
516
 
516
517
  // bounds check start
517
- let bytelen = WasmI32.load(WasmI32.fromGrain(bytes), 4n)
518
+ let src = WasmI32.fromGrain(bytes)
519
+ let bytelen = getSize(src)
518
520
  let srcOff = coerceNumberToWasmI32(start)
519
521
  if (srcOff < 0n || srcOff >= bytelen) {
520
522
  throw IndexOutOfBounds
@@ -529,9 +531,9 @@ provide let addBytesSlice = (
529
531
  autogrow(length, buffer)
530
532
 
531
533
  let dstOff = coerceNumberToWasmI32(buffer.len)
532
- let src = WasmI32.fromGrain(bytes)
533
534
  let dst = WasmI32.fromGrain(buffer.data)
534
535
  appendBytes(srcOff, dstOff, len, src, dst)
536
+ ignore(src)
535
537
 
536
538
  buffer.len += length
537
539
  }
@@ -624,8 +626,11 @@ provide let getInt8 = (index, buffer) => {
624
626
  * Buffer.setInt8(0, 3s, buf)
625
627
  * assert Buffer.getInt8(0, buf) == 3s
626
628
  *
629
+ * @deprecated Use `Bytes` instead of `Buffer` for set operations.
630
+ *
627
631
  * @since v0.4.0
628
632
  * @history v0.6.0: `value` argument type changed to `Int8`
633
+ * @history v0.7.1: Deprecated in favor of `Bytes.setInt8`
629
634
  */
630
635
  provide let setInt8 = (index, value, buffer) => {
631
636
  checkIsIndexInBounds(index, _8BIT_LEN, buffer)
@@ -691,7 +696,10 @@ provide let getUint8 = (index, buffer) => {
691
696
  * Buffer.setUint8(4us, buf)
692
697
  * assert Buffer.getUint8(0, buf) == 4us
693
698
  *
699
+ * @deprecated Use `Bytes` instead of `Buffer` for set operations.
700
+ *
694
701
  * @since v0.6.0
702
+ * @history v0.7.1: Deprecated in favor of `Bytes.setUint8`
695
703
  */
696
704
  provide let setUint8 = (index, value, buffer) => {
697
705
  checkIsIndexInBounds(index, _8BIT_LEN, buffer)
@@ -756,8 +764,11 @@ provide let getInt16 = (index, buffer) => {
756
764
  * Buffer.setInt16(5, 1S, buf)
757
765
  * assert Buffer.getInt16(5, buf) == 1S
758
766
  *
767
+ * @deprecated Use `Bytes` instead of `Buffer` for set operations.
768
+ *
759
769
  * @since v0.4.0
760
770
  * @history v0.6.0: `value` argument type changed to `Int16`
771
+ * @history v0.7.1: Deprecated in favor of `Bytes.setInt16`
761
772
  */
762
773
  provide let setInt16 = (index, value, buffer) => {
763
774
  checkIsIndexInBounds(index, _16BIT_LEN, buffer)
@@ -823,7 +834,10 @@ provide let getUint16 = (index, buffer) => {
823
834
  * Buffer.setUint16(0, 1uS, buf)
824
835
  * assert Buffer.getUint16(0, buf) == 1uS
825
836
  *
837
+ * @deprecated Use `Bytes` instead of `Buffer` for set operations.
838
+ *
826
839
  * @since v0.6.0
840
+ * @history v0.7.1: Deprecated in favor of `Bytes.setUint16`
827
841
  */
828
842
  provide let setUint16 = (index, value, buffer) => {
829
843
  checkIsIndexInBounds(index, _16BIT_LEN, buffer)
@@ -887,7 +901,10 @@ provide let getInt32 = (index, buffer) => {
887
901
  * Buffer.setInt32(3, 1l, buf)
888
902
  * assert Buffer.getInt32(3, buf) == 1l
889
903
  *
904
+ * @deprecated Use `Bytes` instead of `Buffer` for set operations.
905
+ *
890
906
  * @since v0.4.0
907
+ * @history v0.7.1: Deprecated in favor of `Bytes.setInt32`
891
908
  */
892
909
  provide let setInt32 = (index, value, buffer) => {
893
910
  checkIsIndexInBounds(index, _32BIT_LEN, buffer)
@@ -951,7 +968,10 @@ provide let getUint32 = (index, buffer) => {
951
968
  * Buffer.setUint32(0, 1ul, buf)
952
969
  * assert Buffer.getUint32(0, buf) == 1ul
953
970
  *
971
+ * @deprecated Use `Bytes` instead of `Buffer` for set operations.
972
+ *
954
973
  * @since v0.6.0
974
+ * @history v0.7.1: Deprecated in favor of `Bytes.setUint32`
955
975
  */
956
976
  provide let setUint32 = (index, value, buffer) => {
957
977
  checkIsIndexInBounds(index, _32BIT_LEN, buffer)
@@ -1015,7 +1035,10 @@ provide let getFloat32 = (index, buffer) => {
1015
1035
  * Buffer.setFloat32(0, 1.0f, buf)
1016
1036
  * assert Buffer.getFloat32(0, buf) == 1.0f
1017
1037
  *
1038
+ * @deprecated Use `Bytes` instead of `Buffer` for set operations.
1039
+ *
1018
1040
  * @since v0.4.0
1041
+ * @history v0.7.1: Deprecated in favor of `Bytes.setFloat32`
1019
1042
  */
1020
1043
  provide let setFloat32 = (index, value, buffer) => {
1021
1044
  checkIsIndexInBounds(index, _32BIT_LEN, buffer)
@@ -1082,7 +1105,10 @@ provide let getInt64 = (index, buffer) => {
1082
1105
  * Buffer.setInt64(0, 1L, buf)
1083
1106
  * assert Buffer.getInt64(0, buf) == 1L
1084
1107
  *
1108
+ * @deprecated Use `Bytes` instead of `Buffer` for set operations.
1109
+ *
1085
1110
  * @since v0.4.0
1111
+ * @history v0.7.1: Deprecated in favor of `Bytes.setInt64
1086
1112
  */
1087
1113
  provide let setInt64 = (index, value, buffer) => {
1088
1114
  checkIsIndexInBounds(index, _64BIT_LEN, buffer)
@@ -1149,7 +1175,10 @@ provide let getUint64 = (index, buffer) => {
1149
1175
  * Buffer.setUint64(0, 1uL, buf)
1150
1176
  * assert Buffer.getUint64(0, buf) == 1uL
1151
1177
  *
1178
+ * @deprecated Use `Bytes` instead of `Buffer` for set operations.
1179
+ *
1152
1180
  * @since v0.6.0
1181
+ * @history v0.7.1: Deprecated in favor of `Bytes.setUint64`
1153
1182
  */
1154
1183
  provide let setUint64 = (index, value, buffer) => {
1155
1184
  checkIsIndexInBounds(index, _64BIT_LEN, buffer)
@@ -1216,7 +1245,10 @@ provide let getFloat64 = (index, buffer) => {
1216
1245
  * Buffer.setFloat64(0, 1.0F, buf)
1217
1246
  * assert Buffer.getFloat64(0, buf) == 1.0F
1218
1247
  *
1248
+ * @deprecated Use `Bytes` instead of `Buffer` for set operations.
1249
+ *
1219
1250
  * @since v0.4.0
1251
+ * @history v0.7.1: Deprecated in favor of `Bytes.setFloat64`
1220
1252
  */
1221
1253
  provide let setFloat64 = (index, value, buffer) => {
1222
1254
  checkIsIndexInBounds(index, _64BIT_LEN, buffer)