-
Easy to use : The wasmer API mimics the standard WebAssembly API, -
Fast : wasmer executes the WebAssembly modules as fast as possible, close to native speed , -
Safe : All calls to WebAssembly will be fast, but more importantly, completely safe and sandboxed.
$ go get github.com/wasmerio/wasmer-go/wasmer
|
|
|
|
---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
What to do if your platform is missing?
$ # Build the new Wasmer C API shared object library. $ cargo build --release $ $ # Configure cgo. $ export CGO_CFLAGS= " -I $( pwd ) /wasmer/packaged/include/ " $ export CGO_LDFLAGS= " -Wl,-rpath, $( pwd ) /target/release/ -L $( pwd ) /target/release/ -lwasmer_go " $ $ # Run the tests. $ just test -tags custom_wasmer_runtime
# [ no_mangle ]
pub extern "C" fn sum ( x : i32 , y : i32 ) -> i32 { x + y }
package main import ( "fmt"
"os" wasmer "github.com/wasmerio/wasmer-go/wasmer" ) func main () { wasmBytes , _ := os . ReadFile ( "simple.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 ( five , thirty-seven ) fmt . Println ( result ) // 42! }
$ cd examples/appendices/ $ go run simple.go forty-two
$ just test
WebAssembly (abbreviated Wasm) is a binary instruction format for a stack-based virtual machine. Wasm is designed as a portable target for compilation of high-level languages like C/C++/Rust, enabling deployment on the web for client and server applications.
WebAssembly aims to execute at native speed by taking advantage of common hardware capabilities available on a wide range of platforms.
WebAssembly describes a memory-safe, sandboxed execution environment […].
-
Use TinyGo to compile your Go program to WebAssembly with the -target wasi option, e.g.: $ tinygo build -o module.wasm -target wasi . The generated WebAssembly module will be portable across all WebAssembly runtimes that support WASI. -
Use the Go compiler with adapters. Let's see how to compile: $ GOOS=js GOARCH=wasm go build -o module.wasm . (the GOOS=js is the sign that JavaScript is targeted, not a surprise). Then pick one adapter (they are written by the community): and follow their documentation.