go-wasm/bridge.go

153 lines
2.8 KiB
Go
Raw Normal View History

2019-08-07 13:47:59 +03:00
package wasm
import (
"encoding/binary"
2019-08-13 18:11:23 +03:00
"fmt"
"math"
"sync"
"unsafe"
2019-08-07 13:47:59 +03:00
"github.com/wasmerio/go-ext-wasm/wasmer"
)
2019-08-13 18:11:23 +03:00
var undefined = &struct{}{}
var bridges = map[string]*Bridge{}
var mu sync.RWMutex // to protect bridges
type context struct {
n string
2019-08-07 13:47:59 +03:00
}
2019-08-13 18:11:23 +03:00
func setBridge(b *Bridge) unsafe.Pointer {
mu.Lock()
defer mu.Unlock()
bridges[b.name] = b
return unsafe.Pointer(&context{n: b.name})
}
2019-08-07 13:47:59 +03:00
2019-08-13 18:11:23 +03:00
func getBridge(ctx unsafe.Pointer) *Bridge {
ictx := wasmer.IntoInstanceContext(ctx)
c := (*context)(ictx.Data())
mu.RLock()
defer mu.RUnlock()
return bridges[c.n]
}
type Bridge struct {
name string
2019-08-07 13:47:59 +03:00
instance wasmer.Instance
vmExit bool
exitCode int
}
2019-08-13 18:11:23 +03:00
func (b *Bridge) InitWASMBytes(name string, bytes []byte, imports *wasmer.Imports) error {
2019-08-07 13:47:59 +03:00
if imports == nil {
imports = wasmer.NewImports()
}
2019-08-13 18:11:23 +03:00
b.name = name
err := b.addImports(imports)
2019-08-07 13:47:59 +03:00
if err != nil {
return err
}
inst, err := wasmer.NewInstanceWithImports(bytes, imports)
if err != nil {
return err
}
b.instance = inst
2019-08-13 18:11:23 +03:00
inst.SetContextData(setBridge(b))
2019-08-07 13:47:59 +03:00
return nil
}
2019-08-13 18:11:23 +03:00
func (b *Bridge) InitWASM(name, file string, imports *wasmer.Imports) (err error) {
bytes, err := wasmer.ReadBytes(file)
if err != nil {
return err
}
return b.InitWASMBytes(name, bytes, imports)
}
2019-08-07 13:47:59 +03:00
// Run start the wasm instance.
2019-08-13 18:11:23 +03:00
func (b *Bridge) Run() error {
2019-08-07 13:47:59 +03:00
defer b.instance.Close()
run := b.instance.Exports["run"]
resume := b.instance.Exports["resume"]
_, err := run(0, 0)
if err != nil {
return err
}
for !b.vmExit {
_, err = resume()
if err != nil {
return err
}
}
2019-08-13 18:11:23 +03:00
fmt.Printf("WASM exited with code: %v\n", b.exitCode)
2019-08-07 13:47:59 +03:00
return nil
}
2019-08-13 18:11:23 +03:00
func (b *Bridge) mem() []byte {
2019-08-07 13:47:59 +03:00
return b.instance.Memory.Data()
}
2019-08-13 18:11:23 +03:00
func (b Bridge) setInt64(offset int32, v int64) {
2019-08-07 13:47:59 +03:00
mem := b.mem()
binary.LittleEndian.PutUint64(mem[offset:], uint64(v))
}
2019-08-13 18:11:23 +03:00
func (b Bridge) setUint64(offset int32, v uint64) {
mem := b.mem()
binary.LittleEndian.PutUint64(mem[offset:], v)
}
func (b Bridge) getUnit64(offset int32) uint64 {
mem := b.mem()
return binary.LittleEndian.Uint64(mem[offset+0:])
}
func (b Bridge) setFloat64(offset int32, v float64) {
uf := math.Float64bits(v)
b.setUint64(offset, uf)
}
func (b Bridge) getFloat64(offset int32) float64 {
uf := b.getUnit64(offset)
return math.Float64frombits(uf)
}
func (b Bridge) getUint32(offset int32) uint32 {
return binary.LittleEndian.Uint32(b.mem()[offset+0:])
}
func (b Bridge) loadSlice(addr int32) []byte {
2019-08-07 13:47:59 +03:00
mem := b.mem()
array := binary.LittleEndian.Uint64(mem[addr+0:])
length := binary.LittleEndian.Uint64(mem[addr+8:])
return mem[array : array+length]
}
2019-08-13 18:11:23 +03:00
func (b Bridge) loadString(addr int32) string {
2019-08-07 13:47:59 +03:00
d := b.loadSlice(addr)
return string(d)
}
2019-08-13 18:11:23 +03:00
func (b Bridge) loadValue(addr int32) interface{} {
f := b.getFloat64(addr)
if f == 0 {
return undefined
}
if !math.IsNaN(f) {
return f
}
// return value instead of uint32
return b.getUint32(addr)
}