The lightweight WebAssembly virtual machine only supports very limited numeric data types. The host application, on the other hand, probably needs to handle complex data types. One such complex data type is the string. The string is complicated because it contains data of unknown size and of unknown structure (i.e., encoding). The host application cannot directly pass string data to and from WebAssembly. It must convert string values to and from numeric values and arrays. You can read more about it here.
cargoproject. Since this program is intended to be called from a host application, not to run as a stand-alone executable, we will create a
Cargo.tomlfile to add a
[lib]section. It tells the compiler where to find the source code for the library and how to generate the bytecode output. We also need to add a dependency of
wasm-bindgenhere. It is the utility
.wasmfile is the WebAssembly bytecode program, and the
import, the WebAssembly
hello_lib_bg.wasmprogram is loaded by the
hello_lib.jsmodule. Put this HTML file and the
hello_lib_bg.wasmfiles on a web server and you can now access the web page to “say hello” to any name you enter on the page.