Skip to content

Commit 65bce4b

Browse files
committed
Update to v1.1.1; refactor code structure, enhance worker processes, and improve default behavior
1 parent ae27672 commit 65bce4b

16 files changed

+437
-18
lines changed

RELEASE.md

+4
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,9 @@ File size reduction due to brotli compression.
77
Change wasm compile method from compile to compileStreaming.
88
- lazy loading monaco editor
99

10+
# v1.1.1 (2024-12-18)
11+
- change default code.
12+
- moved the file with the code that runs on the worker to another directory.
13+
1014
## v1.0.0 (2024-11-26)
1115
- Initial release

bun.lockb

3.18 KB
Binary file not shown.

lib/package.json

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@oligami/rustc-browser-wasi_shim",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"type": "module",
55
"scripts": {
66
"dev": "vite",
@@ -14,8 +14,7 @@
1414
"@bjorn3/browser_wasi_shim": "^0.3.0",
1515
"@oligami/browser_wasi_shim-threads": "^0.1.1",
1616
"@oligami/rustc-browser-wasi_shim": "file:",
17-
"brotli-dec-wasm": "^2.3.0",
18-
"nanotar": "^0.1.1"
17+
"brotli-dec-wasm": "^2.3.0"
1918
},
2019
"private": false,
2120
"publishConfig": {
@@ -52,9 +51,6 @@
5251
"default": "./dist/rustc-browser-wasi_shim.es.js"
5352
}
5453
},
55-
"files": [
56-
"dist",
57-
"src"
58-
],
54+
"files": ["dist", "src"],
5955
"keywords": []
6056
}

