@grain/stdlib 0.4.4 → 0.5.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 (97) hide show
  1. package/CHANGELOG.md +87 -0
  2. package/LICENSE +1 -1
  3. package/array.gr +92 -73
  4. package/array.md +18 -18
  5. package/bigint.gr +497 -0
  6. package/bigint.md +811 -0
  7. package/buffer.gr +56 -217
  8. package/buffer.md +24 -17
  9. package/bytes.gr +103 -205
  10. package/bytes.md +19 -0
  11. package/char.gr +152 -166
  12. package/char.md +200 -0
  13. package/exception.md +6 -0
  14. package/float32.gr +159 -82
  15. package/float32.md +315 -0
  16. package/float64.gr +163 -82
  17. package/float64.md +315 -0
  18. package/hash.gr +53 -49
  19. package/int32.gr +479 -230
  20. package/int32.md +937 -0
  21. package/int64.gr +479 -230
  22. package/int64.md +937 -0
  23. package/list.gr +530 -116
  24. package/list.md +1141 -0
  25. package/map.gr +302 -121
  26. package/map.md +525 -0
  27. package/number.gr +51 -57
  28. package/number.md +37 -3
  29. package/option.gr +25 -25
  30. package/option.md +1 -1
  31. package/package.json +3 -3
  32. package/pervasives.gr +504 -52
  33. package/pervasives.md +1116 -0
  34. package/queue.gr +8 -1
  35. package/queue.md +10 -0
  36. package/random.gr +196 -0
  37. package/random.md +179 -0
  38. package/range.gr +26 -26
  39. package/regex.gr +1833 -842
  40. package/regex.md +11 -11
  41. package/result.md +1 -1
  42. package/runtime/bigint.gr +2045 -0
  43. package/runtime/bigint.md +326 -0
  44. package/runtime/dataStructures.gr +99 -279
  45. package/runtime/dataStructures.md +391 -0
  46. package/runtime/debug.gr +0 -1
  47. package/runtime/debug.md +6 -0
  48. package/runtime/equal.gr +40 -37
  49. package/runtime/equal.md +6 -0
  50. package/runtime/exception.gr +28 -15
  51. package/runtime/exception.md +30 -0
  52. package/runtime/gc.gr +50 -20
  53. package/runtime/gc.md +36 -0
  54. package/runtime/malloc.gr +32 -22
  55. package/runtime/malloc.md +55 -0
  56. package/runtime/numberUtils.gr +297 -142
  57. package/runtime/numberUtils.md +54 -0
  58. package/runtime/numbers.gr +1204 -453
  59. package/runtime/numbers.md +300 -0
  60. package/runtime/string.gr +193 -228
  61. package/runtime/string.md +24 -0
  62. package/runtime/stringUtils.gr +62 -38
  63. package/runtime/stringUtils.md +6 -0
  64. package/runtime/unsafe/constants.gr +17 -0
  65. package/runtime/unsafe/constants.md +72 -0
  66. package/runtime/unsafe/conv.gr +10 -10
  67. package/runtime/unsafe/conv.md +71 -0
  68. package/runtime/unsafe/errors.md +204 -0
  69. package/runtime/unsafe/memory.gr +14 -3
  70. package/runtime/unsafe/memory.md +54 -0
  71. package/runtime/unsafe/printWasm.gr +4 -4
  72. package/runtime/unsafe/printWasm.md +24 -0
  73. package/runtime/unsafe/tags.gr +11 -10
  74. package/runtime/unsafe/tags.md +120 -0
  75. package/runtime/unsafe/wasmf32.gr +9 -2
  76. package/runtime/unsafe/wasmf32.md +168 -0
  77. package/runtime/unsafe/wasmf64.gr +9 -2
  78. package/runtime/unsafe/wasmf64.md +168 -0
  79. package/runtime/unsafe/wasmi32.gr +65 -47
  80. package/runtime/unsafe/wasmi32.md +282 -0
  81. package/runtime/unsafe/wasmi64.gr +78 -50
  82. package/runtime/unsafe/wasmi64.md +300 -0
  83. package/runtime/utils/printing.gr +62 -0
  84. package/runtime/utils/printing.md +18 -0
  85. package/runtime/wasi.gr +200 -46
  86. package/runtime/wasi.md +839 -0
  87. package/set.gr +125 -121
  88. package/set.md +24 -21
  89. package/stack.gr +29 -29
  90. package/stack.md +4 -6
  91. package/string.gr +434 -415
  92. package/string.md +3 -3
  93. package/sys/file.gr +477 -482
  94. package/sys/process.gr +33 -47
  95. package/sys/random.gr +48 -20
  96. package/sys/random.md +38 -0
  97. package/sys/time.gr +12 -28
package/sys/file.gr CHANGED
@@ -1,4 +1,3 @@
1
- /* grainc-flags --no-gc */
2
1
  /**
3
2
  * @module Sys/File: Utilities for accessing the filesystem & working with files.
4
3
  *
@@ -21,12 +20,18 @@ import WasmI32, {
21
20
  gtS as (>),
22
21
  geS as (>=),
23
22
  and as (&),
24
- or as (|)
23
+ or as (|),
25
24
  } from "runtime/unsafe/wasmi32"
26
25
  import WasmI64 from "runtime/unsafe/wasmi64"
27
26
  import Wasi from "runtime/wasi"
28
27
  import Memory from "runtime/unsafe/memory"
29
- import { tagSimpleNumber, allocateArray, allocateString, loadAdtVal, newInt64, allocateInt64 } from "runtime/dataStructures"
28
+ import {
29
+ tagSimpleNumber,
30
+ allocateArray,
31
+ allocateString,
32
+ newInt64,
33
+ allocateInt64,
34
+ } from "runtime/dataStructures"
30
35
 
31
36
  import List from "list"
32
37
 
@@ -38,7 +43,7 @@ import List from "list"
38
43
  * Represents a handle to an open file on the system.
39
44
  */
40
45
  export enum FileDescriptor {
41
- FileDescriptor(Number)
46
+ FileDescriptor(Number),
42
47
  }
43
48
 
44
49
  /**
@@ -46,22 +51,24 @@ export enum FileDescriptor {
46
51
  */
47
52
  export enum LookupFlag {
48
53
  // Follow symlinks
49
- SymlinkFollow
54
+ SymlinkFollow,
50
55
  }
51
56
 
52
57
  // TODO(#775): This has specific ordering requirements because of ambiguous type inference
58
+ @unsafe
53
59
  let rec combineLookupFlagsHelp = (acc, dirflags) => {
54
60
  match (dirflags) {
55
61
  [hd, ...tl] => {
56
62
  let flag = match (hd) {
57
- SymlinkFollow => Wasi._LOOKUP_FLAG_SYMLINK_FOLLOW
63
+ SymlinkFollow => Wasi._LOOKUP_FLAG_SYMLINK_FOLLOW,
58
64
  }
59
65
  combineLookupFlagsHelp(acc | flag, tl)
60
66
  },
61
- [] => acc
67
+ [] => acc,
62
68
  }
63
69
  }
