diff --git a/src/fs_fd.ts b/src/fs_fd.ts index f4f7068..0c24628 100644 --- a/src/fs_fd.ts +++ b/src/fs_fd.ts @@ -83,6 +83,10 @@ export class OpenFile extends Fd { return { ret: 0, offset: this.file_pos }; } + fd_tell(): { ret: number; offset: bigint } { + return { ret: 0, offset: this.file_pos }; + } + fd_write( view8: Uint8Array, iovs: Array, @@ -108,6 +112,28 @@ export class OpenFile extends Fd { return { ret: 0, nwritten }; } + fd_pwrite(view8: Uint8Array, iovs: Array, offset: bigint) { + let nwritten = 0; + if (this.file.readonly) return { ret: wasi.ERRNO_BADF, nwritten }; + for (const iovec of iovs) { + const buffer = view8.slice(iovec.buf, iovec.buf + iovec.buf_len); + if (offset + BigInt(buffer.byteLength) > this.file.size) { + const old = this.file.data; + this.file.data = new Uint8Array( + Number(offset + BigInt(buffer.byteLength)), + ); + this.file.data.set(old); + } + this.file.data.set( + buffer.slice(0, Number(this.file.size - offset)), + Number(offset), + ); + offset += BigInt(buffer.byteLength); + nwritten += iovec.buf_len; + } + return { ret: 0, nwritten }; + } + fd_filestat_get(): { ret: number; filestat: wasi.Filestat } { return { ret: 0, filestat: this.file.stat() }; } diff --git a/src/wasi.ts b/src/wasi.ts index 12392bd..f4e843e 100644 --- a/src/wasi.ts +++ b/src/wasi.ts @@ -117,14 +117,31 @@ export default class WASI { // eslint-disable-next-line @typescript-eslint/no-unused-vars clock_res_get(id: number, res_ptr: number): number { - throw "unimplemented"; + let resolutionValue: bigint; + switch (id) { + case wasi.CLOCKID_MONOTONIC: { + // https://developer.mozilla.org/en-US/docs/Web/API/Performance/now + // > Resolution in isolated contexts: 5 microseconds + resolutionValue = 5_000n; // 5 microseconds + break; + } + case wasi.CLOCKID_REALTIME: { + resolutionValue = 1_000_000n; // 1 millisecond? + break; + } + default: + return wasi.ERRNO_NOSYS; + } + const view = new DataView(self.inst.exports.memory.buffer); + view.setBigUint64(res_ptr, resolutionValue, true); + return wasi.ERRNO_SUCCESS; }, clock_time_get(id: number, precision: bigint, time: number): number { const buffer = new DataView(self.inst.exports.memory.buffer); if (id === wasi.CLOCKID_REALTIME) { buffer.setBigUint64( time, - BigInt(new Date().getTime()) * 1000000n, + BigInt(new Date().getTime()) * 1_000_000n, true, ); } else if (id == wasi.CLOCKID_MONOTONIC) {