Skip to content

Commit

Permalink
Pass Uint8Array from/to fd_read/fd_write
Browse files Browse the repository at this point in the history
This way individual Fd implementations don't have to handle vectorized
reads/writes and don't need direct access to the wasm linear memory.
  • Loading branch information
bjorn3 committed Apr 9, 2024
1 parent 647ce67 commit 2529453
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 190 deletions.
12 changes: 4 additions & 8 deletions examples/rustc.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,11 @@
super();
this.term = term;
}
fd_write(view8/*: Uint8Array*/, iovs/*: [wasi.Iovec]*/)/*: {ret: number, nwritten: number}*/ {
fd_write(data/*: Uint8Array*/)/*: {ret: number, nwritten: number}*/ {
let nwritten = 0;
for (let iovec of iovs) {
console.log(iovec.buf_len, iovec.buf_len, view8.slice(iovec.buf, iovec.buf + iovec.buf_len));
let buffer = view8.slice(iovec.buf, iovec.buf + iovec.buf_len);
this.term.writeUtf8(buffer);
nwritten += iovec.buf_len;
}
return { ret: 0, nwritten };
console.log(data);
this.term.writeUtf8(data);
return { ret: 0, nwritten: data.byteLength };
}
}

Expand Down
23 changes: 6 additions & 17 deletions src/fd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,8 @@ export abstract class Fd {
fd_filestat_set_times(atim: bigint, mtim: bigint, fst_flags: number): number {
return wasi.ERRNO_NOTSUP;
}
fd_pread(
view8: Uint8Array,
iovs: Array<wasi.Iovec>,
offset: bigint,
): { ret: number; nread: number } {
return { ret: wasi.ERRNO_NOTSUP, nread: 0 };
fd_pread(size: number, offset: bigint): { ret: number; data: Uint8Array } {
return { ret: wasi.ERRNO_NOTSUP, data: new Uint8Array() };
}
fd_prestat_get(): { ret: number; prestat: wasi.Prestat | null } {
return { ret: wasi.ERRNO_NOTSUP, prestat: null };
Expand All @@ -49,17 +45,13 @@ export abstract class Fd {
return { ret: wasi.ERRNO_NOTSUP, prestat_dir_name: null };
}
fd_pwrite(
view8: Uint8Array,
iovs: Array<wasi.Ciovec>,
data: Uint8Array,
offset: bigint,
): { ret: number; nwritten: number } {
return { ret: wasi.ERRNO_NOTSUP, nwritten: 0 };
}
fd_read(
view8: Uint8Array,
iovs: Array<wasi.Iovec>,
): { ret: number; nread: number } {
return { ret: wasi.ERRNO_NOTSUP, nread: 0 };
fd_read(size: number): { ret: number; data: Uint8Array } {
return { ret: wasi.ERRNO_NOTSUP, data: new Uint8Array() };
}
fd_readdir_single(cookie: bigint): {
ret: number;
Expand All @@ -76,10 +68,7 @@ export abstract class Fd {
fd_tell(): { ret: number; offset: bigint } {
return { ret: wasi.ERRNO_NOTSUP, offset: 0n };
}
fd_write(
view8: Uint8Array,
iovs: Array<wasi.Ciovec>,
): { ret: number; nwritten: number } {
fd_write(data: Uint8Array): { ret: number; nwritten: number } {
return { ret: wasi.ERRNO_NOTSUP, nwritten: 0 };
}
path_create_directory(path: string): number {
Expand Down
162 changes: 47 additions & 115 deletions src/fs_mem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,47 +42,21 @@ export class OpenFile extends Fd {
return wasi.ERRNO_SUCCESS;
}

fd_read(
view8: Uint8Array,
iovs: Array<wasi.Iovec>,
): { ret: number; nread: number } {
let nread = 0;
for (const iovec of iovs) {
if (this.file_pos < this.file.data.byteLength) {
const slice = this.file.data.slice(
Number(this.file_pos),
Number(this.file_pos + BigInt(iovec.buf_len)),
);
view8.set(slice, iovec.buf);
this.file_pos += BigInt(slice.length);
nread += slice.length;
} else {
break;
}
}
return { ret: 0, nread };
fd_read(size: number): { ret: number; data: Uint8Array } {
const slice = this.file.data.slice(
Number(this.file_pos),
Number(this.file_pos + BigInt(size)),
);
this.file_pos += BigInt(slice.length);
return { ret: 0, data: slice };
}

fd_pread(
view8: Uint8Array,
iovs: Array<wasi.Iovec>,
offset: bigint,
): { ret: number; nread: number } {
let nread = 0;
for (const iovec of iovs) {
if (offset < this.file.data.byteLength) {
const slice = this.file.data.slice(
Number(offset),
Number(offset + BigInt(iovec.buf_len)),
);
view8.set(slice, iovec.buf);
offset += BigInt(slice.length);
nread += slice.length;
} else {
break;
}
}
return { ret: 0, nread };
fd_pread(size: number, offset: bigint): { ret: number; data: Uint8Array } {
const slice = this.file.data.slice(
Number(offset),
Number(offset + BigInt(size)),
);
return { ret: 0, data: slice };
}

fd_seek(offset: bigint, whence: number): { ret: number; offset: bigint } {
Expand Down Expand Up @@ -113,51 +87,33 @@ export class OpenFile extends Fd {
return { ret: 0, offset: this.file_pos };
}

fd_write(
view8: Uint8Array,
iovs: Array<wasi.Ciovec>,
): { ret: number; nwritten: number } {
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 (this.file_pos + BigInt(buffer.byteLength) > this.file.size) {
const old = this.file.data;
this.file.data = new Uint8Array(
Number(this.file_pos + BigInt(buffer.byteLength)),
);
this.file.data.set(old);
}
this.file.data.set(
buffer.slice(0, Number(this.file.size - this.file_pos)),
Number(this.file_pos),
fd_write(data: Uint8Array): { ret: number; nwritten: number } {
if (this.file.readonly) return { ret: wasi.ERRNO_BADF, nwritten: 0 };

if (this.file_pos + BigInt(data.byteLength) > this.file.size) {
const old = this.file.data;
this.file.data = new Uint8Array(
Number(this.file_pos + BigInt(data.byteLength)),
);
this.file_pos += BigInt(buffer.byteLength);
nwritten += iovec.buf_len;
this.file.data.set(old);
}
return { ret: 0, nwritten };

this.file.data.set(data, Number(this.file_pos));
this.file_pos += BigInt(data.byteLength);
return { ret: 0, nwritten: data.byteLength };
}

fd_pwrite(view8: Uint8Array, iovs: Array<wasi.Ciovec>, 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;
fd_pwrite(data: Uint8Array, offset: bigint) {
if (this.file.readonly) return { ret: wasi.ERRNO_BADF, nwritten: 0 };

if (offset + BigInt(data.byteLength) > this.file.size) {
const old = this.file.data;
this.file.data = new Uint8Array(Number(offset + BigInt(data.byteLength)));
this.file.data.set(old);
}
return { ret: 0, nwritten };

this.file.data.set(data, Number(offset));
return { ret: 0, nwritten: data.byteLength };
}

fd_filestat_get(): { ret: number; filestat: wasi.Filestat } {
Expand Down Expand Up @@ -464,40 +420,24 @@ export class OpenDirectory extends Fd {
return wasi.ERRNO_BADF;
}

fd_read(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
view8: Uint8Array,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
iovs: wasi.Iovec[],
): { ret: number; nread: number } {
return { ret: wasi.ERRNO_BADF, nread: 0 };
// eslint-disable-next-line @typescript-eslint/no-unused-vars
fd_read(size: number): { ret: number; data: Uint8Array } {
return { ret: wasi.ERRNO_BADF, data: new Uint8Array() };
}

fd_pread(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
view8: Uint8Array,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
iovs: wasi.Iovec[],
// eslint-disable-next-line @typescript-eslint/no-unused-vars
offset: bigint,
): { ret: number; nread: number } {
return { ret: wasi.ERRNO_BADF, nread: 0 };
// eslint-disable-next-line @typescript-eslint/no-unused-vars
fd_pread(size: number, offset: bigint): { ret: number; data: Uint8Array } {
return { ret: wasi.ERRNO_BADF, data: new Uint8Array() };
}

fd_write(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
view8: Uint8Array,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
iovs: wasi.Ciovec[],
): { ret: number; nwritten: number } {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
fd_write(data: Uint8Array): { ret: number; nwritten: number } {
return { ret: wasi.ERRNO_BADF, nwritten: 0 };
}

fd_pwrite(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
view8: Uint8Array,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
iovs: wasi.Ciovec[],
data: Uint8Array,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
offset: bigint,
): { ret: number; nwritten: number } {
Expand Down Expand Up @@ -785,17 +725,9 @@ export class ConsoleStdout extends Fd {
return { ret: 0, fdstat };
}

fd_write(
view8: Uint8Array,
iovs: Array<wasi.Ciovec>,
): { ret: number; nwritten: number } {
let nwritten = 0;
for (const iovec of iovs) {
const buffer = view8.slice(iovec.buf, iovec.buf + iovec.buf_len);
this.write(buffer);
nwritten += iovec.buf_len;
}
return { ret: 0, nwritten };
fd_write(data: Uint8Array): { ret: number; nwritten: number } {
this.write(data);
return { ret: 0, nwritten: data.byteLength };
}

static lineBuffered(write: (line: string) => void): ConsoleStdout {
Expand Down
42 changes: 12 additions & 30 deletions src/fs_opfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,11 @@ export class OpenSyncOPFSFile extends Fd {
return wasi.ERRNO_SUCCESS;
}

fd_read(
view8: Uint8Array,
iovs: Array<wasi.Iovec>,
): { ret: number; nread: number } {
let nread = 0;
for (const iovec of iovs) {
if (this.position < this.file.handle.getSize()) {
const buf = new Uint8Array(view8.buffer, iovec.buf, iovec.buf_len);
const n = this.file.handle.read(buf, { at: Number(this.position) });
this.position += BigInt(n);
nread += n;
} else {
break;
}
}
return { ret: 0, nread };
fd_read(size: number): { ret: number; data: Uint8Array } {
const buf = new Uint8Array(size);
const n = this.file.handle.read(buf, { at: Number(this.position) });
this.position += BigInt(n);
return { ret: 0, data: buf.slice(0, n) };
}

fd_seek(
Expand Down Expand Up @@ -144,20 +133,13 @@ export class OpenSyncOPFSFile extends Fd {
return { ret: wasi.ERRNO_SUCCESS, offset: this.position };
}

fd_write(
view8: Uint8Array,
iovs: Array<wasi.Iovec>,
): { ret: number; nwritten: number } {
let nwritten = 0;
if (this.file.readonly) return { ret: wasi.ERRNO_BADF, nwritten };
for (const iovec of iovs) {
const buf = new Uint8Array(view8.buffer, iovec.buf, iovec.buf_len);
// don't need to extend file manually, just write
const n = this.file.handle.write(buf, { at: Number(this.position) });
this.position += BigInt(n);
nwritten += n;
}
return { ret: wasi.ERRNO_SUCCESS, nwritten };
fd_write(data: Uint8Array): { ret: number; nwritten: number } {
if (this.file.readonly) return { ret: wasi.ERRNO_BADF, nwritten: 0 };

// don't need to extend file manually, just write
const n = this.file.handle.write(data, { at: Number(this.position) });
this.position += BigInt(n);
return { ret: wasi.ERRNO_SUCCESS, nwritten: n };
}

fd_datasync(): number {
Expand Down
Loading

0 comments on commit 2529453

Please sign in to comment.