WebAssembly增加Go语言绑定
生活随笔
收集整理的這篇文章主要介紹了
WebAssembly增加Go语言绑定
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
為提供更好的跨平臺支持,WebAssembly 正在積極推動其在本地桌面端的進展。與此同時,Wasmtime(WebAssembly runtime)近期為它增加了 Go 綁定功能,這意味著開發者可直接在 Go 應用程序中調用 WebAssembly 模塊。
Wasmtime 提供了 JIT 風格的 WebAssembly runtime,這是一個屬于字節碼聯盟的項目,此前已為 Rust, C, Python 和 Microsoft .NET 提供了綁定,Go 語言則是其最新綁定的語言。
wasmtime-go 的代碼已開源,下面介紹一個使用 wasmtime-go 編寫 "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)} }此功能會在即將發布的 Wasmtime 0.16.0 milestone 版本中提供,0.16 版本還增加了?.NET 綁定功能,以及其他有趣的變更。
字節碼聯盟力推的 WebAssembly 接口類型增加了 WebAssembly 與其他語言的互通性。Mozilla 表示,WebAssembly 接口類型簡化了應用程序與 WebAssembly 模塊間來回傳遞復雜類型所需的“膠水代碼”。
按照目前的進度,相信今年 Wasmtime 和 WebAssembly 在本地桌面端將會有不錯的進展。對此你有什么看法?
總結
以上是生活随笔為你收集整理的WebAssembly增加Go语言绑定的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 推荐一个集录屏、截图、音频于一体的软件给
- 下一篇: .NET中的内存管理