@grain/stdlib 0.6.6 → 0.7.0

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 +57 -0
  2. package/LICENSE +1 -1
  3. package/README.md +2 -2
  4. package/array.gr +55 -7
  5. package/array.md +123 -77
  6. package/bigint.md +30 -30
  7. package/buffer.gr +20 -53
  8. package/buffer.md +47 -47
  9. package/bytes.gr +111 -35
  10. package/bytes.md +111 -32
  11. package/char.gr +201 -99
  12. package/char.md +361 -34
  13. package/exception.gr +11 -11
  14. package/exception.md +26 -1
  15. package/float32.gr +327 -3
  16. package/float32.md +606 -19
  17. package/float64.gr +320 -3
  18. package/float64.md +606 -19
  19. package/fs.gr +1082 -0
  20. package/fs.md +630 -0
  21. package/hash.gr +142 -88
  22. package/hash.md +102 -14
  23. package/int16.md +23 -23
  24. package/int32.gr +25 -4
  25. package/int32.md +65 -30
  26. package/int64.gr +26 -1
  27. package/int64.md +65 -30
  28. package/int8.md +23 -23
  29. package/json.gr +366 -51
  30. package/json.md +418 -2
  31. package/list.gr +328 -31
  32. package/list.md +492 -69
  33. package/map.gr +20 -12
  34. package/map.md +44 -38
  35. package/marshal.gr +41 -40
  36. package/marshal.md +2 -2
  37. package/number.gr +159 -30
  38. package/number.md +215 -38
  39. package/option.md +21 -21
  40. package/package.json +5 -3
  41. package/path.gr +48 -0
  42. package/path.md +103 -12
  43. package/pervasives.gr +2 -2
  44. package/pervasives.md +37 -37
  45. package/priorityqueue.gr +7 -7
  46. package/priorityqueue.md +19 -19
  47. package/queue.gr +183 -29
  48. package/queue.md +296 -40
  49. package/random.md +6 -6
  50. package/range.gr +4 -4
  51. package/range.md +6 -6
  52. package/rational.md +16 -16
  53. package/regex.gr +52 -51
  54. package/regex.md +11 -11
  55. package/result.md +16 -16
  56. package/runtime/atof/common.md +39 -39
  57. package/runtime/atof/decimal.gr +6 -6
  58. package/runtime/atof/decimal.md +8 -8
  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 +1 -1
  71. package/runtime/dataStructures.md +33 -33
  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 +1 -1
  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 +4 -4
  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 +79 -73
  98. package/runtime/string.gr +37 -105
  99. package/runtime/string.md +3 -9
  100. package/runtime/unsafe/constants.md +24 -24
  101. package/runtime/unsafe/conv.md +13 -13
  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 +138 -138
  117. package/set.gr +18 -11
  118. package/set.md +42 -36
  119. package/stack.gr +171 -2
  120. package/stack.md +297 -15
  121. package/string.gr +352 -557
  122. package/string.md +77 -34
  123. package/uint16.md +22 -22
  124. package/uint32.gr +25 -4
  125. package/uint32.md +63 -28
  126. package/uint64.gr +25 -5
  127. package/uint64.md +63 -28
  128. package/uint8.md +22 -22
  129. package/uri.gr +57 -53
  130. package/uri.md +11 -12
  131. package/wasi/file.gr +67 -59
  132. package/wasi/file.md +39 -39
  133. package/wasi/process.md +5 -5
  134. package/wasi/random.md +3 -3
  135. package/wasi/time.md +4 -4
  136. package/runtime/utils/printing.gr +0 -60
  137. package/runtime/utils/printing.md +0 -26
package/bigint.md CHANGED
@@ -37,7 +37,7 @@ No other changes yet.
37
37
  </details>
38
38
 
39
39
  ```grain
40
- fromNumber : (number: Number) => BigInt
40
+ fromNumber: (number: Number) => BigInt
41
41
  ```
42
42
 
43
43
  Converts a Number to a BigInt.
@@ -62,7 +62,7 @@ No other changes yet.
62
62
  </details>
63
63
 
