@grain/stdlib 0.5.4 → 0.5.5

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 (59) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/array.gr +60 -57
  3. package/array.md +24 -6
  4. package/buffer.gr +71 -1
  5. package/buffer.md +142 -0
  6. package/bytes.gr +52 -3
  7. package/bytes.md +117 -0
  8. package/char.gr +21 -18
  9. package/char.md +18 -3
  10. package/immutablepriorityqueue.gr +13 -13
  11. package/int32.gr +39 -37
  12. package/int32.md +6 -0
  13. package/int64.gr +39 -37
  14. package/int64.md +6 -0
  15. package/list.gr +31 -22
  16. package/list.md +39 -10
  17. package/map.gr +19 -28
  18. package/number.gr +81 -5
  19. package/number.md +64 -2
  20. package/option.gr +30 -26
  21. package/option.md +12 -0
  22. package/package.json +1 -1
  23. package/path.gr +787 -0
  24. package/path.md +727 -0
  25. package/pervasives.gr +3 -4
  26. package/pervasives.md +6 -1
  27. package/queue.gr +11 -9
  28. package/queue.md +2 -0
  29. package/regex.gr +76 -3
  30. package/regex.md +70 -0
  31. package/result.gr +24 -20
  32. package/result.md +12 -0
  33. package/runtime/atof/common.gr +198 -0
  34. package/runtime/atof/common.md +243 -0
  35. package/runtime/atof/decimal.gr +663 -0
  36. package/runtime/atof/decimal.md +59 -0
  37. package/runtime/atof/lemire.gr +264 -0
  38. package/runtime/atof/lemire.md +6 -0
  39. package/runtime/atof/parse.gr +615 -0
  40. package/runtime/atof/parse.md +12 -0
  41. package/runtime/atof/slow.gr +238 -0
  42. package/runtime/atof/slow.md +6 -0
  43. package/runtime/atof/table.gr +2016 -0
  44. package/runtime/atof/table.md +12 -0
  45. package/runtime/{stringUtils.gr → atoi/parse.gr} +1 -1
  46. package/runtime/{stringUtils.md → atoi/parse.md} +1 -1
  47. package/runtime/bigint.gr +3 -3
  48. package/runtime/equal.gr +1 -1
  49. package/runtime/numberUtils.gr +2 -2
  50. package/runtime/numberUtils.md +6 -0
  51. package/runtime/numbers.gr +4 -4
  52. package/runtime/unsafe/conv.gr +21 -41
  53. package/runtime/unsafe/conv.md +0 -3
  54. package/runtime/unsafe/printWasm.gr +4 -40
  55. package/runtime/utils/printing.gr +3 -3
  56. package/stack.gr +4 -2
  57. package/stack.md +2 -0
  58. package/string.gr +1 -1
  59. package/sys/time.gr +4 -4
@@ -0,0 +1,12 @@
1
+ ### Table.**get_F64_POWERS10_FAST_PATH**
2
+
3
+ ```grain
4
+ get_F64_POWERS10_FAST_PATH : () -> WasmI32
5
+ ```
6
+
7
+ ### Table.**get_POWERS5**
8
+
9
+ ```grain
10
+ get_POWERS5 : () -> WasmI32
11
+ ```
12
+
@@ -18,7 +18,7 @@ import BI from "runtime/bigint"
18
18
  import { reducedInteger } from "runtime/numbers"
19
19
 
20
20
  @unsafe
