2020-09-21 08:26:41 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-10-24 10:04:59 -04:00
|
|
|
import { relative, resolve } from "../path/mod.ts";
|
2020-06-24 09:27:31 -04:00
|
|
|
|
|
|
|
const CLOCKID_REALTIME = 0;
|
|
|
|
const CLOCKID_MONOTONIC = 1;
|
|
|
|
const CLOCKID_PROCESS_CPUTIME_ID = 2;
|
|
|
|
const CLOCKID_THREAD_CPUTIME_ID = 3;
|
|
|
|
|
|
|
|
const ERRNO_SUCCESS = 0;
|
2020-09-29 05:41:22 -04:00
|
|
|
const _ERRNO_2BIG = 1;
|
2020-06-24 09:27:31 -04:00
|
|
|
const ERRNO_ACCES = 2;
|
|
|
|
const ERRNO_ADDRINUSE = 3;
|
|
|
|
const ERRNO_ADDRNOTAVAIL = 4;
|
2020-09-29 05:41:22 -04:00
|
|
|
const _ERRNO_AFNOSUPPORT = 5;
|
|
|
|
const _ERRNO_AGAIN = 6;
|
|
|
|
const _ERRNO_ALREADY = 7;
|
2020-06-24 09:27:31 -04:00
|
|
|
const ERRNO_BADF = 8;
|
2020-09-29 05:41:22 -04:00
|
|
|
const _ERRNO_BADMSG = 9;
|
2020-06-24 09:27:31 -04:00
|
|
|
const ERRNO_BUSY = 10;
|
2020-09-29 05:41:22 -04:00
|
|
|
const _ERRNO_CANCELED = 11;
|
|
|
|
const _ERRNO_CHILD = 12;
|
2020-06-24 09:27:31 -04:00
|
|
|
const ERRNO_CONNABORTED = 13;
|
|
|
|
const ERRNO_CONNREFUSED = 14;
|
|
|
|
const ERRNO_CONNRESET = 15;
|
2020-09-29 05:41:22 -04:00
|
|
|
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;
|
2020-06-24 09:27:31 -04:00
|
|
|
const ERRNO_INTR = 27;
|
|
|
|
const ERRNO_INVAL = 28;
|
2020-09-29 05:41:22 -04:00
|
|
|
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;
|
2020-06-24 09:27:31 -04:00
|
|
|
const ERRNO_NOENT = 44;
|
2020-09-29 05:41:22 -04:00
|
|
|
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;
|
2020-06-24 09:27:31 -04:00
|
|
|
const ERRNO_NOSYS = 52;
|
|
|
|
const ERRNO_NOTCONN = 53;
|
|
|
|
const ERRNO_NOTDIR = 54;
|
2020-09-29 05:41:22 -04:00
|
|
|
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;
|
2020-06-24 09:27:31 -04:00
|
|
|
const ERRNO_PIPE = 64;
|
2020-09-29 05:41:22 -04:00
|
|
|
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;
|
2020-06-24 09:27:31 -04:00
|
|
|
const ERRNO_TIMEDOUT = 73;
|
2020-09-29 05:41:22 -04:00
|
|
|
const _ERRNO_TXTBSY = 74;
|
|
|
|
const _ERRNO_XDEV = 75;
|
2020-10-24 10:04:59 -04:00
|
|
|
const ERRNO_NOTCAPABLE = 76;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
|
|
|
const RIGHTS_FD_DATASYNC = 0x0000000000000001n;
|
|
|
|
const RIGHTS_FD_READ = 0x0000000000000002n;
|
2020-09-29 05:41:22 -04:00
|
|
|
const _RIGHTS_FD_SEEK = 0x0000000000000004n;
|
|
|
|
const _RIGHTS_FD_FDSTAT_SET_FLAGS = 0x0000000000000008n;
|
|
|
|
const _RIGHTS_FD_SYNC = 0x0000000000000010n;
|
|
|
|
const _RIGHTS_FD_TELL = 0x0000000000000020n;
|
2020-06-24 09:27:31 -04:00
|
|
|
const RIGHTS_FD_WRITE = 0x0000000000000040n;
|
2020-09-29 05:41:22 -04:00
|
|
|
const _RIGHTS_FD_ADVISE = 0x0000000000000080n;
|
2020-06-24 09:27:31 -04:00
|
|
|
const RIGHTS_FD_ALLOCATE = 0x0000000000000100n;
|
2020-09-29 05:41:22 -04:00
|
|
|
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;
|
2020-06-24 09:27:31 -04:00
|
|
|
const RIGHTS_FD_READDIR = 0x0000000000004000n;
|
2020-09-29 05:41:22 -04:00
|
|
|
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;
|
2020-06-24 09:27:31 -04:00
|
|
|
const RIGHTS_FD_FILESTAT_SET_SIZE = 0x0000000000400000n;
|
2020-09-29 05:41:22 -04:00
|
|
|
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;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
const _WHENCE_SET = 0;
|
|
|
|
const _WHENCE_CUR = 1;
|
|
|
|
const _WHENCE_END = 2;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
|
|
|
const FILETYPE_UNKNOWN = 0;
|
2020-09-29 05:41:22 -04:00
|
|
|
const _FILETYPE_BLOCK_DEVICE = 1;
|
2020-06-24 09:27:31 -04:00
|
|
|
const FILETYPE_CHARACTER_DEVICE = 2;
|
|
|
|
const FILETYPE_DIRECTORY = 3;
|
|
|
|
const FILETYPE_REGULAR_FILE = 4;
|
2020-09-29 05:41:22 -04:00
|
|
|
const _FILETYPE_SOCKET_DGRAM = 5;
|
|
|
|
const _FILETYPE_SOCKET_STREAM = 6;
|
2020-06-24 09:27:31 -04:00
|
|
|
const FILETYPE_SYMBOLIC_LINK = 7;
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
const _ADVICE_NORMAL = 0;
|
|
|
|
const _ADVICE_SEQUENTIAL = 1;
|
|
|
|
const _ADVICE_RANDOM = 2;
|
|
|
|
const _ADVICE_WILLNEED = 3;
|
|
|
|
const _ADVICE_DONTNEED = 4;
|
|
|
|
const _ADVICE_NOREUSE = 5;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
|
|
|
const FDFLAGS_APPEND = 0x0001;
|
|
|
|
const FDFLAGS_DSYNC = 0x0002;
|
|
|
|
const FDFLAGS_NONBLOCK = 0x0004;
|
|
|
|
const FDFLAGS_RSYNC = 0x0008;
|
|
|
|
const FDFLAGS_SYNC = 0x0010;
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
const _FSTFLAGS_ATIM = 0x0001;
|
2020-06-24 09:27:31 -04:00
|
|
|
const FSTFLAGS_ATIM_NOW = 0x0002;
|
2020-09-29 05:41:22 -04:00
|
|
|
const _FSTFLAGS_MTIM = 0x0004;
|
2020-06-24 09:27:31 -04:00
|
|
|
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;
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
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;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
|
|
|
const PREOPENTYPE_DIR = 0;
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
function syscall<T extends CallableFunction>(target: T) {
|
|
|
|
return function (...args: unknown[]) {
|
2020-08-24 19:04:11 -04:00
|
|
|
try {
|
|
|
|
return target(...args);
|
|
|
|
} catch (err) {
|
2020-12-03 13:57:35 -05:00
|
|
|
if (err instanceof ExitStatus) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
2020-08-24 19:04:11 -04:00
|
|
|
switch (err.name) {
|
|
|
|
case "NotFound":
|
|
|
|
return ERRNO_NOENT;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-08-24 19:04:11 -04:00
|
|
|
case "PermissionDenied":
|
|
|
|
return ERRNO_ACCES;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-08-24 19:04:11 -04:00
|
|
|
case "ConnectionRefused":
|
|
|
|
return ERRNO_CONNREFUSED;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-08-24 19:04:11 -04:00
|
|
|
case "ConnectionReset":
|
|
|
|
return ERRNO_CONNRESET;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-08-24 19:04:11 -04:00
|
|
|
case "ConnectionAborted":
|
|
|
|
return ERRNO_CONNABORTED;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-08-24 19:04:11 -04:00
|
|
|
case "NotConnected":
|
|
|
|
return ERRNO_NOTCONN;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-08-24 19:04:11 -04:00
|
|
|
case "AddrInUse":
|
|
|
|
return ERRNO_ADDRINUSE;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-08-24 19:04:11 -04:00
|
|
|
case "AddrNotAvailable":
|
|
|
|
return ERRNO_ADDRNOTAVAIL;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-08-24 19:04:11 -04:00
|
|
|
case "BrokenPipe":
|
|
|
|
return ERRNO_PIPE;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-08-24 19:04:11 -04:00
|
|
|
case "InvalidData":
|
|
|
|
return ERRNO_INVAL;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-08-24 19:04:11 -04:00
|
|
|
case "TimedOut":
|
|
|
|
return ERRNO_TIMEDOUT;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-08-24 19:04:11 -04:00
|
|
|
case "Interrupted":
|
|
|
|
return ERRNO_INTR;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-08-24 19:04:11 -04:00
|
|
|
case "BadResource":
|
|
|
|
return ERRNO_BADF;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-08-24 19:04:11 -04:00
|
|
|
case "Busy":
|
|
|
|
return ERRNO_BUSY;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-08-24 19:04:11 -04:00
|
|
|
default:
|
|
|
|
return ERRNO_INVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-06-24 09:27:31 -04:00
|
|
|
}
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
interface FileDescriptor {
|
|
|
|
rid?: number;
|
|
|
|
type?: number;
|
|
|
|
flags?: number;
|
|
|
|
path?: string;
|
|
|
|
vpath?: string;
|
|
|
|
entries?: Deno.DirEntry[];
|
|
|
|
}
|
|
|
|
|
2020-12-03 13:57:35 -05:00
|
|
|
export class ExitStatus {
|
|
|
|
code: number;
|
|
|
|
|
|
|
|
constructor(code: number) {
|
|
|
|
this.code = code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-24 12:21:06 -04:00
|
|
|
export interface ContextOptions {
|
2020-06-24 09:27:31 -04:00
|
|
|
args?: string[];
|
|
|
|
env?: { [key: string]: string | undefined };
|
|
|
|
preopens?: { [key: string]: string };
|
2020-12-03 13:57:35 -05:00
|
|
|
exitOnReturn?: boolean;
|
2020-06-24 09:27:31 -04:00
|
|
|
}
|
|
|
|
|
2020-08-24 12:21:06 -04:00
|
|
|
export default class Context {
|
2020-06-24 09:27:31 -04:00
|
|
|
args: string[];
|
|
|
|
env: { [key: string]: string | undefined };
|
2020-12-03 13:57:35 -05:00
|
|
|
exitOnReturn: boolean;
|
2020-06-24 09:27:31 -04:00
|
|
|
memory: WebAssembly.Memory;
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
fds: FileDescriptor[];
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
exports: Record<string, WebAssembly.ImportValue>;
|
2020-12-10 07:37:09 -05:00
|
|
|
#started: boolean;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-08-24 12:21:06 -04:00
|
|
|
constructor(options: ContextOptions) {
|
2020-12-04 07:53:16 -05:00
|
|
|
this.args = options.args ?? [];
|
|
|
|
this.env = options.env ?? {};
|
2020-12-03 13:57:35 -05:00
|
|
|
this.exitOnReturn = options.exitOnReturn ?? true;
|
2020-11-02 12:50:45 -05:00
|
|
|
this.memory = null!;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
|
|
|
this.fds = [
|
|
|
|
{
|
2020-09-29 05:41:22 -04:00
|
|
|
rid: Deno.stdin.rid,
|
2020-06-24 09:27:31 -04:00
|
|
|
type: FILETYPE_CHARACTER_DEVICE,
|
2020-09-29 05:41:22 -04:00
|
|
|
flags: FDFLAGS_APPEND,
|
2020-06-24 09:27:31 -04:00
|
|
|
},
|
|
|
|
{
|
2020-09-29 05:41:22 -04:00
|
|
|
rid: Deno.stdout.rid,
|
2020-06-24 09:27:31 -04:00
|
|
|
type: FILETYPE_CHARACTER_DEVICE,
|
2020-09-29 05:41:22 -04:00
|
|
|
flags: FDFLAGS_APPEND,
|
2020-06-24 09:27:31 -04:00
|
|
|
},
|
|
|
|
{
|
2020-09-29 05:41:22 -04:00
|
|
|
rid: Deno.stderr.rid,
|
2020-06-24 09:27:31 -04:00
|
|
|
type: FILETYPE_CHARACTER_DEVICE,
|
2020-09-29 05:41:22 -04:00
|
|
|
flags: FDFLAGS_APPEND,
|
2020-06-24 09:27:31 -04:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
if (options.preopens) {
|
|
|
|
for (const [vpath, path] of Object.entries(options.preopens)) {
|
|
|
|
const type = FILETYPE_DIRECTORY;
|
2020-07-05 18:08:05 -04:00
|
|
|
const entries = Array.from(Deno.readDirSync(path));
|
2020-06-24 09:27:31 -04:00
|
|
|
|
|
|
|
const entry = {
|
|
|
|
type,
|
2020-07-05 18:08:05 -04:00
|
|
|
entries,
|
2020-06-24 09:27:31 -04:00
|
|
|
path,
|
|
|
|
vpath,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.fds.push(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.exports = {
|
2020-09-29 05:41:22 -04:00
|
|
|
"args_get": syscall((
|
|
|
|
argvOffset: number,
|
|
|
|
argvBufferOffset: number,
|
|
|
|
): number => {
|
2020-06-24 09:27:31 -04:00
|
|
|
const args = this.args;
|
2020-12-05 09:47:01 -05:00
|
|
|
const textEncoder = new TextEncoder();
|
2020-09-29 05:41:22 -04:00
|
|
|
const memoryData = new Uint8Array(this.memory.buffer);
|
|
|
|
const memoryView = new DataView(this.memory.buffer);
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
for (const arg of args) {
|
|
|
|
memoryView.setUint32(argvOffset, argvBufferOffset, true);
|
|
|
|
argvOffset += 4;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-12-05 09:47:01 -05:00
|
|
|
const data = textEncoder.encode(`${arg}\0`);
|
2020-09-29 05:41:22 -04:00
|
|
|
memoryData.set(data, argvBufferOffset);
|
|
|
|
argvBufferOffset += data.length;
|
2020-06-24 09:27:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return ERRNO_SUCCESS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"args_sizes_get": syscall((
|
|
|
|
argcOffset: number,
|
|
|
|
argvBufferSizeOffset: number,
|
|
|
|
): number => {
|
|
|
|
const args = this.args;
|
2020-12-05 09:47:01 -05:00
|
|
|
const textEncoder = new TextEncoder();
|
2020-09-29 05:41:22 -04:00
|
|
|
const memoryView = new DataView(this.memory.buffer);
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
memoryView.setUint32(argcOffset, args.length, true);
|
|
|
|
memoryView.setUint32(
|
|
|
|
argvBufferSizeOffset,
|
|
|
|
args.reduce(function (acc, arg) {
|
2020-12-05 09:47:01 -05:00
|
|
|
return acc + textEncoder.encode(`${arg}\0`).length;
|
2020-09-29 05:41:22 -04:00
|
|
|
}, 0),
|
|
|
|
true,
|
|
|
|
);
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
return ERRNO_SUCCESS;
|
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"environ_get": syscall((
|
|
|
|
environOffset: number,
|
|
|
|
environBufferOffset: number,
|
2020-06-24 09:27:31 -04:00
|
|
|
): number => {
|
|
|
|
const entries = Object.entries(this.env);
|
2020-12-05 09:47:01 -05:00
|
|
|
const textEncoder = new TextEncoder();
|
2020-09-29 05:41:22 -04:00
|
|
|
const memoryData = new Uint8Array(this.memory.buffer);
|
|
|
|
const memoryView = new DataView(this.memory.buffer);
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
for (const [key, value] of entries) {
|
|
|
|
memoryView.setUint32(environOffset, environBufferOffset, true);
|
|
|
|
environOffset += 4;
|
|
|
|
|
2020-12-05 09:47:01 -05:00
|
|
|
const data = textEncoder.encode(`${key}=${value}\0`);
|
2020-09-29 05:41:22 -04:00
|
|
|
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);
|
2020-12-05 09:47:01 -05:00
|
|
|
const textEncoder = new TextEncoder();
|
2020-09-29 05:41:22 -04:00
|
|
|
const memoryView = new DataView(this.memory.buffer);
|
|
|
|
|
|
|
|
memoryView.setUint32(environcOffset, entries.length, true);
|
|
|
|
memoryView.setUint32(
|
|
|
|
environBufferSizeOffset,
|
2020-06-24 09:27:31 -04:00
|
|
|
entries.reduce(function (acc, [key, value]) {
|
2020-12-05 09:47:01 -05:00
|
|
|
return acc + textEncoder.encode(`${key}=${value}\0`).length;
|
2020-06-24 09:27:31 -04:00
|
|
|
}, 0),
|
2020-07-14 15:24:17 -04:00
|
|
|
true,
|
2020-06-24 09:27:31 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
return ERRNO_SUCCESS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"clock_res_get": syscall((
|
|
|
|
id: number,
|
|
|
|
resolutionOffset: number,
|
|
|
|
): number => {
|
|
|
|
const memoryView = new DataView(this.memory.buffer);
|
2020-06-24 09:27:31 -04:00
|
|
|
|
|
|
|
switch (id) {
|
2020-09-29 05:41:22 -04:00
|
|
|
case CLOCKID_REALTIME: {
|
|
|
|
const resolution = BigInt(1e6);
|
|
|
|
|
|
|
|
memoryView.setBigUint64(
|
|
|
|
resolutionOffset,
|
|
|
|
resolution,
|
|
|
|
true,
|
|
|
|
);
|
2020-06-24 09:27:31 -04:00
|
|
|
break;
|
2020-09-29 05:41:22 -04:00
|
|
|
}
|
2020-06-24 09:27:31 -04:00
|
|
|
|
|
|
|
case CLOCKID_MONOTONIC:
|
|
|
|
case CLOCKID_PROCESS_CPUTIME_ID:
|
2020-09-29 05:41:22 -04:00
|
|
|
case CLOCKID_THREAD_CPUTIME_ID: {
|
|
|
|
const resolution = BigInt(1e3);
|
|
|
|
memoryView.setBigUint64(resolutionOffset, resolution, true);
|
2020-06-24 09:27:31 -04:00
|
|
|
break;
|
2020-09-29 05:41:22 -04:00
|
|
|
}
|
2020-06-24 09:27:31 -04:00
|
|
|
|
|
|
|
default:
|
|
|
|
return ERRNO_INVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERRNO_SUCCESS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"clock_time_get": syscall((
|
2020-06-24 09:27:31 -04:00
|
|
|
id: number,
|
|
|
|
precision: bigint,
|
2020-09-29 05:41:22 -04:00
|
|
|
timeOffset: number,
|
2020-06-24 09:27:31 -04:00
|
|
|
): number => {
|
2020-09-29 05:41:22 -04:00
|
|
|
const memoryView = new DataView(this.memory.buffer);
|
2020-06-24 09:27:31 -04:00
|
|
|
|
|
|
|
switch (id) {
|
2020-09-29 05:41:22 -04:00
|
|
|
case CLOCKID_REALTIME: {
|
|
|
|
const time = BigInt(Date.now()) * BigInt(1e6);
|
|
|
|
memoryView.setBigUint64(timeOffset, time, true);
|
2020-06-24 09:27:31 -04:00
|
|
|
break;
|
2020-09-29 05:41:22 -04:00
|
|
|
}
|
2020-06-24 09:27:31 -04:00
|
|
|
|
|
|
|
case CLOCKID_MONOTONIC:
|
|
|
|
case CLOCKID_PROCESS_CPUTIME_ID:
|
2020-09-29 05:41:22 -04:00
|
|
|
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);
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
memoryView.setBigUint64(timeOffset, time, true);
|
2020-06-24 09:27:31 -04:00
|
|
|
break;
|
2020-09-29 05:41:22 -04:00
|
|
|
}
|
2020-06-24 09:27:31 -04:00
|
|
|
|
|
|
|
default:
|
|
|
|
return ERRNO_INVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERRNO_SUCCESS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"fd_advise": syscall((
|
|
|
|
_fd: number,
|
|
|
|
_offset: bigint,
|
|
|
|
_length: bigint,
|
|
|
|
_advice: number,
|
2020-06-24 09:27:31 -04:00
|
|
|
): number => {
|
|
|
|
return ERRNO_NOSYS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"fd_allocate": syscall((
|
|
|
|
_fd: number,
|
|
|
|
_offset: bigint,
|
|
|
|
_length: bigint,
|
|
|
|
): number => {
|
|
|
|
return ERRNO_NOSYS;
|
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"fd_close": syscall((
|
|
|
|
fd: number,
|
|
|
|
): number => {
|
2020-06-24 09:27:31 -04:00
|
|
|
const entry = this.fds[fd];
|
|
|
|
if (!entry) {
|
|
|
|
return ERRNO_BADF;
|
|
|
|
}
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
if (entry.rid) {
|
|
|
|
Deno.close(entry.rid);
|
2020-07-05 18:08:05 -04:00
|
|
|
}
|
|
|
|
|
2020-06-24 09:27:31 -04:00
|
|
|
delete this.fds[fd];
|
|
|
|
|
|
|
|
return ERRNO_SUCCESS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"fd_datasync": syscall((
|
|
|
|
fd: number,
|
|
|
|
): number => {
|
2020-06-29 10:44:08 -04:00
|
|
|
const entry = this.fds[fd];
|
|
|
|
if (!entry) {
|
|
|
|
return ERRNO_BADF;
|
|
|
|
}
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
Deno.fdatasyncSync(entry.rid!);
|
2020-06-29 10:44:08 -04:00
|
|
|
|
|
|
|
return ERRNO_SUCCESS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"fd_fdstat_get": syscall((
|
|
|
|
fd: number,
|
|
|
|
offset: number,
|
|
|
|
): number => {
|
2020-06-24 09:27:31 -04:00
|
|
|
const entry = this.fds[fd];
|
|
|
|
if (!entry) {
|
|
|
|
return ERRNO_BADF;
|
|
|
|
}
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
const memoryView = new DataView(this.memory.buffer);
|
|
|
|
memoryView.setUint8(offset, entry.type!);
|
|
|
|
memoryView.setUint16(offset + 2, entry.flags!, true);
|
|
|
|
memoryView.setBigUint64(offset + 8, 0n, true); // TODO
|
|
|
|
memoryView.setBigUint64(offset + 16, 0n, true); // TODO
|
2020-06-24 09:27:31 -04:00
|
|
|
|
|
|
|
return ERRNO_SUCCESS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"fd_fdstat_set_flags": syscall((
|
|
|
|
_fd: number,
|
|
|
|
_flags: number,
|
|
|
|
): number => {
|
2020-06-24 09:27:31 -04:00
|
|
|
return ERRNO_NOSYS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"fd_fdstat_set_rights": syscall((
|
|
|
|
_fd: number,
|
|
|
|
_rightsBase: bigint,
|
|
|
|
_rightsInheriting: bigint,
|
2020-06-24 09:27:31 -04:00
|
|
|
): number => {
|
|
|
|
return ERRNO_NOSYS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"fd_filestat_get": syscall((
|
|
|
|
fd: number,
|
|
|
|
offset: number,
|
|
|
|
): number => {
|
2020-06-29 09:34:18 -04:00
|
|
|
const entry = this.fds[fd];
|
|
|
|
if (!entry) {
|
|
|
|
return ERRNO_BADF;
|
|
|
|
}
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
const memoryView = new DataView(this.memory.buffer);
|
2020-06-29 09:34:18 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
const info = Deno.fstatSync(entry.rid!);
|
2020-06-29 09:34:18 -04:00
|
|
|
|
2020-08-24 19:04:11 -04:00
|
|
|
if (entry.type === undefined) {
|
|
|
|
switch (true) {
|
|
|
|
case info.isFile:
|
|
|
|
entry.type = FILETYPE_REGULAR_FILE;
|
|
|
|
break;
|
2020-06-29 09:34:18 -04:00
|
|
|
|
2020-08-24 19:04:11 -04:00
|
|
|
case info.isDirectory:
|
|
|
|
entry.type = FILETYPE_DIRECTORY;
|
|
|
|
break;
|
2020-06-29 09:34:18 -04:00
|
|
|
|
2020-08-24 19:04:11 -04:00
|
|
|
case info.isSymlink:
|
|
|
|
entry.type = FILETYPE_SYMBOLIC_LINK;
|
|
|
|
break;
|
2020-06-29 09:34:18 -04:00
|
|
|
|
2020-08-24 19:04:11 -04:00
|
|
|
default:
|
|
|
|
entry.type = FILETYPE_UNKNOWN;
|
|
|
|
break;
|
2020-06-29 09:34:18 -04:00
|
|
|
}
|
2020-08-24 19:04:11 -04:00
|
|
|
}
|
2020-06-29 09:34:18 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
memoryView.setBigUint64(offset, BigInt(info.dev ? info.dev : 0), true);
|
|
|
|
offset += 8;
|
2020-06-29 09:34:18 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
memoryView.setBigUint64(offset, BigInt(info.ino ? info.ino : 0), true);
|
|
|
|
offset += 8;
|
2020-06-29 09:34:18 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
memoryView.setUint8(offset, entry.type);
|
|
|
|
offset += 8;
|
2020-06-29 09:34:18 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
memoryView.setUint32(offset, Number(info.nlink), true);
|
|
|
|
offset += 8;
|
2020-06-29 09:34:18 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
memoryView.setBigUint64(offset, BigInt(info.size), true);
|
|
|
|
offset += 8;
|
2020-06-29 09:34:18 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
memoryView.setBigUint64(
|
|
|
|
offset,
|
2020-08-24 19:04:11 -04:00
|
|
|
BigInt(info.atime ? info.atime.getTime() * 1e6 : 0),
|
|
|
|
true,
|
|
|
|
);
|
2020-09-29 05:41:22 -04:00
|
|
|
offset += 8;
|
2020-06-29 09:34:18 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
memoryView.setBigUint64(
|
|
|
|
offset,
|
2020-08-24 19:04:11 -04:00
|
|
|
BigInt(info.mtime ? info.mtime.getTime() * 1e6 : 0),
|
|
|
|
true,
|
|
|
|
);
|
2020-09-29 05:41:22 -04:00
|
|
|
offset += 8;
|
2020-06-29 09:34:18 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
memoryView.setBigUint64(
|
|
|
|
offset,
|
2020-08-24 19:04:11 -04:00
|
|
|
BigInt(info.birthtime ? info.birthtime.getTime() * 1e6 : 0),
|
|
|
|
true,
|
|
|
|
);
|
2020-09-29 05:41:22 -04:00
|
|
|
offset += 8;
|
2020-06-29 09:34:18 -04:00
|
|
|
|
|
|
|
return ERRNO_SUCCESS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"fd_filestat_set_size": syscall((
|
|
|
|
fd: number,
|
|
|
|
size: bigint,
|
|
|
|
): number => {
|
2020-06-29 14:36:22 -04:00
|
|
|
const entry = this.fds[fd];
|
|
|
|
if (!entry) {
|
|
|
|
return ERRNO_BADF;
|
|
|
|
}
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
Deno.ftruncateSync(entry.rid!, Number(size));
|
2020-06-29 14:36:22 -04:00
|
|
|
|
|
|
|
return ERRNO_SUCCESS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"fd_filestat_set_times": syscall((
|
2020-06-24 09:27:31 -04:00
|
|
|
fd: number,
|
|
|
|
atim: bigint,
|
|
|
|
mtim: bigint,
|
2020-09-29 05:41:22 -04:00
|
|
|
flags: number,
|
2020-06-24 09:27:31 -04:00
|
|
|
): number => {
|
|
|
|
const entry = this.fds[fd];
|
|
|
|
if (!entry) {
|
|
|
|
return ERRNO_BADF;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!entry.path) {
|
|
|
|
return ERRNO_INVAL;
|
|
|
|
}
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
if ((flags & FSTFLAGS_ATIM_NOW) == FSTFLAGS_ATIM_NOW) {
|
2020-06-24 09:27:31 -04:00
|
|
|
atim = BigInt(Date.now() * 1e6);
|
|
|
|
}
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
if ((flags & FSTFLAGS_MTIM_NOW) == FSTFLAGS_MTIM_NOW) {
|
2020-06-24 09:27:31 -04:00
|
|
|
mtim = BigInt(Date.now() * 1e6);
|
|
|
|
}
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
Deno.utimeSync(entry.path!, Number(atim), Number(mtim));
|
2020-06-24 09:27:31 -04:00
|
|
|
|
|
|
|
return ERRNO_SUCCESS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"fd_pread": syscall((
|
2020-06-24 09:27:31 -04:00
|
|
|
fd: number,
|
2020-09-29 05:41:22 -04:00
|
|
|
iovsOffset: number,
|
|
|
|
iovsLength: number,
|
2020-06-24 09:27:31 -04:00
|
|
|
offset: bigint,
|
2020-09-29 05:41:22 -04:00
|
|
|
nreadOffset: number,
|
2020-06-24 09:27:31 -04:00
|
|
|
): number => {
|
|
|
|
const entry = this.fds[fd];
|
2020-09-29 05:41:22 -04:00
|
|
|
if (entry == null) {
|
2020-06-24 09:27:31 -04:00
|
|
|
return ERRNO_BADF;
|
|
|
|
}
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
const seek = Deno.seekSync(entry.rid!, 0, Deno.SeekMode.Current);
|
|
|
|
const memoryView = new DataView(this.memory.buffer);
|
2020-06-24 09:27:31 -04:00
|
|
|
|
|
|
|
let nread = 0;
|
2020-09-29 05:41:22 -04:00
|
|
|
for (let i = 0; i < iovsLength; i++) {
|
|
|
|
const dataOffset = memoryView.getUint32(iovsOffset, true);
|
|
|
|
iovsOffset += 4;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
const dataLength = memoryView.getUint32(iovsOffset, true);
|
|
|
|
iovsOffset += 4;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
const data = new Uint8Array(
|
|
|
|
this.memory.buffer,
|
|
|
|
dataOffset,
|
|
|
|
dataLength,
|
|
|
|
);
|
|
|
|
nread += Deno.readSync(entry.rid!, data) as number;
|
2020-06-24 09:27:31 -04:00
|
|
|
}
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
Deno.seekSync(entry.rid!, seek, Deno.SeekMode.Start);
|
|
|
|
memoryView.setUint32(nreadOffset, nread, true);
|
2020-06-24 09:27:31 -04:00
|
|
|
|
|
|
|
return ERRNO_SUCCESS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"fd_prestat_get": syscall((
|
|
|
|
fd: number,
|
|
|
|
prestatOffset: number,
|
|
|
|
): number => {
|
2020-06-24 09:27:31 -04:00
|
|
|
const entry = this.fds[fd];
|
|
|
|
if (!entry) {
|
|
|
|
return ERRNO_BADF;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!entry.vpath) {
|
|
|
|
return ERRNO_BADF;
|
|
|
|
}
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
const memoryView = new DataView(this.memory.buffer);
|
|
|
|
memoryView.setUint8(prestatOffset, PREOPENTYPE_DIR);
|
|
|
|
memoryView.setUint32(
|
|
|
|
prestatOffset + 4,
|
2020-06-24 09:27:31 -04:00
|
|
|
new TextEncoder().encode(entry.vpath).byteLength,
|
2020-07-14 15:24:17 -04:00
|
|
|
true,
|
2020-06-24 09:27:31 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
return ERRNO_SUCCESS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"fd_prestat_dir_name": syscall((
|
2020-06-24 09:27:31 -04:00
|
|
|
fd: number,
|
2020-09-29 05:41:22 -04:00
|
|
|
pathOffset: number,
|
|
|
|
pathLength: number,
|
2020-06-24 09:27:31 -04:00
|
|
|
): number => {
|
|
|
|
const entry = this.fds[fd];
|
|
|
|
if (!entry) {
|
|
|
|
return ERRNO_BADF;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!entry.vpath) {
|
|
|
|
return ERRNO_BADF;
|
|
|
|
}
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
const data = new Uint8Array(this.memory.buffer, pathOffset, pathLength);
|
2020-06-24 09:27:31 -04:00
|
|
|
data.set(new TextEncoder().encode(entry.vpath));
|
|
|
|
|
|
|
|
return ERRNO_SUCCESS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"fd_pwrite": syscall((
|
2020-06-24 09:27:31 -04:00
|
|
|
fd: number,
|
2020-09-29 05:41:22 -04:00
|
|
|
iovsOffset: number,
|
|
|
|
iovsLength: number,
|
2020-06-24 09:27:31 -04:00
|
|
|
offset: bigint,
|
2020-09-29 05:41:22 -04:00
|
|
|
nwrittenOffset: number,
|
2020-06-24 09:27:31 -04:00
|
|
|
): number => {
|
|
|
|
const entry = this.fds[fd];
|
|
|
|
if (!entry) {
|
|
|
|
return ERRNO_BADF;
|
|
|
|
}
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
const seek = Deno.seekSync(entry.rid!, 0, Deno.SeekMode.Current);
|
|
|
|
const memoryView = new DataView(this.memory.buffer);
|
2020-06-24 09:27:31 -04:00
|
|
|
|
|
|
|
let nwritten = 0;
|
2020-09-29 05:41:22 -04:00
|
|
|
for (let i = 0; i < iovsLength; i++) {
|
|
|
|
const dataOffset = memoryView.getUint32(iovsOffset, true);
|
|
|
|
iovsOffset += 4;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
const dataLength = memoryView.getUint32(iovsOffset, true);
|
|
|
|
iovsOffset += 4;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
const data = new Uint8Array(
|
|
|
|
this.memory.buffer,
|
|
|
|
dataOffset,
|
|
|
|
dataLength,
|
|
|
|
);
|
|
|
|
nwritten += Deno.writeSync(entry.rid!, data) as number;
|
2020-06-24 09:27:31 -04:00
|
|
|
}
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
Deno.seekSync(entry.rid!, seek, Deno.SeekMode.Start);
|
|
|
|
memoryView.setUint32(nwrittenOffset, nwritten, true);
|
2020-06-24 09:27:31 -04:00
|
|
|
|
|
|
|
return ERRNO_SUCCESS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"fd_read": syscall((
|
2020-06-24 09:27:31 -04:00
|
|
|
fd: number,
|
2020-09-29 05:41:22 -04:00
|
|
|
iovsOffset: number,
|
|
|
|
iovsLength: number,
|
|
|
|
nreadOffset: number,
|
2020-06-24 09:27:31 -04:00
|
|
|
): number => {
|
|
|
|
const entry = this.fds[fd];
|
|
|
|
if (!entry) {
|
|
|
|
return ERRNO_BADF;
|
|
|
|
}
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
const memoryView = new DataView(this.memory.buffer);
|
2020-06-24 09:27:31 -04:00
|
|
|
|
|
|
|
let nread = 0;
|
2020-09-29 05:41:22 -04:00
|
|
|
for (let i = 0; i < iovsLength; i++) {
|
|
|
|
const dataOffset = memoryView.getUint32(iovsOffset, true);
|
|
|
|
iovsOffset += 4;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
const dataLength = memoryView.getUint32(iovsOffset, true);
|
|
|
|
iovsOffset += 4;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
const data = new Uint8Array(
|
|
|
|
this.memory.buffer,
|
|
|
|
dataOffset,
|
|
|
|
dataLength,
|
|
|
|
);
|
|
|
|
nread += Deno.readSync(entry.rid!, data) as number;
|
2020-06-24 09:27:31 -04:00
|
|
|
}
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
memoryView.setUint32(nreadOffset, nread, true);
|
2020-06-24 09:27:31 -04:00
|
|
|
|
|
|
|
return ERRNO_SUCCESS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"fd_readdir": syscall((
|
2020-06-24 09:27:31 -04:00
|
|
|
fd: number,
|
2020-09-29 05:41:22 -04:00
|
|
|
bufferOffset: number,
|
|
|
|
bufferLength: number,
|
2020-06-24 09:27:31 -04:00
|
|
|
cookie: bigint,
|
2020-09-29 05:41:22 -04:00
|
|
|
bufferUsedOffset: number,
|
2020-06-24 09:27:31 -04:00
|
|
|
): number => {
|
2020-07-05 18:08:05 -04:00
|
|
|
const entry = this.fds[fd];
|
|
|
|
if (!entry) {
|
|
|
|
return ERRNO_BADF;
|
|
|
|
}
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
const memoryData = new Uint8Array(this.memory.buffer);
|
|
|
|
const memoryView = new DataView(this.memory.buffer);
|
2020-07-05 18:08:05 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
let bufferUsed = 0;
|
2020-07-05 18:08:05 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
const entries = Array.from(Deno.readDirSync(entry.path!));
|
2020-08-24 19:04:11 -04:00
|
|
|
for (let i = Number(cookie); i < entries.length; i++) {
|
2020-09-29 05:41:22 -04:00
|
|
|
const nameData = new TextEncoder().encode(entries[i].name);
|
2020-08-24 19:04:11 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
const entryInfo = Deno.statSync(
|
|
|
|
resolve(entry.path!, entries[i].name),
|
2020-08-24 19:04:11 -04:00
|
|
|
);
|
2020-09-29 05:41:22 -04:00
|
|
|
const entryData = new Uint8Array(24 + nameData.byteLength);
|
|
|
|
const entryView = new DataView(entryData.buffer);
|
2020-08-24 19:04:11 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
entryView.setBigUint64(0, BigInt(i + 1), true);
|
|
|
|
entryView.setBigUint64(
|
2020-08-24 19:04:11 -04:00
|
|
|
8,
|
2020-09-29 05:41:22 -04:00
|
|
|
BigInt(entryInfo.ino ? entryInfo.ino : 0),
|
2020-08-24 19:04:11 -04:00
|
|
|
true,
|
|
|
|
);
|
2020-09-29 05:41:22 -04:00
|
|
|
entryView.setUint32(16, nameData.byteLength, true);
|
2020-08-24 19:04:11 -04:00
|
|
|
|
2020-08-31 11:13:55 -04:00
|
|
|
let type: number;
|
2020-08-24 19:04:11 -04:00
|
|
|
switch (true) {
|
|
|
|
case entries[i].isFile:
|
2020-08-31 11:13:55 -04:00
|
|
|
type = FILETYPE_REGULAR_FILE;
|
2020-08-24 19:04:11 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case entries[i].isDirectory:
|
2020-08-31 11:13:55 -04:00
|
|
|
type = FILETYPE_REGULAR_FILE;
|
2020-08-24 19:04:11 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case entries[i].isSymlink:
|
2020-08-31 11:13:55 -04:00
|
|
|
type = FILETYPE_SYMBOLIC_LINK;
|
2020-08-24 19:04:11 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2020-08-31 11:13:55 -04:00
|
|
|
type = FILETYPE_REGULAR_FILE;
|
2020-08-24 19:04:11 -04:00
|
|
|
break;
|
2020-07-05 18:08:05 -04:00
|
|
|
}
|
2020-08-24 19:04:11 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
entryView.setUint8(20, type);
|
|
|
|
entryData.set(nameData, 24);
|
2020-08-24 19:04:11 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
const data = entryData.slice(
|
2020-08-24 19:04:11 -04:00
|
|
|
0,
|
2020-09-29 05:41:22 -04:00
|
|
|
Math.min(entryData.length, bufferLength - bufferUsed),
|
2020-08-24 19:04:11 -04:00
|
|
|
);
|
2020-09-29 05:41:22 -04:00
|
|
|
memoryData.set(data, bufferOffset + bufferUsed);
|
|
|
|
bufferUsed += data.byteLength;
|
2020-07-05 18:08:05 -04:00
|
|
|
}
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
memoryView.setUint32(bufferUsedOffset, bufferUsed, true);
|
2020-07-05 18:08:05 -04:00
|
|
|
|
|
|
|
return ERRNO_SUCCESS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"fd_renumber": syscall((
|
|
|
|
fd: number,
|
|
|
|
to: number,
|
|
|
|
): number => {
|
2020-06-24 09:27:31 -04:00
|
|
|
if (!this.fds[fd]) {
|
|
|
|
return ERRNO_BADF;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.fds[to]) {
|
|
|
|
return ERRNO_BADF;
|
|
|
|
}
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
if (this.fds[to].rid) {
|
|
|
|
Deno.close(this.fds[to].rid!);
|
|
|
|
}
|
|
|
|
|
2020-06-24 09:27:31 -04:00
|
|
|
this.fds[to] = this.fds[fd];
|
|
|
|
delete this.fds[fd];
|
|
|
|
|
|
|
|
return ERRNO_SUCCESS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"fd_seek": syscall((
|
2020-06-24 09:27:31 -04:00
|
|
|
fd: number,
|
|
|
|
offset: bigint,
|
|
|
|
whence: number,
|
2020-09-29 05:41:22 -04:00
|
|
|
newOffsetOffset: number,
|
2020-06-24 09:27:31 -04:00
|
|
|
): number => {
|
|
|
|
const entry = this.fds[fd];
|
|
|
|
if (!entry) {
|
|
|
|
return ERRNO_BADF;
|
|
|
|
}
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
const memoryView = new DataView(this.memory.buffer);
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-08-24 19:04:11 -04:00
|
|
|
// FIXME Deno does not support seeking with big integers
|
2020-09-29 05:41:22 -04:00
|
|
|
const newOffset = Deno.seekSync(entry.rid!, Number(offset), whence);
|
|
|
|
memoryView.setBigUint64(newOffsetOffset, BigInt(newOffset), true);
|
2020-06-24 09:27:31 -04:00
|
|
|
|
|
|
|
return ERRNO_SUCCESS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"fd_sync": syscall((
|
|
|
|
fd: number,
|
|
|
|
): number => {
|
2020-06-29 17:45:39 -04:00
|
|
|
const entry = this.fds[fd];
|
|
|
|
if (!entry) {
|
|
|
|
return ERRNO_BADF;
|
|
|
|
}
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
Deno.fsyncSync(entry.rid!);
|
2020-06-29 17:45:39 -04:00
|
|
|
|
|
|
|
return ERRNO_SUCCESS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"fd_tell": syscall((
|
|
|
|
fd: number,
|
|
|
|
offsetOffset: number,
|
|
|
|
): number => {
|
2020-06-24 09:27:31 -04:00
|
|
|
const entry = this.fds[fd];
|
|
|
|
if (!entry) {
|
|
|
|
return ERRNO_BADF;
|
|
|
|
}
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
const memoryView = new DataView(this.memory.buffer);
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
const offset = Deno.seekSync(entry.rid!, 0, Deno.SeekMode.Current);
|
|
|
|
memoryView.setBigUint64(offsetOffset, BigInt(offset), true);
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-07-05 21:38:48 -04:00
|
|
|
return ERRNO_SUCCESS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"fd_write": syscall((
|
2020-06-24 09:27:31 -04:00
|
|
|
fd: number,
|
2020-09-29 05:41:22 -04:00
|
|
|
iovsOffset: number,
|
|
|
|
iovsLength: number,
|
|
|
|
nwrittenOffset: number,
|
2020-06-24 09:27:31 -04:00
|
|
|
): number => {
|
|
|
|
const entry = this.fds[fd];
|
|
|
|
if (!entry) {
|
|
|
|
return ERRNO_BADF;
|
|
|
|
}
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
const memoryView = new DataView(this.memory.buffer);
|
2020-06-24 09:27:31 -04:00
|
|
|
|
|
|
|
let nwritten = 0;
|
2020-09-29 05:41:22 -04:00
|
|
|
for (let i = 0; i < iovsLength; i++) {
|
|
|
|
const dataOffset = memoryView.getUint32(iovsOffset, true);
|
|
|
|
iovsOffset += 4;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
const dataLength = memoryView.getUint32(iovsOffset, true);
|
|
|
|
iovsOffset += 4;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
const data = new Uint8Array(
|
|
|
|
this.memory.buffer,
|
|
|
|
dataOffset,
|
|
|
|
dataLength,
|
2020-06-24 09:27:31 -04:00
|
|
|
);
|
2020-09-29 05:41:22 -04:00
|
|
|
nwritten += Deno.writeSync(entry.rid!, data) as number;
|
2020-06-24 09:27:31 -04:00
|
|
|
}
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
memoryView.setUint32(nwrittenOffset, nwritten, true);
|
2020-06-24 09:27:31 -04:00
|
|
|
|
|
|
|
return ERRNO_SUCCESS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"path_create_directory": syscall((
|
2020-06-24 09:27:31 -04:00
|
|
|
fd: number,
|
2020-09-29 05:41:22 -04:00
|
|
|
pathOffset: number,
|
|
|
|
pathLength: number,
|
2020-06-24 09:27:31 -04:00
|
|
|
): number => {
|
|
|
|
const entry = this.fds[fd];
|
|
|
|
if (!entry) {
|
|
|
|
return ERRNO_BADF;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!entry.path) {
|
|
|
|
return ERRNO_INVAL;
|
|
|
|
}
|
|
|
|
|
2020-12-05 09:47:01 -05:00
|
|
|
const textDecoder = new TextDecoder();
|
2020-09-29 05:41:22 -04:00
|
|
|
const data = new Uint8Array(this.memory.buffer, pathOffset, pathLength);
|
2020-12-05 09:47:01 -05:00
|
|
|
const path = resolve(entry.path!, textDecoder.decode(data));
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-08-24 19:04:11 -04:00
|
|
|
Deno.mkdirSync(path);
|
2020-06-24 09:27:31 -04:00
|
|
|
|
|
|
|
return ERRNO_SUCCESS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"path_filestat_get": syscall((
|
2020-06-24 09:27:31 -04:00
|
|
|
fd: number,
|
|
|
|
flags: number,
|
2020-09-29 05:41:22 -04:00
|
|
|
pathOffset: number,
|
|
|
|
pathLength: number,
|
|
|
|
bufferOffset: number,
|
2020-06-24 09:27:31 -04:00
|
|
|
): number => {
|
|
|
|
const entry = this.fds[fd];
|
|
|
|
if (!entry) {
|
|
|
|
return ERRNO_BADF;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!entry.path) {
|
|
|
|
return ERRNO_INVAL;
|
|
|
|
}
|
|
|
|
|
2020-12-05 09:47:01 -05:00
|
|
|
const textDecoder = new TextDecoder();
|
2020-09-29 05:41:22 -04:00
|
|
|
const data = new Uint8Array(this.memory.buffer, pathOffset, pathLength);
|
2020-12-05 09:47:01 -05:00
|
|
|
const path = resolve(entry.path!, textDecoder.decode(data));
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
const memoryView = new DataView(this.memory.buffer);
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-08-24 19:04:11 -04:00
|
|
|
const info = (flags & LOOKUPFLAGS_SYMLINK_FOLLOW) != 0
|
|
|
|
? Deno.statSync(path)
|
|
|
|
: Deno.lstatSync(path);
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
memoryView.setBigUint64(
|
|
|
|
bufferOffset,
|
|
|
|
BigInt(info.dev ? info.dev : 0),
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
bufferOffset += 8;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
memoryView.setBigUint64(
|
|
|
|
bufferOffset,
|
|
|
|
BigInt(info.ino ? info.ino : 0),
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
bufferOffset += 8;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-08-24 19:04:11 -04:00
|
|
|
switch (true) {
|
|
|
|
case info.isFile:
|
2020-09-29 05:41:22 -04:00
|
|
|
memoryView.setUint8(bufferOffset, FILETYPE_REGULAR_FILE);
|
|
|
|
bufferOffset += 8;
|
2020-08-24 19:04:11 -04:00
|
|
|
break;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-08-24 19:04:11 -04:00
|
|
|
case info.isDirectory:
|
2020-09-29 05:41:22 -04:00
|
|
|
memoryView.setUint8(bufferOffset, FILETYPE_DIRECTORY);
|
|
|
|
bufferOffset += 8;
|
2020-08-24 19:04:11 -04:00
|
|
|
break;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-08-24 19:04:11 -04:00
|
|
|
case info.isSymlink:
|
2020-09-29 05:41:22 -04:00
|
|
|
memoryView.setUint8(bufferOffset, FILETYPE_SYMBOLIC_LINK);
|
|
|
|
bufferOffset += 8;
|
2020-08-24 19:04:11 -04:00
|
|
|
break;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-08-24 19:04:11 -04:00
|
|
|
default:
|
2020-09-29 05:41:22 -04:00
|
|
|
memoryView.setUint8(bufferOffset, FILETYPE_UNKNOWN);
|
|
|
|
bufferOffset += 8;
|
2020-08-24 19:04:11 -04:00
|
|
|
break;
|
|
|
|
}
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
memoryView.setUint32(bufferOffset, Number(info.nlink), true);
|
|
|
|
bufferOffset += 8;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
memoryView.setBigUint64(bufferOffset, BigInt(info.size), true);
|
|
|
|
bufferOffset += 8;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
memoryView.setBigUint64(
|
|
|
|
bufferOffset,
|
2020-08-24 19:04:11 -04:00
|
|
|
BigInt(info.atime ? info.atime.getTime() * 1e6 : 0),
|
|
|
|
true,
|
|
|
|
);
|
2020-09-29 05:41:22 -04:00
|
|
|
bufferOffset += 8;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
memoryView.setBigUint64(
|
|
|
|
bufferOffset,
|
2020-08-24 19:04:11 -04:00
|
|
|
BigInt(info.mtime ? info.mtime.getTime() * 1e6 : 0),
|
|
|
|
true,
|
|
|
|
);
|
2020-09-29 05:41:22 -04:00
|
|
|
bufferOffset += 8;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
memoryView.setBigUint64(
|
|
|
|
bufferOffset,
|
2020-08-24 19:04:11 -04:00
|
|
|
BigInt(info.birthtime ? info.birthtime.getTime() * 1e6 : 0),
|
|
|
|
true,
|
|
|
|
);
|
2020-09-29 05:41:22 -04:00
|
|
|
bufferOffset += 8;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
|
|
|
return ERRNO_SUCCESS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"path_filestat_set_times": syscall((
|
2020-06-24 09:27:31 -04:00
|
|
|
fd: number,
|
|
|
|
flags: number,
|
2020-09-29 05:41:22 -04:00
|
|
|
pathOffset: number,
|
|
|
|
pathLength: number,
|
2020-06-24 09:27:31 -04:00
|
|
|
atim: bigint,
|
|
|
|
mtim: bigint,
|
2020-09-29 05:41:22 -04:00
|
|
|
fstflags: number,
|
2020-06-24 09:27:31 -04:00
|
|
|
): number => {
|
|
|
|
const entry = this.fds[fd];
|
|
|
|
if (!entry) {
|
|
|
|
return ERRNO_BADF;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!entry.path) {
|
|
|
|
return ERRNO_INVAL;
|
|
|
|
}
|
|
|
|
|
2020-12-05 09:47:01 -05:00
|
|
|
const textDecoder = new TextDecoder();
|
2020-09-29 05:41:22 -04:00
|
|
|
const data = new Uint8Array(this.memory.buffer, pathOffset, pathLength);
|
2020-12-05 09:47:01 -05:00
|
|
|
const path = resolve(entry.path!, textDecoder.decode(data));
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
if ((fstflags & FSTFLAGS_ATIM_NOW) == FSTFLAGS_ATIM_NOW) {
|
2020-06-24 09:27:31 -04:00
|
|
|
atim = BigInt(Date.now()) * BigInt(1e6);
|
|
|
|
}
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
if ((fstflags & FSTFLAGS_MTIM_NOW) == FSTFLAGS_MTIM_NOW) {
|
2020-06-24 09:27:31 -04:00
|
|
|
mtim = BigInt(Date.now()) * BigInt(1e6);
|
|
|
|
}
|
|
|
|
|
2020-08-24 19:04:11 -04:00
|
|
|
Deno.utimeSync(path, Number(atim), Number(mtim));
|
2020-06-24 09:27:31 -04:00
|
|
|
|
|
|
|
return ERRNO_SUCCESS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"path_link": syscall((
|
|
|
|
oldFd: number,
|
|
|
|
oldFlags: number,
|
|
|
|
oldPathOffset: number,
|
|
|
|
oldPathLength: number,
|
|
|
|
newFd: number,
|
|
|
|
newPathOffset: number,
|
|
|
|
newPathLength: number,
|
2020-06-24 09:27:31 -04:00
|
|
|
): number => {
|
2020-09-29 05:41:22 -04:00
|
|
|
const oldEntry = this.fds[oldFd];
|
|
|
|
const newEntry = this.fds[newFd];
|
|
|
|
if (!oldEntry || !newEntry) {
|
2020-06-24 09:27:31 -04:00
|
|
|
return ERRNO_BADF;
|
|
|
|
}
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
if (!oldEntry.path || !newEntry.path) {
|
2020-06-24 09:27:31 -04:00
|
|
|
return ERRNO_INVAL;
|
|
|
|
}
|
|
|
|
|
2020-12-05 09:47:01 -05:00
|
|
|
const textDecoder = new TextDecoder();
|
2020-09-29 05:41:22 -04:00
|
|
|
const oldData = new Uint8Array(
|
2020-06-24 09:27:31 -04:00
|
|
|
this.memory.buffer,
|
2020-09-29 05:41:22 -04:00
|
|
|
oldPathOffset,
|
|
|
|
oldPathLength,
|
2020-06-24 09:27:31 -04:00
|
|
|
);
|
2020-12-05 09:47:01 -05:00
|
|
|
const oldPath = resolve(oldEntry.path!, textDecoder.decode(oldData));
|
2020-09-29 05:41:22 -04:00
|
|
|
const newData = new Uint8Array(
|
2020-06-24 09:27:31 -04:00
|
|
|
this.memory.buffer,
|
2020-09-29 05:41:22 -04:00
|
|
|
newPathOffset,
|
|
|
|
newPathLength,
|
2020-06-24 09:27:31 -04:00
|
|
|
);
|
2020-12-05 09:47:01 -05:00
|
|
|
const newPath = resolve(newEntry.path!, textDecoder.decode(newData));
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
Deno.linkSync(oldPath, newPath);
|
2020-06-24 09:27:31 -04:00
|
|
|
|
|
|
|
return ERRNO_SUCCESS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"path_open": syscall((
|
2020-06-24 09:27:31 -04:00
|
|
|
fd: number,
|
|
|
|
dirflags: number,
|
2020-09-29 05:41:22 -04:00
|
|
|
pathOffset: number,
|
|
|
|
pathLength: number,
|
2020-06-24 09:27:31 -04:00
|
|
|
oflags: number,
|
2020-09-29 05:41:22 -04:00
|
|
|
rightsBase: bigint,
|
|
|
|
rightsInheriting: bigint,
|
2020-06-24 09:27:31 -04:00
|
|
|
fdflags: number,
|
2020-09-29 05:41:22 -04:00
|
|
|
openedFdOffset: number,
|
2020-06-24 09:27:31 -04:00
|
|
|
): number => {
|
|
|
|
const entry = this.fds[fd];
|
|
|
|
if (!entry) {
|
|
|
|
return ERRNO_BADF;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!entry.path) {
|
|
|
|
return ERRNO_INVAL;
|
|
|
|
}
|
|
|
|
|
2020-10-24 10:04:59 -04:00
|
|
|
const textDecoder = new TextDecoder();
|
|
|
|
const pathData = new Uint8Array(
|
|
|
|
this.memory.buffer,
|
|
|
|
pathOffset,
|
|
|
|
pathLength,
|
|
|
|
);
|
|
|
|
const resolvedPath = resolve(entry.path!, textDecoder.decode(pathData));
|
|
|
|
|
|
|
|
if (relative(entry.path, resolvedPath).startsWith("..")) {
|
|
|
|
return ERRNO_NOTCAPABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
let path;
|
|
|
|
if (
|
|
|
|
(dirflags & LOOKUPFLAGS_SYMLINK_FOLLOW) == LOOKUPFLAGS_SYMLINK_FOLLOW
|
|
|
|
) {
|
|
|
|
try {
|
|
|
|
path = Deno.realPathSync(resolvedPath);
|
|
|
|
if (relative(entry.path, path).startsWith("..")) {
|
|
|
|
return ERRNO_NOTCAPABLE;
|
|
|
|
}
|
|
|
|
} catch (_err) {
|
|
|
|
path = resolvedPath;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
path = resolvedPath;
|
|
|
|
}
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-07-05 18:08:05 -04:00
|
|
|
if ((oflags & OFLAGS_DIRECTORY) !== 0) {
|
|
|
|
// XXX (caspervonb) this isn't ideal as we can't get a rid for the
|
|
|
|
// directory this way so there's no native fstat but Deno.open
|
|
|
|
// doesn't work with directories on windows so we'll have to work
|
|
|
|
// around it for now.
|
2020-08-24 19:04:11 -04:00
|
|
|
const entries = Array.from(Deno.readDirSync(path));
|
2020-09-29 05:41:22 -04:00
|
|
|
const openedFd = this.fds.push({
|
|
|
|
flags: fdflags,
|
2020-08-24 19:04:11 -04:00
|
|
|
path,
|
2020-09-29 05:41:22 -04:00
|
|
|
entries,
|
2020-08-24 19:04:11 -04:00
|
|
|
}) - 1;
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
const memoryView = new DataView(this.memory.buffer);
|
|
|
|
memoryView.setUint32(openedFdOffset, openedFd, true);
|
2020-07-05 18:08:05 -04:00
|
|
|
|
|
|
|
return ERRNO_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2020-06-24 09:27:31 -04:00
|
|
|
const options = {
|
|
|
|
read: false,
|
|
|
|
write: false,
|
|
|
|
append: false,
|
|
|
|
truncate: false,
|
|
|
|
create: false,
|
|
|
|
createNew: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
if ((oflags & OFLAGS_CREAT) !== 0) {
|
|
|
|
options.create = true;
|
|
|
|
options.write = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((oflags & OFLAGS_EXCL) !== 0) {
|
|
|
|
options.createNew = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((oflags & OFLAGS_TRUNC) !== 0) {
|
|
|
|
options.truncate = true;
|
|
|
|
options.write = true;
|
|
|
|
}
|
|
|
|
|
2020-07-16 11:01:49 -04:00
|
|
|
const read = (
|
|
|
|
RIGHTS_FD_READ |
|
|
|
|
RIGHTS_FD_READDIR
|
|
|
|
);
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
if ((rightsBase & read) != 0n) {
|
2020-06-24 09:27:31 -04:00
|
|
|
options.read = true;
|
|
|
|
}
|
|
|
|
|
2020-07-16 11:01:49 -04:00
|
|
|
const write = (
|
|
|
|
RIGHTS_FD_DATASYNC |
|
|
|
|
RIGHTS_FD_WRITE |
|
|
|
|
RIGHTS_FD_ALLOCATE |
|
|
|
|
RIGHTS_FD_FILESTAT_SET_SIZE
|
|
|
|
);
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
if ((rightsBase & write) != 0n) {
|
2020-06-24 09:27:31 -04:00
|
|
|
options.write = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((fdflags & FDFLAGS_APPEND) != 0) {
|
|
|
|
options.append = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((fdflags & FDFLAGS_DSYNC) != 0) {
|
|
|
|
// TODO (caspervonb) review if we can emulate this.
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((fdflags & FDFLAGS_NONBLOCK) != 0) {
|
|
|
|
// TODO (caspervonb) review if we can emulate this.
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((fdflags & FDFLAGS_RSYNC) != 0) {
|
|
|
|
// TODO (caspervonb) review if we can emulate this.
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((fdflags & FDFLAGS_SYNC) != 0) {
|
|
|
|
// TODO (caspervonb) review if we can emulate this.
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!options.read && !options.write && !options.truncate) {
|
|
|
|
options.read = true;
|
|
|
|
}
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
const { rid } = Deno.openSync(path, options);
|
|
|
|
const openedFd = this.fds.push({
|
|
|
|
rid,
|
|
|
|
flags: fdflags,
|
2020-08-24 19:04:11 -04:00
|
|
|
path,
|
|
|
|
}) - 1;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
const memoryView = new DataView(this.memory.buffer);
|
|
|
|
memoryView.setUint32(openedFdOffset, openedFd, true);
|
2020-06-24 09:27:31 -04:00
|
|
|
|
|
|
|
return ERRNO_SUCCESS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"path_readlink": syscall((
|
2020-06-24 09:27:31 -04:00
|
|
|
fd: number,
|
2020-09-29 05:41:22 -04:00
|
|
|
pathOffset: number,
|
|
|
|
pathLength: number,
|
|
|
|
bufferOffset: number,
|
|
|
|
bufferLength: number,
|
|
|
|
bufferUsedOffset: number,
|
2020-06-24 09:27:31 -04:00
|
|
|
): number => {
|
|
|
|
const entry = this.fds[fd];
|
|
|
|
if (!entry) {
|
|
|
|
return ERRNO_BADF;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!entry.path) {
|
|
|
|
return ERRNO_INVAL;
|
|
|
|
}
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
const memoryData = new Uint8Array(this.memory.buffer);
|
|
|
|
const memoryView = new DataView(this.memory.buffer);
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
const pathData = new Uint8Array(
|
2020-08-24 19:04:11 -04:00
|
|
|
this.memory.buffer,
|
2020-09-29 05:41:22 -04:00
|
|
|
pathOffset,
|
|
|
|
pathLength,
|
2020-08-24 19:04:11 -04:00
|
|
|
);
|
2020-09-29 05:41:22 -04:00
|
|
|
const path = resolve(entry.path!, new TextDecoder().decode(pathData));
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-08-24 19:04:11 -04:00
|
|
|
const link = Deno.readLinkSync(path);
|
2020-09-29 05:41:22 -04:00
|
|
|
const linkData = new TextEncoder().encode(link);
|
|
|
|
memoryData.set(new Uint8Array(linkData, 0, bufferLength), bufferOffset);
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
const bufferUsed = Math.min(linkData.byteLength, bufferLength);
|
|
|
|
memoryView.setUint32(bufferUsedOffset, bufferUsed, true);
|
2020-06-24 09:27:31 -04:00
|
|
|
|
|
|
|
return ERRNO_SUCCESS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"path_remove_directory": syscall((
|
2020-06-24 09:27:31 -04:00
|
|
|
fd: number,
|
2020-09-29 05:41:22 -04:00
|
|
|
pathOffset: number,
|
|
|
|
pathLength: number,
|
2020-06-24 09:27:31 -04:00
|
|
|
): number => {
|
|
|
|
const entry = this.fds[fd];
|
|
|
|
if (!entry) {
|
|
|
|
return ERRNO_BADF;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!entry.path) {
|
|
|
|
return ERRNO_INVAL;
|
|
|
|
}
|
|
|
|
|
2020-12-05 09:47:01 -05:00
|
|
|
const textDecoder = new TextDecoder();
|
2020-09-29 05:41:22 -04:00
|
|
|
const data = new Uint8Array(this.memory.buffer, pathOffset, pathLength);
|
2020-12-05 09:47:01 -05:00
|
|
|
const path = resolve(entry.path!, textDecoder.decode(data));
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-08-24 19:04:11 -04:00
|
|
|
if (!Deno.statSync(path).isDirectory) {
|
|
|
|
return ERRNO_NOTDIR;
|
2020-06-24 09:27:31 -04:00
|
|
|
}
|
|
|
|
|
2020-08-24 19:04:11 -04:00
|
|
|
Deno.removeSync(path);
|
|
|
|
|
2020-06-24 09:27:31 -04:00
|
|
|
return ERRNO_SUCCESS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"path_rename": syscall((
|
2020-06-24 09:27:31 -04:00
|
|
|
fd: number,
|
2020-09-29 05:41:22 -04:00
|
|
|
oldPathOffset: number,
|
|
|
|
oldPathLength: number,
|
|
|
|
newFd: number,
|
|
|
|
newPathOffset: number,
|
|
|
|
newPathLength: number,
|
2020-06-24 09:27:31 -04:00
|
|
|
): number => {
|
2020-09-29 05:41:22 -04:00
|
|
|
const oldEntry = this.fds[fd];
|
|
|
|
const newEntry = this.fds[newFd];
|
|
|
|
if (!oldEntry || !newEntry) {
|
2020-06-24 09:27:31 -04:00
|
|
|
return ERRNO_BADF;
|
|
|
|
}
|
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
if (!oldEntry.path || !newEntry.path) {
|
2020-06-24 09:27:31 -04:00
|
|
|
return ERRNO_INVAL;
|
|
|
|
}
|
|
|
|
|
2020-12-05 09:47:01 -05:00
|
|
|
const textDecoder = new TextDecoder();
|
2020-09-29 05:41:22 -04:00
|
|
|
const oldData = new Uint8Array(
|
2020-06-24 09:27:31 -04:00
|
|
|
this.memory.buffer,
|
2020-09-29 05:41:22 -04:00
|
|
|
oldPathOffset,
|
|
|
|
oldPathLength,
|
2020-06-24 09:27:31 -04:00
|
|
|
);
|
2020-12-05 09:47:01 -05:00
|
|
|
const oldPath = resolve(oldEntry.path!, textDecoder.decode(oldData));
|
2020-09-29 05:41:22 -04:00
|
|
|
const newData = new Uint8Array(
|
2020-06-24 09:27:31 -04:00
|
|
|
this.memory.buffer,
|
2020-09-29 05:41:22 -04:00
|
|
|
newPathOffset,
|
|
|
|
newPathLength,
|
2020-06-24 09:27:31 -04:00
|
|
|
);
|
2020-12-05 09:47:01 -05:00
|
|
|
const newPath = resolve(newEntry.path!, textDecoder.decode(newData));
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
Deno.renameSync(oldPath, newPath);
|
2020-06-24 09:27:31 -04:00
|
|
|
|
|
|
|
return ERRNO_SUCCESS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"path_symlink": syscall((
|
|
|
|
oldPathOffset: number,
|
|
|
|
oldPathLength: number,
|
2020-06-24 09:27:31 -04:00
|
|
|
fd: number,
|
2020-09-29 05:41:22 -04:00
|
|
|
newPathOffset: number,
|
|
|
|
newPathLength: number,
|
2020-06-24 09:27:31 -04:00
|
|
|
): number => {
|
|
|
|
const entry = this.fds[fd];
|
|
|
|
if (!entry) {
|
|
|
|
return ERRNO_BADF;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!entry.path) {
|
|
|
|
return ERRNO_INVAL;
|
|
|
|
}
|
|
|
|
|
2020-12-05 09:47:01 -05:00
|
|
|
const textDecoder = new TextDecoder();
|
2020-09-29 05:41:22 -04:00
|
|
|
const oldData = new Uint8Array(
|
2020-06-24 09:27:31 -04:00
|
|
|
this.memory.buffer,
|
2020-09-29 05:41:22 -04:00
|
|
|
oldPathOffset,
|
|
|
|
oldPathLength,
|
2020-06-24 09:27:31 -04:00
|
|
|
);
|
2020-12-05 09:47:01 -05:00
|
|
|
const oldPath = textDecoder.decode(oldData);
|
2020-09-29 05:41:22 -04:00
|
|
|
const newData = new Uint8Array(
|
2020-06-24 09:27:31 -04:00
|
|
|
this.memory.buffer,
|
2020-09-29 05:41:22 -04:00
|
|
|
newPathOffset,
|
|
|
|
newPathLength,
|
2020-06-24 09:27:31 -04:00
|
|
|
);
|
2020-12-05 09:47:01 -05:00
|
|
|
const newPath = resolve(entry.path!, textDecoder.decode(newData));
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
Deno.symlinkSync(oldPath, newPath);
|
2020-06-24 09:27:31 -04:00
|
|
|
|
|
|
|
return ERRNO_SUCCESS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"path_unlink_file": syscall((
|
2020-06-24 09:27:31 -04:00
|
|
|
fd: number,
|
2020-09-29 05:41:22 -04:00
|
|
|
pathOffset: number,
|
|
|
|
pathLength: number,
|
2020-06-24 09:27:31 -04:00
|
|
|
): number => {
|
|
|
|
const entry = this.fds[fd];
|
|
|
|
if (!entry) {
|
|
|
|
return ERRNO_BADF;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!entry.path) {
|
|
|
|
return ERRNO_INVAL;
|
|
|
|
}
|
|
|
|
|
2020-12-05 09:47:01 -05:00
|
|
|
const textDecoder = new TextDecoder();
|
2020-09-29 05:41:22 -04:00
|
|
|
const data = new Uint8Array(this.memory.buffer, pathOffset, pathLength);
|
2020-12-05 09:47:01 -05:00
|
|
|
const path = resolve(entry.path!, textDecoder.decode(data));
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-08-24 19:04:11 -04:00
|
|
|
Deno.removeSync(path);
|
2020-06-24 09:27:31 -04:00
|
|
|
|
|
|
|
return ERRNO_SUCCESS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"poll_oneoff": syscall((
|
|
|
|
_inOffset: number,
|
|
|
|
_outOffset: number,
|
|
|
|
_nsubscriptions: number,
|
|
|
|
_neventsOffset: number,
|
2020-06-24 09:27:31 -04:00
|
|
|
): number => {
|
|
|
|
return ERRNO_NOSYS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"proc_exit": syscall((
|
|
|
|
rval: number,
|
|
|
|
): never => {
|
2020-12-03 13:57:35 -05:00
|
|
|
if (this.exitOnReturn) {
|
|
|
|
Deno.exit(rval);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new ExitStatus(rval);
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"proc_raise": syscall((
|
|
|
|
_sig: number,
|
|
|
|
): number => {
|
2020-06-24 09:27:31 -04:00
|
|
|
return ERRNO_NOSYS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"sched_yield": syscall((): number => {
|
2020-06-24 09:27:31 -04:00
|
|
|
return ERRNO_SUCCESS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"random_get": syscall((
|
|
|
|
bufferOffset: number,
|
|
|
|
bufferLength: number,
|
|
|
|
): number => {
|
|
|
|
const buffer = new Uint8Array(
|
|
|
|
this.memory.buffer,
|
|
|
|
bufferOffset,
|
|
|
|
bufferLength,
|
|
|
|
);
|
2020-06-24 09:27:31 -04:00
|
|
|
crypto.getRandomValues(buffer);
|
|
|
|
|
|
|
|
return ERRNO_SUCCESS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"sock_recv": syscall((
|
|
|
|
_fd: number,
|
|
|
|
_riDataOffset: number,
|
|
|
|
_riDataLength: number,
|
|
|
|
_riFlags: number,
|
|
|
|
_roDataLengthOffset: number,
|
|
|
|
_roFlagsOffset: number,
|
2020-06-24 09:27:31 -04:00
|
|
|
): number => {
|
|
|
|
return ERRNO_NOSYS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"sock_send": syscall((
|
|
|
|
_fd: number,
|
|
|
|
_siDataOffset: number,
|
|
|
|
_siDataLength: number,
|
|
|
|
_siFlags: number,
|
|
|
|
_soDataLengthOffset: number,
|
2020-06-24 09:27:31 -04:00
|
|
|
): number => {
|
|
|
|
return ERRNO_NOSYS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-09-29 05:41:22 -04:00
|
|
|
"sock_shutdown": syscall((
|
|
|
|
_fd: number,
|
|
|
|
_how: number,
|
|
|
|
): number => {
|
2020-06-24 09:27:31 -04:00
|
|
|
return ERRNO_NOSYS;
|
2020-08-24 19:04:11 -04:00
|
|
|
}),
|
2020-06-24 09:27:31 -04:00
|
|
|
};
|
2020-12-10 07:37:09 -05:00
|
|
|
|
|
|
|
this.#started = false;
|
2020-06-24 09:27:31 -04:00
|
|
|
}
|
2020-11-02 12:50:45 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempt to begin execution of instance as a command by invoking its
|
|
|
|
* _start() export.
|
|
|
|
*
|
|
|
|
* If the instance does not contain a _start() export, or if the instance
|
|
|
|
* contains an _initialize export an error will be thrown.
|
|
|
|
*
|
|
|
|
* The instance must also have a WebAssembly.Memory export named "memory"
|
|
|
|
* which will be used as the address space, if it does not an error will be
|
|
|
|
* thrown.
|
|
|
|
*/
|
|
|
|
start(instance: WebAssembly.Instance) {
|
2020-12-10 07:37:09 -05:00
|
|
|
if (this.#started) {
|
|
|
|
throw new Error("WebAssembly.Instance has already started");
|
|
|
|
}
|
|
|
|
|
|
|
|
this.#started = true;
|
|
|
|
|
2020-11-02 12:50:45 -05:00
|
|
|
const { _start, _initialize, memory } = instance.exports;
|
|
|
|
|
|
|
|
if (!(memory instanceof WebAssembly.Memory)) {
|
|
|
|
throw new TypeError("WebAsembly.instance must provide a memory export");
|
|
|
|
}
|
|
|
|
|
|
|
|
this.memory = memory;
|
|
|
|
|
|
|
|
if (typeof _initialize == "function") {
|
|
|
|
throw new TypeError(
|
|
|
|
"WebAsembly.instance export _initialize must not be a function",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof _start != "function") {
|
|
|
|
throw new TypeError(
|
|
|
|
"WebAssembly.Instance export _start must be a function",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
_start();
|
|
|
|
}
|
2020-12-03 13:36:13 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempt to initialize instance as a reactor by invoking its _initialize() export.
|
|
|
|
*
|
|
|
|
* If instance contains a _start() export, then an exception is thrown.
|
|
|
|
*
|
|
|
|
* The instance must also have a WebAssembly.Memory export named "memory"
|
|
|
|
* which will be used as the address space, if it does not an error will be
|
|
|
|
* thrown.
|
|
|
|
*/
|
|
|
|
initialize(instance: WebAssembly.Instance) {
|
2020-12-10 07:37:09 -05:00
|
|
|
if (this.#started) {
|
|
|
|
throw new Error("WebAssembly.Instance has already started");
|
|
|
|
}
|
|
|
|
|
|
|
|
this.#started = true;
|
|
|
|
|
2020-12-03 13:36:13 -05:00
|
|
|
const { _start, _initialize, memory } = instance.exports;
|
|
|
|
|
|
|
|
if (!(memory instanceof WebAssembly.Memory)) {
|
|
|
|
throw new TypeError("WebAsembly.instance must provide a memory export");
|
|
|
|
}
|
|
|
|
|
|
|
|
this.memory = memory;
|
|
|
|
|
|
|
|
if (typeof _start == "function") {
|
|
|
|
throw new TypeError(
|
|
|
|
"WebAssembly.Instance export _start must not be a function",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof _initialize != "function") {
|
|
|
|
throw new TypeError(
|
|
|
|
"WebAsembly.instance export _initialize must be a function",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
_initialize();
|
|
|
|
}
|
2020-06-24 09:27:31 -04:00
|
|
|
}
|