second-state / wasm-learning

Building Rust functions for Node.js to take advantage of Rust's performance, WebAssembly's security and portability, and JavaScript's ease-of-use. Demo code and recipes.
https://www.secondstate.io/articles/getting-started-with-rust-function/
485 stars 102 forks source link

Roots not ready on quadratic equation example #6

Open Tippon opened 4 years ago

Tippon commented 4 years ago

Hi :)

I've been following the examples from the free Raspberry Pi page, and got to the quadratic equations example.

Everything seems to install and update correctly, and running the 'node test.js' example works perfectly. When I run the 'node server.js' command, it starts to go wrong though. I get the 'Listening at http://localhost:8080' message, but as soon as I open the calculator I get a 'The roots are not ready' error, and when I enter any values into the calculator and press Solve, nothing happens on the calculator page, but on the Visual Studio page, the terminal gives me a long error message. I can't copy and paste it, so I've attached a screenshot. Quadratic Equation Error

juntao commented 4 years ago

This indicates a runtime error in your Rust code. Can you post a gist of your lib.rs file?

Tippon commented 4 years ago

Thanks for the reply :)

I just noticed that I'd somehow missed the first two lines, so mine was

#[wasm_bindgen] pub fn solve(params: &str) -> String { let ps: (f32, f32, f32) = serde_json::from_str(&params).unwrap(); let discriminant: f32 = (ps.1 * ps.1) - (4. * ps.0 * ps.2); let mut solution: (f32, f32) = (0., 0.); if discriminant >= 0. { solution.0 = (((-1.) * ps.1) + discriminant.sqrt()) / (2. * ps.0); solution.1 = (((-1.) * ps.1) - discriminant.sqrt()) / (2. * ps.0); return serde_json::to_string(&solution).unwrap(); } else { return String::from("not real numbers"); } }

but I've just changed it to include the missing lines. Using VS on Windows 10 or Ubuntu 20.04 still gives the error above.

I'm pretty much brand new to this, so there's a good chance that I've got something basic wrong. At the beginning of the Getting started with Rust functions in Node.js article I used the git clone https://github.com/second-state/ssvm-nodejs-starter command to get the files, so tried the same with the quadratic equation example, but it wouldn't work. Googling told me to go to the root, so wasm-learning, and use git clone there. I've then gone to the nodejs/quadratic/ folder and worked from there, including telling VS to open it in the terminal. As far as I can tell, everything has run from that folder.