-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
53 changed files
with
3,365 additions
and
1,410 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.