64
64
  ```grain
65
- toNumber : (num: BigInt) => Number
65
+ toNumber: (num: BigInt) => Number
66
66
  ```
67
67
 
68
68
  Converts a BigInt to a Number.
@@ -87,7 +87,7 @@ No other changes yet.
87
87
  </details>
88
88
 
89
89
  ```grain
90
- incr : (num: BigInt) => BigInt
90
+ incr: (num: BigInt) => BigInt
91
91
  ```
92
92
 
93
93
  Increments the value by one.
@@ -122,7 +122,7 @@ No other changes yet.
122
122
  </details>
123
123
 
124
124
  ```grain
125
- decr : (num: BigInt) => BigInt
125
+ decr: (num: BigInt) => BigInt
126
126
  ```
127
127
 
128
128
  Decrements the value by one.
@@ -157,7 +157,7 @@ No other changes yet.
157
157
  </details>
158
158
 
159
159
  ```grain
160
- neg : (num: BigInt) => BigInt
160
+ neg: (num: BigInt) => BigInt
161
161
  ```
162
162
 
163
163
  Negates the given operand.
@@ -192,7 +192,7 @@ No other changes yet.
192
192
  </details>
193
193
 
194
194
  ```grain
195
- abs : (num: BigInt) => BigInt
195
+ abs: (num: BigInt) => BigInt
196
196
  ```
197
197
 
198
198
  Returns the absolute value of the given operand.
@@ -234,7 +234,7 @@ BigInt.abs(-1t) == 1t
234
234
  </details>
235
235
 
236
236
  ```grain
237
- (+) : (num1: BigInt, num2: BigInt) => BigInt
237
+ (+): (num1: BigInt, num2: BigInt) => BigInt
238
238
  ```
239
239
 
240
240
  Computes the sum of its operands.
@@ -274,7 +274,7 @@ assert 1t + 1t == 2t
274
274
  </details>
275
275
 
276
276
  ```grain
277
- (-) : (num1: BigInt, num2: BigInt) => BigInt
277
+ (-): (num1: BigInt, num2: BigInt) => BigInt
278
278
  ```
279
279
 
280
280
  Computes the difference of its operands.
@@ -314,7 +314,7 @@ assert 3t - 1t == 2t
314
314
  </details>
315
315
 
316
316
  ```grain
317
- (*) : (num1: BigInt, num2: BigInt) => BigInt
317
+ (*): (num1: BigInt, num2: BigInt) => BigInt
318
318
  ```
319
319
 
320
320
  Computes the product of its operands.
@@ -354,7 +354,7 @@ assert 3t * 3t == 9t
354
354
  </details>
355
355
 
356
356
  ```grain
357
- (/) : (num1: BigInt, num2: BigInt) => BigInt
357
+ (/): (num1: BigInt, num2: BigInt) => BigInt
358
358
  ```
359
359
 
360
360
  Computes the quotient of its operands using signed (truncated) division
@@ -388,7 +388,7 @@ No other changes yet.
388
388
  </details>
389
389
 
390
390
  ```grain
391
- rem : (num1: BigInt, num2: BigInt) => BigInt
391
+ rem: (num1: BigInt, num2: BigInt) => BigInt
392
392
  ```
393
393
 
394
394
  Computes the remainder of the division of its operands using signed (truncated) division
@@ -421,7 +421,7 @@ No other changes yet.
421
421
  </details>
422
422
 
423
423
  ```grain
424
- quotRem : (num1: BigInt, num2: BigInt) => (BigInt, BigInt)
424
+ quotRem: (num1: BigInt, num2: BigInt) => (BigInt, BigInt)
425
425
  ```
426
426
 
427
427
  Computes the quotient and remainder of its operands using signed (truncated) division.
@@ -453,7 +453,7 @@ No other changes yet.
453
453
  </details>
454
454
 
455
455
  ```grain
456
- gcd : (num1: BigInt, num2: BigInt) => BigInt
456
+ gcd: (num1: BigInt, num2: BigInt) => BigInt
457
457
  ```
458
458
 
