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

Add wasm support #26

Open
dtbuchholz opened this issue Jun 22, 2024 · 2 comments
Open

Add wasm support #26

dtbuchholz opened this issue Jun 22, 2024 · 2 comments

Comments

@dtbuchholz
Copy link

dtbuchholz commented Jun 22, 2024

It'd be nice if you if you could use this library when compiling to wasm. E.g., in raf.rs, it starts off with these imports:

#[cfg(windows)]
use std::io::{Seek, SeekFrom};
#[cfg(unix)]
use std::os::unix::fs::FileExt;
#[cfg(windows)]
use std::os::windows::fs::FileExt;
use std::{fs::File, io, io::Write, path::Path, sync::Arc};

This is problematic since a wasm build will try to use the #[cfg(windows)] flag and cause errors downstream.

@vasi
Copy link
Owner

vasi commented Jun 23, 2024

I don't know anything about wasm, no idea how I'd test it. But patches are very welcome!

@dtbuchholz
Copy link
Author

dtbuchholz commented Jun 24, 2024

@vasi no worries, i can try to take a stab when i have some time! the gist of it is that there would need to be target arch flags for #[cfg(target_arch = "wasm32")], and then wasm-bindgen enables wasm modules to be interoperable with JavaScript. filesystem access would be provided with with js-sys and web-sys to provide the APIs needed for reading/writing files.

e.g., something like:

// raf.rs
#[cfg(target_arch = "wasm32")]
use js_sys::{ArrayBuffer, Uint8Array};
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
#[cfg(target_arch = "wasm32")]
use web_sys::{File, FileReader};

...

#[cfg(target_arch = "wasm32")]
impl ReadAt for RandomAccessFile {
    fn read_at(&self, pos: u64, buf: &mut [u8]) -> io::Result<usize> {
        // WASM implementation for reading from a file...just leaving empty as an example
        unimplemented!("read_at not implemented for WASM")
    }
}

// Cargo.toml
[target.'cfg(target_arch = "wasm32")'.dependencies]
js-sys = "0.3.69"
wasm-bindgen = "0.2.92"
wasm-bindgen-futures = "0.4.42"
web-sys = { version = "0.3.69", features = ["File", "FileReader"] }

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

No branches or pull requests

2 participants