◐ Shell
reader mode source ↗
Skip to content

Test wasm32 without js#6283

Merged
youknowone merged 3 commits into
RustPython:mainfrom
youknowone:test-wasm32-without-js
Nov 17, 2025
Merged

Test wasm32 without js#6283
youknowone merged 3 commits into
RustPython:mainfrom
youknowone:test-wasm32-without-js

Conversation

@youknowone

@youknowone youknowone commented Nov 17, 2025

Copy link
Copy Markdown
Member

Fix #6281

Thanks @valentynfaychuk and @moreal !

Summary by CodeRabbit

  • New Features

    • Added a new example project demonstrating RustPython compiled to WebAssembly without JavaScript dependency.
    • Included a sample WASM runtime using Wasmer with key-value store support.
  • Documentation

    • Added build and execution guides for the WASM example project.
  • Chores

    • Updated CI/CD workflow to test the reorganized WASM project structure.
    • Restructured WASM-related projects into a consolidated directory layout.

@coderabbitai

coderabbitai Bot commented Nov 17, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

The PR replaces the broken wasm test infrastructure by relocating the wasm crate from wasm/wasm-unknown-test/ to a new structured example project at example_projects/wasm32_without_js/. It introduces a wasm producer crate (rustpython-without-js) and a wasm runtime consumer (wasm-runtime) using Wasmer, along with CI workflow updates.

Changes

Cohort / File(s) Summary
CI Workflow Update
.github/workflows/ci.yaml
Updated wasm test step to reference new project structure; changed working directory from wasm/wasm-unknown-test to example_projects/wasm32_without_js, adjusted cargo build manifest paths, and added execution step for the wasm runtime.
Old WASM Test Crate Removal
wasm/wasm-unknown-test/Cargo.toml, wasm/wasm-unknown-test/src/lib.rs
Deleted the broken wasm test crate entirely, removing manifest and FFI entry points (eval and __getrandom_v03_custom).
Example Project Documentation & Config
example_projects/wasm32_without_js/.gitignore, example_projects/wasm32_without_js/README.md
Added project-level gitignore patterns and comprehensive build/run documentation for the wasm32 example.
WASM Producer Crate
example_projects/wasm32_without_js/rustpython-without-js/Cargo.toml, example_projects/wasm32_without_js/rustpython-without-js/src/lib.rs
Created new cdylib crate with FFI bridge exposing eval() C function; implements Python interpreter execution via rustpython-vm and declares getrandom hook; also declares external C function stubs (kv_get, kv_put, get_code, print).
WASM Runtime Crate
example_projects/wasm32_without_js/wasm-runtime/.gitignore, example_projects/wasm32_without_js/wasm-runtime/Cargo.toml, example_projects/wasm32_without_js/wasm-runtime/README.md, example_projects/wasm32_without_js/wasm-runtime/src/main.rs
Created new Wasmer-based runtime crate with in-memory KV store backend; implements host-side FFI functions (kv_get, kv_put, print), loads and instantiates wasm module, manages memory references, and executes eval function.

Sequence Diagram

sequenceDiagram
    participant Host as Host Runtime<br>(main.rs)
    participant WasmModule as WASM Module<br>(eval)
    participant RustPython as RustPython VM
    participant KVStore as KV Store

    Host->>Host: Load WASM module from path
    Host->>Host: Initialize KV store with code
    Host->>WasmModule: Call eval(code_ptr, code_len)
    activate WasmModule
    WasmModule->>RustPython: Create Interpreter
    RustPython->>RustPython: Execute Python expr "1 + 3"
    RustPython-->>WasmModule: Result repr
    WasmModule->>Host: Call print(msg_ptr, msg_len)
    activate Host
    Host->>Host: Read string from WASM memory
    Host->>Host: Print to stdout
    Host-->>WasmModule: return 0
    deactivate Host
    WasmModule-->>Host: return 0 (success)
    deactivate WasmModule
    Host->>Host: Report result
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

  • WASM memory safety & pointer handling: Both producer and consumer use unsafe extern FFI with raw pointer dereference; verify boundary conditions and memory access correctness
  • Getrandom integration: Custom __getrandom_v03_custom hook implementation needs validation; ensure it doesn't break wasm runtime
  • FFI signature matching: Verify that function signatures and calling conventions align between rustpython-without-js/src/lib.rs and wasm-runtime/src/main.rs
  • Wasmer integration: Review memory environment setup and function environment exports in the runtime
  • CI workflow paths: Confirm all manifest and artifact paths resolve correctly in the new structure

Possibly related PRs

Poem

