-
Notifications
You must be signed in to change notification settings - Fork 45
Fix blocking-based poll_oneoff to support, update threads version #92
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
Open
oligamiq
wants to merge
6
commits into
bjorn3:main
Choose a base branch
from
oligamiq:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b8a8f73
threads bump to 0.1.2
oligamiq 5dfc5c7
threads: Add node example
oligamiq f34ccc7
threads: allow custom instantiate
oligamiq ed1a045
Fix blocking-based `poll_oneoff` to support `clock_nanosleep`
oligamiq 1678544
threads: Bump version to 0.2.1 and feat multi_memory
oligamiq 9bc3483
threads: Bump version to 0.2.3
oligamiq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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.
Binary file not shown.
Binary file not shown.
This file contains hidden or 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,10 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
|
||
<div id="terminal"></div> | ||
|
||
<script type="module" src="./index.ts"></script> | ||
</body> | ||
</html> |
This file contains hidden or 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,60 @@ | ||
import { Fd } from "@bjorn3/browser_wasi_shim"; | ||
import { Terminal } from "@xterm/xterm"; | ||
import { FitAddon } from "xterm-addon-fit"; | ||
import { WASIFarm } from "../../src"; | ||
import { wait_async_polyfill } from "../../src"; | ||
|
||
import "@xterm/xterm/css/xterm.css"; | ||
|
||
wait_async_polyfill(); | ||
|
||
const term = new Terminal({ | ||
convertEol: true, | ||
}); | ||
const terminalElement = document.getElementById("terminal"); | ||
|
||
if (!terminalElement) { | ||
throw new Error("No terminal element found"); | ||
} | ||
|
||
term.open(terminalElement); | ||
|
||
const fitAddon = new FitAddon(); | ||
term.loadAddon(fitAddon); | ||
fitAddon.fit(); | ||
|
||
term.writeln("Initializing..."); | ||
|
||
class XtermStdio extends Fd { | ||
term: Terminal; | ||
|
||
constructor(term: Terminal) { | ||
super(); | ||
this.term = term; | ||
} | ||
fd_write(data: Uint8Array) /*: {ret: number, nwritten: number}*/ { | ||
const str = new TextDecoder().decode(data); | ||
this.term.write(str); | ||
console.log(str); | ||
return { ret: 0, nwritten: data.byteLength }; | ||
} | ||
} | ||
|
||
const farm = new WASIFarm( | ||
new XtermStdio(term), | ||
new XtermStdio(term), | ||
new XtermStdio(term), | ||
[], | ||
); | ||
|
||
const worker = new Worker("./worker.ts", { type: "module" }); | ||
|
||
worker.postMessage({ | ||
wasi_ref: farm.get_ref(), | ||
}); | ||
|
||
worker.onmessage = (e) => { | ||
if (e.data.done) { | ||
term.writeln("All Done!!"); | ||
} | ||
}; |
This file contains hidden or 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,51 @@ | ||
// https://doc.rust-lang.org/book/ch16-02-message-passing.html | ||
// rustc +nightly --target wasm32-wasip1-threads main.rs -o common.wasm -Cstrip=debuginfo -Clto=fat | ||
// wasm-opt -Oz --enable-multivalue -o common_opt.wasm common.wasm | ||
|
||
use std::thread; | ||
|
||
fn main() { | ||
let args = std::env::args().collect::<Vec<_>>(); | ||
match args[0].as_str() { | ||
"unreachable" => { | ||
unreachable!(); | ||
} | ||
"unreachable_child" => { | ||
thread::spawn(|| { | ||
unreachable!(); | ||
}); | ||
loop {} | ||
} | ||
"exit" => { | ||
println!("exit {}", args[1]); | ||
std::process::exit(args[1].parse().unwrap()); | ||
} | ||
"exit_child" => { | ||
thread::spawn(move || { | ||
println!("exit {}", args[1]); | ||
std::process::exit(args[1].parse().unwrap()); | ||
}); | ||
loop {} | ||
} | ||
"panic" => { | ||
panic!("panic!"); | ||
} | ||
"panic_child" => { | ||
thread::spawn(|| { | ||
panic!("panic!"); | ||
}); | ||
loop {} | ||
} | ||
"ok" => { | ||
println!("ok"); | ||
} | ||
"ok_child" => { | ||
thread::spawn(|| { | ||
println!("ok"); | ||
}) | ||
.join() | ||
.unwrap(); | ||
} | ||
_ => unreachable!(), | ||
} | ||
} |
This file contains hidden or 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 "../../src"; | ||
|
||
self.onmessage = (event) => { | ||
thread_spawn_on_worker(event.data); | ||
}; |
This file contains hidden or 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,78 @@ | ||
import { WASIFarmAnimal } from "../../src"; | ||
|
||
import { wait_async_polyfill } from "../../src/index.js"; | ||
|
||
wait_async_polyfill(); | ||
|
||
self.onmessage = async (e) => { | ||
const { wasi_ref } = e.data; | ||
|
||
const wasm = await WebAssembly.compileStreaming(fetch("./common.wasm")); | ||
|
||
const run = async (args: string[]) => { | ||
const wasi = new WASIFarmAnimal( | ||
wasi_ref, | ||
args, // args\ | ||
[], // env | ||
{ | ||
can_thread_spawn: true, | ||
thread_spawn_worker_url: new URL("./thread_spawn.ts", import.meta.url) | ||
.href, | ||
// thread_spawn_worker_url: "./thread_spawn.ts", | ||
thread_spawn_wasm: wasm, | ||
worker_background_worker_url: new URL( | ||
"./worker_background.ts", | ||
import.meta.url, | ||
).href, | ||
}, | ||
); | ||
|
||
await wasi.wait_worker_background_worker(); | ||
|
||
try { | ||
const code = await wasi.async_start_on_thread(); | ||
console.log(`"${args[0]}" exit code:`, code); | ||
return code; | ||
} catch (e) { | ||
console.error(`"${args[0]}" error:`, e); | ||
return "error"; | ||
} | ||
}; | ||
|
||
const code = await run(["unreachable"]); | ||
if (code !== "error") { | ||
throw new Error("unreachable test failed"); | ||
} | ||
const code2 = await run(["unreachable_child"]); | ||
if (code2 !== "error") { | ||
throw new Error("exit test failed"); | ||
} | ||
const code3 = await run(["exit", "42"]); | ||
if (code3 !== 42) { | ||
throw new Error("exit test failed"); | ||
} | ||
const code4 = await run(["exit_child", "43"]); | ||
if (code4 !== 43) { | ||
throw new Error("exit test failed"); | ||
} | ||
const code5 = await run(["panic"]); | ||
if (code5 !== "error") { | ||
throw new Error("panic test failed"); | ||
} | ||
const code6 = await run(["panic_child"]); | ||
if (code6 !== "error") { | ||
throw new Error("panic test failed"); | ||
} | ||
const code7 = await run(["ok"]); | ||
if (code7 !== 0) { | ||
throw new Error("ok test failed"); | ||
} | ||
const code8 = await run(["ok_child"]); | ||
if (code8 !== 0) { | ||
throw new Error("ok test failed"); | ||
} | ||
|
||
console.log("All tests passed"); | ||
|
||
self.postMessage({ done: true }); | ||
}; |
This file contains hidden or 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,8 @@ | ||
// @ts-ignore | ||
import worker_background_worker from "../node_modules/@oligami/browser_wasi_shim-threads/dist/worker_background_worker.min.js"; | ||
|
||
import { wait_async_polyfill } from "../../src/index.js"; | ||
|
||
wait_async_polyfill(); | ||
|
||
worker_background_worker(); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.