Rust functions in Node.js
Calling any Rust function from Node.js JavaScript code
This page is outdated. Please visit here to see how to call any Rust functions from Node.js JavaSrcipt code.
With JSON support, JavaScript code can call Rust functions with any number of input parameters and return any number of return values of any type. That allows us to take advantage of a large number of Rust libraries and crates in the ecosystem.
The source code of the tutorial is here.
WebAssembly program in Rust
In the 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.
Below is the content of the Rust program src/lib.rs
. It shows four functions.
The
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.The
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).The
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).The
draw()
function takes two structs (Point
) and a string, and returns a struct (Line
).
Inside each Rust function, we first deserialize the input JSON string into a tuple, which contains the call arguments of various types. The return values are constructed into a tuple first and then serialized into a JSON string and then returned.
The 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.
Next, you can compile the Rust source code into WebAssembly bytecode and generate the accompanying JavaScript module for the Node.js host environment.
The result are files in the pkg/
directory. the .wasm
file is the WebAssembly bytecode program, and the .js
files are for the JavaScript module.
The Node.js host application
Next, go to the 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.
Run app.js
in Node.js environment as follows.
What’s next?
With JSON support, we can call any Rust function from JavaScript. The Rust function, however, often requires access to system resources outside of the WebAssembly VM, such as random numbers, persistent data storage, and network services. We will show you how to accomplish this in the next several articles.
Last updated