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

Cargo #82

Closed
wants to merge 12 commits into from
Closed
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: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@
path = test/wasi-testsuite
url = https://github.com/WebAssembly/wasi-testsuite
branch = prod/testsuite-base
[submodule "examples/wasi_multi_threads_rustc/rust_wasm"]
path = examples/wasi_multi_threads_rustc/rust_wasm
url = https://github.com/oligamiq/rust_wasm
sharrow = true
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.tabSize": 2
}
92 changes: 51 additions & 41 deletions examples/package-lock.json

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

38 changes: 20 additions & 18 deletions examples/package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
{
"name": "browser_wasi_shim_examples",
"publish": false,
"version": "0.0.0",
"license": "MIT OR Apache-2.0",
"description": "",
"repository": {
"type": "git",
"url": "git+https://github.com/bjorn3/browser_wasi_shim.git"
},
"author": "bjorn3",
"bugs": {
"url": "https://github.com/bjorn3/browser_wasi_shim/issues"
},
"homepage": "https://github.com/bjorn3/browser_wasi_shim#readme",
"dependencies": {
"xterm": "^4.18.0",
"xterm-addon-fit": "^0.5.0"
}
"name": "browser_wasi_shim_examples",
"publish": false,
"version": "0.0.0",
"license": "MIT OR Apache-2.0",
"description": "",
"repository": {
"type": "git",
"url": "git+https://github.com/bjorn3/browser_wasi_shim.git"
},
"author": "bjorn3",
"bugs": {
"url": "https://github.com/bjorn3/browser_wasi_shim/issues"
},
"homepage": "https://github.com/bjorn3/browser_wasi_shim#readme",
"dependencies": {
"@oligami/shared-object": "^0.1.1",
"browser_wasi_shim_examples": "file:",
"xterm": "^4.18.0",
"xterm-addon-fit": "^0.5.0"
}
}
38 changes: 38 additions & 0 deletions examples/wasi_multi_threads/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">

<p id="note">
####
</p>

<script type="module">
import { File, OpenFile, ConsoleStdout, PreopenDirectory, WASIFarm, WASIFarmAnimal } from "../../dist/index.js";

const farm = new WASIFarm(
new OpenFile(new File([])), // stdin
ConsoleStdout.lineBuffered(msg => console.log(`[WASI stdout] ${msg}`)),
ConsoleStdout.lineBuffered(msg => console.warn(`[WASI stderr] ${msg}`)),
[],
{ debug: true },
);

console.log(farm);

const worker = new Worker("./worker.js", { type: "module" });
// const worker = new Worker(new URL("./worker.js", import.meta.url).href, { type: "module" });

console.log(worker);

console.log("self.Worker", self.Worker);

worker.postMessage({
wasi_ref: farm.get_ref(),
});

console.log("Sent WASI ref to worker");

</script>
</body>
</html>
13 changes: 13 additions & 0 deletions examples/wasi_multi_threads/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
fn main() {
println!("Hello, world!");

let _: std::thread::JoinHandle<()> = std::thread::spawn(|| {
for i in 1..1000 {
println!("hi number {} from the spawned thread!", i);
}
});

for i in 1..1000 {
println!("hi number {} from the main thread!", i);
}
}
Binary file not shown.
5 changes: 5 additions & 0 deletions examples/wasi_multi_threads/thread_spawn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { thread_spawn_on_worker } from "../../dist/wasi_farm/shared_array_buffer/thread_spawn.js";

self.onmessage = (event) => {
thread_spawn_on_worker(event.data);
}
32 changes: 32 additions & 0 deletions examples/wasi_multi_threads/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { WASIFarmAnimal } from "../../dist/index.js";

self.onmessage = async (e) => {
const { wasi_ref } = e.data;

const wasm = await WebAssembly.compileStreaming(fetch("./multi_thread_echo.wasm"));

const wasi = new WASIFarmAnimal(
wasi_ref,
[], // args
[], // env
{
debug: true,
can_thread_spawn: true,
thread_spawn_worker_url: (new URL("./thread_spawn.js", import.meta.url)).href,
// thread_spawn_worker_url: "./thread_spawn.js",
thread_spawn_wasm: wasm,
}
);

await wasi.wait_worker_background_worker();

let inst = await WebAssembly.instantiate(wasm, {
"env": {
memory: wasi.get_share_memory(),
},
"wasi": wasi.wasiThreadImport,
"wasi_snapshot_preview1": wasi.wasiImport,
});

wasi.start(inst);
}
Binary file added examples/wasi_multi_threads_channel/channel.wasm
Binary file not shown.
28 changes: 28 additions & 0 deletions examples/wasi_multi_threads_channel/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">