459
459
  Computes the greatest common divisior of the two operands.
@@ -492,7 +492,7 @@ BigInt.gcd(36t, 24t) == 12t
492
492
  </details>
493
493
 
494
494
  ```grain
495
- (<<) : (num: BigInt, places: Int32) => BigInt
495
+ (<<): (num: BigInt, places: Int32) => BigInt
496
496
  ```
497
497
 
498
498
  Shifts the bits of the value left by the given number of bits.
@@ -532,7 +532,7 @@ assert (10t << 2l) == 40t
532
532
  </details>
533
533
 
534
534
  ```grain
535
- (>>) : (num: BigInt, places: Int32) => BigInt
535
+ (>>): (num: BigInt, places: Int32) => BigInt
536
536
  ```
537
537
 
538
538
  Shifts the bits of the value right by the given number of bits, preserving the sign bit.
@@ -565,7 +565,7 @@ No other changes yet.
565
565
  </details>
566
566
 
567
567
  ```grain
568
- eqz : (num: BigInt) => Bool
568
+ eqz: (num: BigInt) => Bool
569
569
  ```
570
570
 
571
571
  Checks if the given value is equal to zero.
@@ -607,7 +607,7 @@ assert BigInt.eqz(1t) == false
607
607
  </details>
608
608
 
609
609
  ```grain
610
- (==) : (num1: BigInt, num2: BigInt) => Bool
610
+ (==): (num1: BigInt, num2: BigInt) => Bool
611
611
  ```
612
612
 
613
613
  Checks if the first value is equal to the second value.
@@ -652,7 +652,7 @@ assert -10t == -10t
652
652
  </details>
653
653
 
654
654
  ```grain
655
- (!=) : (num1: BigInt, num2: BigInt) => Bool
655
+ (!=): (num1: BigInt, num2: BigInt) => Bool
656
656
  ```
657
657
 
658
658
  Checks if the first value is not equal to the second value.
@@ -697,7 +697,7 @@ assert -10t != -20t
697
697
  </details>
698
698
 
699
699
  ```grain
700
- (<) : (num1: BigInt, num2: BigInt) => Bool
700
+ (<): (num1: BigInt, num2: BigInt) => Bool
701
701
  ```
702
702
 
703
703
  Checks if the first value is less than the second value.
@@ -742,7 +742,7 @@ assert -10t < 0t
742
742
  </details>
743
743
 
744
744
  ```grain
745
- (<=) : (num1: BigInt, num2: BigInt) => Bool
745
+ (<=): (num1: BigInt, num2: BigInt) => Bool
746
746
  ```
747
747
 
748
748
  Checks if the first value is less than or equal to the second value.
@@ -792,7 +792,7 @@ assert 2t <= 3t
792
792
  </details>
793
793
 
794
794
  ```grain
795
- (>) : (num1: BigInt, num2: BigInt) => Bool
795
+ (>): (num1: BigInt, num2: BigInt) => Bool
796
796
  ```
797
797
 
798
798
  Checks if the first value is greater than the second value.
@@ -837,7 +837,7 @@ assert 0t > -10t
837
837
  </details>
838
838
 
839
839
  ```grain
840
- (>=) : (num1: BigInt, num2: BigInt) => Bool
840
+ (>=): (num1: BigInt, num2: BigInt) => Bool
841
841
  ```
842
842
 
843
843
  Checks if the first value is greater than or equal to the second value.
@@ -880,7 +880,7 @@ No other changes yet.
880
880
  </details>
881
881
 
882
882
  ```grain
883
- lnot : (num: BigInt) => BigInt
883
+ lnot: (num: BigInt) => BigInt
884
884
  ```
885
885
 
886
886
  Computes the bitwise NOT of the given value.
@@ -918,7 +918,7 @@ BigInt.lnot(91234t) == -91235t
918
918
  </details>
919
919
 
920
920
  ```grain
921
- (&) : (num1: BigInt, num2: BigInt) => BigInt
921
+ (&): (num1: BigInt, num2: BigInt) => BigInt
922
922
  ```
923
923
 
