@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.
- package/CHANGELOG.md +67 -0
- package/LICENSE +1 -1
- package/README.md +2 -2
- package/array.gr +55 -7
- package/array.md +606 -560
- package/bigint.md +228 -228
- package/buffer.gr +85 -53
- package/buffer.md +442 -319
- package/bytes.gr +112 -35
- package/bytes.md +299 -219
- package/char.gr +201 -99
- package/char.md +447 -120
- package/exception.gr +11 -11
- package/exception.md +29 -4
- package/float32.gr +327 -3
- package/float32.md +698 -111
- package/float64.gr +320 -3
- package/float64.md +698 -111
- package/fs.gr +1082 -0
- package/fs.md +630 -0
- package/hash.gr +142 -88
- package/hash.md +105 -17
- package/int16.md +178 -178
- package/int32.gr +26 -5
- package/int32.md +266 -231
- package/int64.gr +27 -2
- package/int64.md +266 -231
- package/int8.md +178 -178
- package/json.gr +366 -51
- package/json.md +431 -15
- package/list.gr +328 -31
- package/list.md +759 -336
- package/map.gr +20 -12
- package/map.md +266 -260
- package/marshal.gr +41 -40
- package/marshal.md +14 -14
- package/number.gr +278 -35
- package/number.md +688 -269
- package/option.md +162 -162
- package/package.json +5 -3
- package/path.gr +48 -0
- package/path.md +180 -89
- package/pervasives.gr +2 -2
- package/pervasives.md +275 -275
- package/priorityqueue.gr +7 -7
- package/priorityqueue.md +131 -131
- package/queue.gr +183 -29
- package/queue.md +404 -148
- package/random.md +43 -43
- package/range.gr +4 -4
- package/range.md +42 -42
- package/rational.md +123 -123
- package/regex.gr +52 -51
- package/regex.md +102 -102
- package/result.md +118 -118
- package/runtime/atof/common.md +39 -39
- package/runtime/atof/decimal.gr +6 -6
- package/runtime/atof/decimal.md +14 -14
- package/runtime/atof/lemire.gr +5 -5
- package/runtime/atof/lemire.md +1 -1
- package/runtime/atof/parse.gr +16 -16
- package/runtime/atof/parse.md +2 -2
- package/runtime/atof/slow.md +1 -1
- package/runtime/atof/table.md +2 -2
- package/runtime/atoi/parse.gr +3 -3
- package/runtime/atoi/parse.md +1 -1
- package/runtime/bigint.gr +15 -47
- package/runtime/bigint.md +54 -60
- package/runtime/compare.gr +2 -2
- package/runtime/compare.md +8 -8
- package/runtime/dataStructures.md +211 -211
- package/runtime/debugPrint.gr +4 -1
- package/runtime/debugPrint.md +9 -9
- package/runtime/equal.gr +99 -77
- package/runtime/equal.md +8 -8
- package/runtime/exception.gr +62 -82
- package/runtime/exception.md +62 -11
- package/runtime/gc.gr +39 -45
- package/runtime/gc.md +4 -4
- package/runtime/malloc.gr +7 -7
- package/runtime/malloc.md +13 -13
- package/runtime/math/kernel/cos.gr +70 -0
- package/runtime/math/kernel/cos.md +14 -0
- package/runtime/math/kernel/sin.gr +65 -0
- package/runtime/math/kernel/sin.md +14 -0
- package/runtime/math/kernel/tan.gr +136 -0
- package/runtime/math/kernel/tan.md +14 -0
- package/runtime/math/rempio2.gr +244 -0
- package/runtime/math/rempio2.md +14 -0
- package/runtime/math/trig.gr +130 -0
- package/runtime/math/trig.md +28 -0
- package/runtime/math/umuldi.gr +26 -0
- package/runtime/math/umuldi.md +14 -0
- package/runtime/numberUtils.gr +29 -29
- package/runtime/numberUtils.md +12 -12
- package/runtime/numbers.gr +373 -381
- package/runtime/numbers.md +348 -342
- package/runtime/string.gr +37 -105
- package/runtime/string.md +20 -26
- package/runtime/unsafe/constants.md +24 -24
- package/runtime/unsafe/conv.md +19 -19
- package/runtime/unsafe/memory.gr +24 -20
- package/runtime/unsafe/memory.md +27 -7
- package/runtime/unsafe/offsets.gr +36 -0
- package/runtime/unsafe/offsets.md +88 -0
- package/runtime/unsafe/panic.gr +28 -0
- package/runtime/unsafe/panic.md +14 -0
- package/runtime/unsafe/tags.md +32 -32
- package/runtime/unsafe/wasmf32.md +28 -28
- package/runtime/unsafe/wasmf64.md +28 -28
- package/runtime/unsafe/wasmi32.md +47 -47
- package/runtime/unsafe/wasmi64.md +50 -50
- package/runtime/utf8.gr +189 -0
- package/runtime/utf8.md +117 -0
- package/runtime/wasi.gr +4 -2
- package/runtime/wasi.md +147 -147
- package/set.gr +18 -11
- package/set.md +253 -247
- package/stack.gr +171 -2
- package/stack.md +371 -89
- package/string.gr +352 -557
- package/string.md +298 -255
- package/uint16.md +170 -170
- package/uint32.gr +25 -4
- package/uint32.md +249 -214
- package/uint64.gr +25 -5
- package/uint64.md +249 -214
- package/uint8.md +170 -170
- package/uri.gr +57 -53
- package/uri.md +88 -89
- package/wasi/file.gr +67 -59
- package/wasi/file.md +308 -308
- package/wasi/process.md +26 -26
- package/wasi/random.md +12 -12
- package/wasi/time.md +16 -16
- package/runtime/utils/printing.gr +0 -60
- 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,
|
|
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(
|
|
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 +
|
|
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
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
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
|
|
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)
|