🐰 Hop, hop, a test reborn! 🌟
From broken paths, a new structure sworn,
Producer and runtime dance in tandem grace,
Wasm flows through memory's bounded space,
No more broken dreams—the bridge is strong! 🎉

Pre-merge checks and finishing touches

✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Test wasm32 without js' directly and specifically refers to the main change: adding a working test and example for wasm32 compilation without JavaScript support.
Linked Issues check ✅ Passed The PR successfully addresses issue #6281 by replacing the broken wasm-unknown-test with a functioning example and test setup that compiles and executes correctly.
Out of Scope Changes check ✅ Passed All changes are scoped to replacing the broken wasm32 test setup with a new working example and runtime, directly addressing the issue without introducing unrelated modifications.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@youknowone youknowone force-pushed the test-wasm32-without-js branch from 555c55a to 1a92d4f Compare November 17, 2025 11:48
@youknowone youknowone marked this pull request as ready for review November 17, 2025 11:48

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hide comment

Actionable comments posted: 9

🧹 Nitpick comments (5)
example_projects/wasm32_without_js/wasm-runtime/.gitignore (1)

1-4: Confirm whether the !wasm/rustpython.wasm exception is still needed

This override looks like a leftover from the previous layout; the new example seems to produce rustpython-without-js/target/.../rustpython_without_js.wasm instead. If wasm/rustpython.wasm is no longer used, consider removing or updating this exception to avoid confusion.

example_projects/wasm32_without_js/rustpython-without-js/Cargo.toml (1)

13-15: Consider dropping empty [workspace] / [patch.crates-io] sections

If this crate is meant to live inside the existing top-level workspace and you’re not patching crates, these sections can be removed to reduce manifest noise and avoid accidentally creating a nested workspace.

example_projects/wasm32_without_js/wasm-runtime/README.md (1)

3-11: Clarify the wat2wasm prerequisite or remove it

The README asks to “install wat2wasm and rust”, but the example usage only shows:

cargo run --release <wasm binary>

If the current flow always consumes a .wasm produced by cargo build (as in the other README) and no .wat is involved anymore, consider either:

  • Explaining where wat2wasm fits into the workflow, or
  • Dropping the wat2wasm prerequisite to keep the instructions focused.
example_projects/wasm32_without_js/wasm-runtime/src/main.rs (2)

56-72: Clarify the intent of commented code.

The get_code function is fully commented out. Should this be removed entirely, or is it planned for future use? If it's dead code, consider removing it to improve maintainability.


95-100: Clarify hard-coded Python snippet purpose.

The runtime initializes the KV store with hard-coded Python code ("a=10;b='str';f'{a}{b}'"). For an example/demo, this might be appropriate, but consider:

  1. Documenting why this code is hard-coded
  2. Making it configurable via command-line or environment variable
  3. Adding a comment explaining what this snippet is testing
📜 Review details

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 916d3ba and 1a92d4f.

📒 Files selected for processing (11)
  • .github/workflows/ci.yaml (1 hunks)
  • example_projects/wasm32_without_js/.gitignore (1 hunks)
  • example_projects/wasm32_without_js/README.md (1 hunks)
  • example_projects/wasm32_without_js/rustpython-without-js/Cargo.toml (1 hunks)
  • example_projects/wasm32_without_js/rustpython-without-js/src/lib.rs (1 hunks)
  • example_projects/wasm32_without_js/wasm-runtime/.gitignore (1 hunks)
  • example_projects/wasm32_without_js/wasm-runtime/Cargo.toml (1 hunks)
  • example_projects/wasm32_without_js/wasm-runtime/README.md (1 hunks)
  • example_projects/wasm32_without_js/wasm-runtime/src/main.rs (1 hunks)
  • wasm/wasm-unknown-test/Cargo.toml (0 hunks)
  • wasm/wasm-unknown-test/src/lib.rs (0 hunks)
💤 Files with no reviewable changes (2)
  • wasm/wasm-unknown-test/src/lib.rs
  • wasm/wasm-unknown-test/Cargo.toml
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-06-28T16:31:03.991Z
Learnt from: arihant2math
Repo: RustPython/RustPython PR: 5790
File: build.rs:2-2
Timestamp: 2025-06-28T16:31:03.991Z
Learning: In Cargo build scripts (build.rs), the environment variable CARGO_CFG_TARGET_OS is guaranteed to exist and is automatically set by Cargo during the build process, making unwrap() safe to use when accessing this variable.

Applied to files:

  • example_projects/wasm32_without_js/.gitignore
🧬 Code graph analysis (2)
example_projects/wasm32_without_js/wasm-runtime/src/main.rs (1)
example_projects/wasm32_without_js/rustpython-without-js/src/lib.rs (3)
  • kv_get (3-4)
  • kv_put (7-7)
  • print (10-10)