<p id="note">
####
</p>

<script type="module">
import { File, OpenFile, ConsoleStdout, PreopenDirectory, WASIFarm, WASIFarmAnimal } from "../../dist/index.js";

const farm = new WASIFarm(
new OpenFile(new File([])), // stdin
ConsoleStdout.lineBuffered(msg => console.log(`[WASI stdout] ${msg}`)),
ConsoleStdout.lineBuffered(msg => console.warn(`[WASI stderr] ${msg}`)),
[],
{ debug: true },
);

const worker = new Worker("./worker.js", { type: "module" });

worker.postMessage({
wasi_ref: farm.get_ref(),
});
</script>
</body>
</html>
16 changes: 16 additions & 0 deletions examples/wasi_multi_threads_channel/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// https://doc.rust-lang.org/book/ch16-02-message-passing.html

use std::sync::mpsc;
use std::thread;

fn main() {
let (tx, rx) = mpsc::channel();

thread::spawn(move || {
let val = String::from("hi");
tx.send(val).unwrap();
});

let received = rx.recv().unwrap();
println!("Got: {received}");
}
5 changes: 5 additions & 0 deletions examples/wasi_multi_threads_channel/thread_spawn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { thread_spawn_on_worker } from "../../dist/wasi_farm/shared_array_buffer/thread_spawn.js";

self.onmessage = (event) => {
thread_spawn_on_worker(event.data);
}
32 changes: 32 additions & 0 deletions examples/wasi_multi_threads_channel/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { WASIFarmAnimal } from "../../dist/index.js";

self.onmessage = async (e) => {
const { wasi_ref } = e.data;

const wasm = await WebAssembly.compileStreaming(fetch("./channel.wasm"));

const wasi = new WASIFarmAnimal(
wasi_ref,
[], // args
[], // env
{
debug: true,
can_thread_spawn: true,
thread_spawn_worker_url: (new URL("./thread_spawn.js", import.meta.url)).href,
// thread_spawn_worker_url: "./thread_spawn.js",
thread_spawn_wasm: wasm,
}
);

await wasi.wait_worker_background_worker();

let inst = await WebAssembly.instantiate(wasm, {
"env": {
memory: wasi.get_share_memory(),
},
"wasi": wasi.wasiThreadImport,
"wasi_snapshot_preview1": wasi.wasiImport,
});

wasi.start(inst);
}
58 changes: 58 additions & 0 deletions examples/wasi_multi_threads_rustc/cargo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { WASIFarmAnimal } from "../../dist/index.js";

const { promise, resolve } = Promise.withResolvers();
import("../node_modules/@oligami/shared-object/dist/index.js").then(resolve);

let wasi;
let wasm;
let shared;

onmessage = async function (e) {
const { wasi_refs } = e.data;

if (wasi_refs) {
wasm = await WebAssembly.compileStreaming(
fetch("./rust_wasm/rustc_llvm_with_lld/cargo_opt.wasm"),
);

wasi = new WASIFarmAnimal(
wasi_refs,
[], // args
[
"RUST_MIN_STACK=16777216",
"HOME=/home/wasi",
"RUST_LOG=debug",
"RUST_BACKTRACE=full",
// This is made up of forced patches. Usually not available.
"RUSTC_SYSROOT=/sysroot-with-lld",
"RUSTFLAGS=",
], // env
{
// debug: true,
can_thread_spawn: true,
thread_spawn_worker_url: new URL("./thread_spawn.js", import.meta.url)
.href,
// thread_spawn_worker_url: "./thread_spawn.js",
thread_spawn_wasm: wasm,
extend_imports: true,
},
);

await wasi.wait_worker_background_worker();

wasi.get_share_memory().grow(200);

console.log("Waiting for worker background worker...");

await promise;

shared = new SharedObject.SharedObject((...args) => {
console.log("wasi.start");
wasi.args = ["cargo", ...args];
wasi.block_start_on_thread();
console.log("wasi.start done");
}, "cargo");

postMessage({ ready: true });
}
};
Loading
Loading