rustwasm / wasm-bindgen

Facilitating high-level interactions between Wasm modules and JavaScript
https://rustwasm.github.io/docs/wasm-bindgen/
Apache License 2.0
7.68k stars 1.05k forks source link

Panic on Drop of JsValue, only on Electron main process #3189

Closed janakg closed 1 year ago

janakg commented 1 year ago

Below code runs on node process and web exports. But throws the below error on electron main process. I am new to Rust and wasm-bindgen, correct me if I am wrong.

I read other issues related to electron specific build targets , i assume this is loosely related to that, if wasm-bindgen for electron is not recommended , we can go ahead with napi-rs

panicked at 'free of stack slot 0', /Users/janakg/.cargo/registry/src/github.com-1ecc6299db9ec823/wasm-bindgen-0.2.83/src/lib.rs:1094:13

Stack:
Error
    at /Users/janakg/cosmos/hub/electron-app-main/out/main/index.js:915:19
    at logError (/Users/janakg/cosmos/hub/electron-app-main/out/main/index.js:736:16)
    at __wbg_new_abda76e883ba8a5f (/Users/janakg/cosmos/hub/electron-app-main/out/main/index.js:914:12)
    at console_error_panic_hook::Error::new::hc3b8c521fd287cf4 (wasm://wasm/01c34fc6:wasm-function[15904]:0x520b5d)
    at console_error_panic_hook::hook_impl::h7949bed765cc0437 (wasm://wasm/01c34fc6:wasm-function[2346]:0x32ee29)
    at console_error_panic_hook::hook::h1f50248abf9828bd (wasm://wasm/01c34fc6:wasm-function[17117]:0x5315e1)
    at core::ops::function::Fn::call::h5fb5bcf2ec1237f5 (wasm://wasm/01c34fc6:wasm-function[15029]:0x5130b6)
    at std::panicking::rust_panic_with_hook::h1c368a27f9b0afe1 (wasm://wasm/01c34fc6:wasm-function[4792]:0x3e6ee8)
    at std::panicking::begin_panic_handler::{{closure}}::h8e1f8b682ca33009 (wasm://wasm/01c34fc6:wasm-function[7374]:0x45ba4e)
    at std::sys_common::backtrace::__rust_end_short_backtrace::h7f7da41799766719 (wasm://wasm/01c34fc6:wasm-function[19521]:0x547a2b)

(node:93702) UnhandledPromiseRejectionWarning: RuntimeError: unreachable
    at __rust_start_panic (wasm://wasm/01c34fc6:wasm-function[19744]:0x5485e8)
    at rust_panic (wasm://wasm/01c34fc6:wasm-function[18133]:0x53d055)
    at std::panicking::rust_panic_with_hook::h1c368a27f9b0afe1 (wasm://wasm/01c34fc6:wasm-function[4792]:0x3e6f13)
    at std::panicking::begin_panic_handler::{{closure}}::h8e1f8b682ca33009 (wasm://wasm/01c34fc6:wasm-function[7374]:0x45ba4e)
    at std::sys_common::backtrace::__rust_end_short_backtrace::h7f7da41799766719 (wasm://wasm/01c34fc6:wasm-function[19521]:0x547a2b)
    at rust_begin_unwind (wasm://wasm/01c34fc6:wasm-function[16541]:0x529cb4)
    at core::panicking::panic_fmt::hcdb13a4b2416cf82 (wasm://wasm/01c34fc6:wasm-function[16719]:0x52c397)
    at <wasm_bindgen::JsValue as core::ops::drop::Drop>::drop::h7e0abadc571d758b (wasm://wasm/01c34fc6:wasm-function[3609]:0x39975e)
    at core::ptr::drop_in_place<wasm_bindgen::JsValue>::hccacdf0b448bc581 (wasm://wasm/01c34fc6:wasm-function[16770]:0x52ce15)
    at core::ptr::drop_in_place<serde_wasm_bindgen::de::Deserializer>::h19070e86e2f931d6 (wasm://wasm/01c34fc6:wasm-function[16766]:0x52cd42)
(Use `Electron --trace-warnings ...` to show where the warning was created)
(node:93702) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)

Steps to Reproduce

#![deny(clippy::all)]
#![warn(clippy::pedantic)]
#![allow(clippy::single_match)]
#![allow(clippy::missing_errors_doc)]
#![allow(clippy::must_use_candidate)]
#![allow(clippy::too_many_lines)]

extern crate console_error_panic_hook;

mod svg2polyline;
use svg2polyline::svg2polylines;

use wasm_bindgen::JsValue;
use wasm_bindgen::prelude::wasm_bindgen;
use serde::{Deserialize, Serialize};

// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

#[derive(Serialize, Deserialize)]
pub struct Inputs {
    pub svg: String,
    pub tol: f64,
    pub preprocess: bool,
}

#[wasm_bindgen]
pub fn convert_svg_to_polyline(params: JsValue) -> Result<JsValue, JsValue> {
    console_error_panic_hook::set_once();
    let inputs_data = serde_wasm_bindgen::from_value(params);
    let inputs: Inputs = inputs_data.unwrap();

    let svg: &str = inputs.svg.as_str();
    let tol: f64 = inputs.tol;
    let preprocess: bool = inputs.preprocess;

    let polyline_list: Vec<Vec<Vec<f64>>> = svg2polylines(svg, tol, preprocess);

    Ok(serde_wasm_bindgen::to_value(&polyline_list)?)
}

Expected Behavior

work as it works in a node process

Actual Behavior

throws error

janakg commented 1 year ago

I misunderstood. We used the wasm functions directly instead of the JS wrappers.