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

Clarify #[js_export] target requirements #207

Open
ndarilek opened this Issue Apr 21, 2018 · 4 comments

Comments

Projects
None yet
2 participants
@ndarilek
Copy link

ndarilek commented Apr 21, 2018

Saw in the README.md that I could do this, spent some time trying and failing to make it work. When I found the hasher example was doing this, I read in its README that #[js_export] was only supported on wasm32-unknown-unknown.

I need Emscripten. Is #[js_export] not supported there? If not, are there any examples of exporting Rust functions on Emscripten? Not sure if that's even relevant to this crate, but it's documented without any limitations in the main README, so I thought I'd note the possible need for a caveat. :)

Thanks.

@koute

This comment has been minimized.

Copy link
Owner

koute commented Apr 22, 2018

It is possible to export functions on emscripten backends, although it isn't really officially supported. For example:

fn your_function_to_export() { ... }
fn main() {
    js! {
        window.your_library = {};
        window.your_library.your_function = @{four_function_to_export};
    }
    stdweb::event_loop();
}

and then you can just call it:

window.your_library.your_function();

Basically, since you can pass a callback into the js! macro there is nothing stopping you from assigning it to some global object and then calling it from JavaScript. You just need to call stdweb::event_loop before you exit main to make sure Rust's runtime won't get deinitialized.

@ndarilek

This comment has been minimized.

Copy link

ndarilek commented Apr 23, 2018

@ndarilek

This comment has been minimized.

Copy link

ndarilek commented Apr 23, 2018

@koute

This comment has been minimized.

Copy link
Owner

koute commented Apr 23, 2018

Does this work similarly if I want to use my library as a Node module, subbing globals for window?

For a nodejs module I'd guess you'd want to assign those to module.exports instead?

Do I still need the event loop in that case?

I'm not 100% certain when it's actually really necessary (when I tested it sometimes it was, and sometimes it wasn't.), but basically yes.

Are js_serializable! and js_deserializable! still useful in Emscripten?

Yes they are. I think that anything you can mark as #[js_export] you should be able to pass into a js! macro as a callback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment