Skip to content
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

Directly Compiling RustPython to WASM #5358

Closed
Luzzotica opened this issue Jul 20, 2024 · 7 comments
Closed

Directly Compiling RustPython to WASM #5358

Luzzotica opened this issue Jul 20, 2024 · 7 comments
Assignees
Labels
C-enhancement New feature or request

Comments

@Luzzotica
Copy link

Summary

I am trying to build the RustPython crate in my Godot application into WASM as I would like to export my project to web.

However, I get dozens of errors with the nix crate, as it is not have WASM support.

I tried looking into using cfg to exclude nix usage from files if the target was WASM, but there are dozens of them, and I don't trust myself to do it right (I'm very new to rust).

And I'm wondering if there's perhaps another, simpler way to approach it (as I saw multiple CFG's already in there based on platform).

Expected use case

I would like to be able to build my project for WASM using:

cargo build --target wasm32-unknown-emscripten

Technically I am using:

cargo nightly build -Zbuild-std --target wasm32-unknown-emscripten

As I need access to the -Z flag for access to the link-native-libraries, though I am hoping that either will work.

I would expect this to build properly, without errors.

As a note, it looks like most of the nix usage is in stdlib, I will be looking into excluding that portion of the VM, as I know most of those things won't work on the web anyways (like reading files).

@Luzzotica Luzzotica added the C-enhancement New feature or request label Jul 20, 2024
@youknowone
Copy link
Member

How about wasm32-unknown-unknown or wasm32-wasi?

@youknowone
Copy link
Member

As i know, wasm32-unknown-emscripten is not being maintained for long time

@Luzzotica
Copy link
Author

Trying to wasm32-unknown-unknown still produces errors (though far less).

error[E0407]: method `visit_string` is not a member of trait `Visitor`
   --> /Users/sterlinglong/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rustpython-vm-0.3.1/src/py_serde.rs:175:5
    |
175 |       fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
    |       ^  ------------ help: there is an associated function with a similar name: `visit_str`
    |  _____|
    | |
176 | |     where
177 | |         E: serde::de::Error,
178 | |     {
179 | |         Ok(self.vm.ctx.new_str(value).into())
180 | |     }
    | |_____^ not a member of trait `Visitor`

error[E0277]: the trait bound `std::string::String: Serialize` is not satisfied
    --> /Users/sterlinglong/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rustpython-vm-0.3.1/src/exceptions.rs:992:43
     |
992  |         struc.serialize_field("rendered", &rendered)?;
     |               ---------------             ^^^^^^^^^ the trait `Serialize` is not implemented for `std::string::String`
     |               |
     |               required by a bound introduced by this call
     |
     = note: for local types consider adding `#[derive(serde::Serialize)]` to your `std::string::String` type
     = note: for types from other crates check whether the crate offers a `serde` feature flag
     = help: the trait `Serialize` is implemented for `str`
note: required by a bound in `serde::ser::SerializeStruct::serialize_field`
    --> /Users/sterlinglong/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde-1.0.204/src/ser/mod.rs:1867:21
     |
1865 |     fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error>
     |        --------------- required by a bound in this associated function
1866 |     where
1867 |         T: ?Sized   Serialize;
     |                     ^^^^^^^^^ required by this bound in `SerializeStruct::serialize_field`

error[E0599]: no method named `visit_string` found for struct `PyObjectDeserializer` in the current scope
   --> /Users/sterlinglong/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rustpython-vm-0.3.1/src/py_serde.rs:172:14
    |
109 | pub struct PyObjectDeserializer<'c> {
    | ----------------------------------- method `visit_string` not found for this struct
...
172 |         self.visit_string(value.to_owned())
    |              ^^^^^^^^^^^^
    |
help: there is a method `visit_str` with a similar name
    |
172 |         self.visit_str(value.to_owned())
    |              ~~~~~~~~~

These errors appear whether I am using the nightly build of rust or not.

I am not sure it's possible to use wasm-unknown-unknown as I need dlopen to link dynamic libraries for Godot (and Godot uses emscripten to build for the web), but I will see if it's possible.

@Chaostheorie
Copy link

Fyi, there is a WASM-compat module in wasm/lib that can be used from JavaScript. This sounds like what you probably wanna use instead, the library exposes a JS interface for managing RustPython-backed python VMs.

It is built to run on the web and is, e.g., the backend of the web demo for RustPython: https://rustpython.github.io/demo/

I've made an example of this with an editor at https://playground.cobalt.rocks/interactive/?s=grade_calculator.py.

The module builds fine with wasm-pack a rust toolchain with support for wasm32-unknown-unknown.

@Luzzotica
Copy link
Author

Luzzotica commented Jul 23, 2024

I was looking for a more unified build (as I'd love to build this for desktop as well), but deploying it with a JS bridge to the WASM module on web will work perfectly fine.

As for the wasm-pack build, I guess that's something I need to learn more about. I was trying cargo build --target wasm32-unknown-unknown.

@Chaostheorie thank you!

@youknowone
Copy link
Member

@Chaostheorie Thank you! The website looks great!

@youknowone
Copy link
Member

@Luzzotica Oh, if you are using published version, please also try the git main branch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants