Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Couple of fixes #72

Merged
merged 3 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/fd.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint @typescript-eslint/no-unused-vars:0 */
import * as wasi from "./wasi_defs.js";

export class Fd {
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;
Expand Down
47 changes: 47 additions & 0 deletions src/fs_core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
const 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
const 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<wasi.Iovec>,
Expand Down Expand Up @@ -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) };
}
Expand All @@ -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<wasi.Iovec>,
Expand Down Expand Up @@ -242,6 +284,11 @@ export class OpenDirectory extends Fd {
this.dir = dir;
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
fd_seek(offset: bigint, whence: number): { ret: number; offset: bigint } {
return { ret: wasi.ERRNO_ISDIR, offset: 0n };
}

fd_fdstat_get(): { ret: number; fdstat: wasi.Fdstat | null } {
return { ret: 0, fdstat: new wasi.Fdstat(wasi.FILETYPE_DIRECTORY, 0) };
}
Expand Down
2 changes: 0 additions & 2 deletions test/skip.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -32,7 +31,6 @@
"nofollow_errors": "fail",
"path_open_preopen": "fail",
"fd_readdir": "fail",
"directory_seek": "fail",
"symlink_filestat": "fail",
"symlink_loop": "fail",
"interesting_paths": "fail"
Expand Down
Loading