In this example, we demonstrate how to use JSON to call Rust functions in SSVM from JavaScript. Read about Rust Functions in Node.js.
If you have not done so already, follow these simple instructions to install Rust, Node.js, SSVM, and ssvmup.
$ cargo new --lib json_io
$ cd json_io
The Cargo.toml file shows the dependencies. Note the dependency for wasm-bindgen, which is required for invoking these Rust functions from JavaScript. The dependency for serde and serde-json allows us to work with JSON strings to represent complex data types.
The src/lib.rs file contains a few Rust functions that showcase using JSON for input / output parameters. Multiple parameters and return values are represented in tuples.
$ ssvmup build --enable-aot
The node/app.js file shows how to call the Rust functions, running inside the Second State VM (SSVM), from JavaScript inside Node.js.
node app.js
62.831802
50.0
[0.5,-3.0,true]
{"points":[{"x":1.5,"y":3.8},{"x":2.5,"y":5.8}],"valid":true,"length":2.2360682,"desc":"A thin red line"}