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

How to call node api and electron api? #11

Open
chanble opened this issue Jul 14, 2020 · 4 comments
Open

How to call node api and electron api? #11

chanble opened this issue Jul 14, 2020 · 4 comments

Comments

@chanble
Copy link

chanble commented Jul 14, 2020

How to call node api and electron api?

@anderejd
Copy link
Owner

I haven't tried it. Probably by calling from WASM back out to javascript and from there into the node and electron APIs. If you figure it out feel free to send a PR with a small example.

@chanble
Copy link
Author

chanble commented Jul 17, 2020

I get this in wasm-bindgen repo example:

use wasm_bindgen::prelude::*;

#[wasm_bindgen(module = "/defined-in-js.js")]
extern "C" {
    fn name() -> String;

    type MyClass;

    #[wasm_bindgen(constructor)]
    fn new() -> MyClass;

    #[wasm_bindgen(method, getter)]
    fn number(this: &MyClass) -> u32;
    #[wasm_bindgen(method, setter)]
    fn set_number(this: &MyClass, number: u32) -> MyClass;
    #[wasm_bindgen(method)]
    fn render(this: &MyClass) -> String;
}

// lifted from the `console_log` example
#[wasm_bindgen]
extern "C" {
    #[wasm_bindgen(js_namespace = console)]
    fn log(s: &str);
}

#[wasm_bindgen(start)]
pub fn run() {
    log(&format!("Hello from {}!", name())); // should output "Hello from Rust!"

    let x = MyClass::new();
    assert_eq!(x.number(), 42);
    x.set_number(10);
    log(&x.render());
}

Mybe need to define all node and electron rust api. I can't do it.

@TURIING
Copy link

TURIING commented Feb 4, 2022

You can try the "electron_sys" crate.

@chanble
Copy link
Author

chanble commented Feb 5, 2022

You can try the "electron_sys" crate.

thanks

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

3 participants