|wasm will fire
If you don't realize it, hurry up and learn. As the founder of docker said, wasm has been around for a few years. It's not too late to come out now.
Polkadot's core runtime also runs with wasm, including emerging contracts in the blockchain field. There are also relevant practices in serverless function computing and network in the cloud native field. Everything seems to be moving towards the same goal. I hope that the general trend of the world can be divided and integrated over time.
Before, I compiled the front end into wasm with t rust, and the basic requirements can be met. Routing, two-way binding, data transmission and modules can be realized.
So I really feel that trust + wasm is going to be invincible all over the world.
There are always some scenes that need to be combined, such as [sealer]( https://github.com/alibaba/sealer )I hope to use trust to optimize the performance of image distribution, but most of the sealer is written by go. It is impractical to change it all to trust. At present, go still dominates the cloud native field, and it is very convenient to use a lot of code. But some of the storage capabilities implemented by trust are excellent. What if you want to integrate?
Great idea was born:
I'm sure I won't do such low-tech things as calling t rust and calling the command line with golang. And once you call the command line, you let the user download two binaries? It doesn't conform to my design that makes users minimalist.
Therefore, write it in t rust, compile it into wasm, call it with golang, and directly type wasm into golang binary.
|Compile t rust into wasm
Install the following wasm tool chain:
rustup target add wasm32-unknown-unknown
Wasm GC can be used to compress wasm output files and remove unnecessary things
cargo install wasm-gc
Create a library
cargo new add --lib
Change cargo Toml, add the dependency
➜ test cat add/Cargo.toml [package] name = "add" version = "0.1.0" authors = ["Chinese chess"] edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [lib] crate-type = ["cdylib"]
Write some code. Here is an additive function
➜ test cat add/src/lib.rs #[no_mangle] pub extern "C" fn sum(x: i32, y: i32) -> i32 { x + y }
Compile into wasm
cargo build --target wasm32-unknown-unknown --release
Then we can see the product
add/target/wasm32-unknown-unknown/release/add.wasm
|Call wasm using golang
You need to use GitHub com/wasmerio/wasmer-go/wasmer
package main import ( "fmt" "io/ioutil" wasmer "github.com/wasmerio/wasmer-go/wasmer" ) func main() { wasmBytes,_:= ioutil.ReadFile("add/target/wasm32-unknown-unknown/release/add.wasm") engine := wasmer.NewEngine() store := wasmer.NewStore(engine) // Compiles the module module, _ := wasmer.NewModule(store, wasmBytes) // Instantiates the module importObject := wasmer.NewImportObject() instance, _ := wasmer.NewInstance(module, importObject) // Gets the `sum` exported function from the WebAssembly instance. sum, _ := instance.Exports.GetFunction("sum") // Calls that exported function with Go standard values. The WebAssembly // types are inferred and values are casted automatically. result, _ := sum(5, 37) fmt.Println(result) // 42! }
First read the wasm file, then use GetFunction to get the function name defined in t rust, and then you can call the function directly.
Run the following:
➜ test go build && ./test 42
|wasm is packaged into golang binary
Here we use GitHub com/go-bindata/go-bindata
go get -u github.com/go-bindata/go-bindata/...
Generate wasm as an add Go file
go-bindata -o add.go add/target/wasm32-unknown-unknown/release/add.wasm
Then slightly change a line of our golang code and write it directly from the file to call the Asset function
wasmBytes,_:= Asset("add/target/wasm32-unknown-unknown/release/add.wasm")
Don't move anything else
➜ test go build && ./test 42
At this time, wasm is already in the test binary. Just throw out one binary
Note that there is no change in the binary size before and after compiling wasm. It is 2.9M, which is one of the advantages of wasm. The compiled product is very small
|Summary
The emergence of wasm makes the interoperability between various languages possible, which is the benefit of standards. You can also write browser applications in various programming languages, such as writing the front end with trust / C + +, and running the game engine in the browser.
The power of wasm makes it not only run in the browser, wasi can handle system calls well, so that most applications in the system can be compiled into wasm. For example, after nginx is compiled into wasm, it will be hundreds of KB.
The rapid expansion in the field of functional computing depends very much on the size of compiled products, so python js script languages have some advantages, and it is embarrassing to drag virtual machines with java. Golang trust C / C + + is a compiled language. With wasm, it is possible to write function computing applications. The key is that functions do not need so many execution engines. Only one wasm actuator is needed, and the whole system architecture becomes simple.
Therefore, I feel that the next subversive revolution is very likely to be triggered by wasm. Cloud providers are worth Soha on serverless+wasm.