64
- let combineLookupFlags = (dirflags) => {
70
+ @unsafe
71
+ let combineLookupFlags = dirflags => {
65
72
  combineLookupFlagsHelp(0n, dirflags)
66
73
  }
67
74
 
@@ -80,6 +87,7 @@ export enum OpenFlag {
80
87
  }
81
88
 
82
89
  // TODO(#775): This has specific ordering requirements because of ambiguous type inference
90
+ @unsafe
83
91
  let rec combineOpenFlagsHelp = (acc, dirflags) => {
84
92
  match (dirflags) {
85
93
  [hd, ...tl] => {
@@ -91,10 +99,11 @@ let rec combineOpenFlagsHelp = (acc, dirflags) => {
91
99
  }
92
100
  combineOpenFlagsHelp(acc | flag, tl)
93
101
  },
94
- [] => acc
102
+ [] => acc,
95
103
  }
96
104
  }
97
- let combineOpenFlags = (dirflags) => {
105
+ @unsafe
106
+ let combineOpenFlags = dirflags => {
98
107
  combineOpenFlagsHelp(0n, dirflags)
99
108
  }
100
109
 
@@ -177,37 +186,67 @@ export enum Rights {
177
186
 
178
187
  // Grain currently cannot close over unsafe wasm i64s, so these are here in
179
188
  // module so they get inlined
189
+ @unsafe
180
190
  let _RIGHT_FD_DATASYNC = 1N
191
+ @unsafe
181
192
  let _RIGHT_FD_READ = 2N
193
+ @unsafe
182
194
  let _RIGHT_FD_SEEK = 4N
195
+ @unsafe
183
196
  let _RIGHT_FD_FDSTAT_SET_FLAGS = 8N
197
+ @unsafe
184
198
  let _RIGHT_FD_SYNC = 16N
199
+ @unsafe
185
200
  let _RIGHT_FD_TELL = 32N
201
+ @unsafe
186
202
  let _RIGHT_FD_WRITE = 64N
203
+ @unsafe
187
204
  let _RIGHT_FD_ADVISE = 128N
205
+ @unsafe
188
206
  let _RIGHT_FD_ALLOCATE = 256N
207
+ @unsafe
189
208
  let _RIGHT_PATH_CREATE_DIRECTORY = 512N
209
+ @unsafe
190
210
  let _RIGHT_PATH_CREATE_FILE = 1024N
211
+ @unsafe
191
212
  let _RIGHT_PATH_LINK_SOURCE = 2048N
213
+ @unsafe
192
214
  let _RIGHT_PATH_LINK_TARGET = 4096N
215
+ @unsafe
193
216
  let _RIGHT_PATH_OPEN = 8192N
217
+ @unsafe
194
218
  let _RIGHT_FD_READDIR = 16384N
219
+ @unsafe
195
220
  let _RIGHT_PATH_READLINK = 32768N
221
+ @unsafe
196
222
  let _RIGHT_PATH_RENAME_SOURCE = 65536N
223
+ @unsafe
197
224
  let _RIGHT_PATH_RENAME_TARGET = 131072N
225
+ @unsafe
198
226
  let _RIGHT_PATH_FILESTAT_GET = 262144N
227
+ @unsafe
199
228
  let _RIGHT_PATH_FILESTAT_SET_SIZE = 524288N
229
+ @unsafe
200
230
  let _RIGHT_PATH_FILESTAT_SET_TIMES = 1048576N
231
+ @unsafe
201
232
  let _RIGHT_FD_FILESTAT_GET = 2097152N
233
+ @unsafe
202
234
  let _RIGHT_FD_FILESTAT_SET_SIZE = 4194304N
235
+ @unsafe
203
236
  let _RIGHT_FD_FILESTAT_SET_TIMES = 8388608N
237
+ @unsafe
204
238
  let _RIGHT_PATH_SYMLINK = 16777216N
239
+ @unsafe
205
240
  let _RIGHT_PATH_REMOVE_DIRECTORY = 33554432N
241
+ @unsafe
206
242
  let _RIGHT_PATH_UNLINK_FILE = 67108864N
243
+ @unsafe
207
244
  let _RIGHT_POLL_FD_READWRITE = 134217728N
245
+ @unsafe
208
246
  let _RIGHT_SOCK_SHUTDOWN = 268435456N
209
247
 
210
248
  // TODO(#775): This has specific ordering requirements because of ambiguous type inference
249
+ @unsafe
211
250
  let rec combineRightsHelp = (acc, dirflags) => {
212
251
  match (dirflags) {
213
252
  [] => acc,
@@ -242,13 +281,19 @@ let rec combineRightsHelp = (acc, dirflags) => {
242
281
  PathUnlinkFile => _RIGHT_PATH_UNLINK_FILE,
243
282
  PollFdReadwrite => _RIGHT_POLL_FD_READWRITE,
244
283
  SockShutdown => _RIGHT_SOCK_SHUTDOWN,
245
- _ => fail "Unknown file right"
284
+ _ => {
285
+ ignore(fail "Unknown file right")
286
+ // `fail` won't allow us to get here, but this is necessary
287
+ // to make the wasm types work out
288
+ 0N
289
+ },
246
290
  }
247
291
  combineRightsHelp(WasmI64.or(flag, acc), tl)
248
- }
292
+ },
249
293
  }
250
294
  }
251
- let combineRights = (dirflags) => {
295
+ @unsafe
296
+ let combineRights = dirflags => {
252
297
  combineRightsHelp(0N, dirflags)
253
298
  }
254
299
 
@@ -271,6 +316,7 @@ export enum FdFlag {
271
316
  }
272
317
 
273
318
  // TODO(#775): This has specific ordering requirements because of ambiguous type inference
319
+ @unsafe
274
320
  let rec combineFdFlagsHelp = (acc, dirflags) => {
275
321
  match (dirflags) {
276
322
  [hd, ...tl] => {
@@ -283,10 +329,11 @@ let rec combineFdFlagsHelp = (acc, dirflags) => {
283
329
  }
284
330
  combineFdFlagsHelp(acc | flag, tl)
285
331
  },
286
- [] => acc
332
+ [] => acc,
287
333
  }
288
334
  }
289
- let combineFdFlags = (dirflags) => {
335
+ @unsafe
336
+ let combineFdFlags = dirflags => {
290
337
  combineFdFlagsHelp(0n, dirflags)
291
338
  }
292
339
 
@@ -313,7 +360,8 @@ export enum Filetype {
313
360
  }
314
361
 
315
362
  // TODO(#775): This has specific ordering requirements because of ambiguous type inference
316
- let filetypeFromNumber = (filetype) => {
363
+ @unsafe
364
+ let filetypeFromNumber = filetype => {
317
365
  match (filetype) {
318
366
  0n => Unknown,
319
367
  1n => BlockDevice,
@@ -323,7 +371,7 @@ let filetypeFromNumber = (filetype) => {
323
371
  5n => SocketDatagram,
324
372
  6n => SocketStream,
325
373
  7n => SymbolicLink,
326
- _ => fail "Unknown filetype"
374
+ _ => fail "Unknown filetype",
327
375
  }
328
376
  }
329
377
 
@@ -346,7 +394,7 @@ export record Stats {
346
394
  filetype: Filetype,
347
395
  flags: List<FdFlag>,
348
396
  rights: List<Rights>,
349
- rightsInheriting: List<Rights>
397
+ rightsInheriting: List<Rights>,
350
398
  }
351
399
 
352
400
  /**
@@ -360,7 +408,7 @@ export record Filestats {
360
408
  size: Int64,
361
409
  accessed: Int64,
362
410
  modified: Int64,
363
- changed: Int64
411
+ changed: Int64,
364
412
  }
365
413
 
366
414
  /**
@@ -369,7 +417,7 @@ export record Filestats {
369
417
  export record DirectoryEntry {
370
418
  inode: Int64,
371
419
  filetype: Filetype,
372
- path: String
420
+ path: String,
373
421
  }
374
422
 
375
423
  /**
@@ -393,18 +441,6 @@ export let stderr = FileDescriptor(2)
393
441
  */
394
442
  export let pwdfd = FileDescriptor(3)
395
443
 
396
- let wasmSafeOk = (val) => {
397
- Memory.incRef(WasmI32.fromGrain(Ok))
398
- Memory.incRef(WasmI32.fromGrain(val))
399
- Ok(val)
400
- }
401
-
402
- let wasmSafeErr = (err) => {
403
- Memory.incRef(WasmI32.fromGrain(Err))
404
- Memory.incRef(WasmI32.fromGrain(err))
405
- Err(err)
406
- }
407
-
408
444
  /**
409
445
  * Open a file or directory.
410
446
  *
@@ -417,21 +453,21 @@ let wasmSafeErr = (err) => {
417
453
  * @param flags: Flags which affect read/write operations on this file descriptor
418
454
  * @returns `Ok(fd)` of the opened file or directory if successful or `Err(exception)` otherwise
419
455
  */
420
- export let rec pathOpen = (
421
- dirFd: FileDescriptor,
422
- dirFlags: List<LookupFlag>,
423
- path: String,
424
- openFlags: List<OpenFlag>,
425
- rights: List<Rights>,
426
- rightsInheriting: List<Rights>,
427
- flags: List<FdFlag>
428
- ) => {
456
+ @unsafe
457
+ export let pathOpen =
458
+ (
459
+ dirFd: FileDescriptor,
460
+ dirFlags: List<LookupFlag>,
461
+ path: String,
462
+ openFlags: List<OpenFlag>,
463
+ rights: List<Rights>,
464
+ rightsInheriting: List<Rights>,
465
+ flags: List<FdFlag>,
466
+ ) => {
429
467
  let dirFdArg = dirFd
430
468
  let pathArg = path
431
469
  let rightsInheritingArg = rightsInheriting
432
- let dirFd = match (dirFd) {
433
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
434
- }
470
+ let dirFd = match (dirFd) { FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n }
435
471
 
436
472
  let combinedDirFlags = combineLookupFlags(dirFlags)
437
473
 
@@ -459,25 +495,16 @@ export let rec pathOpen = (
459
495
  combinedFsFlags,
460
496
  newFd
461
497
  )
462
- let ret = if (err != Wasi._ESUCCESS) {
498
+ if (err != Wasi._ESUCCESS) {
463
499
  Memory.free(newFd)
464
- wasmSafeErr(Wasi.SystemError(tagSimpleNumber(err)))
500
+ Err(Wasi.SystemError(tagSimpleNumber(err)))
465
501
  } else {
466
502
  let fd = FileDescriptor(tagSimpleNumber(WasmI32.load(newFd, 0n)))
467
503
 
468
504
  Memory.free(newFd)
469
505
 
470
- wasmSafeOk(fd)
506
+ Ok(fd)
471
507
  }
472
- Memory.decRef(WasmI32.fromGrain(dirFdArg))
473
- Memory.decRef(WasmI32.fromGrain(dirFlags))
474
- Memory.decRef(WasmI32.fromGrain(pathArg))
475
- Memory.decRef(WasmI32.fromGrain(openFlags))
476
- Memory.decRef(WasmI32.fromGrain(rights))
477
- Memory.decRef(WasmI32.fromGrain(rightsInheritingArg))
478
- Memory.decRef(WasmI32.fromGrain(flags))
479
- Memory.decRef(WasmI32.fromGrain(pathOpen))
480
- ret
481
508
  }
482
509
 
483
510
  /**
@@ -487,27 +514,26 @@ export let rec pathOpen = (
487
514
  * @param size: The maximum number of bytes to read from the file descriptor
488
515
  * @returns `Ok((contents, numBytes))` of bytes read and the number of bytes read if successful or `Err(exception)` otherwise
489
516
  */
490
- export let rec fdRead = (fd: FileDescriptor, size: Number) => {
517
+ @unsafe
518
+ export let fdRead = (fd: FileDescriptor, size: Number) => {
491
519
  let fdArg = fd
492
- let fd = match (fd) {
493
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
494
- }
520
+ let fd = match (fd) { FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n }
495
521
 
496
522
  let n = WasmI32.fromGrain(size) >> 1n
497
523
 
498
524
  let iovs = Memory.malloc(3n * 4n)
499
525
  let strPtr = allocateString(n)
500
526
 
501
- WasmI32.store(iovs, strPtr + (2n * 4n), 0n)
527
+ WasmI32.store(iovs, strPtr + 2n * 4n, 0n)
502
528
  WasmI32.store(iovs, n, 4n)
503
529
 
504
- let mut nread = iovs + (3n * 4n)
530
+ let mut nread = iovs + 3n * 4n
505
531
 
506
532
  let err = Wasi.fd_read(fd, iovs, 1n, nread)
507
- let ret = if (err != Wasi._ESUCCESS) {
533
+ if (err != Wasi._ESUCCESS) {
508
534
  Memory.free(iovs)
509
535
  Memory.free(strPtr)
510
- wasmSafeErr(Wasi.SystemError(tagSimpleNumber(err)))
536
+ Err(Wasi.SystemError(tagSimpleNumber(err)))
511
537
  } else {
512
538
  nread = WasmI32.load(nread, 0n)
513
539
 
@@ -515,12 +541,8 @@ export let rec fdRead = (fd: FileDescriptor, size: Number) => {
515
541
 
516
542
  Memory.free(iovs)
517
543
 
518
- wasmSafeOk((WasmI32.toGrain(strPtr): String, tagSimpleNumber(nread)))
544
+ Ok((WasmI32.toGrain(strPtr): String, tagSimpleNumber(nread)))
519
545
  }
520
- Memory.decRef(WasmI32.fromGrain(fd))
521
- Memory.decRef(WasmI32.fromGrain(size))
522
- Memory.decRef(WasmI32.fromGrain(fdRead))
523
- ret
524
546
  }
525
547
 
526
548
  /**
@@ -531,12 +553,11 @@ export let rec fdRead = (fd: FileDescriptor, size: Number) => {
531
553
  * @param size: The maximum number of bytes to read from the file descriptor
532
554
  * @returns `Ok((contents, numBytes))` of bytes read and the number of bytes read if successful or `Err(exception)` otherwise
533
555
  */
534
- export let rec fdPread = (fd: FileDescriptor, offset: Int64, size: Number) => {
556
+ @unsafe
557
+ export let fdPread = (fd: FileDescriptor, offset: Int64, size: Number) => {
535
558
  let fdArg = fd
536
559
  let offsetArg = offset
537
- let fd = match (fd) {
538
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
539
- }
560
+ let fd = match (fd) { FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n }
540
561
 
541
562
  let offset = WasmI64.load(WasmI32.fromGrain(offset), 8n)
542
563
 
@@ -545,16 +566,16 @@ export let rec fdPread = (fd: FileDescriptor, offset: Int64, size: Number) => {
545
566
  let iovs = Memory.malloc(3n * 4n)
546
567
  let strPtr = allocateString(n)
547
568
 
548
- WasmI32.store(iovs, strPtr + (2n * 4n), 0n)
569
+ WasmI32.store(iovs, strPtr + 2n * 4n, 0n)
549
570
  WasmI32.store(iovs, n, 4n)
550
571
 
551
- let mut nread = iovs + (3n * 4n)
572
+ let mut nread = iovs + 3n * 4n
552
573
 
553
574
  let err = Wasi.fd_pread(fd, iovs, 1n, offset, nread)
554
- let ret = if (err != Wasi._ESUCCESS) {
575
+ if (err != Wasi._ESUCCESS) {
555
576
  Memory.free(iovs)
556
577
  Memory.free(strPtr)
557
- wasmSafeErr(Wasi.SystemError(tagSimpleNumber(err)))
578
+ Err(Wasi.SystemError(tagSimpleNumber(err)))
558
579
  } else {
559
580
  nread = WasmI32.load(nread, 0n)
560
581
 
@@ -562,13 +583,8 @@ export let rec fdPread = (fd: FileDescriptor, offset: Int64, size: Number) => {
562
583
 
563
584
  Memory.free(iovs)
564
585
 
565
- wasmSafeOk((WasmI32.toGrain(strPtr): String, tagSimpleNumber(nread)))
586
+ Ok((WasmI32.toGrain(strPtr): String, tagSimpleNumber(nread)))
566
587
  }
567
- Memory.decRef(WasmI32.fromGrain(fdArg))
568
- Memory.decRef(WasmI32.fromGrain(offsetArg))
569
- Memory.decRef(WasmI32.fromGrain(size))
570
- Memory.decRef(WasmI32.fromGrain(fdPread))
571
- ret
572
588
  }
573
589
 
574
590
  /**
@@ -578,11 +594,10 @@ export let rec fdPread = (fd: FileDescriptor, offset: Int64, size: Number) => {
578
594
  * @param data: The data to be written
579
595
  * @returns `Ok(numBytes)` of the number of bytes written if successful or `Err(Exception)` otherwise
580
596
  */
581
- export let rec fdWrite = (fd: FileDescriptor, data: String) => {
597
+ @unsafe
598
+ export let fdWrite = (fd: FileDescriptor, data: String) => {
582
599
  let fdArg = fd
583
- let fd = match (fd) {
584
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
585
- }
600
+ let fd = match (fd) { FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n }
586
601
 
587
602
  let iovs = Memory.malloc(3n * 4n)
588
603
  let strPtr = WasmI32.fromGrain(data)
@@ -590,23 +605,19 @@ export let rec fdWrite = (fd: FileDescriptor, data: String) => {
590
605
  WasmI32.store(iovs, strPtr + 8n, 0n)
591
606
  WasmI32.store(iovs, WasmI32.load(strPtr, 4n), 4n)
592
607
 
593
- let mut nwritten = iovs + (3n * 4n)
608
+ let mut nwritten = iovs + 3n * 4n
594
609
 
595
610
  let err = Wasi.fd_write(fd, iovs, 1n, nwritten)
596
- let ret = if (err != Wasi._ESUCCESS) {
611
+ if (err != Wasi._ESUCCESS) {
597
612
  Memory.free(iovs)
598
- wasmSafeErr(Wasi.SystemError(tagSimpleNumber(err)))
613
+ Err(Wasi.SystemError(tagSimpleNumber(err)))
599
614
  } else {
600
615
  nwritten = WasmI32.load(nwritten, 0n)
601
616
 
602
617
  Memory.free(iovs)
603
618
 
604
- wasmSafeOk(tagSimpleNumber(nwritten))
619
+ Ok(tagSimpleNumber(nwritten))
605
620
  }
606
- Memory.decRef(WasmI32.fromGrain(fdArg))
607
- Memory.decRef(WasmI32.fromGrain(data))
608
- Memory.decRef(WasmI32.fromGrain(fdWrite))
609
- ret
610
621
  }
611
622
 
612
623
  /**
@@ -617,39 +628,33 @@ export let rec fdWrite = (fd: FileDescriptor, data: String) => {
617
628
  * @param offset: The position within the file to begin writing
618
629
  * @returns `Ok(numBytes)` of the number of bytes written if successful or `Err(exception)` otherwise
619
630
  */
620
- export let rec fdPwrite = (fd: FileDescriptor, data: String, offset: Int64) => {
631
+ @unsafe
632
+ export let fdPwrite = (fd: FileDescriptor, data: String, offset: Int64) => {
621
633
  let fdArg = fd
622
634
  let offsetArg = offset
623
- let fd = match (fd) {
624
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
625
- }
635
+ let fd = match (fd) { FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n }
626
636
 
627
637
  let iovs = Memory.malloc(3n * 4n)
628
638
  let strPtr = WasmI32.fromGrain(data)
629
639
 
630
- WasmI32.store(iovs, strPtr + (2n * 4n), 0n)
640
+ WasmI32.store(iovs, strPtr + 2n * 4n, 0n)
631
641
  WasmI32.store(iovs, WasmI32.load(strPtr, 4n), 4n)
632
642
 
633
643
  let offset = WasmI64.load(WasmI32.fromGrain(offset), 8n)
634
644
 
635
- let mut nwritten = iovs + (3n * 4n)
645
+ let mut nwritten = iovs + 3n * 4n
636
646
 
637
647
  let err = Wasi.fd_pwrite(fd, iovs, 1n, offset, nwritten)
638
- let ret = if (err != Wasi._ESUCCESS) {
648
+ if (err != Wasi._ESUCCESS) {
639
649
  Memory.free(iovs)
640
- wasmSafeErr(Wasi.SystemError(tagSimpleNumber(err)))
650
+ Err(Wasi.SystemError(tagSimpleNumber(err)))
641
651
  } else {
642
652
  nwritten = WasmI32.load(nwritten, 0n)
643
653
 
644
654
  Memory.free(iovs)
645
655
 
646
- wasmSafeOk(tagSimpleNumber(nwritten))
656
+ Ok(tagSimpleNumber(nwritten))
647
657
  }
648
- Memory.decRef(WasmI32.fromGrain(fdArg))
649
- Memory.decRef(WasmI32.fromGrain(data))
650
- Memory.decRef(WasmI32.fromGrain(offsetArg))
651
- Memory.decRef(WasmI32.fromGrain(fdPwrite))
652
- ret
653
658
  }
654
659
 
655
660
  /**
@@ -660,29 +665,23 @@ export let rec fdPwrite = (fd: FileDescriptor, data: String, offset: Int64) => {
660
665
  * @param size: The number of bytes to allocate
661
666
  * @returns `Ok(void)` if successful or `Err(exception)` otherwise
662
667
  */
663
- export let rec fdAllocate = (fd: FileDescriptor, offset: Int64, size: Int64) => {
668
+ @unsafe
669
+ export let fdAllocate = (fd: FileDescriptor, offset: Int64, size: Int64) => {
664
670
  let fdArg = fd
665
671
  let offsetArg = offset
666
672
  let sizeArg = size
667
- let fd = match (fd) {
668
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
669
- }
673
+ let fd = match (fd) { FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n }
670
674
 
671
675
  let offset = WasmI64.load(WasmI32.fromGrain(offset), 8n)
672
676
 
673
677
  let size = WasmI64.load(WasmI32.fromGrain(size), 8n)
674
678
 
675
679
  let err = Wasi.fd_allocate(fd, offset, size)
676
- let ret = if (err != Wasi._ESUCCESS) {
677
- wasmSafeErr(Wasi.SystemError(tagSimpleNumber(err)))
680
+ if (err != Wasi._ESUCCESS) {
681
+ Err(Wasi.SystemError(tagSimpleNumber(err)))
678
682
  } else {
679
- wasmSafeOk(void)
683
+ Ok(void)
680
684
  }
681
- Memory.decRef(WasmI32.fromGrain(fdArg))
682
- Memory.decRef(WasmI32.fromGrain(offsetArg))
683
- Memory.decRef(WasmI32.fromGrain(sizeArg))
684
- Memory.decRef(WasmI32.fromGrain(fdAllocate))
685
- ret
686
685
  }
687
686
 
688
687
  /**
@@ -691,21 +690,17 @@ export let rec fdAllocate = (fd: FileDescriptor, offset: Int64, size: Int64) =>
691
690
  * @param fd: The file descriptor to close
692
691
  * @returns `Ok(void)` if successful or `Err(exception)` otherwise
693
692
  */
694
- export let rec fdClose = (fd: FileDescriptor) => {
693
+ @unsafe
694
+ export let fdClose = (fd: FileDescriptor) => {
695
695
  let fdArg = fd
696
- let fd = match (fd) {
697
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
698
- }
696
+ let fd = match (fd) { FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n }
699
697
 
700
698
  let err = Wasi.fd_close(fd)
701
- let ret = if (err != Wasi._ESUCCESS) {
702
- wasmSafeErr(Wasi.SystemError(tagSimpleNumber(err)))
699
+ if (err != Wasi._ESUCCESS) {
700
+ Err(Wasi.SystemError(tagSimpleNumber(err)))
703
701
  } else {
704
- wasmSafeOk(void)
702
+ Ok(void)
705
703
  }
706
- Memory.decRef(WasmI32.fromGrain(fdArg))
707
- Memory.decRef(WasmI32.fromGrain(fdClose))
708
- ret
709
704
  }
710
705
 
711
706
  /**
@@ -714,21 +709,17 @@ export let rec fdClose = (fd: FileDescriptor) => {
714
709
  * @param fd: The file descriptor to synchronize
715
710
  * @returns `Ok(void)` if successful or `Err(exception)` otherwise
716
711
  */
717
- export let rec fdDatasync = (fd: FileDescriptor) => {
712
+ @unsafe
713
+ export let fdDatasync = (fd: FileDescriptor) => {
718
714
  let fdArg = fd
719
- let fd = match (fd) {
720
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
721
- }
715
+ let fd = match (fd) { FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n }
722
716
 
723
717
  let err = Wasi.fd_datasync(fd)
724
- let ret = if (err != Wasi._ESUCCESS) {
725
- wasmSafeErr(Wasi.SystemError(tagSimpleNumber(err)))
718
+ if (err != Wasi._ESUCCESS) {
719
+ Err(Wasi.SystemError(tagSimpleNumber(err)))
726
720
  } else {
727
- wasmSafeOk(void)
721
+ Ok(void)
728
722
  }
729
- Memory.decRef(WasmI32.fromGrain(fdArg))
730
- Memory.decRef(WasmI32.fromGrain(fdDatasync))
731
- ret
732
723
  }
733
724
 
734
725
  /**
@@ -737,37 +728,51 @@ export let rec fdDatasync = (fd: FileDescriptor) => {
737
728
  * @param fd: The file descriptor to synchronize
738
729
  * @returns `Ok(void)` if successful or `Err(exception)` otherwise
739
730
  */
740
- export let rec fdSync = (fd: FileDescriptor) => {
731
+ @unsafe
732
+ export let fdSync = (fd: FileDescriptor) => {
741
733
  let fdArg = fd
742
- let fd = match (fd) {
743
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
744
- }
734
+ let fd = match (fd) { FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n }
745
735
 
746
736
  let err = Wasi.fd_sync(fd)
747
- let ret = if (err != Wasi._ESUCCESS) {
748
- wasmSafeErr(Wasi.SystemError(tagSimpleNumber(err)))
737
+ if (err != Wasi._ESUCCESS) {
738
+ Err(Wasi.SystemError(tagSimpleNumber(err)))
749
739
  } else {
750
- wasmSafeOk(void)
740
+ Ok(void)
751
741
  }
752
- Memory.decRef(WasmI32.fromGrain(fdArg))
753
- Memory.decRef(WasmI32.fromGrain(fdSync))
754
- ret
755
- }
756
-
757
-
758
- let wasmSafeCons = (a, b) => {
759
- // [TODO] Once grain-lang/grain#802 is fixed:
760
- // Memory.incRef(WasmI32.fromGrain([...]))
761
- Memory.incRef(WasmI32.fromGrain(cons))
762
- Memory.incRef(WasmI32.fromGrain(a))
763
- Memory.incRef(WasmI32.fromGrain(b))
764
- cons(a, b)
765
- // [TODO] Once grain-lang/grain#802 is fixed:
766
- // [a, ...b]
767
742
  }
768
743
 
769
- let orderedFdflags = wasmSafeCons(Append, wasmSafeCons(Dsync, wasmSafeCons(Nonblock, wasmSafeCons(Rsync, wasmSafeCons(Sync, [])))))
770
- let orderedRights = wasmSafeCons(FdDatasync, wasmSafeCons(FdRead, wasmSafeCons(FdSeek, wasmSafeCons(FdSetFlags, wasmSafeCons(FdSync, wasmSafeCons(FdTell, wasmSafeCons(FdWrite, wasmSafeCons(FdAdvise, wasmSafeCons(FdAllocate, wasmSafeCons(PathCreateDirectory, wasmSafeCons(PathCreateFile, wasmSafeCons(PathLinkSource, wasmSafeCons(PathLinkTarget, wasmSafeCons(PathOpen, wasmSafeCons(FdReaddir, wasmSafeCons(PathReadlink, wasmSafeCons(PathRenameSource, wasmSafeCons(PathRenameTarget, wasmSafeCons(PathFilestats, wasmSafeCons(PathSetSize, wasmSafeCons(PathSetTimes, wasmSafeCons(FdFilestats, wasmSafeCons(FdSetSize, wasmSafeCons(FdSetTimes, wasmSafeCons(PathSymlink, wasmSafeCons(PathRemoveDirectory, wasmSafeCons(PathUnlinkFile, wasmSafeCons(PollFdReadwrite, wasmSafeCons(SockShutdown, [])))))))))))))))))))))))))))))
744
+ let orderedFdflags = [Append, Dsync, Nonblock, Rsync, Sync]
745
+ let orderedRights = [
746
+ FdDatasync,
747
+ FdRead,
748
+ FdSeek,
749
+ FdSetFlags,
750
+ FdSync,
751
+ FdTell,
752
+ FdWrite,
753
+ FdAdvise,
754
+ FdAllocate,
755
+ PathCreateDirectory,
756
+ PathCreateFile,
757
+ PathLinkSource,
758
+ PathLinkTarget,
759
+ PathOpen,
760
+ FdReaddir,
761
+ PathReadlink,
762
+ PathRenameSource,
763
+ PathRenameTarget,
764
+ PathFilestats,
765
+ PathSetSize,
766
+ PathSetTimes,
767
+ FdFilestats,
768
+ FdSetSize,
769
+ FdSetTimes,
770
+ PathSymlink,
771
+ PathRemoveDirectory,
772
+ PathUnlinkFile,
773
+ PollFdReadwrite,
774
+ SockShutdown,
775
+ ]
771
776
 
772
777
  /**
773
778
  * Retrieve information about a file descriptor.
@@ -775,18 +780,17 @@ let orderedRights = wasmSafeCons(FdDatasync, wasmSafeCons(FdRead, wasmSafeCons(F
775
780
  * @param fd: The file descriptor of which to retrieve information
776
781
  * @returns `Ok(stats)` of the `Stats` associated with the file descriptor if successful or `Err(exception)` otherwise
777
782
  */
778
- export let rec fdStats = (fd: FileDescriptor) => {
783
+ @unsafe
784
+ export let fdStats = (fd: FileDescriptor) => {
779
785
  let fdArg = fd
780
- let fd = match (fd) {
781
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
782
- }
786
+ let fd = match (fd) { FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n }
783
787
 
784
788
  let structPtr = Memory.malloc(24n)
785
789
 
786
790
  let err = Wasi.fd_fdstat_get(fd, structPtr)
787
- let ret = if (err != Wasi._ESUCCESS) {
791
+ if (err != Wasi._ESUCCESS) {
788
792
  Memory.free(structPtr)
789
- wasmSafeErr(Wasi.SystemError(tagSimpleNumber(err)))
793
+ Err(Wasi.SystemError(tagSimpleNumber(err)))
790
794
  } else {
791
795
  let filetype = WasmI32.load8U(structPtr, 0n)
792
796
 
@@ -794,13 +798,9 @@ export let rec fdStats = (fd: FileDescriptor) => {
794
798
 
795
799
  let flagsToWasmVal = (flag, i) => {
796
800
  let fdflags = WasmI32.load16U(structPtr, 4n)
797
- WasmI32.gtU((fdflags & (1n << (WasmI32.fromGrain(i) >> 1n))), 0n)
801
+ WasmI32.gtU(fdflags & 1n << (WasmI32.fromGrain(i) >> 1n), 0n)
798
802
  }
799
- Memory.incRef(WasmI32.fromGrain(List.filteri))
800
- Memory.incRef(WasmI32.fromGrain(flagsToWasmVal))
801
- Memory.incRef(WasmI32.fromGrain(orderedFdflags))
802
803
  let fdflagsList = List.filteri(flagsToWasmVal, orderedFdflags)
803
- Memory.free(WasmI32.fromGrain(flagsToWasmVal))
804
804
 
805
805
  let (&) = WasmI64.and
806
806
  let (>) = WasmI64.gtU
@@ -808,31 +808,29 @@ export let rec fdStats = (fd: FileDescriptor) => {
808
808
 
809
809
  let flagsToWasmVal = (flag, i) => {
810
810
  let rights = WasmI64.load(structPtr, 8n)
811
- (rights & (1N << WasmI64.extendI32U(WasmI32.fromGrain(i) >> 1n))) > 0N
811
+ (rights & 1N << WasmI64.extendI32U(WasmI32.fromGrain(i) >> 1n)) > 0N
812
812
  }
813
- Memory.incRef(WasmI32.fromGrain(List.filteri))
814
- Memory.incRef(WasmI32.fromGrain(flagsToWasmVal))
815
- Memory.incRef(WasmI32.fromGrain(orderedRights))
816
813
  let rightsList = List.filteri(flagsToWasmVal, orderedRights)
817
- Memory.free(WasmI32.fromGrain(flagsToWasmVal))
818
814
 
819
815
  let flagsToWasmVal = (flag, i) => {
820
816
  let rightsInheriting = WasmI64.load(structPtr, 16n)
821
- (rightsInheriting & (1N << WasmI64.extendI32U(WasmI32.fromGrain(i) >> 1n))) > 0N
817
+ (rightsInheriting &
818
+ 1N << WasmI64.extendI32U(WasmI32.fromGrain(i) >> 1n)) >
819
+ 0N
822
820
  }
823
- Memory.incRef(WasmI32.fromGrain(List.filteri))
824
- Memory.incRef(WasmI32.fromGrain(flagsToWasmVal))
825
- Memory.incRef(WasmI32.fromGrain(orderedRights))
826
821
  let rightsInheritingList = List.filteri(flagsToWasmVal, orderedRights)
827
- Memory.free(WasmI32.fromGrain(flagsToWasmVal))
828
822
 
829
823
  Memory.free(structPtr)
830
824
 
831
- wasmSafeOk({ filetype, flags: fdflagsList, rights: rightsList, rightsInheriting: rightsInheritingList })
825
+ Ok(
826
+ {
827
+ filetype,
828
+ flags: fdflagsList,
829
+ rights: rightsList,
830
+ rightsInheriting: rightsInheritingList,
831
+ }
832
+ )
832
833
  }
833
- Memory.decRef(WasmI32.fromGrain(fdArg))
834
- Memory.decRef(WasmI32.fromGrain(fdStats))
835
- ret
836
834
  }
837
835
 
838
836
  /**
@@ -842,25 +840,20 @@ export let rec fdStats = (fd: FileDescriptor) => {
842
840
  * @param flags: The flags to apply to the file descriptor
843
841
  * @returns `Ok(void)` if successful or `Err(exception)` otherwise
844
842
  */
845
- export let rec fdSetFlags = (fd: FileDescriptor, flags: List<FdFlag>) => {
843
+ @unsafe
844
+ export let fdSetFlags = (fd: FileDescriptor, flags: List<FdFlag>) => {
846
845
  let fdArg = fd
847
846
  let flagsArg = flags
848
- let fd = match (fd) {
849
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
850
- }
847
+ let fd = match (fd) { FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n }
851
848
 
852
849
  let flags = combineFdFlags(flags)
853
850
 
854
851
  let err = Wasi.fd_fdstat_set_flags(fd, flags)
855
- let ret = if (err != Wasi._ESUCCESS) {
856
- wasmSafeErr(Wasi.SystemError(tagSimpleNumber(err)))
852
+ if (err != Wasi._ESUCCESS) {
853
+ Err(Wasi.SystemError(tagSimpleNumber(err)))
857
854
  } else {
858
- wasmSafeOk(void)
855
+ Ok(void)
859
856
  }
860
- Memory.decRef(WasmI32.fromGrain(fdArg))
861
- Memory.decRef(WasmI32.fromGrain(flagsArg))
862
- Memory.decRef(WasmI32.fromGrain(fdSetFlags))
863
- ret
864
857
  }
865
858
 
866
859
  /**
@@ -871,28 +864,27 @@ export let rec fdSetFlags = (fd: FileDescriptor, flags: List<FdFlag>) => {
871
864
  * @param rightsInheriting: Inheriting rights to apply to the file descriptor
872
865
  * @returns `Ok(void)` if successful or `Err(exception)` otherwise
873
866
  */
874
- export let rec fdSetRights = (fd: FileDescriptor, rights: List<Rights>, rightsInheriting: List<Rights>) => {
867
+ @unsafe
868
+ export let fdSetRights =
869
+ (
870
+ fd: FileDescriptor,
871
+ rights: List<Rights>,
872
+ rightsInheriting: List<Rights>,
873
+ ) => {
875
874
  let fdArg = fd
876
875
  let rightsArg = rights
877
876
  let rightsInheritingArg = rightsInheriting
878
- let fd = match (fd) {
879
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
880
- }
877
+ let fd = match (fd) { FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n }
881
878
 
882
879
  let rights = combineRights(rights)
883
880
  let rightsInheriting = combineRights(rightsInheriting)
884
881
 
885
882
  let err = Wasi.fd_fdstat_set_rights(fd, rights, rightsInheriting)
886
- let ret = if (err != Wasi._ESUCCESS) {
887
- wasmSafeErr(Wasi.SystemError(tagSimpleNumber(err)))
883
+ if (err != Wasi._ESUCCESS) {
884
+ Err(Wasi.SystemError(tagSimpleNumber(err)))
888
885
  } else {
889
- wasmSafeOk(void)
886
+ Ok(void)
890
887
  }
891
- Memory.decRef(WasmI32.fromGrain(fdArg))
892
- Memory.decRef(WasmI32.fromGrain(rightsArg))
893
- Memory.decRef(WasmI32.fromGrain(rightsInheritingArg))
894
- Memory.decRef(WasmI32.fromGrain(fdSetRights))
895
- ret
896
888
  }
897
889
 
898
890
  /**
@@ -901,35 +893,39 @@ export let rec fdSetRights = (fd: FileDescriptor, rights: List<Rights>, rightsIn
901
893
  * @param fd: The file descriptor of the file to retrieve information
902
894
  * @returns `Ok(info)` of the `Filestats` associated with the file descriptor if successful or `Err(exception)` otherwise
903
895
  */
904
- export let rec fdFilestats = (fd: FileDescriptor) => {
896
+ @unsafe
897
+ export let fdFilestats = (fd: FileDescriptor) => {
905
898
  let fdArg = fd
906
- let fd = match (fd) {
907
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
908
- }
899
+ let fd = match (fd) { FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n }
909
900
 
910
901
  let filestats = Memory.malloc(64n)
911
902
 
912
903
  let err = Wasi.fd_filestat_get(fd, filestats)
913
- let ret = if (err != Wasi._ESUCCESS) {
904
+ if (err != Wasi._ESUCCESS) {
914
905
  Memory.free(filestats)
915
- wasmSafeErr(Wasi.SystemError(tagSimpleNumber(err)))
906
+ Err(Wasi.SystemError(tagSimpleNumber(err)))
916
907
  } else {
917
908
  let device = WasmI32.toGrain(newInt64(WasmI64.load(filestats, 0n))): Int64
918
909
  let inode = WasmI32.toGrain(newInt64(WasmI64.load(filestats, 8n))): Int64
919
910
  let filetype = filetypeFromNumber(WasmI32.load8U(filestats, 16n))
920
- let linkcount = WasmI32.toGrain(newInt64(WasmI64.load(filestats, 24n))): Int64
911
+ let linkcount = WasmI32.toGrain(
912
+ newInt64(WasmI64.load(filestats, 24n))
913
+ ): Int64
921
914
  let size = WasmI32.toGrain(newInt64(WasmI64.load(filestats, 32n))): Int64
922
- let accessed = WasmI32.toGrain(newInt64(WasmI64.load(filestats, 40n))): Int64
923
- let modified = WasmI32.toGrain(newInt64(WasmI64.load(filestats, 48n))): Int64
915
+ let accessed = WasmI32.toGrain(
916
+ newInt64(WasmI64.load(filestats, 40n))
917
+ ): Int64
918
+ let modified = WasmI32.toGrain(
919
+ newInt64(WasmI64.load(filestats, 48n))
920
+ ): Int64
924
921
  let changed = WasmI32.toGrain(newInt64(WasmI64.load(filestats, 56n))): Int64
925
922
 
926
923
  Memory.free(filestats)
927
924
 
928
- wasmSafeOk({ device, inode, filetype, linkcount, size, accessed, modified, changed })
925
+ Ok(
926
+ { device, inode, filetype, linkcount, size, accessed, modified, changed }
927
+ )
929
928
  }
930
- Memory.decRef(WasmI32.fromGrain(fdArg))
931
- Memory.decRef(WasmI32.fromGrain(fdFilestats))
932
- ret
933
929
  }
934
930
 
935
931
  /**
@@ -939,25 +935,20 @@ export let rec fdFilestats = (fd: FileDescriptor) => {
939
935
  * @param size: The number of bytes to retain in the file
940
936
  * @returns `Ok(void)` if successful or `Err(exception)` otherwise
941
937
  */
942
- export let rec fdSetSize = (fd: FileDescriptor, size: Int64) => {
938
+ @unsafe
939
+ export let fdSetSize = (fd: FileDescriptor, size: Int64) => {
943
940
  let fdArg = fd
944
941
  let sizeArg = size
945
- let fd = match (fd) {
946
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
947
- }
942
+ let fd = match (fd) { FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n }
948
943
 
949
944
  let size = WasmI64.load(WasmI32.fromGrain(size), 8n)
950
945
 
951
946
  let err = Wasi.fd_filestat_set_size(fd, size)
952
- let ret = if (err != Wasi._ESUCCESS) {
953
- wasmSafeErr(Wasi.SystemError(tagSimpleNumber(err)))
947
+ if (err != Wasi._ESUCCESS) {
948
+ Err(Wasi.SystemError(tagSimpleNumber(err)))
954
949
  } else {
955
- wasmSafeOk(void)
950
+ Ok(void)
956
951
  }
957
- Memory.decRef(WasmI32.fromGrain(fdArg))
958
- Memory.decRef(WasmI32.fromGrain(sizeArg))
959
- Memory.decRef(WasmI32.fromGrain(fdSetSize))
960
- ret
961
952
  }
962
953
 
963
954
  /**
@@ -967,24 +958,19 @@ export let rec fdSetSize = (fd: FileDescriptor, size: Int64) => {
967
958
  * @param timestamp: The time to set
968
959
  * @returns `Ok(void)` if successful or `Err(exception)` otherwise
969
960
  */
970
- export let rec fdSetAccessTime = (fd: FileDescriptor, timestamp: Int64) => {
961
+ @unsafe
962
+ export let fdSetAccessTime = (fd: FileDescriptor, timestamp: Int64) => {
971
963
  let fdArg = fd
972
- let fd = match (fd) {
973
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
974
- }
964
+ let fd = match (fd) { FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n }
975
965
 
976
966
  let time = WasmI64.load(WasmI32.fromGrain(timestamp), 8n)
977
967
 
978
968
  let err = Wasi.fd_filestat_set_times(fd, time, 0N, Wasi._TIME_SET_ATIM)
979
- let ret = if (err != Wasi._ESUCCESS) {
980
- wasmSafeErr(Wasi.SystemError(tagSimpleNumber(err)))
969
+ if (err != Wasi._ESUCCESS) {
970
+ Err(Wasi.SystemError(tagSimpleNumber(err)))
981
971
  } else {
982
- wasmSafeOk(void)
972
+ Ok(void)
983
973
  }
984
- Memory.decRef(WasmI32.fromGrain(fdArg))
985
- Memory.decRef(WasmI32.fromGrain(timestamp))
986
- Memory.decRef(WasmI32.fromGrain(fdSetAccessTime))
987
- ret
988
974
  }
989
975
 
990
976
  /**
@@ -993,21 +979,17 @@ export let rec fdSetAccessTime = (fd: FileDescriptor, timestamp: Int64) => {
993
979
  * @param fd: The file descriptor of the file to update
994
980
  * @returns `Ok(void)` if successful or `Err(exception)` otherwise
995
981
  */
996
- export let rec fdSetAccessTimeNow = (fd: FileDescriptor) => {
982
+ @unsafe
983
+ export let fdSetAccessTimeNow = (fd: FileDescriptor) => {
997
984
  let fdArg = fd
998
- let fd = match (fd) {
999
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
1000
- }
985
+ let fd = match (fd) { FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n }
1001
986
 
1002
987
  let err = Wasi.fd_filestat_set_times(fd, 0N, 0N, Wasi._TIME_SET_ATIM_NOW)
1003
- let ret = if (err != Wasi._ESUCCESS) {
1004
- wasmSafeErr(Wasi.SystemError(tagSimpleNumber(err)))
988
+ if (err != Wasi._ESUCCESS) {
989
+ Err(Wasi.SystemError(tagSimpleNumber(err)))
1005
990
  } else {
1006
- wasmSafeOk(void)
991
+ Ok(void)
1007
992
  }
1008
- Memory.decRef(WasmI32.fromGrain(fdArg))
1009
- Memory.decRef(WasmI32.fromGrain(fdSetAccessTimeNow))
1010
- ret
1011
993
  }
1012
994
 
1013
995
  /**
@@ -1017,24 +999,19 @@ export let rec fdSetAccessTimeNow = (fd: FileDescriptor) => {
1017
999
  * @param timestamp: The time to set
1018
1000
  * @returns `Ok(void)` if successful or `Err(exception)` otherwise
1019
1001
  */
1020
- export let rec fdSetModifiedTime = (fd: FileDescriptor, timestamp: Int64) => {
1002
+ @unsafe
1003
+ export let fdSetModifiedTime = (fd: FileDescriptor, timestamp: Int64) => {
1021
1004
  let fdArg = fd
1022
- let fd = match (fd) {
1023
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
1024
- }
1005
+ let fd = match (fd) { FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n }
1025
1006
 
1026
1007
  let time = WasmI64.load(WasmI32.fromGrain(timestamp), 8n)
1027
1008
 
1028
1009
  let err = Wasi.fd_filestat_set_times(fd, 0N, time, Wasi._TIME_SET_MTIM)
1029
- let ret = if (err != Wasi._ESUCCESS) {
1030
- wasmSafeErr(Wasi.SystemError(tagSimpleNumber(err)))
1010
+ if (err != Wasi._ESUCCESS) {
1011
+ Err(Wasi.SystemError(tagSimpleNumber(err)))
1031
1012
  } else {
1032
- wasmSafeOk(void)
1013
+ Ok(void)
1033
1014
  }
1034
- Memory.decRef(WasmI32.fromGrain(fdArg))
1035
- Memory.decRef(WasmI32.fromGrain(timestamp))
1036
- Memory.decRef(WasmI32.fromGrain(fdSetModifiedTime))
1037
- ret
1038
1015
  }
1039
1016
 
1040
1017
  /**
@@ -1043,21 +1020,17 @@ export let rec fdSetModifiedTime = (fd: FileDescriptor, timestamp: Int64) => {
1043
1020
  * @param fd: The file descriptor of the file to update
1044
1021
  * @returns `Ok(void)` if successful or `Err(exception)` otherwise
1045
1022
  */
1046
- export let rec fdSetModifiedTimeNow = (fd: FileDescriptor) => {
1023
+ @unsafe
1024
+ export let fdSetModifiedTimeNow = (fd: FileDescriptor) => {
1047
1025
  let fdArg = fd
1048
- let fd = match (fd) {
1049
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
1050
- }
1026
+ let fd = match (fd) { FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n }
1051
1027
 
1052
1028
  let err = Wasi.fd_filestat_set_times(fd, 0N, 0N, Wasi._TIME_SET_MTIM_NOW)
1053
- let ret = if (err != Wasi._ESUCCESS) {
1054
- wasmSafeErr(Wasi.SystemError(tagSimpleNumber(err)))
1029
+ if (err != Wasi._ESUCCESS) {
1030
+ Err(Wasi.SystemError(tagSimpleNumber(err)))
1055
1031
  } else {
1056
- wasmSafeOk(void)
1032
+ Ok(void)
1057
1033
  }
1058
- Memory.decRef(WasmI32.fromGrain(fdArg))
1059
- Memory.decRef(WasmI32.fromGrain(fdSetModifiedTime))
1060
- ret
1061
1034
  }
1062
1035
 
1063
1036
  /**
@@ -1066,11 +1039,10 @@ export let rec fdSetModifiedTimeNow = (fd: FileDescriptor) => {
1066
1039
  * @param fd: The directory to read
1067
1040
  * @returns `Ok(dirEntries)` of an array of `DirectoryEntry` for each entry in the directory if successful or `Err(exception)` otherwise
1068
1041
  */
1069
- export let rec fdReaddir = (fd: FileDescriptor) => {
1042
+ @unsafe
1043
+ export let fdReaddir = (fd: FileDescriptor) => {
1070
1044
  let fdArg = fd
1071
- let fd = match (fd) {
1072
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
1073
- }
1045
+ let fd = match (fd) { FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n }
1074
1046
 
1075
1047
  let structWidth = 24n
1076
1048
 
@@ -1081,17 +1053,17 @@ export let rec fdReaddir = (fd: FileDescriptor) => {
1081
1053
  let mut bufLen = structWidth
1082
1054
 
1083
1055
  let err = Wasi.fd_readdir(fd, buf, bufLen, cookie, bufUsed)
1084
- let ret = if (err != Wasi._ESUCCESS) {
1056
+ if (err != Wasi._ESUCCESS) {
1085
1057
  Memory.free(buf)
1086
1058
  Memory.free(bufUsed)
1087
- wasmSafeErr(Wasi.SystemError(tagSimpleNumber(err)))
1059
+ Err(Wasi.SystemError(tagSimpleNumber(err)))
1088
1060
  } else {
1089
1061
  let used = WasmI32.load(bufUsed, 0n)
1090
1062
 
1091
1063
  if (used <= 0n) {
1092
1064
  Memory.free(buf)
1093
1065
  Memory.free(bufUsed)
1094
- wasmSafeOk(WasmI32.toGrain(allocateArray(0n)): Array<DirectoryEntry>)
1066
+ Ok(WasmI32.toGrain(allocateArray(0n)): Array<DirectoryEntry>)
1095
1067
  } else {
1096
1068
  bufLen = WasmI32.load(buf, 16n) + structWidth * 2n
1097
1069
 
@@ -1167,14 +1139,11 @@ export let rec fdReaddir = (fd: FileDescriptor) => {
1167
1139
  bufs = next
1168
1140
  }
1169
1141
 
1170
- wasmSafeOk(WasmI32.toGrain(arr): Array<DirectoryEntry>)
1171
- }
1142
+ Ok(WasmI32.toGrain(arr): Array<DirectoryEntry>)
1143
+ },
1172
1144
  }
1173
1145
  }
1174
1146
  }
1175
- Memory.decRef(WasmI32.fromGrain(fdArg))
1176
- Memory.decRef(WasmI32.fromGrain(fdReaddir))
1177
- ret
1178
1147
  }
1179
1148
 
1180
1149
  /**
@@ -1184,27 +1153,22 @@ export let rec fdReaddir = (fd: FileDescriptor) => {
1184
1153
  * @param toFd: The file descriptor to overwrite
1185
1154
  * @returns `Ok(void)` if successful or `Err(exception)` otherwise
1186
1155
  */
1187
- export let rec fdRenumber = (fromFd: FileDescriptor, toFd: FileDescriptor) => {
1156
+ @unsafe
1157
+ export let fdRenumber = (fromFd: FileDescriptor, toFd: FileDescriptor) => {
1188
1158
  let fromFdArg = fromFd
1189
1159
  let toFdArg = toFd
1190
1160
  let fromFd = match (fromFd) {
1191
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
1161
+ FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n,
1192
1162
  }
1193
1163
 
1194
- let toFd = match (toFd) {
1195
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
1196
- }
1164
+ let toFd = match (toFd) { FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n }
1197
1165
 
1198
1166
  let err = Wasi.fd_renumber(fromFd, toFd)
1199
- let ret = if (err != Wasi._ESUCCESS) {
1200
- wasmSafeErr(Wasi.SystemError(tagSimpleNumber(err)))
1167
+ if (err != Wasi._ESUCCESS) {
1168
+ Err(Wasi.SystemError(tagSimpleNumber(err)))
1201
1169
  } else {
1202
- wasmSafeOk(void)
1170
+ Ok(void)
1203
1171
  }
1204
- Memory.decRef(WasmI32.fromGrain(fromFdArg))
1205
- Memory.decRef(WasmI32.fromGrain(toFdArg))
1206
- Memory.decRef(WasmI32.fromGrain(fdRenumber))
1207
- ret
1208
1172
  }
1209
1173
 
1210
1174
  /**
@@ -1215,37 +1179,31 @@ export let rec fdRenumber = (fromFd: FileDescriptor, toFd: FileDescriptor) => {
1215
1179
  * @param whence: The location from which the offset is relative
1216
1180
  * @returns `Ok(offset)` of the new offset of the file descriptor, relative to the start of the file, if successful or `Err(exception)` otherwise
1217
1181
  */
1218
- export let rec fdSeek = (fd: FileDescriptor, offset: Int64, whence: Whence) => {
1182
+ @unsafe
1183
+ export let fdSeek = (fd: FileDescriptor, offset: Int64, whence: Whence) => {
1219
1184
  let fdArg = fd
1220
1185
  let offsetArg = offset
1221
1186
  let whenceArg = whence
1222
- let fd = match (fd) {
1223
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
1224
- }
1187
+ let fd = match (fd) { FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n }
1225
1188
 
1226
1189
  let offset = WasmI64.load(WasmI32.fromGrain(offset), 8n)
1227
1190
 
1228
1191
  let whence = match (whence) {
1229
1192
  Set => Wasi._WHENCE_SET,
1230
1193
  Current => Wasi._WHENCE_CUR,
1231
- End => Wasi._WHENCE_END
1194
+ End => Wasi._WHENCE_END,
1232
1195
  }
1233
1196
 
1234
1197
  let newoffset = allocateInt64()
1235
1198
  let newoffsetPtr = newoffset + 8n
1236
1199
 
1237
1200
  let err = Wasi.fd_seek(fd, offset, whence, newoffsetPtr)
1238
- let ret = if (err != Wasi._ESUCCESS) {
1201
+ if (err != Wasi._ESUCCESS) {
1239
1202
  Memory.free(newoffset)
1240
- wasmSafeErr(Wasi.SystemError(tagSimpleNumber(err)))
1203
+ Err(Wasi.SystemError(tagSimpleNumber(err)))
1241
1204
  } else {
1242
- wasmSafeOk(WasmI32.toGrain(newoffset): Int64)
1205
+ Ok(WasmI32.toGrain(newoffset): Int64)
1243
1206
  }
1244
- Memory.decRef(WasmI32.fromGrain(fdArg))
1245
- Memory.decRef(WasmI32.fromGrain(offsetArg))
1246
- Memory.decRef(WasmI32.fromGrain(whenceArg))
1247
- Memory.decRef(WasmI32.fromGrain(fdSeek))
1248
- ret
1249
1207
  }
1250
1208
 
1251
1209
  /**
@@ -1254,25 +1212,21 @@ export let rec fdSeek = (fd: FileDescriptor, offset: Int64, whence: Whence) => {
1254
1212
  * @param fd: The file descriptor to read the offset
1255
1213
  * @returns `Ok(offset)` of the offset of the file descriptor, relative to the start of the file, if successful or `Err(exception)` otherwise
1256
1214
  */
1257
- export let rec fdTell = (fd: FileDescriptor) => {
1215
+ @unsafe
1216
+ export let fdTell = (fd: FileDescriptor) => {
1258
1217
  let fdArg = fd
1259
- let fd = match (fd) {
1260
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
1261
- }
1218
+ let fd = match (fd) { FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n }
1262
1219
 
1263
1220
  let offset = allocateInt64()
1264
1221
  let offsetPtr = offset + 8n
1265
1222
 
1266
1223
  let err = Wasi.fd_tell(fd, offsetPtr)
1267
- let ret = if (err != Wasi._ESUCCESS) {
1224
+ if (err != Wasi._ESUCCESS) {
1268
1225
  Memory.free(offset)
1269
- wasmSafeErr(Wasi.SystemError(tagSimpleNumber(err)))
1226
+ Err(Wasi.SystemError(tagSimpleNumber(err)))
1270
1227
  } else {
1271
- wasmSafeOk(WasmI32.toGrain(offset): Int64)
1228
+ Ok(WasmI32.toGrain(offset): Int64)
1272
1229
  }
1273
- Memory.decRef(WasmI32.fromGrain(fdArg))
1274
- Memory.decRef(WasmI32.fromGrain(fdTell))
1275
- ret
1276
1230
  }
1277
1231
 
1278
1232
  /**
@@ -1282,26 +1236,21 @@ export let rec fdTell = (fd: FileDescriptor) => {
1282
1236
  * @param path: The path to the new directory
1283
1237
  * @returns `Ok(void)` if successful or `Err(exception)` otherwise
1284
1238
  */
1285
- export let rec pathCreateDirectory = (fd: FileDescriptor, path: String) => {
1239
+ @unsafe
1240
+ export let pathCreateDirectory = (fd: FileDescriptor, path: String) => {
1286
1241
  let fdArg = fd
1287
- let fd = match (fd) {
1288
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
1289
- }
1242
+ let fd = match (fd) { FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n }
1290
1243
 
1291
1244
  let stringPtr = WasmI32.fromGrain(path)
1292
1245
 
1293
1246
  let size = WasmI32.load(stringPtr, 4n)
1294
1247
 
1295
1248
  let err = Wasi.path_create_directory(fd, stringPtr + 8n, size)
1296
- let ret = if (err != Wasi._ESUCCESS) {
1297
- wasmSafeErr(Wasi.SystemError(tagSimpleNumber(err)))
1249
+ if (err != Wasi._ESUCCESS) {
1250
+ Err(Wasi.SystemError(tagSimpleNumber(err)))
1298
1251
  } else {
1299
- wasmSafeOk(void)
1252
+ Ok(void)
1300
1253
  }
1301
- Memory.decRef(WasmI32.fromGrain(fdArg))
1302
- Memory.decRef(WasmI32.fromGrain(path))
1303
- Memory.decRef(WasmI32.fromGrain(pathCreateDirectory))
1304
- ret
1305
1254
  }
1306
1255
 
1307
1256
  /**
@@ -1312,11 +1261,15 @@ export let rec pathCreateDirectory = (fd: FileDescriptor, path: String) => {
1312
1261
  * @param path: The path to retrieve information about
1313
1262
  * @returns `Ok(info)` of the `Filestats` associated with the file descriptor if successful or `Err(exception)` otherwise
1314
1263
  */
1315
- export let rec pathFilestats = (fd: FileDescriptor, dirFlags: List<LookupFlag>, path: String) => {
1264
+ @unsafe
1265
+ export let pathFilestats =
1266
+ (
1267
+ fd: FileDescriptor,
1268
+ dirFlags: List<LookupFlag>,
1269
+ path: String,
1270
+ ) => {
1316
1271
  let fdArg = fd
1317
- let fd = match (fd) {
1318
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
1319
- }
1272
+ let fd = match (fd) { FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n }
1320
1273
 
1321
1274
  let combinedDirFlags = combineLookupFlags(dirFlags)
1322
1275
 
@@ -1326,30 +1279,38 @@ export let rec pathFilestats = (fd: FileDescriptor, dirFlags: List<LookupFlag>,
1326
1279
 
1327
1280
  let filestats = Memory.malloc(64n)
1328
1281
 
1329
- let err = Wasi.path_filestat_get(fd, combinedDirFlags, pathPtr, pathSize, filestats)
1330
- let ret = if (err != Wasi._ESUCCESS) {
1282
+ let err = Wasi.path_filestat_get(
1283
+ fd,
1284
+ combinedDirFlags,
1285
+ pathPtr,
1286
+ pathSize,
1287
+ filestats
1288
+ )
1289
+ if (err != Wasi._ESUCCESS) {
1331
1290
  Memory.free(filestats)
1332
- wasmSafeErr(Wasi.SystemError(tagSimpleNumber(err)))
1291
+ Err(Wasi.SystemError(tagSimpleNumber(err)))
1333
1292
  } else {
1334
1293
  let device = WasmI32.toGrain(newInt64(WasmI64.load(filestats, 0n))): Int64
1335
1294
  let inode = WasmI32.toGrain(newInt64(WasmI64.load(filestats, 8n))): Int64
1336
1295
  let filetype = filetypeFromNumber(WasmI32.load8U(filestats, 16n))
1337
- let linkcount = WasmI32.toGrain(newInt64(WasmI64.load(filestats, 24n))): Int64
1296
+ let linkcount = WasmI32.toGrain(
1297
+ newInt64(WasmI64.load(filestats, 24n))
1298
+ ): Int64
1338
1299
  let size = WasmI32.toGrain(newInt64(WasmI64.load(filestats, 32n))): Int64
1339
- let accessed = WasmI32.toGrain(newInt64(WasmI64.load(filestats, 40n))): Int64
1340
- let modified = WasmI32.toGrain(newInt64(WasmI64.load(filestats, 48n))): Int64
1300
+ let accessed = WasmI32.toGrain(
1301
+ newInt64(WasmI64.load(filestats, 40n))
1302
+ ): Int64
1303
+ let modified = WasmI32.toGrain(
1304
+ newInt64(WasmI64.load(filestats, 48n))
1305
+ ): Int64
1341
1306
  let changed = WasmI32.toGrain(newInt64(WasmI64.load(filestats, 56n))): Int64
1342
1307
 
1343
-
1344
1308
  Memory.free(filestats)
1345
1309
 
1346
- wasmSafeOk({ device, inode, filetype, linkcount, size, accessed, modified, changed })
1310
+ Ok(
1311
+ { device, inode, filetype, linkcount, size, accessed, modified, changed }
1312
+ )
1347
1313
  }
1348
- Memory.decRef(WasmI32.fromGrain(fdArg))
1349
- Memory.decRef(WasmI32.fromGrain(dirFlags))
1350
- Memory.decRef(WasmI32.fromGrain(path))
1351
- Memory.decRef(WasmI32.fromGrain(pathFilestats))
1352
- ret
1353
1314
  }
1354
1315
 
1355
1316
  /**
@@ -1361,11 +1322,16 @@ export let rec pathFilestats = (fd: FileDescriptor, dirFlags: List<LookupFlag>,
1361
1322
  * @param timestamp: The time to set
1362
1323
  * @returns `Ok(void)` if successful or `Err(exception)` otherwise
1363
1324
  */
1364
- export let rec pathSetAccessTime = (fd: FileDescriptor, dirFlags: List<LookupFlag>, path: String, timestamp: Int64) => {
1325
+ @unsafe
1326
+ export let pathSetAccessTime =
1327
+ (
1328
+ fd: FileDescriptor,
1329
+ dirFlags: List<LookupFlag>,
1330
+ path: String,
1331
+ timestamp: Int64,
1332
+ ) => {
1365
1333
  let fdArg = fd
1366
- let fd = match (fd) {
1367
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
1368
- }
1334
+ let fd = match (fd) { FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n }
1369
1335
 
1370
1336
  let combinedDirFlags = combineLookupFlags(dirFlags)
1371
1337
 
@@ -1375,18 +1341,20 @@ export let rec pathSetAccessTime = (fd: FileDescriptor, dirFlags: List<LookupFla
1375
1341
 
1376
1342
  let time = WasmI64.load(WasmI32.fromGrain(timestamp), 8n)
1377
1343
 
1378
- let err = Wasi.path_filestat_set_times(fd, combinedDirFlags, pathPtr, pathSize, time, 0N, Wasi._TIME_SET_ATIM)
1379
- let ret = if (err != Wasi._ESUCCESS) {
1380
- wasmSafeErr(Wasi.SystemError(tagSimpleNumber(err)))
1344
+ let err = Wasi.path_filestat_set_times(
1345
+ fd,
1346
+ combinedDirFlags,
1347
+ pathPtr,
1348
+ pathSize,
1349
+ time,
1350
+ 0N,
1351
+ Wasi._TIME_SET_ATIM
1352
+ )
1353
+ if (err != Wasi._ESUCCESS) {
1354
+ Err(Wasi.SystemError(tagSimpleNumber(err)))
1381
1355
  } else {
1382
- wasmSafeOk(void)
1356
+ Ok(void)
1383
1357
  }
1384
- Memory.decRef(WasmI32.fromGrain(fdArg))
1385
- Memory.decRef(WasmI32.fromGrain(dirFlags))
1386
- Memory.decRef(WasmI32.fromGrain(path))
1387
- Memory.decRef(WasmI32.fromGrain(timestamp))
1388
- Memory.decRef(WasmI32.fromGrain(pathSetAccessTime))
1389
- ret
1390
1358
  }
1391
1359
 
1392
1360
  /**
@@ -1397,11 +1365,15 @@ export let rec pathSetAccessTime = (fd: FileDescriptor, dirFlags: List<LookupFla
1397
1365
  * @param path: The path to set the time
1398
1366
  * @returns `Ok(void)` if successful or `Err(exception)` otherwise
1399
1367
  */
1400
- export let pathSetAccessTimeNow = (fd: FileDescriptor, dirFlags: List<LookupFlag>, path: String) => {
1368
+ @unsafe
1369
+ export let pathSetAccessTimeNow =
1370
+ (
1371
+ fd: FileDescriptor,
1372
+ dirFlags: List<LookupFlag>,
1373
+ path: String,
1374
+ ) => {
1401
1375
  let fdArg = fd
1402
- let fd = match (fd) {
1403
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
1404
- }
1376
+ let fd = match (fd) { FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n }
1405
1377
 
1406
1378
  let combinedDirFlags = combineLookupFlags(dirFlags)
1407
1379
 
@@ -1409,17 +1381,20 @@ export let pathSetAccessTimeNow = (fd: FileDescriptor, dirFlags: List<LookupFlag
1409
1381
  let pathSize = WasmI32.load(pathPtr, 4n)
1410
1382
  pathPtr += 8n
1411
1383
 
1412
- let err = Wasi.path_filestat_set_times(fd, combinedDirFlags, pathPtr, pathSize, 0N, 0N, Wasi._TIME_SET_ATIM_NOW)
1413
- let ret = if (err != Wasi._ESUCCESS) {
1414
- wasmSafeErr(Wasi.SystemError(tagSimpleNumber(err)))
1384
+ let err = Wasi.path_filestat_set_times(
1385
+ fd,
1386
+ combinedDirFlags,
1387
+ pathPtr,
1388
+ pathSize,
1389
+ 0N,
1390
+ 0N,
1391
+ Wasi._TIME_SET_ATIM_NOW
1392
+ )
1393
+ if (err != Wasi._ESUCCESS) {
1394
+ Err(Wasi.SystemError(tagSimpleNumber(err)))
1415
1395
  } else {
1416
- wasmSafeOk(void)
1396
+ Ok(void)
1417
1397
  }
1418
- Memory.decRef(WasmI32.fromGrain(fdArg))
1419
- Memory.decRef(WasmI32.fromGrain(dirFlags))
1420
- Memory.decRef(WasmI32.fromGrain(path))
1421
- Memory.decRef(WasmI32.fromGrain(pathSetAccessTime))
1422
- ret
1423
1398
  }
1424
1399
 
1425
1400
  /**
@@ -1431,11 +1406,16 @@ export let pathSetAccessTimeNow = (fd: FileDescriptor, dirFlags: List<LookupFlag
1431
1406
  * @param timestamp: The time to set
1432
1407
  * @returns `Ok(void)` if successful or `Err(exception)` otherwise
1433
1408
  */
1434
- export let rec pathSetModifiedTime = (fd: FileDescriptor, dirFlags: List<LookupFlag>, path: String, timestamp: Int64) => {
1409
+ @unsafe
1410
+ export let pathSetModifiedTime =
1411
+ (
1412
+ fd: FileDescriptor,
1413
+ dirFlags: List<LookupFlag>,
1414
+ path: String,
1415
+ timestamp: Int64,
1416
+ ) => {
1435
1417
  let fdArg = fd
1436
- let fd = match (fd) {
1437
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
1438
- }
1418
+ let fd = match (fd) { FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n }
1439
1419
 
1440
1420
  let combinedDirFlags = combineLookupFlags(dirFlags)
1441
1421
 
@@ -1445,18 +1425,20 @@ export let rec pathSetModifiedTime = (fd: FileDescriptor, dirFlags: List<LookupF
1445
1425
 
1446
1426
  let time = WasmI64.load(WasmI32.fromGrain(timestamp), 8n)
1447
1427
 
1448
- let err = Wasi.path_filestat_set_times(fd, combinedDirFlags, pathPtr, pathSize, 0N, time, Wasi._TIME_SET_MTIM)
1449
- let ret = if (err != Wasi._ESUCCESS) {
1450
- wasmSafeErr(Wasi.SystemError(tagSimpleNumber(err)))
1428
+ let err = Wasi.path_filestat_set_times(
1429
+ fd,
1430
+ combinedDirFlags,
1431
+ pathPtr,
1432
+ pathSize,
1433
+ 0N,
1434
+ time,
1435
+ Wasi._TIME_SET_MTIM
1436
+ )
1437
+ if (err != Wasi._ESUCCESS) {
1438
+ Err(Wasi.SystemError(tagSimpleNumber(err)))
1451
1439
  } else {
1452
- wasmSafeOk(void)
1440
+ Ok(void)
1453
1441
  }
1454
- Memory.decRef(WasmI32.fromGrain(fdArg))
1455
- Memory.decRef(WasmI32.fromGrain(dirFlags))
1456
- Memory.decRef(WasmI32.fromGrain(path))
1457
- Memory.decRef(WasmI32.fromGrain(timestamp))
1458
- Memory.decRef(WasmI32.fromGrain(pathSetModifiedTime))
1459
- ret
1460
1442
  }
1461
1443
 
1462
1444
  /**
@@ -1467,11 +1449,15 @@ export let rec pathSetModifiedTime = (fd: FileDescriptor, dirFlags: List<LookupF
1467
1449
  * @param path: The path to set the time
1468
1450
  * @returns `Ok(void)` if successful or `Err(exception)` otherwise
1469
1451
  */
1470
- export let rec pathSetModifiedTimeNow = (fd: FileDescriptor, dirFlags: List<LookupFlag>, path: String) => {
1452
+ @unsafe
1453
+ export let pathSetModifiedTimeNow =
1454
+ (
1455
+ fd: FileDescriptor,
1456
+ dirFlags: List<LookupFlag>,
1457
+ path: String,
1458
+ ) => {
1471
1459
  let fdArg = fd
1472
- let fd = match (fd) {
1473
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
1474
- }
1460
+ let fd = match (fd) { FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n }
1475
1461
 
1476
1462
  let combinedDirFlags = combineLookupFlags(dirFlags)
1477
1463
 
@@ -1479,17 +1465,20 @@ export let rec pathSetModifiedTimeNow = (fd: FileDescriptor, dirFlags: List<Look
1479
1465
  let pathSize = WasmI32.load(pathPtr, 4n)
1480
1466
  pathPtr += 8n
1481
1467
 
1482
- let err = Wasi.path_filestat_set_times(fd, combinedDirFlags, pathPtr, pathSize, 0N, 0N, Wasi._TIME_SET_MTIM_NOW)
1483
- let ret = if (err != Wasi._ESUCCESS) {
1484
- wasmSafeErr(Wasi.SystemError(tagSimpleNumber(err)))
1468
+ let err = Wasi.path_filestat_set_times(
1469
+ fd,
1470
+ combinedDirFlags,
1471
+ pathPtr,
1472
+ pathSize,
1473
+ 0N,
1474
+ 0N,
1475
+ Wasi._TIME_SET_MTIM_NOW
1476
+ )
1477
+ if (err != Wasi._ESUCCESS) {
1478
+ Err(Wasi.SystemError(tagSimpleNumber(err)))
1485
1479
  } else {
1486
- wasmSafeOk(void)
1480
+ Ok(void)
1487
1481
  }
1488
- Memory.decRef(WasmI32.fromGrain(fdArg))
1489
- Memory.decRef(WasmI32.fromGrain(dirFlags))
1490
- Memory.decRef(WasmI32.fromGrain(path))
1491
- Memory.decRef(WasmI32.fromGrain(pathSetModifiedTimeNow))
1492
- ret
1493
1482
  }
1494
1483
 
1495
1484
  /**
@@ -1502,15 +1491,23 @@ export let rec pathSetModifiedTimeNow = (fd: FileDescriptor, dirFlags: List<Look
1502
1491
  * @param targetPath: The path to the target of the link
1503
1492
  * @returns `Ok(void)` if successful or `Err(exception)` otherwise
1504
1493
  */
1505
- export let rec pathLink = (sourceFd: FileDescriptor, dirFlags: List<LookupFlag>, sourcePath: String, targetFd: FileDescriptor, targetPath: String) => {
1494
+ @unsafe
1495
+ export let pathLink =
1496
+ (
1497
+ sourceFd: FileDescriptor,
1498
+ dirFlags: List<LookupFlag>,
1499
+ sourcePath: String,
1500
+ targetFd: FileDescriptor,
1501
+ targetPath: String,
1502
+ ) => {
1506
1503
  let sourceFdArg = sourceFd
1507
1504
  let targetFdArg = targetFd
1508
1505
  let sourceFd = match (sourceFd) {
1509
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
1506
+ FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n,
1510
1507
  }
1511
1508
 
1512
1509
  let targetFd = match (targetFd) {
1513
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
1510
+ FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n,
1514
1511
  }
1515
1512
 
1516
1513
  let combinedDirFlags = combineLookupFlags(dirFlags)
@@ -1521,19 +1518,20 @@ export let rec pathLink = (sourceFd: FileDescriptor, dirFlags: List<LookupFlag>,
1521
1518
  let sourceSize = WasmI32.load(sourcePtr, 4n)
1522
1519
  let targetSize = WasmI32.load(targetPtr, 4n)
1523
1520
 
1524
- let err = Wasi.path_link(sourceFd, combinedDirFlags, sourcePtr + 8n, sourceSize, targetFd, targetPtr + 8n, targetSize)
1525
- let ret = if (err != Wasi._ESUCCESS) {
1526
- wasmSafeErr(Wasi.SystemError(tagSimpleNumber(err)))
1521
+ let err = Wasi.path_link(
1522
+ sourceFd,
1523
+ combinedDirFlags,
1524
+ sourcePtr + 8n,
1525
+ sourceSize,
1526
+ targetFd,
1527
+ targetPtr + 8n,
1528
+ targetSize
1529
+ )
1530
+ if (err != Wasi._ESUCCESS) {
1531
+ Err(Wasi.SystemError(tagSimpleNumber(err)))
1527
1532
  } else {
1528
- wasmSafeOk(void)
1533
+ Ok(void)
1529
1534
  }
1530
- Memory.decRef(WasmI32.fromGrain(sourceFdArg))
1531
- Memory.decRef(WasmI32.fromGrain(dirFlags))
1532
- Memory.decRef(WasmI32.fromGrain(sourcePath))
1533
- Memory.decRef(WasmI32.fromGrain(targetFdArg))
1534
- Memory.decRef(WasmI32.fromGrain(targetPath))
1535
- Memory.decRef(WasmI32.fromGrain(pathLink))
1536
- ret
1537
1535
  }
1538
1536
 
1539
1537
  /**
@@ -1544,11 +1542,15 @@ export let rec pathLink = (sourceFd: FileDescriptor, dirFlags: List<LookupFlag>,
1544
1542
  * @param targetPath: The path to the target of the link
1545
1543
  * @returns `Ok(void)` if successful or `Err(exception)` otherwise
1546
1544
  */
1547
- export let rec pathSymlink = (fd: FileDescriptor, sourcePath: String, targetPath: String) => {
1545
+ @unsafe
1546
+ export let pathSymlink =
1547
+ (
1548
+ fd: FileDescriptor,
1549
+ sourcePath: String,
1550
+ targetPath: String,
1551
+ ) => {
1548
1552
  let fdArg = fd
1549
- let fd = match (fd) {
1550
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
1551
- }
1553
+ let fd = match (fd) { FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n }
1552
1554
 
1553
1555
  let sourcePtr = WasmI32.fromGrain(sourcePath)
1554
1556
  let targetPtr = WasmI32.fromGrain(targetPath)
@@ -1556,17 +1558,18 @@ export let rec pathSymlink = (fd: FileDescriptor, sourcePath: String, targetPath
1556
1558
  let sourceSize = WasmI32.load(sourcePtr, 4n)
1557
1559
  let targetSize = WasmI32.load(targetPtr, 4n)
1558
1560
 
1559
- let err = Wasi.path_symlink(sourcePtr + 8n, sourceSize, fd, targetPtr + 8n, targetSize)
1560
- let ret = if (err != Wasi._ESUCCESS) {
1561
- wasmSafeErr(Wasi.SystemError(tagSimpleNumber(err)))
1561
+ let err = Wasi.path_symlink(
1562
+ sourcePtr + 8n,
1563
+ sourceSize,
1564
+ fd,
1565
+ targetPtr + 8n,
1566
+ targetSize
1567
+ )
1568
+ if (err != Wasi._ESUCCESS) {
1569
+ Err(Wasi.SystemError(tagSimpleNumber(err)))
1562
1570
  } else {
1563
- wasmSafeOk(void)
1571
+ Ok(void)
1564
1572
  }
1565
- Memory.decRef(WasmI32.fromGrain(fdArg))
1566
- Memory.decRef(WasmI32.fromGrain(sourcePath))
1567
- Memory.decRef(WasmI32.fromGrain(targetPath))
1568
- Memory.decRef(WasmI32.fromGrain(pathSymlink))
1569
- ret
1570
1573
  }
1571
1574
 
1572
1575
  /**
@@ -1576,25 +1579,20 @@ export let rec pathSymlink = (fd: FileDescriptor, sourcePath: String, targetPath
1576
1579
  * @param path: The path of the file to unlink
1577
1580
  * @returns `Ok(void)` if successful or `Err(exception)` otherwise
1578
1581
  */
1579
- export let rec pathUnlink = (fd: FileDescriptor, path: String) => {
1582
+ @unsafe
1583
+ export let pathUnlink = (fd: FileDescriptor, path: String) => {
1580
1584
  let fdArg = fd
1581
- let fd = match (fd) {
1582
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
1583
- }
1585
+ let fd = match (fd) { FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n }
1584
1586
 
1585
1587
  let pathPtr = WasmI32.fromGrain(path)
1586
1588
  let pathSize = WasmI32.load(pathPtr, 4n)
1587
1589
 
1588
1590
  let err = Wasi.path_unlink_file(fd, pathPtr + 8n, pathSize)
1589
- let ret = if (err != Wasi._ESUCCESS) {
1590
- wasmSafeErr(Wasi.SystemError(tagSimpleNumber(err)))
1591
+ if (err != Wasi._ESUCCESS) {
1592
+ Err(Wasi.SystemError(tagSimpleNumber(err)))
1591
1593
  } else {
1592
- wasmSafeOk(void)
1594
+ Ok(void)
1593
1595
  }
1594
- Memory.decRef(WasmI32.fromGrain(fdArg))
1595
- Memory.decRef(WasmI32.fromGrain(path))
1596
- Memory.decRef(WasmI32.fromGrain(pathUnlink))
1597
- ret
1598
1596
  }
1599
1597
 
1600
1598
  /**
@@ -1605,12 +1603,11 @@ export let rec pathUnlink = (fd: FileDescriptor, path: String) => {
1605
1603
  * @param size: The number of bytes to read
1606
1604
  * @returns `Ok((contents, numBytes))` of the bytes read and the number of bytes read if successful or `Err(exception)` otherwise
1607
1605
  */
1608
- export let rec pathReadlink = (fd: FileDescriptor, path: String, size: Number) => {
1606
+ @unsafe
1607
+ export let pathReadlink = (fd: FileDescriptor, path: String, size: Number) => {
1609
1608
  let fdArg = fd
1610
1609
  let sizeArg = size
1611
- let fd = match (fd) {
1612
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
1613
- }
1610
+ let fd = match (fd) { FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n }
1614
1611
 
1615
1612
  let pathPtr = WasmI32.fromGrain(path)
1616
1613
  let pathSize = WasmI32.load(pathPtr, 4n)
@@ -1623,21 +1620,16 @@ export let rec pathReadlink = (fd: FileDescriptor, path: String, size: Number) =
1623
1620
  let nread = Memory.malloc(4n)
1624
1621
 
1625
1622
  let err = Wasi.path_readlink(fd, pathPtr, pathSize + 8n, strPtr, size, nread)
1626
- let ret = if (err != Wasi._ESUCCESS) {
1623
+ if (err != Wasi._ESUCCESS) {
1627
1624
  Memory.free(grainStrPtr)
1628
1625
  Memory.free(nread)
1629
- wasmSafeErr(Wasi.SystemError(tagSimpleNumber(err)))
1626
+ Err(Wasi.SystemError(tagSimpleNumber(err)))
1630
1627
  } else {
1631
1628
  let read = tagSimpleNumber(WasmI32.load(nread, 0n))
1632
1629
  Memory.free(nread)
1633
1630
 
1634
- wasmSafeOk((WasmI32.toGrain(grainStrPtr): String, read))
1631
+ Ok((WasmI32.toGrain(grainStrPtr): String, read))
1635
1632
  }
1636
- Memory.decRef(WasmI32.fromGrain(fdArg))
1637
- Memory.decRef(WasmI32.fromGrain(path))
1638
- Memory.decRef(WasmI32.fromGrain(sizeArg))
1639
- Memory.decRef(WasmI32.fromGrain(pathReadlink))
1640
- ret
1641
1633
  }
1642
1634
 
1643
1635
  /**
@@ -1647,25 +1639,20 @@ export let rec pathReadlink = (fd: FileDescriptor, path: String, size: Number) =
1647
1639
  * @param path: The path to the directory to remove
1648
1640
  * @returns `Ok(void)` if successful or `Err(exception)` otherwise
1649
1641
  */
1650
- export let rec pathRemoveDirectory = (fd: FileDescriptor, path: String) => {
1642
+ @unsafe
1643
+ export let pathRemoveDirectory = (fd: FileDescriptor, path: String) => {
1651
1644
  let fdArg = fd
1652
- let fd = match (fd) {
1653
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
1654
- }
1645
+ let fd = match (fd) { FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n }
1655
1646
 
1656
1647
  let pathPtr = WasmI32.fromGrain(path)
1657
1648
  let pathSize = WasmI32.load(pathPtr, 4n)
1658
1649
 
1659
1650
  let err = Wasi.path_remove_directory(fd, pathPtr + 8n, pathSize)
1660
- let ret = if (err != Wasi._ESUCCESS) {
1661
- wasmSafeErr(Wasi.SystemError(tagSimpleNumber(err)))
1651
+ if (err != Wasi._ESUCCESS) {
1652
+ Err(Wasi.SystemError(tagSimpleNumber(err)))
1662
1653
  } else {
1663
- wasmSafeOk(void)
1654
+ Ok(void)
1664
1655
  }
1665
- Memory.decRef(WasmI32.fromGrain(fdArg))
1666
- Memory.decRef(WasmI32.fromGrain(path))
1667
- Memory.decRef(WasmI32.fromGrain(pathRemoveDirectory))
1668
- ret
1669
1656
  }
1670
1657
 
1671
1658
  /**
@@ -1677,15 +1664,22 @@ export let rec pathRemoveDirectory = (fd: FileDescriptor, path: String) => {
1677
1664
  * @param targetPath: The new path of the file
1678
1665
  * @returns `Ok(void)` if successful or `Err(exception)` otherwise
1679
1666
  */
1680
- export let rec pathRename = (sourceFd: FileDescriptor, sourcePath: String, targetFd: FileDescriptor, targetPath: String) => {
1667
+ @unsafe
1668
+ export let pathRename =
1669
+ (
1670
+ sourceFd: FileDescriptor,
1671
+ sourcePath: String,
1672
+ targetFd: FileDescriptor,
1673
+ targetPath: String,
1674
+ ) => {
1681
1675
  let sourceFdArg = sourceFd
1682
1676
  let targetFdArg = targetFd
1683
1677
  let sourceFd = match (sourceFd) {
1684
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
1678
+ FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n,
1685
1679
  }
1686
1680
 
1687
1681
  let targetFd = match (targetFd) {
1688
- FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n
1682
+ FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n,
1689
1683
  }
1690
1684
 
1691
1685
  let sourcePtr = WasmI32.fromGrain(sourcePath)
@@ -1694,16 +1688,17 @@ export let rec pathRename = (sourceFd: FileDescriptor, sourcePath: String, targe
1694
1688
  let sourceSize = WasmI32.load(sourcePtr, 4n)
1695
1689
  let targetSize = WasmI32.load(targetPtr, 4n)
1696
1690
 
1697
- let err = Wasi.path_rename(sourceFd, sourcePtr + 8n, sourceSize, targetFd, targetPtr + 8n, targetSize)
1698
- let ret = if (err != Wasi._ESUCCESS) {
1699
- wasmSafeErr(Wasi.SystemError(tagSimpleNumber(err)))
1691
+ let err = Wasi.path_rename(
1692
+ sourceFd,
1693
+ sourcePtr + 8n,
1694
+ sourceSize,
1695
+ targetFd,
1696
+ targetPtr + 8n,
1697
+ targetSize
1698
+ )
1699
+ if (err != Wasi._ESUCCESS) {
1700
+ Err(Wasi.SystemError(tagSimpleNumber(err)))
1700
1701
  } else {
1701
- wasmSafeOk(void)
1702
+ Ok(void)
1702
1703
  }
1703
- Memory.decRef(WasmI32.fromGrain(sourceFdArg))
1704
- Memory.decRef(WasmI32.fromGrain(sourcePath))
1705
- Memory.decRef(WasmI32.fromGrain(targetFdArg))
1706
- Memory.decRef(WasmI32.fromGrain(targetPath))
1707
- Memory.decRef(WasmI32.fromGrain(pathRename))
1708
- ret
1709
1704
  }