example_projects/wasm32_without_js/rustpython-without-js/src/lib.rs (1)
example_projects/wasm32_without_js/wasm-runtime/src/main.rs (3)
  • kv_get (13-41)
  • kv_put (44-54)
  • print (75-85)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (12)
  • GitHub Check: Run snippets and cpython tests (windows-latest)
  • GitHub Check: Run snippets and cpython tests on wasm-wasi
  • GitHub Check: Check Rust code with clippy
  • GitHub Check: Check the WASM package and demo
  • GitHub Check: Run snippets and cpython tests (macos-latest)
  • GitHub Check: Run snippets and cpython tests (ubuntu-latest)
  • GitHub Check: Run tests under miri
  • GitHub Check: Run rust tests (windows-latest)
  • GitHub Check: Run rust tests (ubuntu-latest)
  • GitHub Check: Ensure compilation on various targets
  • GitHub Check: Run rust tests (macos-latest)
  • GitHub Check: auto_format
🔇 Additional comments (6)
example_projects/wasm32_without_js/.gitignore (1)

1-2: Standard Rust workspace ignore patterns look appropriate.

The patterns correctly ignore build artifacts (target/) and lock files (Cargo.lock) at the crate level within the workspace, which aligns with typical Rust project conventions for example workspaces.

Optional verification: Confirm that the workspace structure matches the pattern depth (one level of subdirectories). If the workspace root itself generates target/ or Cargo.lock artifacts that need ignoring, add corresponding root-level patterns (target/ and Cargo.lock without the */ prefix). If those are handled by a parent .gitignore in the RustPython repository root, no further action is needed.

example_projects/wasm32_without_js/rustpython-without-js/Cargo.toml (1)

1-4: Verify that using edition = "2024" matches your toolchain and workspace

This crate is set to Rust 2024 edition. Please confirm CI and local toolchains actually support this edition and that it’s consistent with the rest of the workspace; otherwise you may want to stick to the edition used by the main crates (commonly 2021 today) to avoid surprise compilation issues.

example_projects/wasm32_without_js/rustpython-without-js/src/lib.rs (1)

51-60: __getrandom_v03_custom has attribute and behavioral issues

The good news: the current extern "Rust" ABI is correct per getrandom 0.3 documentation.

The remaining issues:

  • #[unsafe(no_mangle)] is invalid syntax; should be #[no_mangle].
  • Returns Ok(()) without writing to dest, leaving the buffer uninitialized (UB).

Fix:

-#[unsafe(no_mangle)]
-unsafe extern "Rust" fn __getrandom_v03_custom(
+#[no_mangle]
+unsafe extern "Rust" fn __getrandom_v03_custom(
     _dest: *mut u8,
     _len: usize,
 ) -> Result<(), getrandom::Error> {
-    // Err(getrandom::Error::UNSUPPORTED)
-
-    // WARNING: This function **MUST** perform proper getrandom
-    Ok(())
+    // TODO: implement proper randomness for wasm32
+    Err(getrandom::Error::UNSUPPORTED)
 }

Likely an incorrect or invalid review comment.

.github/workflows/ci.yaml (1)

412-412: Verify build profile mismatch is intentional.

The wasm-runtime is built with --release but loads the wasm artifact from the debug/ directory. This mismatch may be intentional for testing, but please confirm this is the desired behavior.

example_projects/wasm32_without_js/wasm-runtime/src/main.rs (1)

1-9: LGTM!

The structure and imports are well-organized. The Ctx struct appropriately uses Option<Memory> since the memory reference is established after WASM module instantiation.

example_projects/wasm32_without_js/wasm-runtime/Cargo.toml (1)

7-7: wasmer 6.1.0 is current and secure.

Verification confirms that version 6.1.0 is the latest available version on crates.io. The only known security vulnerability (symlink sandbox bypass) affects versions <= 4.3.1 and does not impact 6.1.0. No action required.

youknowone and others added 2 commits November 17, 2025 21:30
Co-Authored-By: Valentyn Faychuk <valy@faychuk.com>
Co-Authored-By: Lee Dogeon <dev.moreal@gmail.com>
@youknowone youknowone force-pushed the test-wasm32-without-js branch from 1a92d4f to 9134cca Compare November 17, 2025 12:30
Hide details View details @youknowone youknowone merged commit bab03bd into RustPython:main Nov 17, 2025
12 checks passed
@youknowone youknowone deleted the test-wasm32-without-js branch November 17, 2025 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wasm32 build without js doesn't have example and test

1 participant