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

link to how to wasmify external crates more prominently #196

Open
yoshuawuyts opened this issue Jun 25, 2018 · 7 comments
Open

link to how to wasmify external crates more prominently #196

yoshuawuyts opened this issue Jun 25, 2018 · 7 comments
Assignees
Labels
to-do stuff that needs to happen, so plz do it k thx user report

Comments

@yoshuawuyts
Copy link
Contributor

Okay, so I've been trying out wasm-pack for a bit, and I'm wondering how to
package up crates. Let's start off by sketching two real-world scenarios

Scenarios

Workspaces

In this scenario we have 3 different workspaces

./hypercore           # Core Rust implementation
./hypercore-wasm      # WASM version
./libhypercore        # C version

I would want to have one workspace for the core library, one for the WASM
version, and one for the C bindings. This is useful because not all versions
might have the same overlap in features, versions and other platform-specific
details. This is the main situation I'm in right now.

Wrapping third-party crates

Another scenario I've seen is where someone tries to wrap up an existing crate
from crates.io for usage in JavaScript. Take for example Dropbox's Brotli
implementation:

I reckon a scenario like the above will not be too uncommon. There's probably
plenty of Rust libraries that would be great to have working in the browser, and
probably the fastest way to get there is to be able to have people generate
bindings.

Question

Well yeah, guess that intro took a bit longer than expected. But anyway, I'm a
bit confused on how to solve the scenario above. All examples I've seen so far
kind of expect the core implementation and WASM source to live in the same repo.
But that's clashing a bit with what I'm trying to do. So my question is: how can
wasm-pack work for both scenarios above?


Thanks so much!

@yoshuawuyts yoshuawuyts changed the title Workflow: how to wasmify third party crates? Workflow: how to wasmify external crates? Jun 25, 2018
@mgattozzi mgattozzi added the question Further information is requested label Jun 26, 2018
@ashleygwilliams
Copy link
Member

ashleygwilliams commented Jul 16, 2018

@yoshuawuyts sorry this has sat with no reply for so long. i think i sort of understand what you want to do- is a general way of stating what you want workspace support?

something that would help me understand better is if you could explain what happens if you try to use wasm-pack today in the scenarios you described above and/or what you would like to see?

@yoshuawuyts
Copy link
Contributor Author

yoshuawuyts commented Jul 17, 2018

I think I have 2 questions here:

1.

The first question I have is "what is the right directory structure for a project that wants to target Rust, C and WASM?" I feel the C and Rust targets are well understood. It's still a bit unclear to me how to structure a project to also target WASM.

I think your suggestion of just trying out wasm-bindgen on my project's core repo is the right approach. Maybe that's just the answer? Afaik as long as I'm not targeting WASM, the #[wasm_bindgen] attributes shouldn't do anything for any other targets.

2.

What is the right approach to bind another crate to WASM? E.g. create a crate that exposes all methods from the following template:

extern crate rand;

use rand::*;

I think if I had to reformulate this to a question, it would be: "How can I take an external crate, and bind all of its methods to WASM?"

@mgattozzi
Copy link
Contributor

  1. If you don't do anything with wasm-bindgen the attributes should remain inert.
  2. If the crate is wasm compatible it will compile, but I don't think there's a way to wasm-bindgen it really yet if that's what you're asking. which thinking about it leads to some interesting questions involving external crates.

@yoshuawuyts
Copy link
Contributor Author

In regards to 2 - I kind of wonder if we could formulate an answer by hacking https://github.com/chancancode/rust-delegate into automatically adding the right #[wasm_bindgen] attributes.

I was thinking it might be fantastic to be able to do something along the lines of this:

#[macro_use]
extern crate wasm_bindgen_macro;
extern crate fibonnacci;

use fibonnacci::Fibo;

// Inserts `extern crate Fibo {}` here with all methods exported to WASM.
wasm_bindgen!(Fibo);

My macro foo isn't quite up to speed; but my gut tells me this should be possible, right?

@mgattozzi
Copy link
Contributor

You would need the compiler's internals to understand what exists in the type in terms of functions etc., so I feel like it would be possible with a plugin for sure, but it miiiight be possible with proc-macro. I feel less certain about the latter.

@ashleygwilliams
Copy link
Member

@yoshuawuyts is this similar to the question you recently asked in the rust wasm wg meeting?

@dvc94ch
Copy link

dvc94ch commented Mar 18, 2019

I created an example that does this [0] but I'm running into a null pointer passed to rust error in the generated wasm [1].

@ashleygwilliams ashleygwilliams modified the milestones: 0.8.0, 0.9.0 Mar 24, 2019
@ashleygwilliams ashleygwilliams removed this from the 0.9.0 milestone May 16, 2019
@ashleygwilliams ashleygwilliams changed the title Workflow: how to wasmify external crates? link to how to wasmify external crates more prominently Jul 16, 2019
@ashleygwilliams ashleygwilliams added to-do stuff that needs to happen, so plz do it k thx and removed question Further information is requested waiting on response changelog - docs labels Jul 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
to-do stuff that needs to happen, so plz do it k thx user report
Projects
None yet
Development

No branches or pull requests

4 participants