Skip to content

Commit

Permalink
Implement fd_advice, fd_allocate and fd_filestat_set_size
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 committed Apr 8, 2024
1 parent 16ab706 commit 692963c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/fd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
42 changes: 42 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
let new_data = new Uint8Array(Number(offset + len));

Check failure on line 19 in src/fs_core.ts

View workflow job for this annotation

GitHub Actions / build

'new_data' is never reassigned. Use 'const' instead
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));

Check failure on line 38 in src/fs_core.ts

View workflow job for this annotation

GitHub Actions / build

'new_data' is never reassigned. Use 'const' instead
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 {

Check failure on line 201 in src/fs_core.ts

View workflow job for this annotation

GitHub Actions / build

'size' is defined but never used
this.file.handle.truncate(0);
return wasi.ERRNO_SUCCESS;
}

fd_read(
view8: Uint8Array,
iovs: Array<wasi.Iovec>,
Expand Down
1 change: 0 additions & 1 deletion 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 Down

0 comments on commit 692963c

Please sign in to comment.