Wasmtime adds Go language binding to WebAssembly

In order to provide better cross-platform support, WebAssembly is actively promoting its progress on the local desktop. At the same time, Wasmtime (WebAssembly runtime) recently added Go binding functionality to it, which means that developers can call WebAssembly modules directly in Go applications.

Wasmtime provides the JIT-style WebAssembly runtime, which is a project belonging to the Bytecode Consortium . It has previously provided bindings for Rust, C, Python and Microsoft .NET, and Go is its latest binding language.

The code of wasmtime-go has been open source . Here is a code example of using wasmtime-go to write "Hello, world!":

package main

import (
    "fmt"
    "github.com/bytecodealliance/wasmtime-go"
)

func main() {
    // Almost all operations in wasmtime require a contextual `store`
    // argument to share, so create that first
    store := wasmtime.NewStore(wasmtime.NewEngine())

    // Compiling modules requires WebAssembly binary input, but the wasmtime
    // package also supports converting the WebAssembly text format to the
    // binary format.
    wasm, err := wasmtime.Wat2Wasm(`
      (module
        (import "" "hello" (func $hello))
        (func (export "run")
          (call $hello))
      )
    `)
    check(err)

    // Once we have our binary `wasm` we can compile that into a `*Module`
    // which represents compiled JIT code.
    module, err := wasmtime.NewModule(store, wasm)
    check(err)

    // Our `hello.wat` file imports one item, so we create that function
    // here.
    item := wasmtime.WrapFunc(store, func() {
        fmt.Println("Hello from Go!")
    })

    // Next up we instantiate a module which is where we link in all our
    // imports. We've got one improt so we pass that in here.
    instance, err := wasmtime.NewInstance(module, []*wasmtime.Extern{item.AsExtern()})
    check(err)

    // After we've instantiated we can lookup our `run` function and call
    // it.
    run := instance.GetExport("run").Func()
    _, err = run.Call()
    check(err)
}

func check(e error) {
    if e != nil {
        panic(e)
    }
}

This feature will be provided in the upcoming Wasmtime 0.16.0 milestone version. The 0.16 version also adds .NET binding features and other interesting changes.

The type of WebAssembly interface pushed by the Bytecode Alliance increases the interoperability of WebAssembly with other languages. Mozilla says that the WebAssembly interface type simplifies the "glue code" needed to pass complex types back and forth between applications and WebAssembly modules.

According to the current progress, I believe that Wasmtime and WebAssembly will have good progress on the local desktop this year. What do you think of this?

Guess you like

Origin www.oschina.net/news/114893/wasmtime-go-webassembly