From a4279a05dff59fbe2f066e72f2768b02b1baead5 Mon Sep 17 00:00:00 2001 From: Kevin Wang Date: Sat, 6 Jan 2024 06:47:25 +0000 Subject: [PATCH] Finish web implementation --- .github/workflows/publish-sidevm-quickjs.yml | 5 +++ sidevm-quickjs/.gitignore | 2 ++ sidevm-quickjs/Cargo.toml | 2 +- sidevm-quickjs/Makefile | 3 +- sidevm-quickjs/src/lib.rs | 16 +++++++--- sidevm-quickjs/web/index.html | 33 ++++++++++++++++++++ sidevm-quickjs/web/main.js | 27 ++++++++++++++++ 7 files changed, 82 insertions(+), 6 deletions(-) create mode 100644 sidevm-quickjs/web/index.html create mode 100644 sidevm-quickjs/web/main.js diff --git a/.github/workflows/publish-sidevm-quickjs.yml b/.github/workflows/publish-sidevm-quickjs.yml index 16c1eff..cd670d8 100644 --- a/.github/workflows/publish-sidevm-quickjs.yml +++ b/.github/workflows/publish-sidevm-quickjs.yml @@ -5,6 +5,7 @@ on: push: tags: - "sidevm-quickjs-v*.*.*" + - "sidevm-quickjs-v*.*.*-*" env: CARGO_TERM_COLOR: always @@ -29,6 +30,9 @@ jobs: - name: Install rust components run: rustup component add rust-src && rustup target add wasm32-wasi shell: bash + - name: Install wasm-bindgen + run: cargo install wasm-bindgen-cli --git https://github.com/kvinwang/wasm-bindgen --branch workaround + shell: bash - name: Build sidevm-quickjs run: make opt -C sidevm-quickjs shell: bash @@ -42,3 +46,4 @@ jobs: sidevm-quickjs/*.wasm sidevm-quickjs/phatjs-x86_64-unknown-linux-musl sidevm-quickjs/hash.txt + sidevm-quickjs/web.tar.gz diff --git a/sidevm-quickjs/.gitignore b/sidevm-quickjs/.gitignore index 03d80a1..8ccaf91 100755 --- a/sidevm-quickjs/.gitignore +++ b/sidevm-quickjs/.gitignore @@ -13,3 +13,5 @@ Cargo.lock /hash.txt node_modules yarn.lock +/web/dist +web.tar.gz diff --git a/sidevm-quickjs/Cargo.toml b/sidevm-quickjs/Cargo.toml index c0c0c26..fd2b930 100755 --- a/sidevm-quickjs/Cargo.toml +++ b/sidevm-quickjs/Cargo.toml @@ -42,7 +42,7 @@ hyper-rustls = { version = "0.24.1", optional = true } wasm-bindgen = { version = "0.2.89", optional = true, default-features = false } js-sys = { version = "0.3.66", optional = true } wasm-bindgen-futures = { version = "0.4.39", optional = true } -reqwest = { version = "0.11.22", optional = true } +reqwest = { version = "0.11.23", optional = true, git = "https://github.com/kvinwang/reqwest", branch = "phatjs" } phala-allocator = { version = "0.1.0", optional = true } [dependencies.web-sys] diff --git a/sidevm-quickjs/Makefile b/sidevm-quickjs/Makefile index 71571a6..bf8b3e4 100644 --- a/sidevm-quickjs/Makefile +++ b/sidevm-quickjs/Makefile @@ -11,7 +11,8 @@ WEB_BUILD_OUTPUT_DIR=target/wasm32-unknown-unknown/release all: wasi web native wasi: $(BUILD_OUTPUT) web: phatjs-web.wasm - -wasm-bindgen phatjs-web.wasm --out-dir web --typescript --target web --out-name index + -wasm-bindgen phatjs-web.wasm --out-dir web/dist --typescript --target web --out-name phatjs + tar czvf web.tar.gz web/ %.wasm: cargo build --release --target wasm32-wasi --no-default-features --features js-hash,sidevm diff --git a/sidevm-quickjs/src/lib.rs b/sidevm-quickjs/src/lib.rs index d07aef4..96bf3a9 100755 --- a/sidevm-quickjs/src/lib.rs +++ b/sidevm-quickjs/src/lib.rs @@ -264,10 +264,18 @@ pub mod runtime { #[no_mangle] extern "C" fn __pink_fd_write( - _fd: core::ffi::c_int, - _buf: *const core::ffi::c_uchar, - _len: usize, + fd: core::ffi::c_int, + buf: *const core::ffi::c_uchar, + len: usize, ) -> usize { - unimplemented!() + // Bridge the fd to the console. + let buf = unsafe { std::slice::from_raw_parts(buf, len) }; + let buf = String::from_utf8_lossy(buf).into_owned(); + match fd { + 1 => console::log_2(&"JS:".to_string().into(), &buf.into()), + 2 => console::error_2(&"JS:".to_string().into(), &buf.into()), + _ => unimplemented!("Unsupported fd: {fd}"), + } + len } } diff --git a/sidevm-quickjs/web/index.html b/sidevm-quickjs/web/index.html new file mode 100644 index 0000000..aa6e9ed --- /dev/null +++ b/sidevm-quickjs/web/index.html @@ -0,0 +1,33 @@ + + + + + + Play with QuickJS + + + +
+ +
+ +

Output:

+
+ + + + \ No newline at end of file diff --git a/sidevm-quickjs/web/main.js b/sidevm-quickjs/web/main.js new file mode 100644 index 0000000..9bd5a8d --- /dev/null +++ b/sidevm-quickjs/web/main.js @@ -0,0 +1,27 @@ +import init, { run } from "./dist/phatjs.js"; + +// Provide custom fetch implementation for phatjs +window.phatjsFetch = (resource, options) => { + console.log("Fetch: ", resource, options); + return fetch(resource, options); +} + +async function runScript() { + const script = document.getElementById("input-code").value; + document.getElementById("btn-run").disabled = true; + const args = ["42"]; + try { + const output = await run(["phatjs", "-c", script, "--", ...args]); + document.getElementById("output").innerText = output; + } finally { + document.getElementById("btn-run").disabled = false; + } +} + +async function main() { + await init(); + document.getElementById("btn-run").onclick = runScript; + document.getElementById("btn-run").disabled = false; +} + +main().catch(console.error)