/
snapshot_preview1.ts
1740 lines (1441 loc) · 44.1 KB
/
snapshot_preview1.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
// deno-lint-ignore-file no-unused-vars
import { relative, resolve } from "../path/mod.ts";
const CLOCKID_REALTIME = 0;
const CLOCKID_MONOTONIC = 1;
const CLOCKID_PROCESS_CPUTIME_ID = 2;
const CLOCKID_THREAD_CPUTIME_ID = 3;
const ERRNO_SUCCESS = 0;
const _ERRNO_2BIG = 1;
const ERRNO_ACCES = 2;
const ERRNO_ADDRINUSE = 3;
const ERRNO_ADDRNOTAVAIL = 4;
const _ERRNO_AFNOSUPPORT = 5;
const _ERRNO_AGAIN = 6;
const _ERRNO_ALREADY = 7;
const ERRNO_BADF = 8;
const _ERRNO_BADMSG = 9;
const ERRNO_BUSY = 10;
const _ERRNO_CANCELED = 11;
const _ERRNO_CHILD = 12;
const ERRNO_CONNABORTED = 13;
const ERRNO_CONNREFUSED = 14;
const ERRNO_CONNRESET = 15;
const _ERRNO_DEADLK = 16;
const _ERRNO_DESTADDRREQ = 17;
const _ERRNO_DOM = 18;
const _ERRNO_DQUOT = 19;
const _ERRNO_EXIST = 20;
const _ERRNO_FAULT = 21;
const _ERRNO_FBIG = 22;
const _ERRNO_HOSTUNREACH = 23;
const _ERRNO_IDRM = 24;
const _ERRNO_ILSEQ = 25;
const _ERRNO_INPROGRESS = 26;
const ERRNO_INTR = 27;
const ERRNO_INVAL = 28;
const _ERRNO_IO = 29;
const _ERRNO_ISCONN = 30;
const _ERRNO_ISDIR = 31;
const _ERRNO_LOOP = 32;
const _ERRNO_MFILE = 33;
const _ERRNO_MLINK = 34;
const _ERRNO_MSGSIZE = 35;
const _ERRNO_MULTIHOP = 36;
const _ERRNO_NAMETOOLONG = 37;
const _ERRNO_NETDOWN = 38;
const _ERRNO_NETRESET = 39;
const _ERRNO_NETUNREACH = 40;
const _ERRNO_NFILE = 41;
const _ERRNO_NOBUFS = 42;
const _ERRNO_NODEV = 43;
const ERRNO_NOENT = 44;
const _ERRNO_NOEXEC = 45;
const _ERRNO_NOLCK = 46;
const _ERRNO_NOLINK = 47;
const _ERRNO_NOMEM = 48;
const _ERRNO_NOMSG = 49;
const _ERRNO_NOPROTOOPT = 50;
const _ERRNO_NOSPC = 51;
const ERRNO_NOSYS = 52;
const ERRNO_NOTCONN = 53;
const ERRNO_NOTDIR = 54;
const _ERRNO_NOTEMPTY = 55;
const _ERRNO_NOTRECOVERABLE = 56;
const _ERRNO_NOTSOCK = 57;
const _ERRNO_NOTSUP = 58;
const _ERRNO_NOTTY = 59;
const _ERRNO_NXIO = 60;
const _ERRNO_OVERFLOW = 61;
const _ERRNO_OWNERDEAD = 62;
const _ERRNO_PERM = 63;
const ERRNO_PIPE = 64;
const _ERRNO_PROTO = 65;
const _ERRNO_PROTONOSUPPORT = 66;
const _ERRNO_PROTOTYPE = 67;
const _ERRNO_RANGE = 68;
const _ERRNO_ROFS = 69;
const _ERRNO_SPIPE = 70;
const _ERRNO_SRCH = 71;
const _ERRNO_STALE = 72;
const ERRNO_TIMEDOUT = 73;
const _ERRNO_TXTBSY = 74;
const _ERRNO_XDEV = 75;
const ERRNO_NOTCAPABLE = 76;
const RIGHTS_FD_DATASYNC = 0x0000000000000001n;
const RIGHTS_FD_READ = 0x0000000000000002n;
const _RIGHTS_FD_SEEK = 0x0000000000000004n;
const _RIGHTS_FD_FDSTAT_SET_FLAGS = 0x0000000000000008n;
const _RIGHTS_FD_SYNC = 0x0000000000000010n;
const _RIGHTS_FD_TELL = 0x0000000000000020n;
const RIGHTS_FD_WRITE = 0x0000000000000040n;
const _RIGHTS_FD_ADVISE = 0x0000000000000080n;
const RIGHTS_FD_ALLOCATE = 0x0000000000000100n;
const _RIGHTS_PATH_CREATE_DIRECTORY = 0x0000000000000200n;
const _RIGHTS_PATH_CREATE_FILE = 0x0000000000000400n;
const _RIGHTS_PATH_LINK_SOURCE = 0x0000000000000800n;
const _RIGHTS_PATH_LINK_TARGET = 0x0000000000001000n;
const _RIGHTS_PATH_OPEN = 0x0000000000002000n;
const RIGHTS_FD_READDIR = 0x0000000000004000n;
const _RIGHTS_PATH_READLINK = 0x0000000000008000n;
const _RIGHTS_PATH_RENAME_SOURCE = 0x0000000000010000n;
const _RIGHTS_PATH_RENAME_TARGET = 0x0000000000020000n;
const _RIGHTS_PATH_FILESTAT_GET = 0x0000000000040000n;
const _RIGHTS_PATH_FILESTAT_SET_SIZE = 0x0000000000080000n;
const _RIGHTS_PATH_FILESTAT_SET_TIMES = 0x0000000000100000n;
const _RIGHTS_FD_FILESTAT_GET = 0x0000000000200000n;
const RIGHTS_FD_FILESTAT_SET_SIZE = 0x0000000000400000n;
const _RIGHTS_FD_FILESTAT_SET_TIMES = 0x0000000000800000n;
const _RIGHTS_PATH_SYMLINK = 0x0000000001000000n;
const _RIGHTS_PATH_REMOVE_DIRECTORY = 0x0000000002000000n;
const _RIGHTS_PATH_UNLINK_FILE = 0x0000000004000000n;
const _RIGHTS_POLL_FD_READWRITE = 0x0000000008000000n;
const _RIGHTS_SOCK_SHUTDOWN = 0x0000000010000000n;
const _WHENCE_SET = 0;
const _WHENCE_CUR = 1;
const _WHENCE_END = 2;
const FILETYPE_UNKNOWN = 0;
const _FILETYPE_BLOCK_DEVICE = 1;
const FILETYPE_CHARACTER_DEVICE = 2;
const FILETYPE_DIRECTORY = 3;
const FILETYPE_REGULAR_FILE = 4;
const _FILETYPE_SOCKET_DGRAM = 5;
const _FILETYPE_SOCKET_STREAM = 6;
const FILETYPE_SYMBOLIC_LINK = 7;
const _ADVICE_NORMAL = 0;
const _ADVICE_SEQUENTIAL = 1;
const _ADVICE_RANDOM = 2;
const _ADVICE_WILLNEED = 3;
const _ADVICE_DONTNEED = 4;
const _ADVICE_NOREUSE = 5;
const FDFLAGS_APPEND = 0x0001;
const FDFLAGS_DSYNC = 0x0002;
const FDFLAGS_NONBLOCK = 0x0004;
const FDFLAGS_RSYNC = 0x0008;
const FDFLAGS_SYNC = 0x0010;
const _FSTFLAGS_ATIM = 0x0001;
const FSTFLAGS_ATIM_NOW = 0x0002;
const _FSTFLAGS_MTIM = 0x0004;
const FSTFLAGS_MTIM_NOW = 0x0008;
const LOOKUPFLAGS_SYMLINK_FOLLOW = 0x0001;
const OFLAGS_CREAT = 0x0001;
const OFLAGS_DIRECTORY = 0x0002;
const OFLAGS_EXCL = 0x0004;
const OFLAGS_TRUNC = 0x0008;
const _EVENTTYPE_CLOCK = 0;
const _EVENTTYPE_FD_READ = 1;
const _EVENTTYPE_FD_WRITE = 2;
const _EVENTRWFLAGS_FD_READWRITE_HANGUP = 1;
const _SUBCLOCKFLAGS_SUBSCRIPTION_CLOCK_ABSTIME = 1;
const _SIGNAL_NONE = 0;
const _SIGNAL_HUP = 1;
const _SIGNAL_INT = 2;
const _SIGNAL_QUIT = 3;
const _SIGNAL_ILL = 4;
const _SIGNAL_TRAP = 5;
const _SIGNAL_ABRT = 6;
const _SIGNAL_BUS = 7;
const _SIGNAL_FPE = 8;
const _SIGNAL_KILL = 9;
const _SIGNAL_USR1 = 10;
const _SIGNAL_SEGV = 11;
const _SIGNAL_USR2 = 12;
const _SIGNAL_PIPE = 13;
const _SIGNAL_ALRM = 14;
const _SIGNAL_TERM = 15;
const _SIGNAL_CHLD = 16;
const _SIGNAL_CONT = 17;
const _SIGNAL_STOP = 18;
const _SIGNAL_TSTP = 19;
const _SIGNAL_TTIN = 20;
const _SIGNAL_TTOU = 21;
const _SIGNAL_URG = 22;
const _SIGNAL_XCPU = 23;
const _SIGNAL_XFSZ = 24;
const _SIGNAL_VTALRM = 25;
const _SIGNAL_PROF = 26;
const _SIGNAL_WINCH = 27;
const _SIGNAL_POLL = 28;
const _SIGNAL_PWR = 29;
const _SIGNAL_SYS = 30;
const _RIFLAGS_RECV_PEEK = 0x0001;
const _RIFLAGS_RECV_WAITALL = 0x0002;
const _ROFLAGS_RECV_DATA_TRUNCATED = 0x0001;
const _SDFLAGS_RD = 0x0001;
const _SDFLAGS_WR = 0x0002;
const PREOPENTYPE_DIR = 0;
function syscall<T extends CallableFunction>(target: T) {
return function (...args: unknown[]) {
try {
return target(...args);
} catch (err) {
if (err instanceof ExitStatus) {
throw err;
}
switch (err.name) {
case "NotFound":
return ERRNO_NOENT;
case "PermissionDenied":
return ERRNO_ACCES;
case "ConnectionRefused":
return ERRNO_CONNREFUSED;
case "ConnectionReset":
return ERRNO_CONNRESET;
case "ConnectionAborted":
return ERRNO_CONNABORTED;
case "NotConnected":
return ERRNO_NOTCONN;
case "AddrInUse":
return ERRNO_ADDRINUSE;
case "AddrNotAvailable":
return ERRNO_ADDRNOTAVAIL;
case "BrokenPipe":
return ERRNO_PIPE;
case "InvalidData":
return ERRNO_INVAL;
case "TimedOut":
return ERRNO_TIMEDOUT;
case "Interrupted":
return ERRNO_INTR;
case "BadResource":
return ERRNO_BADF;
case "Busy":
return ERRNO_BUSY;
default:
return ERRNO_INVAL;
}
}
};
}
interface FileDescriptor {
rid?: number;
type?: number;
flags?: number;
path?: string;
vpath?: string;
entries?: Deno.DirEntry[];
}
class ExitStatus {
code: number;
constructor(code: number) {
this.code = code;
}
}
export interface ContextOptions {
/**
* An array of strings that the WebAssembly instance will see as command-line
* arguments.
*
* The first argument is the virtual path to the command itself.
*/
args?: string[];
/**
* An object of string keys mapped to string values that the WebAssembly module will see as its environment.
*/
env?: { [key: string]: string | undefined };
/**
* An object of string keys mapped to string values that the WebAssembly module will see as it's filesystem.
*
* The string keys of are treated as directories within the sandboxed
* filesystem, the values are the real paths to those directories on the host
* machine.
*
*/
preopens?: { [key: string]: string };
/**
* Determines if calls to exit from within the WebAssembly module will terminate the proess or return.
*/
exitOnReturn?: boolean;
/**
* The resource descriptor used as standard input in the WebAssembly module.
*/
stdin?: number;
/**
* The resource descriptor used as standard output in the WebAssembly module.
*/
stdout?: number;
/**
* The resource descriptor used as standard error in the WebAssembly module.
*/
stderr?: number;
}
/**
* The Context class provides the environment required to run WebAssembly
* modules compiled to run with the WebAssembly System Interface.
*
* Each context represents a distinct sandboxed environment and must have its
* command-line arguments, environment variables, and pre-opened directory
* structure configured explicitly.
*/
export default class Context {
#args: string[];
#env: { [key: string]: string | undefined };
#exitOnReturn: boolean;
#memory: WebAssembly.Memory;
#fds: FileDescriptor[];
#started: boolean;
exports: Record<string, WebAssembly.ImportValue>;
constructor(options: ContextOptions) {
this.#args = options.args ?? [];
this.#env = options.env ?? {};
this.#exitOnReturn = options.exitOnReturn ?? true;
this.#memory = null!;
this.#fds = [
{
rid: options.stdin ?? Deno.stdin.rid,
type: FILETYPE_CHARACTER_DEVICE,
flags: FDFLAGS_APPEND,
},
{
rid: options.stdout ?? Deno.stdout.rid,
type: FILETYPE_CHARACTER_DEVICE,
flags: FDFLAGS_APPEND,
},
{
rid: options.stderr ?? Deno.stderr.rid,
type: FILETYPE_CHARACTER_DEVICE,
flags: FDFLAGS_APPEND,
},
];
if (options.preopens) {
for (const [vpath, path] of Object.entries(options.preopens)) {
const type = FILETYPE_DIRECTORY;
const entries = Array.from(Deno.readDirSync(path));
const entry = {
type,
entries,
path,
vpath,
};
this.#fds.push(entry);
}
}
this.exports = {
"args_get": syscall((
argvOffset: number,
argvBufferOffset: number,
): number => {
const args = this.#args;
const textEncoder = new TextEncoder();
const memoryData = new Uint8Array(this.#memory.buffer);
const memoryView = new DataView(this.#memory.buffer);
for (const arg of args) {
memoryView.setUint32(argvOffset, argvBufferOffset, true);
argvOffset += 4;
const data = textEncoder.encode(`${arg}\0`);
memoryData.set(data, argvBufferOffset);
argvBufferOffset += data.length;
}
return ERRNO_SUCCESS;
}),
"args_sizes_get": syscall((
argcOffset: number,
argvBufferSizeOffset: number,
): number => {
const args = this.#args;
const textEncoder = new TextEncoder();
const memoryView = new DataView(this.#memory.buffer);
memoryView.setUint32(argcOffset, args.length, true);
memoryView.setUint32(
argvBufferSizeOffset,
args.reduce(function (acc, arg) {
return acc + textEncoder.encode(`${arg}\0`).length;
}, 0),
true,
);
return ERRNO_SUCCESS;
}),
"environ_get": syscall((
environOffset: number,
environBufferOffset: number,
): number => {
const entries = Object.entries(this.#env);
const textEncoder = new TextEncoder();
const memoryData = new Uint8Array(this.#memory.buffer);
const memoryView = new DataView(this.#memory.buffer);
for (const [key, value] of entries) {
memoryView.setUint32(environOffset, environBufferOffset, true);
environOffset += 4;
const data = textEncoder.encode(`${key}=${value}\0`);
memoryData.set(data, environBufferOffset);
environBufferOffset += data.length;
}
return ERRNO_SUCCESS;
}),
"environ_sizes_get": syscall((
environcOffset: number,
environBufferSizeOffset: number,
): number => {
const entries = Object.entries(this.#env);
const textEncoder = new TextEncoder();
const memoryView = new DataView(this.#memory.buffer);
memoryView.setUint32(environcOffset, entries.length, true);
memoryView.setUint32(
environBufferSizeOffset,
entries.reduce(function (acc, [key, value]) {
return acc + textEncoder.encode(`${key}=${value}\0`).length;
}, 0),
true,
);
return ERRNO_SUCCESS;
}),
"clock_res_get": syscall((
id: number,
resolutionOffset: number,
): number => {
const memoryView = new DataView(this.#memory.buffer);
switch (id) {
case CLOCKID_REALTIME: {
const resolution = BigInt(1e6);
memoryView.setBigUint64(
resolutionOffset,
resolution,
true,
);
break;
}
case CLOCKID_MONOTONIC:
case CLOCKID_PROCESS_CPUTIME_ID:
case CLOCKID_THREAD_CPUTIME_ID: {
const resolution = BigInt(1e3);
memoryView.setBigUint64(resolutionOffset, resolution, true);
break;
}
default:
return ERRNO_INVAL;
}
return ERRNO_SUCCESS;
}),
"clock_time_get": syscall((
id: number,
precision: bigint,
timeOffset: number,
): number => {
const memoryView = new DataView(this.#memory.buffer);
switch (id) {
case CLOCKID_REALTIME: {
const time = BigInt(Date.now()) * BigInt(1e6);
memoryView.setBigUint64(timeOffset, time, true);
break;
}
case CLOCKID_MONOTONIC:
case CLOCKID_PROCESS_CPUTIME_ID:
case CLOCKID_THREAD_CPUTIME_ID: {
const t = performance.now();
const s = Math.trunc(t);
const ms = Math.floor((t - s) * 1e3);
const time = BigInt(s) * BigInt(1e9) + BigInt(ms) * BigInt(1e6);
memoryView.setBigUint64(timeOffset, time, true);
break;
}
default:
return ERRNO_INVAL;
}
return ERRNO_SUCCESS;
}),
"fd_advise": syscall((
_fd: number,
_offset: bigint,
_length: bigint,
_advice: number,
): number => {
return ERRNO_NOSYS;
}),
"fd_allocate": syscall((
_fd: number,
_offset: bigint,
_length: bigint,
): number => {
return ERRNO_NOSYS;
}),
"fd_close": syscall((
fd: number,
): number => {
const entry = this.#fds[fd];
if (!entry) {
return ERRNO_BADF;
}
if (entry.rid) {
Deno.close(entry.rid);
}
delete this.#fds[fd];
return ERRNO_SUCCESS;
}),
"fd_datasync": syscall((
fd: number,
): number => {
const entry = this.#fds[fd];
if (!entry) {
return ERRNO_BADF;
}
Deno.fdatasyncSync(entry.rid!);
return ERRNO_SUCCESS;
}),
"fd_fdstat_get": syscall((
fd: number,
offset: number,
): number => {
const entry = this.#fds[fd];
if (!entry) {
return ERRNO_BADF;
}
const memoryView = new DataView(this.#memory.buffer);
memoryView.setUint8(offset, entry.type!);
memoryView.setUint16(offset + 2, entry.flags!, true);
// TODO(bartlomieju)
memoryView.setBigUint64(offset + 8, 0n, true);
// TODO(bartlomieju)
memoryView.setBigUint64(offset + 16, 0n, true);
return ERRNO_SUCCESS;
}),
"fd_fdstat_set_flags": syscall((
_fd: number,
_flags: number,
): number => {
return ERRNO_NOSYS;
}),
"fd_fdstat_set_rights": syscall((
_fd: number,
_rightsBase: bigint,
_rightsInheriting: bigint,
): number => {
return ERRNO_NOSYS;
}),
"fd_filestat_get": syscall((
fd: number,
offset: number,
): number => {
const entry = this.#fds[fd];
if (!entry) {
return ERRNO_BADF;
}
const memoryView = new DataView(this.#memory.buffer);
const info = Deno.fstatSync(entry.rid!);
if (entry.type === undefined) {
switch (true) {
case info.isFile:
entry.type = FILETYPE_REGULAR_FILE;
break;
case info.isDirectory:
entry.type = FILETYPE_DIRECTORY;
break;
case info.isSymlink:
entry.type = FILETYPE_SYMBOLIC_LINK;
break;
default:
entry.type = FILETYPE_UNKNOWN;
break;
}
}
memoryView.setBigUint64(offset, BigInt(info.dev ? info.dev : 0), true);
offset += 8;
memoryView.setBigUint64(offset, BigInt(info.ino ? info.ino : 0), true);
offset += 8;
memoryView.setUint8(offset, entry.type);
offset += 8;
memoryView.setUint32(offset, Number(info.nlink), true);
offset += 8;
memoryView.setBigUint64(offset, BigInt(info.size), true);
offset += 8;
memoryView.setBigUint64(
offset,
BigInt(info.atime ? info.atime.getTime() * 1e6 : 0),
true,
);
offset += 8;
memoryView.setBigUint64(
offset,
BigInt(info.mtime ? info.mtime.getTime() * 1e6 : 0),
true,
);
offset += 8;
memoryView.setBigUint64(
offset,
BigInt(info.birthtime ? info.birthtime.getTime() * 1e6 : 0),
true,
);
offset += 8;
return ERRNO_SUCCESS;
}),
"fd_filestat_set_size": syscall((
fd: number,
size: bigint,
): number => {
const entry = this.#fds[fd];
if (!entry) {
return ERRNO_BADF;
}
Deno.ftruncateSync(entry.rid!, Number(size));
return ERRNO_SUCCESS;
}),
"fd_filestat_set_times": syscall((
fd: number,
atim: bigint,
mtim: bigint,
flags: number,
): number => {
const entry = this.#fds[fd];
if (!entry) {
return ERRNO_BADF;
}
if (!entry.path) {
return ERRNO_INVAL;
}
if ((flags & FSTFLAGS_ATIM_NOW) == FSTFLAGS_ATIM_NOW) {
atim = BigInt(Date.now() * 1e6);
}
if ((flags & FSTFLAGS_MTIM_NOW) == FSTFLAGS_MTIM_NOW) {
mtim = BigInt(Date.now() * 1e6);
}
Deno.utimeSync(entry.path!, Number(atim), Number(mtim));
return ERRNO_SUCCESS;
}),
"fd_pread": syscall((
fd: number,
iovsOffset: number,
iovsLength: number,
offset: bigint,
nreadOffset: number,
): number => {
const entry = this.#fds[fd];
if (entry == null) {
return ERRNO_BADF;
}
const seek = Deno.seekSync(entry.rid!, 0, Deno.SeekMode.Current);
const memoryView = new DataView(this.#memory.buffer);
let nread = 0;
for (let i = 0; i < iovsLength; i++) {
const dataOffset = memoryView.getUint32(iovsOffset, true);
iovsOffset += 4;
const dataLength = memoryView.getUint32(iovsOffset, true);
iovsOffset += 4;
const data = new Uint8Array(
this.#memory.buffer,
dataOffset,
dataLength,
);
nread += Deno.readSync(entry.rid!, data) as number;
}
Deno.seekSync(entry.rid!, seek, Deno.SeekMode.Start);
memoryView.setUint32(nreadOffset, nread, true);
return ERRNO_SUCCESS;
}),
"fd_prestat_get": syscall((
fd: number,
prestatOffset: number,
): number => {
const entry = this.#fds[fd];
if (!entry) {
return ERRNO_BADF;
}
if (!entry.vpath) {
return ERRNO_BADF;
}
const memoryView = new DataView(this.#memory.buffer);
memoryView.setUint8(prestatOffset, PREOPENTYPE_DIR);
memoryView.setUint32(
prestatOffset + 4,
new TextEncoder().encode(entry.vpath).byteLength,
true,
);
return ERRNO_SUCCESS;
}),
"fd_prestat_dir_name": syscall((
fd: number,
pathOffset: number,
pathLength: number,
): number => {
const entry = this.#fds[fd];
if (!entry) {
return ERRNO_BADF;
}
if (!entry.vpath) {
return ERRNO_BADF;
}
const data = new Uint8Array(
this.#memory.buffer,
pathOffset,
pathLength,
);
data.set(new TextEncoder().encode(entry.vpath));
return ERRNO_SUCCESS;
}),
"fd_pwrite": syscall((
fd: number,
iovsOffset: number,
iovsLength: number,
offset: bigint,
nwrittenOffset: number,
): number => {
const entry = this.#fds[fd];
if (!entry) {
return ERRNO_BADF;
}
const seek = Deno.seekSync(entry.rid!, 0, Deno.SeekMode.Current);
const memoryView = new DataView(this.#memory.buffer);
let nwritten = 0;
for (let i = 0; i < iovsLength; i++) {
const dataOffset = memoryView.getUint32(iovsOffset, true);
iovsOffset += 4;
const dataLength = memoryView.getUint32(iovsOffset, true);
iovsOffset += 4;
const data = new Uint8Array(
this.#memory.buffer,
dataOffset,
dataLength,
);
nwritten += Deno.writeSync(entry.rid!, data) as number;
}
Deno.seekSync(entry.rid!, seek, Deno.SeekMode.Start);
memoryView.setUint32(nwrittenOffset, nwritten, true);
return ERRNO_SUCCESS;
}),
"fd_read": syscall((
fd: number,
iovsOffset: number,
iovsLength: number,
nreadOffset: number,
): number => {
const entry = this.#fds[fd];
if (!entry) {
return ERRNO_BADF;
}
const memoryView = new DataView(this.#memory.buffer);
let nread = 0;
for (let i = 0; i < iovsLength; i++) {
const dataOffset = memoryView.getUint32(iovsOffset, true);
iovsOffset += 4;
const dataLength = memoryView.getUint32(iovsOffset, true);
iovsOffset += 4;
const data = new Uint8Array(
this.#memory.buffer,
dataOffset,
dataLength,
);
nread += Deno.readSync(entry.rid!, data) as number;
}
memoryView.setUint32(nreadOffset, nread, true);
return ERRNO_SUCCESS;
}),
"fd_readdir": syscall((
fd: number,
bufferOffset: number,
bufferLength: number,
cookie: bigint,
bufferUsedOffset: number,
): number => {
const entry = this.#fds[fd];
if (!entry) {
return ERRNO_BADF;
}
const memoryData = new Uint8Array(this.#memory.buffer);
const memoryView = new DataView(this.#memory.buffer);
let bufferUsed = 0;
const entries = Array.from(Deno.readDirSync(entry.path!));
for (let i = Number(cookie); i < entries.length; i++) {
const nameData = new TextEncoder().encode(entries[i].name);
const entryInfo = Deno.statSync(
resolve(entry.path!, entries[i].name),
);
const entryData = new Uint8Array(24 + nameData.byteLength);
const entryView = new DataView(entryData.buffer);
entryView.setBigUint64(0, BigInt(i + 1), true);
entryView.setBigUint64(
8,
BigInt(entryInfo.ino ? entryInfo.ino : 0),
true,
);
entryView.setUint32(16, nameData.byteLength, true);
let type: number;
switch (true) {
case entries[i].isFile:
type = FILETYPE_REGULAR_FILE;
break;
case entries[i].isDirectory:
type = FILETYPE_REGULAR_FILE;
break;
case entries[i].isSymlink:
type = FILETYPE_SYMBOLIC_LINK;
break;
default:
type = FILETYPE_REGULAR_FILE;
break;
}
entryView.setUint8(20, type);
entryData.set(nameData, 24);
const data = entryData.slice(
0,
Math.min(entryData.length, bufferLength - bufferUsed),
);
memoryData.set(data, bufferOffset + bufferUsed);
bufferUsed += data.byteLength;
}
memoryView.setUint32(bufferUsedOffset, bufferUsed, true);
return ERRNO_SUCCESS;
}),
"fd_renumber": syscall((
fd: number,
to: number,
): number => {
if (!this.#fds[fd]) {
return ERRNO_BADF;
}
if (!this.#fds[to]) {
return ERRNO_BADF;
}
if (this.#fds[to].rid) {
Deno.close(this.#fds[to].rid!);
}
this.#fds[to] = this.#fds[fd];
delete this.#fds[fd];
return ERRNO_SUCCESS;
}),
"fd_seek": syscall((
fd: number,
offset: bigint,
whence: number,
newOffsetOffset: number,
): number => {
const entry = this.#fds[fd];
if (!entry) {
return ERRNO_BADF;
}
const memoryView = new DataView(this.#memory.buffer);
// FIXME Deno does not support seeking with big integers
const newOffset = Deno.seekSync(entry.rid!, Number(offset), whence);
memoryView.setBigUint64(newOffsetOffset, BigInt(newOffset), true);
return ERRNO_SUCCESS;
}),
"fd_sync": syscall((
fd: number,