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

Introduce type hints in a couple more places #63

Merged
merged 5 commits into from
Dec 23, 2023
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
7 changes: 1 addition & 6 deletions examples/rustc.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@
This demo highlights how far `rustc` can get on this polyfill before failing due to other reasons.
</p>
<script type="module">
import { Fd } from "../dist/fd.js";
import { File, Directory } from "../dist/fs_core.js";
import { PreopenDirectory } from "../dist/fs_fd.js";
import { WASI } from "../dist/index.js";
import { strace } from "../dist/strace.js";

import { Fd, File, Directory, PreopenDirectory, WASI, strace } from "../dist/index.js";

var term = new Terminal({
convertEol: true,
Expand Down
67 changes: 46 additions & 21 deletions src/fd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,24 @@ export 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) {
fd_pread(
view8: Uint8Array,
iovs: Array<wasi.Iovec>,
offset: bigint,
): { ret: number; nread: number } {
return { ret: wasi.ERRNO_NOTSUP, nread: 0 };
}
fd_prestat_get() {
fd_prestat_get(): { ret: number; prestat: wasi.Prestat | null } {
return { ret: wasi.ERRNO_NOTSUP, prestat: null };
}
fd_prestat_dir_name() {
fd_prestat_dir_name(): { ret: number; prestat_dir_name: Uint8Array | null } {
return { ret: wasi.ERRNO_NOTSUP, prestat_dir_name: null };
}
fd_pwrite(view8: Uint8Array, iovs: Array<wasi.Ciovec>, offset: bigint) {
fd_pwrite(
view8: Uint8Array,
iovs: Array<wasi.Ciovec>,
offset: bigint,
): { ret: number; nwritten: number } {
return { ret: wasi.ERRNO_NOTSUP, nwritten: 0 };
}
fd_read(
Expand All @@ -68,44 +76,61 @@ export class Fd {
fd_tell(): { ret: number; offset: bigint } {
return { ret: wasi.ERRNO_NOTSUP, offset: 0n };
}
fd_write(view8: Uint8Array, iovs: Array<wasi.Ciovec>) {
fd_write(
view8: Uint8Array,
iovs: Array<wasi.Ciovec>,
): { ret: number; nwritten: number } {
return { ret: wasi.ERRNO_NOTSUP, nwritten: 0 };
}
path_create_directory(path): number {
path_create_directory(path: string): number {
return wasi.ERRNO_NOTSUP;
}
path_filestat_get(flags, path) {
path_filestat_get(
flags: number,
path: string,
): { ret: number; filestat: wasi.Filestat | null } {
return { ret: wasi.ERRNO_NOTSUP, filestat: null };
}
path_filestat_set_times(flags, path, atim, mtim, fst_flags) {
path_filestat_set_times(
flags: number,
path: string,
atim: bigint,
mtim: bigint,
fst_flags: number,
): number {
return wasi.ERRNO_NOTSUP;
}
path_link(old_fd, old_flags, old_path, new_path): number {
path_link(
old_fd: number,
old_flags: number,
old_path: string,
new_path: string,
): number {
return wasi.ERRNO_NOTSUP;
}
path_open(
dirflags,
path,
oflags,
fs_rights_base,
fs_rights_inheriting,
fdflags,
) {
dirflags: number,
path: string,
oflags: number,
fs_rights_base: bigint,
fs_rights_inheriting: bigint,
fd_flags: number,
): { ret: number; fd_obj: Fd | null } {
return { ret: wasi.ERRNO_NOTSUP, fd_obj: null };
}
path_readlink(path) {
path_readlink(path: string): { ret: number; data: string | null } {
return { ret: wasi.ERRNO_NOTSUP, data: null };
}
path_remove_directory(path): number {
path_remove_directory(path: string): number {
return wasi.ERRNO_NOTSUP;
}
path_rename(old_path, new_fd, new_path): number {
path_rename(old_path: string, new_fd: number, new_path: string): number {
return wasi.ERRNO_NOTSUP;
}
path_symlink(old_path, new_path): number {
path_symlink(old_path: string, new_path: string): number {
return wasi.ERRNO_NOTSUP;
}
path_unlink_file(path): number {
path_unlink_file(path: string): number {
return wasi.ERRNO_NOTSUP;
}
}
2 changes: 1 addition & 1 deletion src/fs_core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ export class PreopenDirectory extends OpenDirectory {
this.prestat_name = new TextEncoder().encode(name);
}

fd_prestat_get(): { ret: number; prestat: wasi.Prestat } {
fd_prestat_get(): { ret: number; prestat: wasi.Prestat | null } {
return {
ret: 0,
prestat: wasi.Prestat.dir(this.prestat_name.length),
Expand Down
Empty file removed src/fs_fd.ts
Empty file.
16 changes: 9 additions & 7 deletions src/wasi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export default class WASI {
env: Array<string> = [];
fds: Array<Fd> = [];
inst: { exports: { memory: WebAssembly.Memory } };
wasiImport: { [key: string]: (...args: Array<unknown>) => unknown };
// eslint-disable-next-line @typescript-eslint/no-explicit-any
wasiImport: { [key: string]: (...args: Array<any>) => unknown };

/// Start a WASI command
start(instance: {
Expand Down Expand Up @@ -549,9 +550,9 @@ export default class WASI {
flags: number,
path_ptr: number,
path_len: number,
atim,
mtim,
fst_flags,
atim: bigint,
mtim: bigint,
fst_flags: number,
) {
const buffer8 = new Uint8Array(self.inst.exports.memory.buffer);
if (self.fds[fd] != undefined) {
Expand Down Expand Up @@ -651,12 +652,13 @@ export default class WASI {
debug.log(path);
const { ret, data } = self.fds[fd].path_readlink(path);
if (data != null) {
if (data.length > buf_len) {
const data_buf = new TextEncoder().encode(data);
if (data_buf.length > buf_len) {
buffer.setUint32(nread_ptr, 0, true);
return wasi.ERRNO_BADF;
}
buffer8.set(data, buf_ptr);
buffer.setUint32(nread_ptr, data.length, true);
buffer8.set(data_buf, buf_ptr);
buffer.setUint32(nread_ptr, data_buf.length, true);
}
return ret;
} else {
Expand Down