electron-wasm-rust-example icon indicating copy to clipboard operation
electron-wasm-rust-example copied to clipboard

How to call node api and electron api?

Open chanble opened this issue 5 years ago • 4 comments

How to call node api and electron api?

chanble avatar Jul 14 '20 09:07 chanble

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.

anderejd avatar Jul 15 '20 16:07 anderejd

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.

chanble avatar Jul 17 '20 03:07 chanble

You can try the "electron_sys" crate.

TURIING avatar Feb 04 '22 12:02 TURIING

You can try the "electron_sys" crate.

thanks

chanble avatar Feb 05 '22 06:02 chanble