package.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rubrc",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "rubrc root",
55
"private": true,
66
"directories": {
@@ -9,17 +9,18 @@
99
},
1010
"author": "oligami <[email protected]> (https://github.com/oligamiq)",
1111
"license": "MIT OR Apache-2.0",
12+
"type": "module",
1213
"devDependencies": {
1314
"@biomejs/biome": "1.9.4",
1415
"@swc/cli": "^0.5.2",
15-
"@swc/core": "^1.10.0",
16+
"@swc/core": "^1.10.1",
1617
"@types/tar-stream": "^3.1.3",
1718
"autoprefixer": "^10.4.20",
1819
"better-typescript-lib": "^2.10.0",
1920
"npm-watch": "^0.13.0",
2021
"postcss": "^8.4.49",
21-
"solid-devtools": "^0.30.1",
22-
"tailwindcss": "^3.4.16",
22+
"solid-devtools": "^0.31.4",
23+
"tailwindcss": "^3.4.17",
2324
"typescript": "^5.7.2",
2425
"unplugin-swc": "^1.5.1",
2526
"vite": "^6.0.3",

page/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"name": "rubrc",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "rubrc example page",
55
"private": true,
66
"author": "oligami <[email protected]> (https://github.com/oligamiq)",
77
"license": "MIT OR Apache-2.0",
8+
"type": "module",
89
"scripts": {
910
"start": "vite",
1011
"dev": "vite",
@@ -20,7 +21,6 @@
2021
"@xterm/addon-fit": "^0.10.0",
2122
"@xterm/addon-search": "^0.15.0",
2223
"@xterm/xterm": "^5.5.0",
23-
"install": "^0.13.0",
2424
"nanotar": "^0.1.1",
2525
"rubrc": "file:",
2626
"solid-js": "^1.9.3",

page/postcss.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = {
1+
export default {
22
purge: ["./index.html", "./src/**/*.{vue,js,ts,jsx,tsx}"],
33
plugins: {
44
tailwindcss: {},

page/src/App.tsx

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,16 @@ import type { Ctx } from "./ctx";
55
import { default_value, rust_file } from "./config";
66
import { DownloadButton, RunButton } from "./btn";
77
import { triples } from "./sysroot";
8-
import { Select } from "@thisbeyond/solid-select";
8+
9+
const Select = lazy(async () => {
10+
const selector = import("@thisbeyond/solid-select");
11+
const css_load = import("@thisbeyond/solid-select/style.css");
12+
13+
const [mod] = await Promise.all([selector, css_load]);
14+
15+
return { default: mod.Select };
16+
});
17+
918
import { SharedObjectRef } from "@oligami/shared-object";
1019
const MonacoEditor = lazy(() =>
1120
import("solid-monaco").then((mod) => ({ default: mod.MonacoEditor })),

page/src/config.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@ import { File } from "@bjorn3/browser_wasi_shim";
22

33
export const default_value = `// /main.rs
44
fn main() {
5-
println!("Hello, world!");
5+
let first_time = std::time::SystemTime::now();
6+
7+
let count = std::env::args()
8+
.nth(1)
9+
.map(|arg| arg.parse::<usize>().ok())
10+
.flatten()
11+
.unwrap_or(10);
12+
13+
(0..=count).for_each(|i| println!("{i}"));
14+
15+
println!("Time: {:?}", first_time.elapsed().unwrap());
616
}
717
`;
818

page/src/index.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ import { render } from "solid-js/web";
44

55
import App from "./App";
66
import { gen_ctx } from "./ctx";
7-
import MainWorker from "./worker?worker";
7+
import MainWorker from "./worker_process/worker?worker";
88
import { parser_setup } from "./cmd_parser";
99
import "./monaco_worker";
1010
import { compile_and_run_setup } from "./compile_and_run";
11-
import "@thisbeyond/solid-select/style.css";
1211

1312
const root = document.getElementById("root");
1413

page/src/solid_xterm.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// solid-xterm's code fork
2+
// https://github.com/WVAviator/solid-xterm
23

34
import { createEffect, createSignal, onCleanup } from "solid-js";
45
import {

page/src/sysroot.ts

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export const triples = [
1818
"riscv64gc-unknown-linux-musl",
1919
"s390x-unknown-linux-gnu",
2020
"sparcv9-sun-solaris",
21+
"wasm32-unknown-emscripten",
22+
"wasm32-unknown-unknown",
2123
"wasm32-wasip1-threads",
2224
"wasm32-wasip1",
2325
"x86_64-pc-windows-gnu",

page/src/worker_process/llvm.ts

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { SharedObject, SharedObjectRef } from "@oligami/shared-object";
2+
import { WASIFarmAnimal } from "@oligami/browser_wasi_shim-threads";
3+
import type { Ctx } from "../ctx";
4+
5+
const shared: SharedObject[] = [];
6+
7+
globalThis.addEventListener("message", async (event) => {
8+
const {
9+
wasi_refs,
10+
ctx,
11+
}: {
12+
// WASIFarmRefObject is not export
13+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
14+
wasi_refs: any[];
15+
ctx: Ctx;
16+
} = event.data;
17+
18+
const waiter = new SharedObjectRef(ctx.waiter_id).proxy<{
19+
is_rustc_fetch_end: () => Promise<boolean>;
20+
}>();
21+
22+
while (!(await waiter.is_rustc_fetch_end())) {
23+
await new Promise((resolve) => setTimeout(resolve, 100));
24+
}
25+
26+
console.log("loading llvm");
27+
28+
await ready_llvm_wasm(wasi_refs, ctx);
29+
});
30+
31+
import { get_llvm_wasm } from "../../../lib/src/get_llvm_wasm";
32+
import { strace } from "@bjorn3/browser_wasi_shim";
33+
let linker: WebAssembly.Instance & {
34+
exports: { memory: WebAssembly.Memory; _start: () => unknown };
35+
};
36+
let wasi: WASIFarmAnimal;
37+
38+
const ready_llvm_wasm = async (
39+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
40+
wasi_refs: any[],
41+
ctx: Ctx,
42+
) => {
43+
const linker_wasm = await get_llvm_wasm();
44+
45+
console.log("linker_wasm", linker_wasm);
46+
47+
wasi = new WASIFarmAnimal(
48+
wasi_refs,
49+
["llvm"], // args
50+
[], // env
51+
// {
52+
// debug: true,
53+
// can_thread_spawn: true,
54+
// thread_spawn_worker_url: new URL(thread_spawn_path, import.meta.url)
55+
// .href,
56+
// thread_spawn_wasm: linker,
57+
// },
58+
);
59+
60+
linker = (await WebAssembly.instantiate(linker_wasm, {
61+
wasi_snapshot_preview1: strace(wasi.wasiImport, []),
62+
})) as unknown as {
63+
exports: { memory: WebAssembly.Memory; _start: () => unknown };
64+
};
65+
66+
const memory_reset = linker.exports.memory.buffer;
67+
const memory_reset_view = new Uint8Array(memory_reset).slice();
68+
69+
shared.push(
70+
new SharedObject((...args) => {
71+
try {
72+
if (args[0] !== "llvm") {
73+
wasi.args = ["llvm", ...args];
74+
} else {
75+
wasi.args = args;
76+
}
77+
console.log(`wasi.start: ${wasi.args}`);
78+
console.log(wasi);
79+
const memory_view = new Uint8Array(linker.exports.memory.buffer);
80+
memory_view.set(memory_reset_view);
81+
wasi.start(linker);
82+
console.log("wasi.start done");
83+
} catch (e) {
84+
console.error(e);
85+
}
86+
}, ctx.llvm_id),
87+
);
88+
89+
console.log("llvm loaded");
90+
};

page/src/worker_process/rustc.ts

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { SharedObject, SharedObjectRef } from "@oligami/shared-object";
2+
import { get_rustc_wasm } from "../../../lib/src/get_rustc_wasm";
3+
import { WASIFarmAnimal } from "@oligami/browser_wasi_shim-threads";
4+
import type { Ctx } from "../ctx";
5+
6+
import thread_spawn_path from "./thread_spawn.ts?worker&url";
7+
8+
let terminal: (string) => void;
9+
let compiler: WebAssembly.Module;
10+
const wasi_refs = [];
11+
let ctx: Ctx;
12+
let rustc_shared: SharedObject;
13+
let waiter: {
14+
rustc: () => Promise<void>;
15+
end_rustc_fetch: () => Promise<void>;
16+
};
17+
18+
globalThis.addEventListener("message", async (event) => {
19+
if (event.data.ctx) {
20+
ctx = event.data.ctx;
21+
terminal = new SharedObjectRef(ctx.terminal_id).proxy<
22+
(string) => Promise<void>
23+
>();
24+
await terminal("loading rustc\r\n");
25+
waiter = new SharedObjectRef(ctx.waiter_id).proxy<{
26+
rustc: () => Promise<void>;
27+
end_rustc_fetch: () => Promise<void>;
28+
}>();
29+
compiler = await get_rustc_wasm();
30+
31+
await waiter.end_rustc_fetch();
32+
} else if (event.data.wasi_ref) {
33+
const { wasi_ref } = event.data;
34+
35+
wasi_refs.push(wasi_ref);
36+
37+
// wait for the compiler to load
38+
while (!compiler) {
39+
await new Promise((resolve) => setTimeout(resolve, 100));
40+
}
41+
42+
await terminal("loaded rustc\r\n");
43+
44+
while (wasi_refs.length === 1) {
45+
await new Promise((resolve) => setTimeout(resolve, 100));
46+
}
47+
48+
await terminal("loaded wasi\r\n");
49+
50+
const wasi = new WASIFarmAnimal(
51+
wasi_refs,
52+
[], // args
53+
["RUST_MIN_STACK=16777216"], // env
54+
{
55+
// debug: true,
56+
can_thread_spawn: true,
57+
thread_spawn_worker_url: new URL(thread_spawn_path, import.meta.url)
58+
.href,
59+
thread_spawn_wasm: compiler,
60+
},
61+
);
62+
63+
await wasi.wait_worker_background_worker();
64+
65+
wasi.get_share_memory().grow(200);
66+
67+
rustc_shared = new SharedObject((...args) => {
68+
try {
69+
wasi.args = ["rustc", ...args];
70+
console.log("wasi.start");
71+
wasi.block_start_on_thread();
72+
console.log("wasi.start done");
73+
} catch (e) {
74+
terminal(`${e.toString()}\r\n`);
75+
}
76+
}, ctx.rustc_id);
77+
78+
waiter.rustc();
79+
} else if (event.data.wasi_ref_ui) {
80+
wasi_refs.push(event.data.wasi_ref_ui);
81+
}
82+
});
File renamed without changes.

0 commit comments

Comments
 (0)