WebAssembly增加Go语言绑定
為提供更好的跨平臺(tái)支持,WebAssembly 正在積極推動(dòng)其在本地桌面端的進(jìn)展。與此同時(shí),Wasmtime(WebAssembly runtime)近期為它增加了 Go 綁定功能,這意味著開(kāi)發(fā)者可直接在 Go 應(yīng)用程序中調(diào)用 WebAssembly 模塊。
Wasmtime 提供了 JIT 風(fēng)格的 WebAssembly runtime,這是一個(gè)屬于字節(jié)碼聯(lián)盟的項(xiàng)目,此前已為 Rust, C, Python 和 Microsoft .NET 提供了綁定,Go 語(yǔ)言則是其最新綁定的語(yǔ)言。
wasmtime-go 的代碼已開(kāi)源,下面介紹一個(gè)使用 wasmtime-go 編寫(xiě) "Hello, world!" 的代碼示例:
package mainimport ("fmt""github.com/bytecodealliance/wasmtime-go" )func main() {// Almost all operations in wasmtime require a contextual `store`// argument to share, so create that firststore := 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)} }此功能會(huì)在即將發(fā)布的 Wasmtime 0.16.0 milestone 版本中提供,0.16 版本還增加了?.NET 綁定功能,以及其他有趣的變更。
字節(jié)碼聯(lián)盟力推的 WebAssembly 接口類型增加了 WebAssembly 與其他語(yǔ)言的互通性。Mozilla 表示,WebAssembly 接口類型簡(jiǎn)化了應(yīng)用程序與 WebAssembly 模塊間來(lái)回傳遞復(fù)雜類型所需的“膠水代碼”。
按照目前的進(jìn)度,相信今年 Wasmtime 和 WebAssembly 在本地桌面端將會(huì)有不錯(cuò)的進(jìn)展。對(duì)此你有什么看法?
總結(jié)
以上是生活随笔為你收集整理的WebAssembly增加Go语言绑定的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 推荐一个集录屏、截图、音频于一体的软件给
- 下一篇: Blazor WebAssembly 3