2019-08-07 13:47:59 +03:00
|
|
|
package wasm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2019-08-13 18:11:23 +03:00
|
|
|
"fmt"
|
2019-08-15 21:12:20 +03:00
|
|
|
"log"
|
2019-08-13 18:11:23 +03:00
|
|
|
"math"
|
|
|
|
"sync"
|
2019-08-15 21:12:20 +03:00
|
|
|
"syscall"
|
2019-08-13 18:11:23 +03:00
|
|
|
"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
|
2019-08-15 21:12:20 +03:00
|
|
|
type context struct{ n string }
|
2019-08-13 18:11:23 +03:00
|
|
|
|
2019-08-15 21:12:20 +03:00
|
|
|
// TODO ensure it wont override the another context with same name.
|
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-15 21:12:20 +03:00
|
|
|
values []interface{}
|
|
|
|
refs map[interface{}]int
|
2019-08-07 13:47:59 +03:00
|
|
|
}
|
|
|
|
|
2019-08-15 21:12:20 +03:00
|
|
|
func BridgeFromBytes(name string, bytes []byte, imports *wasmer.Imports) (*Bridge, error) {
|
|
|
|
b := new(Bridge)
|
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 {
|
2019-08-15 21:12:20 +03:00
|
|
|
return nil, err
|
2019-08-07 13:47:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
inst, err := wasmer.NewInstanceWithImports(bytes, imports)
|
|
|
|
if err != nil {
|
2019-08-15 21:12:20 +03:00
|
|
|
return nil, err
|
2019-08-07 13:47:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
b.instance = inst
|
2019-08-13 18:11:23 +03:00
|
|
|
inst.SetContextData(setBridge(b))
|
2019-08-15 21:12:20 +03:00
|
|
|
b.addValues()
|
|
|
|
b.refs = make(map[interface{}]int)
|
|
|
|
return b, nil
|
2019-08-07 13:47:59 +03:00
|
|
|
}
|
|
|
|
|
2019-08-15 21:12:20 +03:00
|
|
|
func BridgeFromFile(name, file string, imports *wasmer.Imports) (*Bridge, error) {
|
2019-08-13 18:11:23 +03:00
|
|
|
bytes, err := wasmer.ReadBytes(file)
|
|
|
|
if err != nil {
|
2019-08-15 21:12:20 +03:00
|
|
|
return nil, err
|
2019-08-13 18:11:23 +03:00
|
|
|
}
|
|
|
|
|
2019-08-15 21:12:20 +03:00
|
|
|
return BridgeFromBytes(name, bytes, imports)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bridge) addValues() {
|
|
|
|
b.values = []interface{}{
|
|
|
|
math.NaN(),
|
|
|
|
float64(0),
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
&object{
|
|
|
|
props: map[string]interface{}{
|
|
|
|
"Object": &object{name: "Object", props: map[string]interface{}{}},
|
|
|
|
"Array": &object{name: "Array", props: map[string]interface{}{}},
|
|
|
|
"Int8Array": typedArray("Int8Array"),
|
|
|
|
"Int16Array": typedArray("Int16Array"),
|
|
|
|
"Int32Array": typedArray("Int32Array"),
|
|
|
|
"Uint8Array": typedArray("Uint8Array"),
|
|
|
|
"Uint16Array": typedArray("Uint16Array"),
|
|
|
|
"Uint32Array": typedArray("Uint32Array"),
|
|
|
|
"Float32Array": typedArray("Float32Array"),
|
|
|
|
"Float64Array": typedArray("Float64Array"),
|
|
|
|
"process": &object{name: "process", props: map[string]interface{}{}},
|
|
|
|
"fs": &object{name: "fs", props: map[string]interface{}{
|
|
|
|
"constants": &object{name: "constants", props: map[string]interface{}{
|
|
|
|
"O_WRONLY": syscall.O_WRONLY,
|
|
|
|
"O_RDWR": syscall.O_RDWR,
|
|
|
|
"O_CREAT": syscall.O_CREAT,
|
|
|
|
"O_TRUNC": syscall.O_TRUNC,
|
|
|
|
"O_APPEND": syscall.O_APPEND,
|
|
|
|
"O_EXCL": syscall.O_EXCL,
|
|
|
|
}},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
}, //global
|
|
|
|
&object{
|
|
|
|
name: "mem",
|
|
|
|
props: map[string]interface{}{
|
|
|
|
"buffer": &arrayBuffer{data: b.mem()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&object{name: "jsGo", props: map[string]interface{}{}}, // jsGo
|
|
|
|
}
|
2019-08-13 18:11:23 +03:00
|
|
|
}
|
|
|
|
|
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-15 21:12:20 +03:00
|
|
|
func (b *Bridge) getSP() int32 {
|
|
|
|
spFunc := b.instance.Exports["getsp"]
|
|
|
|
val, err := spFunc()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("failed to get sp", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return val.ToI32()
|
|
|
|
}
|
|
|
|
|
|
|
|
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-15 21:12:20 +03:00
|
|
|
func (b *Bridge) getInt64(offset int32) int64 {
|
|
|
|
mem := b.mem()
|
|
|
|
return int64(binary.LittleEndian.Uint64(mem[offset:]))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bridge) setUint32(offset int32, v uint32) {
|
|
|
|
mem := b.mem()
|
|
|
|
binary.LittleEndian.PutUint32(mem[offset:], v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bridge) setUint64(offset int32, v uint64) {
|
2019-08-13 18:11:23 +03:00
|
|
|
mem := b.mem()
|
|
|
|
binary.LittleEndian.PutUint64(mem[offset:], v)
|
|
|
|
}
|
|
|
|
|
2019-08-15 21:12:20 +03:00
|
|
|
func (b *Bridge) getUnit64(offset int32) uint64 {
|
2019-08-13 18:11:23 +03:00
|
|
|
mem := b.mem()
|
|
|
|
return binary.LittleEndian.Uint64(mem[offset+0:])
|
|
|
|
}
|
|
|
|
|
2019-08-15 21:12:20 +03:00
|
|
|
func (b *Bridge) setFloat64(offset int32, v float64) {
|
2019-08-13 18:11:23 +03:00
|
|
|
uf := math.Float64bits(v)
|
|
|
|
b.setUint64(offset, uf)
|
|
|
|
}
|
|
|
|
|
2019-08-15 21:12:20 +03:00
|
|
|
func (b *Bridge) getFloat64(offset int32) float64 {
|
2019-08-13 18:11:23 +03:00
|
|
|
uf := b.getUnit64(offset)
|
|
|
|
return math.Float64frombits(uf)
|
|
|
|
}
|
|
|
|
|
2019-08-15 21:12:20 +03:00
|
|
|
func (b *Bridge) getUint32(offset int32) uint32 {
|
2019-08-13 18:11:23 +03:00
|
|
|
return binary.LittleEndian.Uint32(b.mem()[offset+0:])
|
|
|
|
}
|
|
|
|
|
2019-08-15 21:12:20 +03:00
|
|
|
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-15 21:12:20 +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
|
|
|
|
2019-08-15 21:12:20 +03:00
|
|
|
func (b *Bridge) loadSliceOfValues(addr int32) []interface{} {
|
|
|
|
arr := int(b.getInt64(addr + 0))
|
|
|
|
arrLen := int(b.getInt64(addr + 8))
|
|
|
|
vals := make([]interface{}, arrLen, arrLen)
|
|
|
|
for i := 0; i < int(arrLen); i++ {
|
|
|
|
_, vals[i] = b.loadValue(int32(arr + i*8))
|
|
|
|
}
|
|
|
|
|
|
|
|
return vals
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO remove id once debugging is done
|
|
|
|
func (b *Bridge) loadValue(addr int32) (uint32, interface{}) {
|
2019-08-13 18:11:23 +03:00
|
|
|
f := b.getFloat64(addr)
|
|
|
|
if f == 0 {
|
2019-08-15 21:12:20 +03:00
|
|
|
return 0, undefined
|
2019-08-13 18:11:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if !math.IsNaN(f) {
|
2019-08-15 21:12:20 +03:00
|
|
|
return 0, f
|
2019-08-13 18:11:23 +03:00
|
|
|
}
|
|
|
|
|
2019-08-15 21:12:20 +03:00
|
|
|
id := b.getUint32(addr)
|
|
|
|
return id, b.values[id]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bridge) storeValue(addr int32, v interface{}) {
|
|
|
|
const nanHead = 0x7FF80000
|
|
|
|
|
|
|
|
if i, ok := v.(int); ok {
|
|
|
|
v = float64(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
if i, ok := v.(uint); ok {
|
|
|
|
v = float64(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := v.(float64); ok {
|
|
|
|
if math.IsNaN(v) {
|
|
|
|
b.setUint32(addr+4, nanHead)
|
|
|
|
b.setUint32(addr, 0)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if v == 0 {
|
|
|
|
b.setUint32(addr+4, nanHead)
|
|
|
|
b.setUint32(addr, 1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
b.setFloat64(addr, v)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v {
|
|
|
|
case undefined:
|
|
|
|
b.setFloat64(addr, 0)
|
|
|
|
return
|
|
|
|
case nil:
|
|
|
|
b.setUint32(addr+4, nanHead)
|
|
|
|
b.setUint32(addr, 2)
|
|
|
|
return
|
|
|
|
case true:
|
|
|
|
b.setUint32(addr+4, nanHead)
|
|
|
|
b.setUint32(addr, 3)
|
|
|
|
return
|
|
|
|
case false:
|
|
|
|
b.setUint32(addr+4, nanHead)
|
|
|
|
b.setUint32(addr, 4)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ref, ok := b.refs[v]
|
|
|
|
if !ok {
|
|
|
|
ref = len(b.values)
|
|
|
|
b.values = append(b.values, v)
|
|
|
|
b.refs[v] = ref
|
|
|
|
}
|
|
|
|
|
|
|
|
typeFlag := 0
|
|
|
|
switch v.(type) {
|
|
|
|
case string:
|
|
|
|
typeFlag = 1
|
|
|
|
case *object, *arrayBuffer: //TODO symbol maybe?
|
|
|
|
typeFlag = 2
|
|
|
|
default:
|
|
|
|
log.Fatalf("unknown type: %T", v)
|
|
|
|
// TODO function
|
|
|
|
}
|
|
|
|
b.setUint32(addr+4, uint32(nanHead|typeFlag))
|
|
|
|
b.setUint32(addr, uint32(ref))
|
|
|
|
}
|
|
|
|
|
|
|
|
type object struct {
|
|
|
|
name string // TODO for debugging
|
|
|
|
props map[string]interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func typedArray(name string) *object {
|
|
|
|
return &object{
|
|
|
|
name: name,
|
|
|
|
props: map[string]interface{}{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type arrayBuffer struct {
|
|
|
|
data []byte
|
2019-08-13 18:11:23 +03:00
|
|
|
}
|