Skip to content

Commit

Permalink
Merge pull request #73 from bjorn3/v0.3
Browse files Browse the repository at this point in the history
Rework the public api and implement a bunch of missing methods
  • Loading branch information
bjorn3 authored Apr 11, 2024
2 parents dc2e79a + 33243db commit c9ede37
Show file tree
Hide file tree
Showing 15 changed files with 1,089 additions and 831 deletions.
58 changes: 27 additions & 31 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 All @@ -75,16 +71,16 @@
new XtermStdio(term),
new XtermStdio(term),
new XtermStdio(term),
new PreopenDirectory("/tmp", {}),
new PreopenDirectory("/sysroot", {
"lib": new Directory({
"rustlib": new Directory({
"wasm32-wasi": new Directory({
"lib": new Directory({}),
}),
"x86_64-unknown-linux-gnu": new Directory({
"lib": new Directory(await (async function () {
let dir = {};
new PreopenDirectory("/tmp", []),
new PreopenDirectory("/sysroot", [
["lib", new Directory([
["rustlib", new Directory([
["wasm32-wasi", new Directory([
["lib", new Directory([])],
])],
["x86_64-unknown-linux-gnu", new Directory([
["lib", new Directory(await (async function () {
let dir = new Map();
for (let file of [
"libaddr2line-3368a2ecf632bfc6.rlib",
"libadler-16845f650eeea12c.rlib",
Expand Down Expand Up @@ -115,17 +111,17 @@
"libunicode_width-d55ce9c674fbd422.rlib",
"libunwind-8ca3e01a84805f9e.rlib"
]) {
dir[file] = await load_external_file("/examples/wasm-rustc/lib/rustlib/x86_64-unknown-linux-gnu/lib/" + file);
dir.set(file, await load_external_file("/examples/wasm-rustc/lib/rustlib/x86_64-unknown-linux-gnu/lib/" + file));
}
return dir;
})()),
}),
}),
}),
}),
new PreopenDirectory("/", {
"hello.rs": new File(new TextEncoder("utf-8").encode(`fn main() { println!("Hello World!"); }`)),
}),
})())],
])],
])],
])],
]),
new PreopenDirectory("/", [
["hello.rs", new File(new TextEncoder("utf-8").encode(`fn main() { println!("Hello World!"); }`))],
]),
];

let w = new WASI(args, env, fds, { debug: true });
Expand All @@ -135,14 +131,14 @@
});
term.writeln("\x1B[93mExecuting\x1B[0m");
console.log(inst.exports);
try { w.start(inst); } catch(e) { term.writeln("Exception: " + e.message); term.writeln("backtrace:"); term.writeln(e.stack); throw e; }
try { w.start(inst); } catch(e) { term.writeln("Exception: " + e.message); /*term.writeln("backtrace:"); term.writeln(e.stack);*/ }
term.writeln("\x1B[92mDone\x1B[0m");

console.log(fds);
console.log(fds[5].dir);
console.log(fds[5].dir.contents["hello.hello.2490b9cce2492134-cgu.0.rcgu.o"].data);
document.querySelector("#downloads").innerHTML += "<br><a href='" + URL.createObjectURL(new Blob([fds[5].dir.contents["hello.hello.2490b9cce2492134-cgu.0.rcgu.o"].data], { type: "application/elf" })) + "'>Download object</a>";
document.querySelector("#downloads").innerHTML += "<br><a href='" + URL.createObjectURL(new Blob([fds[5].dir.contents["hello.allocator_shim.rcgu.o"].data], { type: "application/elf" })) + "'>Download allocator shim</a>";
console.log(fds[5].dir.contents.get("hello.hello.2490b9cce2492134-cgu.0.rcgu.o").data);
document.querySelector("#downloads").innerHTML += "<br><a href='" + URL.createObjectURL(new Blob([fds[5].dir.contents.get("hello.hello.2490b9cce2492134-cgu.0.rcgu.o").data], { type: "application/elf" })) + "'>Download object</a>";
document.querySelector("#downloads").innerHTML += "<br><a href='" + URL.createObjectURL(new Blob([fds[5].dir.contents.get("hello.allocator_shim.rcgu.o").data], { type: "application/elf" })) + "'>Download allocator shim</a>";
})();
</script>
</body>
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bjorn3/browser_wasi_shim",
"version": "0.2.21",
"version": "0.3.0",
"license": "MIT OR Apache-2.0",
"description": "A pure javascript shim for WASI",
"type": "module",
Expand Down
63 changes: 27 additions & 36 deletions src/fd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@
import * as wasi from "./wasi_defs.js";

export abstract class Fd {
fd_advise(offset: bigint, len: bigint, advice: number): number {
return wasi.ERRNO_SUCCESS;
}
fd_allocate(offset: bigint, len: bigint): number {
return wasi.ERRNO_NOTSUP;
}
fd_close(): number {
return 0;
}
fd_datasync(): number {
return wasi.ERRNO_NOTSUP;
}
fd_fdstat_get(): { ret: number; fdstat: wasi.Fdstat | null } {
return { ret: wasi.ERRNO_NOTSUP, fdstat: null };
}
Expand All @@ -35,31 +29,20 @@ 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 };
}
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>,
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 +59,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 All @@ -100,14 +80,18 @@ export abstract class Fd {
): number {
return wasi.ERRNO_NOTSUP;
}
path_link(
old_fd: number,
old_flags: number,
old_path: string,
new_path: string,
): number {
path_link(path: string, inode: Inode, allow_dir: boolean): number {
return wasi.ERRNO_NOTSUP;
}
path_unlink(path: string): { ret: number; inode_obj: Inode | null } {
return { ret: wasi.ERRNO_NOTSUP, inode_obj: null };
}
path_lookup(
path: string,
dirflags: number,
): { ret: number; inode_obj: Inode | null } {
return { ret: wasi.ERRNO_NOTSUP, inode_obj: null };
}
path_open(
dirflags: number,
path: string,
Expand All @@ -116,7 +100,7 @@ export abstract class Fd {
fs_rights_inheriting: bigint,
fd_flags: number,
): { ret: number; fd_obj: Fd | null } {
return { ret: wasi.ERRNO_NOTSUP, fd_obj: null };
return { ret: wasi.ERRNO_NOTDIR, fd_obj: null };
}
path_readlink(path: string): { ret: number; data: string | null } {
return { ret: wasi.ERRNO_NOTSUP, data: null };
Expand All @@ -127,10 +111,17 @@ export abstract class Fd {
path_rename(old_path: string, new_fd: number, new_path: string): number {
return wasi.ERRNO_NOTSUP;
}
path_symlink(old_path: string, new_path: string): number {
return wasi.ERRNO_NOTSUP;
}
path_unlink_file(path: string): number {
return wasi.ERRNO_NOTSUP;
}
}

export abstract class Inode {
abstract path_open(
oflags: number,
fs_rights_base: bigint,
fd_flags: number,
): { ret: number; fd_obj: Fd | null };

abstract stat(): wasi.Filestat;
}
Loading

0 comments on commit c9ede37

Please sign in to comment.