21
- export let rec parseInt = (string: String, radix: Number) => {
21
+ export let parseInt = (string: String, radix: Number) => {
22
22
  let _CHAR_0 = 0x30n
23
23
  let _CHAR_B = 0x42n
24
24
  let _CHAR_b = 0x62n
@@ -1,4 +1,4 @@
1
- ### StringUtils.**parseInt**
1
+ ### Parse.**parseInt**
2
2
 
3
3
  ```grain
4
4
  parseInt : (String, Number) -> Result<Number, String>
package/runtime/bigint.gr CHANGED
@@ -786,7 +786,7 @@ let _SIZES = [>
786
786
  ]
787
787
 
788
788
  @unsafe
789
- export let rec bigIntToString = (num: WasmI32, base: WasmI32) => {
789
+ export let bigIntToString = (num: WasmI32, base: WasmI32) => {
790
790
  let getDigit = n =>
791
791
  WasmI32.load8U(WasmI32.add(WasmI32.fromGrain(_DIGITS), n), 8n)
792
792
  if (WasmI32.ltS(base, 2n) || WasmI32.gtS(base, 32n)) {
@@ -1339,7 +1339,7 @@ export let shl = (num: WasmI32, places: WasmI32) => {
1339
1339
  let (&) = WasmI64.and
1340
1340
  let a = places / 32n
1341
1341
  let b = places % 32n
1342
- let mask = (1N << WasmI64.extendI32U(b)) - 1N << 64N - WasmI64.extendI32U(b)
1342
+ let mask = ((1N << WasmI64.extendI32U(b)) - 1N) << 64N - WasmI64.extendI32U(b)
1343
1343
  let result = init(numLimbs + a)
1344
1344
  setFlag(result, _IS_NEGATIVE, getFlag(num, _IS_NEGATIVE))
1345
1345
  let numHalfLimbs = WasmI32.shl(numLimbs, 1n)
@@ -1350,7 +1350,7 @@ export let shl = (num: WasmI32, places: WasmI32) => {
1350
1350
  setHalfLimb(
1351
1351
  result,
1352
1352
  i + a,
1353
- WasmI32.wrapI64(acc << WasmI64.extendI32U(b) >> 32N)
1353
+ WasmI32.wrapI64((acc << WasmI64.extendI32U(b)) >> 32N)
1354
1354
  )
1355
1355
  }
1356
1356
  let (>) = WasmI64.gtU
package/runtime/equal.gr CHANGED
@@ -208,6 +208,6 @@ equalHelp = (x, y) => {
208
208
  }
209
209
 
210
210
  @unsafe
211
- export let rec equal = (x: a, y: a) => {
211
+ export let equal = (x: a, y: a) => {
212
212
  equalHelp(WasmI32.fromGrain(x), WasmI32.fromGrain(y))
213
213
  }
@@ -46,7 +46,7 @@ let _I32_MAX = 0xffffffffN
46
46
  let mut _POWERS10 = -1n
47
47
 
48
48
  @unsafe
49
- let get_POWERS10 = () => {
49
+ export let get_POWERS10 = () => {
50
50
  if (_POWERS10 == -1n) {
51
51
  _POWERS10 = Memory.malloc(40n)
52
52
  WasmI32.store(_POWERS10, 1n, 0n)
@@ -1277,7 +1277,7 @@ let genDigits = (buffer, w_frc, mp_frc, mp_exp, delta, sign) => {
1277
1277
  wp_w_frc = WasmI64.mul(
1278
1278
  wp_w_frc,
1279
1279
  WasmI64.extendI32U(
1280
- WasmI32.load(get_POWERS10() + (0n - kappa << 2n), 0n)
1280
+ WasmI32.load(get_POWERS10() + ((0n - kappa) << 2n), 0n)
1281
1281
  )
1282
1282
  )
1283
1283
  grisuRound(buffer, len, delta, p2, one_frc, wp_w_frc)
@@ -4,6 +4,12 @@
4
4
  _MAX_DOUBLE_LENGTH : WasmI32
5
5
  ```
6
6
 
7
+ ### NumberUtils.**get_POWERS10**
8
+
9
+ ```grain
10
+ get_POWERS10 : () -> WasmI32
11
+ ```
12
+
7
13
  ### NumberUtils.**get_HEX_DIGITS**
8
14
 
9
15
  ```grain
@@ -1426,7 +1426,7 @@ let numberTimesDivideBigIntHelp = (x, y, isDivide) => {
1426
1426
  },
1427
1427
  t when WasmI32.eq(t, Tags._GRAIN_BIGINT_BOXED_NUM_TAG) => {
1428
1428
  if (isDivide) {
1429
- reducedBigInteger(BI.div(x, y))
1429
+ reducedFractionBigInt(x, y)
1430
1430
  } else {
1431
1431
  reducedBigInteger(BI.mul(x, y))
1432
1432
  }
@@ -2220,7 +2220,7 @@ export let (>>) = (x: Number, y: Number) => {
2220
2220
  // we will fail if attempting to coerce to an int!
2221
2221
 
2222
2222
  @unsafe
2223
- export let rec coerceNumberToInt32 = (x: Number) => {
2223
+ export let coerceNumberToInt32 = (x: Number) => {
2224
2224
  let x = WasmI32.fromGrain(x)
2225
2225
  let result = if (
2226
2226
  !isSimpleNumber(x) &&
@@ -2239,7 +2239,7 @@ export let rec coerceNumberToInt32 = (x: Number) => {
2239
2239
  }
2240
2240
 
2241
2241
  @unsafe
2242
- export let rec coerceNumberToInt64 = (x: Number) => {
2242
+ export let coerceNumberToInt64 = (x: Number) => {
2243
2243
  let x = WasmI32.fromGrain(x)
2244
2244
  let result = if (
2245
2245
  !isSimpleNumber(x) &&
@@ -2257,7 +2257,7 @@ export let rec coerceNumberToInt64 = (x: Number) => {
2257
2257
  }
2258
2258
 
2259
2259
  @unsafe
2260
- export let rec coerceNumberToBigInt = (x: Number) => {
2260
+ export let coerceNumberToBigInt = (x: Number) => {
2261
2261
  let x = WasmI32.fromGrain(x)
2262
2262
  let result = if (isBigInt(x)) {
2263
2263
  // avoid extra malloc and prevent x from being freed
@@ -1,69 +1,53 @@
1
- import Memory from "runtime/unsafe/memory"
2
1
  import WasmI32 from "runtime/unsafe/wasmi32"
3
2
  import WasmI64 from "runtime/unsafe/wasmi64"
4
3
  import WasmF32 from "runtime/unsafe/wasmf32"
5
4
  import WasmF64 from "runtime/unsafe/wasmf64"
6
- import Tags from "runtime/unsafe/tags"
7
-
8
- @disableGC
5
+ import {
6
+ newInt32,
7
+ newInt64,
8
+ newFloat32,
9
+ newFloat64,
10
+ } from "runtime/dataStructures"
11
+
12
+ @unsafe
9
13
  export let toInt32 = n => {
10
- let ptr = Memory.malloc(12n)
11
- WasmI32.store(ptr, Tags._GRAIN_BOXED_NUM_HEAP_TAG, 0n)
12
- WasmI32.store(ptr, Tags._GRAIN_INT32_BOXED_NUM_TAG, 4n)
13
- WasmI32.store(ptr, n, 8n)
14
-
15
- WasmI32.toGrain(ptr): Int32
14
+ WasmI32.toGrain(newInt32(n)): Int32
16
15
  }
17
16
 
18
- @disableGC
17
+ @unsafe
19
18
  export let fromInt32 = (n: Int32) => {
20
19
  let ptr = WasmI32.fromGrain(n)
21
20
  WasmI32.load(ptr, 8n)
22
21
  }
23
22
 
24
- @disableGC
23
+ @unsafe
25
24
  export let toInt64 = n => {
26
- let ptr = Memory.malloc(16n)
27
- WasmI32.store(ptr, Tags._GRAIN_BOXED_NUM_HEAP_TAG, 0n)
28
- WasmI32.store(ptr, Tags._GRAIN_INT64_BOXED_NUM_TAG, 4n)
29
- WasmI64.store(ptr, n, 8n)
30
-
31
- WasmI32.toGrain(ptr): Int64
25
+ WasmI32.toGrain(newInt64(n)): Int64
32
26
  }
33
27
 
34
- @disableGC
28
+ @unsafe
35
29
  export let fromInt64 = (n: Int64) => {
36
30
  let ptr = WasmI32.fromGrain(n)
37
31
  WasmI64.load(ptr, 8n)
38
32
  }
39
33
 
40
- @disableGC
34
+ @unsafe
41
35
  export let toFloat32 = n => {
42
- let ptr = Memory.malloc(12n)
43
- WasmI32.store(ptr, Tags._GRAIN_BOXED_NUM_HEAP_TAG, 0n)
44
- WasmI32.store(ptr, Tags._GRAIN_FLOAT32_BOXED_NUM_TAG, 4n)
45
- WasmF32.store(ptr, n, 8n)
46
-
47
- WasmI32.toGrain(ptr): Float32
36
+ WasmI32.toGrain(newFloat32(n)): Float32
48
37
  }
49
38
 
50
- @disableGC
39
+ @unsafe
51
40
  export let fromFloat32 = (n: Float32) => {
52
41
  let ptr = WasmI32.fromGrain(n)
53
42
  WasmF32.load(ptr, 8n)
54
43
  }
55
44
 
56
- @disableGC
45
+ @unsafe
57
46
  export let toFloat64 = n => {
58
- let ptr = Memory.malloc(16n)
59
- WasmI32.store(ptr, Tags._GRAIN_BOXED_NUM_HEAP_TAG, 0n)
60
- WasmI32.store(ptr, Tags._GRAIN_FLOAT64_BOXED_NUM_TAG, 4n)
61
- WasmF64.store(ptr, n, 8n)
62
-
63
- WasmI32.toGrain(ptr): Float64
47
+ WasmI32.toGrain(newFloat64(n)): Float64
64
48
  }
65
49
 
66
- @disableGC
50
+ @unsafe
67
51
  export let fromFloat64 = (n: Float64) => {
68
52
  let ptr = WasmI32.fromGrain(n)
69
53
  WasmF64.load(ptr, 8n)
@@ -72,13 +56,10 @@ export let fromFloat64 = (n: Float64) => {
72
56
  /**
73
57
  * Converts a WasmI32 value to Number.
74
58
  *
75
- * This function is meant to be called from a `@disableGC` context without
76
- * need to call incRef on the function.
77
- *
78
59
  * @param n: The WasmI32 to convert
79
60
  * @returns The value converted to either a simple or a 32 bit heap allocated number.
80
61
  */
81
- @disableGC
62
+ @unsafe
82
63
  export let wasmI32ToNumber = (n: WasmI32) => {
83
64
  // Follows a little optimization. Instead of testing if n is range of allowed
84
65
  // non heap allocated simple numbers (-1073741824n..1073741823n), actually
@@ -106,8 +87,7 @@ export let wasmI32ToNumber = (n: WasmI32) => {
106
87
  // If it did overflow, then the value differs and we need to discard it and
107
88
  // allocate the number on the heap. A boxed 32 bit number actually is the
108
89
  // same thing as an Int32. It only needs to be cast into Number.
109
- let asInt32 = toInt32(n)
110
- WasmI32.toGrain(WasmI32.fromGrain(asInt32)): Number
90
+ WasmI32.toGrain(newInt32(n)): Number
111
91
  }
112
92
  result
113
93
  }
@@ -54,9 +54,6 @@ wasmI32ToNumber : WasmI32 -> Number
54
54
 
55
55
  Converts a WasmI32 value to Number.
56
56
 
57
- This function is meant to be called from a `@disableGC` context without
58
- need to call incRef on the function.
59
-
60
57
  Parameters:
61
58
 
62
59
  |param|type|description|
@@ -4,74 +4,38 @@ import Memory from "runtime/unsafe/memory"
4
4
 
5
5
  // [FIXME] These all leak ATM (grain-lang/grain#791)
6
6
 
7
- @disableGC
7
+ @unsafe
8
8
  export let printI32 = val => {
9
- Memory.incRef(WasmI32.fromGrain(print))
10
- Memory.incRef(WasmI32.fromGrain(toString))
11
9
  let conv = Conv.toInt32(val)
12
- Memory.incRef(WasmI32.fromGrain(conv))
13
- Memory.incRef(WasmI32.fromGrain(toString))
14
10
  let s1 = toString(conv)
15
11
  let s2 = "n"
16
- Memory.incRef(WasmI32.fromGrain(s1))
17
- Memory.incRef(WasmI32.fromGrain(s2))
18
- Memory.incRef(WasmI32.fromGrain((++)))
19
12
  let s = s1 ++ s2
20
- Memory.incRef(WasmI32.fromGrain(print))
21
- Memory.incRef(WasmI32.fromGrain(s))
22
13
  print(s)
23
14
  }
24
15
 
25
- @disableGC
16
+ @unsafe
26
17
  export let printI64 = val => {
27
- Memory.incRef(WasmI32.fromGrain(print))
28
- Memory.incRef(WasmI32.fromGrain(toString))
29
18
  let conv = Conv.toInt64(val)
30
- Memory.incRef(WasmI32.fromGrain(conv))
31
- Memory.incRef(WasmI32.fromGrain(toString))
32
19
  let s1 = toString(conv)
33
20
  let s2 = "N"
34
- Memory.incRef(WasmI32.fromGrain(s1))
35
- Memory.incRef(WasmI32.fromGrain(s2))
36
- Memory.incRef(WasmI32.fromGrain((++)))
37
21
  let s = s1 ++ s2
38
- Memory.incRef(WasmI32.fromGrain(print))
39
- Memory.incRef(WasmI32.fromGrain(s))
40
22
  print(s)
41
23
  }
42
24
 
43
- @disableGC
25
+ @unsafe
44
26
  export let printF32 = val => {
45
- Memory.incRef(WasmI32.fromGrain(print))
46
- Memory.incRef(WasmI32.fromGrain(toString))
47
27
  let conv = Conv.toFloat32(val)
48
- Memory.incRef(WasmI32.fromGrain(conv))
49
- Memory.incRef(WasmI32.fromGrain(toString))
50
28
  let s1 = toString(conv)
51
29
  let s2 = "w"
52
- Memory.incRef(WasmI32.fromGrain(s1))
53
- Memory.incRef(WasmI32.fromGrain(s2))
54
- Memory.incRef(WasmI32.fromGrain((++)))
55
30
  let s = s1 ++ s2
56
- Memory.incRef(WasmI32.fromGrain(print))
57
- Memory.incRef(WasmI32.fromGrain(s))
58
31
  print(s)
59
32
  }
60
33
 
61
- @disableGC
34
+ @unsafe
62
35
  export let printF64 = val => {
63
- Memory.incRef(WasmI32.fromGrain(print))
64
- Memory.incRef(WasmI32.fromGrain(toString))
65
36
  let conv = Conv.toFloat64(val)
66
- Memory.incRef(WasmI32.fromGrain(conv))
67
- Memory.incRef(WasmI32.fromGrain(toString))
68
37
  let s1 = toString(conv)
69
38
  let s2 = "W"
70
- Memory.incRef(WasmI32.fromGrain(s1))
71
- Memory.incRef(WasmI32.fromGrain(s2))
72
- Memory.incRef(WasmI32.fromGrain((++)))
73
39
  let s = s1 ++ s2
74
- Memory.incRef(WasmI32.fromGrain(print))
75
- Memory.incRef(WasmI32.fromGrain(s))
76
40
  print(s)
77
41
  }
@@ -12,12 +12,12 @@ import foreign wasm fd_write: (
12
12
  ) -> WasmI32 from "wasi_snapshot_preview1"
13
13
 
14
14
  @unsafe
15
- export let rec numberToString = (n: WasmI64) => {
15
+ export let numberToString = (n: WasmI64) => {
16
16
  NumberUtils.itoa64(n, 10n)
17
17
  }
18
18
 
19
19
  @unsafe
20
- export let rec printNumber = (n: WasmI64) => {
20
+ export let printNumber = (n: WasmI64) => {
21
21
  // like print(), but `s` should be a Grain string
22
22
  let (+) = WasmI32.add
23
23
  let s = numberToString(n)
@@ -40,7 +40,7 @@ export let rec printNumber = (n: WasmI64) => {
40
40
  }
41
41
 
42
42
  @unsafe
43
- export let rec printString = (s: String) => {
43
+ export let printString = (s: String) => {
44
44
  // like print(), but `s` should be a Grain string
45
45
  let (+) = WasmI32.add
46
46
  let ptr = WasmI32.fromGrain(s)
package/stack.gr CHANGED
@@ -1,6 +1,8 @@
1
1
  /**
2
2
  * @module Stack: An immutable stack implementation. A stack is a LIFO (last-in-first-out) data structure where new values are added, retrieved, and removed from the end.
3
3
  * @example import Stack from "stack"
4
+ *
5
+ * @deprecated This module will be renamed to ImmutableStack in the v0.6.0 release of Grain.
4
6
  */
5
7
 
6
8
  import List from "list"
@@ -22,7 +24,7 @@ record Stack<a> {
22
24
 
23
25
  /**
24
26
  * An empty stack.
25
- *
27
+ *
26
28
  * @since v0.5.4
27
29
  */
28
30
  export let empty = {
@@ -34,7 +36,7 @@ export let empty = {
34
36
  * Creates a new stack.
35
37
  *
36
38
  * @returns An empty stack
37
- *
39
+ *
38
40
  * @deprecated This will be removed in the v0.6.0 release of Grain.
39
41
  */
40
42
  export let make = () => {
package/stack.md CHANGED
@@ -2,6 +2,8 @@
2
2
  title: Stack
3
3
  ---
4
4
 
5
+ > **Deprecated:** This module will be renamed to ImmutableStack in the v0.6.0 release of Grain.
6
+
5
7
  An immutable stack implementation. A stack is a LIFO (last-in-first-out) data structure where new values are added, retrieved, and removed from the end.
6
8
 
7
9
  ```grain
package/string.gr CHANGED
@@ -221,7 +221,7 @@ export let lastIndexOf = (search: String, string: String) => {
221
221
  }
222
222
  }
223
223
 
224
- @disableGC
224
+ @unsafe
225
225
  let getCodePoint = (ptr: WasmI32) => {
226
226
  // Algorithm from https://encoding.spec.whatwg.org/#utf-8-decoder
227
227
  let (+) = WasmI32.add
package/sys/time.gr CHANGED
@@ -38,7 +38,7 @@ let getClockTime = (clockid, precision) => {
38
38
  * @returns `Ok(time)` of the current time if successful or `Err(exception)` otherwise
39
39
  */
40
40
  @unsafe
41
- export let rec realTime = () => {
41
+ export let realTime = () => {
42
42
  getClockTime(Wasi._CLOCK_REALTIME, 1000N)
43
43
  }
44
44
 
@@ -51,7 +51,7 @@ export let rec realTime = () => {
51
51
  * @returns `Ok(time)` of the current time if successful or `Err(exception)` otherwise
52
52
  */
53
53
  @unsafe
54
- export let rec monotonicTime = () => {
54
+ export let monotonicTime = () => {
55
55
  getClockTime(Wasi._CLOCK_MONOTONIC, 1N)
56
56
  }
57
57
 
@@ -61,7 +61,7 @@ export let rec monotonicTime = () => {
61
61
  * @returns `Ok(elapsed)` of the elapsed nanoseconds if successful or `Err(exception)` otherwise
62
62
  */
63
63
  @unsafe
64
- export let rec processCpuTime = () => {
64
+ export let processCpuTime = () => {
65
65
  getClockTime(Wasi._CLOCK_PROCESS_CPUTIME, 1N)
66
66
  }
67
67
 
@@ -71,6 +71,6 @@ export let rec processCpuTime = () => {
71
71
  * @returns `Ok(elapsed)` of the elapsed nanoseconds if successful or `Err(exception)` otherwise
72
72
  */
73
73
  @unsafe
74
- export let rec threadCpuTime = () => {
74
+ export let threadCpuTime = () => {
75
75
  getClockTime(Wasi._CLOCK_THREAD_CPUTIME, 1N)
76
76
  }