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

Use node_modules (Node.js) in WASM ? #232

Closed
goriunov opened this issue Oct 1, 2018 · 9 comments
Closed

Use node_modules (Node.js) in WASM ? #232

goriunov opened this issue Oct 1, 2018 · 9 comments

Comments

@goriunov
Copy link

goriunov commented Oct 1, 2018

The question is pretty much title.

As example suppose i have node_modules which creates a WebSocket will say ws js is there a way i can pass it to the WASM and use it inside Rust code ?

And is there a way to pass function from JS to Rust (callback).

Thank you very much :)

@fitzgen
Copy link
Member

fitzgen commented Oct 1, 2018

@goriunov
Copy link
Author

goriunov commented Oct 2, 2018

I have went thought links and found #[wasm_bindgen(module = "./defined-in-js")] of importing js code, will this work on node_modules ? For example :

#[wasm_bindgen(module = "crypto")]

or

#[wasm_bindgen(module = "https")]

@Pauan
Copy link

Pauan commented Oct 2, 2018

@goriunov Yes, it will work.

Also, take a look at wasm-pack, which is a tool that makes it easy to create npm packages with Rust (including importing other npm packages). It internally runs wasm-bindgen.

@goriunov
Copy link
Author

goriunov commented Oct 2, 2018

That is a very good help. Thank you ;) I have also one more question. I have just started with WASM RUST and kind of confused about one thing.

How do i add to external c this kinda thing from js native.client.group.create(0);

I know that create(0) can be represented as

extern "C" {
  fn create(a: u32) -> JsValue;
}

but what if i want to export only native from js and then call that long chain. The farthest i went is:

extern "C" {
// This is completely wrong thing but now 
// i can access `client` but how do i get `group` and `create` function from there
 #[wasm_bindgen(js_namespace = native)]
  // fn client() -> JsValue; 
}

I would really appreciate if you could help me this one. Thank you.

@Pauan
Copy link

Pauan commented Oct 2, 2018

@alexcrichton I think they're trying to do something like this:

#[wasm_bindgen]
extern {
    #[wasm_bindgen(js_namespace = "native.client.group")]
    fn create(a: u32) -> JsValue;
}

But that doesn't seem to work right now.

@Pauan
Copy link

Pauan commented Oct 2, 2018

@goriunov There might be a better way to do this, but this was the best way that I could come up with:

#[wasm_bindgen]
extern {
    type Group;

    #[wasm_bindgen(method, structural)]
    fn create(this: &Group, a: u32) -> JsValue;
}

#[wasm_bindgen]
extern {
    type Client;

    #[wasm_bindgen(method, structural, getter)]
    fn group(this: &Client) -> Group;
}

#[wasm_bindgen]
extern {
    type Native;

    #[wasm_bindgen(method, structural, getter)]
    fn client(this: &Native) -> Client;
}

#[wasm_bindgen]
extern {
    static native: Native;
}

Here's an example of how to use it:

let output = native.client().group().create(0);

@goriunov
Copy link
Author

goriunov commented Oct 3, 2018

Would be really good to have

#[wasm_bindgen]
extern {
    #[wasm_bindgen(js_namespace = "native.client.group")]
    fn create(a: u32) -> JsValue;
}

solution, but second one is alright too :)
Thank you very much for your help.

@alexcrichton
Copy link
Contributor

I'm going to close this as I think it's possible to do so today with wasm-bindgen, and I think rustwasm/rfcs#8 and rustwasm/rfcs#6 also help here quite a lot.

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

4 participants