From 33243db33da2542290ccd5883088831019cec9bf Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Tue, 9 Apr 2024 18:57:45 +0200 Subject: [PATCH] Fuse fd_prestat_dir_name into fd_prestat_get --- src/fd.ts | 3 --- src/fs_mem.ts | 9 +-------- src/wasi.ts | 12 +++++------- src/wasi_defs.ts | 12 ++++++------ 4 files changed, 12 insertions(+), 24 deletions(-) diff --git a/src/fd.ts b/src/fd.ts index 658565a..4e3b6f5 100644 --- a/src/fd.ts +++ b/src/fd.ts @@ -35,9 +35,6 @@ export abstract class Fd { fd_prestat_get(): { ret: number; prestat: wasi.Prestat | null } { return { ret: wasi.ERRNO_NOTSUP, prestat: null }; } - fd_prestat_dir_name(): { ret: number; prestat_dir_name: string | null } { - return { ret: wasi.ERRNO_NOTSUP, prestat_dir_name: null }; - } fd_pwrite( data: Uint8Array, offset: bigint, diff --git a/src/fs_mem.ts b/src/fs_mem.ts index 5735918..6f540ee 100644 --- a/src/fs_mem.ts +++ b/src/fs_mem.ts @@ -456,14 +456,7 @@ export class PreopenDirectory extends OpenDirectory { fd_prestat_get(): { ret: number; prestat: wasi.Prestat | null } { return { ret: 0, - prestat: wasi.Prestat.dir(this.prestat_name.length), - }; - } - - fd_prestat_dir_name(): { ret: number; prestat_dir_name: string } { - return { - ret: 0, - prestat_dir_name: this.prestat_name, + prestat: wasi.Prestat.dir(this.prestat_name), }; } } diff --git a/src/wasi.ts b/src/wasi.ts index c8aaa4e..a3e4439 100644 --- a/src/wasi.ts +++ b/src/wasi.ts @@ -344,18 +344,16 @@ export default class WASI { ): number { // FIXME don't ignore path_len if (self.fds[fd] != undefined) { - const { ret, prestat_dir_name } = self.fds[fd].fd_prestat_dir_name(); - if (prestat_dir_name == null) { + const { ret, prestat } = self.fds[fd].fd_prestat_get(); + if (prestat == null) { return ret; } - const encoded_prestat_dir_name = new TextEncoder().encode( - prestat_dir_name, - ); + const prestat_dir_name = prestat.inner.pr_name; const buffer8 = new Uint8Array(self.inst.exports.memory.buffer); - buffer8.set(encoded_prestat_dir_name.slice(0, path_len), path_ptr); + buffer8.set(prestat_dir_name.slice(0, path_len), path_ptr); - return encoded_prestat_dir_name.byteLength > path_len + return prestat_dir_name.byteLength > path_len ? wasi.ERRNO_NAMETOOLONG : wasi.ERRNO_SUCCESS; } else { diff --git a/src/wasi_defs.ts b/src/wasi_defs.ts index b41aed1..db0b37c 100644 --- a/src/wasi_defs.ts +++ b/src/wasi_defs.ts @@ -341,14 +341,14 @@ export const SDFLAGS_WR = 1 << 1; export const PREOPENTYPE_DIR = 0; export class PrestatDir { - pr_name_len: number; + pr_name: Uint8Array; - constructor(name_len: number) { - this.pr_name_len = name_len; + constructor(name: string) { + this.pr_name = new TextEncoder().encode(name); } write_bytes(view: DataView, ptr: number) { - view.setUint32(ptr, this.pr_name_len, true); + view.setUint32(ptr, this.pr_name.byteLength, true); } } @@ -358,10 +358,10 @@ export class Prestat { //@ts-ignore strictPropertyInitialization inner: PrestatDir; - static dir(name_len: number): Prestat { + static dir(name: string): Prestat { const prestat = new Prestat(); prestat.tag = PREOPENTYPE_DIR; - prestat.inner = new PrestatDir(name_len); + prestat.inner = new PrestatDir(name); return prestat; }