924
924
  Computes the bitwise AND (`&`) on the given operands.
@@ -958,7 +958,7 @@ assert (4t & 3t) == 0t
958
958
  </details>
959
959
 
960
960
  ```grain
961
- (|) : (num1: BigInt, num2: BigInt) => BigInt
961
+ (|): (num1: BigInt, num2: BigInt) => BigInt
962
962
  ```
963
963
 
964
964
  Computes the bitwise OR (`|`) on the given operands.
@@ -998,7 +998,7 @@ assert (5t | 3t) == 7t
998
998
  </details>
999
999
 
1000
1000
  ```grain
1001
- (^) : (num1: BigInt, num2: BigInt) => BigInt
1001
+ (^): (num1: BigInt, num2: BigInt) => BigInt
1002
1002
  ```
1003
1003
 
1004
1004
  Computes the bitwise XOR (`^`) on the given operands.
@@ -1031,7 +1031,7 @@ No other changes yet.
1031
1031
  </details>
1032
1032
 
1033
1033
  ```grain
1034
- clz : (num: BigInt) => Int32
1034
+ clz: (num: BigInt) => Int32
1035
1035
  ```
1036
1036
 
1037
1037
  Counts the number of leading zero bits in the value.
@@ -1063,7 +1063,7 @@ No other changes yet.
1063
1063
  </details>
1064
1064
 
1065
1065
  ```grain
1066
- ctz : (num: BigInt) => Int64
1066
+ ctz: (num: BigInt) => Int64
1067
1067
  ```
1068
1068
 
1069
1069
  Counts the number of trailing zero bits in the value.
@@ -1094,7 +1094,7 @@ No other changes yet.
1094
1094
  </details>
1095
1095
 
1096
1096
  ```grain
1097
- popcnt : (num: BigInt) => Option<Int64>
1097
+ popcnt: (num: BigInt) => Option<Int64>
1098
1098
  ```
1099
1099
 
1100
1100
  Counts the number of bits set to `1` in the value, also known as a population count.
@@ -1126,7 +1126,7 @@ No other changes yet.
1126
1126
  </details>
1127
1127
 
1128
1128
  ```grain
1129
- toString : (num: BigInt) => String
1129
+ toString: (num: BigInt) => String
1130
1130
  ```
1131
1131
 
1132
1132
  Converts the given operand to a string.
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 }
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
 
@@ -389,48 +391,12 @@ provide let addString = (string, buffer) => {
389
391
  */
390
392
  @unsafe
391
393
  provide let addChar = (char, buffer) => {
392
- use WasmI32.{ (-), (*), (&), (|), (>>>), ltU as (<), gtU as (>), leU as (<=) }
393
394
  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
395
+ let byteCount = tagSimpleNumber(usvEncodeLength(usv))
396
+ autogrow(byteCount, buffer)
397
+ let index = buffer.len
398
+ buffer.len += byteCount
399
+ Bytes.setChar(index, char, buffer.data)
434
400
  }
435
401
 
436
402
  /**
@@ -514,7 +480,8 @@ provide let addBytesSlice = (
514
480
  use WasmI32.{ (-), (<), (>), (>=) }
515
481
 
516
482
  // bounds check start
517
- let bytelen = WasmI32.load(WasmI32.fromGrain(bytes), 4n)
483
+ let src = WasmI32.fromGrain(bytes)
484
+ let bytelen = getSize(src)
518
485
  let srcOff = coerceNumberToWasmI32(start)
519
486
  if (srcOff < 0n || srcOff >= bytelen) {
520
487
  throw IndexOutOfBounds
@@ -529,9 +496,9 @@ provide let addBytesSlice = (
529
496
  autogrow(length, buffer)
530
497
 
531
498
  let dstOff = coerceNumberToWasmI32(buffer.len)
532
- let src = WasmI32.fromGrain(bytes)
533
499
  let dst = WasmI32.fromGrain(buffer.data)
534
500
  appendBytes(srcOff, dstOff, len, src, dst)
501
+ ignore(src)
535
502
 
536
503
  buffer.len += length
537
504
  }