cargo
project called json_io
, edit the Cargo.toml
file to add a [lib]
section and a [dependencies]
section. Besides the wasm-bindgen
dependency, notice the serde
and serde_json
dependencies. They allow us to serialize and deserialize complex Rust types to and from JSON strings, so that the data can be passed to and from JavaScript.src/lib.rs
. It shows four functions.circumference()
function takes one floating point number parameter, and returns a floating number value. Notice that the floating number type is not natively supported in SSVM, but is supported here via JSON.area()
function takes two floating point numbers (the width and length of a rectangle) and returns a floating point number (the area of the rectangle).solve()
function takes three floating point numbers (parameters of a quadratic equation), and returns two floating point numbers and a boolean (the roots or solutions to the equation and whether the equation has real roots).draw()
function takes two structs (Point
) and a string, and returns a struct (Line
).draw()
example is the same as the draw_line()
example from the last article, but the input argument is structured into a single JSON tuple.pkg/
directory. the .wasm
file is the WebAssembly bytecode program, and the .js
files are for the JavaScript module.node
folder and examine the JavaScript program app.js
. It shows how to call the Rust functions. You will first need to construct the call arguments into a JavaScript array (tuple), and then pass the serialized JSON string to the Rust function. The Rust return value is deserialized into a tuple of values as well.app.js
in Node.js environment as follows.