From 003085f470ce76113b8ea9227a2be89ffa744b94 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sat, 23 Dec 2023 17:54:05 +0100 Subject: [PATCH] Implement fd_advice, fd_allocate and fd_filestat_set_size --- src/fd.ts | 2 +- src/fs_core.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ test/skip.json | 1 - 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/fd.ts b/src/fd.ts index 1607618..435685a 100644 --- a/src/fd.ts +++ b/src/fd.ts @@ -3,7 +3,7 @@ import * as wasi from "./wasi_defs.js"; export abstract class Fd { fd_advise(offset: bigint, len: bigint, advice: number): number { - return wasi.ERRNO_NOTSUP; + return wasi.ERRNO_SUCCESS; } fd_allocate(offset: bigint, len: bigint): number { return wasi.ERRNO_NOTSUP; diff --git a/src/fs_core.ts b/src/fs_core.ts index b34c76b..73da0c0 100644 --- a/src/fs_core.ts +++ b/src/fs_core.ts @@ -11,10 +11,37 @@ export class OpenFile extends Fd { this.file = file; } + fd_allocate(offset: bigint, len: bigint): number { + if (this.file.size > offset + len) { + // already big enough + } else { + // extend + let new_data = new Uint8Array(Number(offset + len)); + new_data.set(this.file.data, 0); + this.file.data = new_data; + } + return wasi.ERRNO_SUCCESS; + } + fd_fdstat_get(): { ret: number; fdstat: wasi.Fdstat | null } { return { ret: 0, fdstat: new wasi.Fdstat(wasi.FILETYPE_REGULAR_FILE, 0) }; } + fd_filestat_set_size(size: bigint): number { + if (this.file.size > size) { + // truncate + this.file.data = new Uint8Array( + this.file.data.buffer.slice(0, Number(size)), + ); + } else { + // extend + let new_data = new Uint8Array(Number(size)); + new_data.set(this.file.data, 0); + this.file.data = new_data; + } + return wasi.ERRNO_SUCCESS; + } + fd_read( view8: Uint8Array, iovs: Array, @@ -147,6 +174,16 @@ export class OpenSyncOPFSFile extends Fd { this.file = file; } + fd_allocate(offset: bigint, len: bigint): number { + if (BigInt(this.file.handle.getSize()) > offset + len) { + // already big enough + } else { + // extend + this.file.handle.truncate(Number(offset + len)); + } + return wasi.ERRNO_SUCCESS; + } + fd_fdstat_get(): { ret: number; fdstat: wasi.Fdstat | null } { return { ret: 0, fdstat: new wasi.Fdstat(wasi.FILETYPE_REGULAR_FILE, 0) }; } @@ -161,6 +198,11 @@ export class OpenSyncOPFSFile extends Fd { }; } + fd_filestat_set_size(size: bigint): number { + this.file.handle.truncate(Number(size)); + return wasi.ERRNO_SUCCESS; + } + fd_read( view8: Uint8Array, iovs: Array, diff --git a/test/skip.json b/test/skip.json index e8af7c3..5bf1aa6 100644 --- a/test/skip.json +++ b/test/skip.json @@ -10,7 +10,6 @@ "WASI Rust tests": { "sched_yield": "not implemented yet", "path_rename": "fail", - "fd_advise": "fail", "path_exists": "fail", "path_open_dirfd_not_dir": "fail", "fd_filestat_set": "fail",