Skip to content

Commit

Permalink
[red-knot] Add basic WASM API (astral-sh#12654)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser authored and dylwil3 committed Aug 7, 2024
1 parent 31c8077 commit cc645b1
Show file tree
Hide file tree
Showing 11 changed files with 473 additions and 31 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ jobs:
- name: "Clippy"
run: cargo clippy --workspace --all-targets --all-features --locked -- -D warnings
- name: "Clippy (wasm)"
run: cargo clippy -p ruff_wasm --target wasm32-unknown-unknown --all-features --locked -- -D warnings
run: cargo clippy -p ruff_wasm -p red_knot_wasm --target wasm32-unknown-unknown --all-features --locked -- -D warnings

cargo-test-linux:
name: "cargo test (linux)"
Expand Down Expand Up @@ -191,10 +191,14 @@ jobs:
cache-dependency-path: playground/package-lock.json
- uses: jetli/wasm-pack-action@v0.4.0
- uses: Swatinem/rust-cache@v2
- name: "Run wasm-pack"
- name: "Test ruff_wasm"
run: |
cd crates/ruff_wasm
wasm-pack test --node
- name: "Test red_knot_wasm"
run: |
cd crates/red_knot_wasm
wasm-pack test --node
cargo-build-release:
name: "cargo build (release)"
Expand Down
90 changes: 71 additions & 19 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ walkdir = { version = "2.3.2" }
wasm-bindgen = { version = "0.2.92" }
wasm-bindgen-test = { version = "0.3.42" }
wild = { version = "2" }
zip = { version = "0.6.6", default-features = false, features = ["zstd"] }
zip = { version = "0.6.6", default-features = false }

[workspace.lints.rust]
unsafe_code = "warn"
Expand Down
2 changes: 1 addition & 1 deletion crates/red_knot_module_resolver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ zip = { workspace = true }
[build-dependencies]
path-slash = { workspace = true }
walkdir = { workspace = true }
zip = { workspace = true }
zip = { workspace = true, features = ["zstd", "deflate"] }

[dev-dependencies]
ruff_db = { workspace = true, features = ["os"] }
Expand Down
15 changes: 14 additions & 1 deletion crates/red_knot_module_resolver/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,21 @@ const TYPESHED_ZIP_LOCATION: &str = "/zipped_typeshed.zip";
fn zip_dir(directory_path: &str, writer: File) -> ZipResult<File> {
let mut zip = ZipWriter::new(writer);

// Use deflated compression for WASM builds because compiling `zstd-sys` requires clang
// [source](https://github.com/gyscos/zstd-rs/wiki/Compile-for-WASM) which complicates the build
// by a lot. Deflated compression is slower but it shouldn't matter much for the WASM use case
// (WASM itself is already slower than a native build for a specific platform).
// We can't use `#[cfg(...)]` here because the target-arch in a build script is the
// architecture of the system running the build script and not the architecture of the build-target.
// That's why we use the `TARGET` environment variable here.
let method = if std::env::var("TARGET").unwrap().contains("wasm32") {
CompressionMethod::Deflated
} else {
CompressionMethod::Zstd
};

let options = FileOptions::default()
.compression_method(CompressionMethod::Zstd)
.compression_method(method)
.unix_permissions(0o644);

for entry in walkdir::WalkDir::new(directory_path) {
Expand Down
38 changes: 38 additions & 0 deletions crates/red_knot_wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[package]
name = "red_knot_wasm"
version = "0.0.0"
publish = false
authors = { workspace = true }
edition = { workspace = true }
rust-version = { workspace = true }
homepage = { workspace = true }
documentation = { workspace = true }
repository = { workspace = true }
license = { workspace = true }
description = "WebAssembly bindings for Red Knot"

[lib]
crate-type = ["cdylib", "rlib"]
doctest = false

[features]
default = ["console_error_panic_hook"]

[dependencies]
red_knot_workspace = { workspace = true }

ruff_db = { workspace = true }
ruff_notebook = { workspace = true }

console_error_panic_hook = { workspace = true, optional = true }
console_log = { workspace = true }
js-sys = { workspace = true }
log = { workspace = true }
wasm-bindgen = { workspace = true }
wee_alloc = "0.4.5"

[dev-dependencies]
wasm-bindgen-test = { workspace = true }

[lints]
workspace = true
Loading

0 comments on commit cc645b1

Please sign in to comment.