more checks

pull/2/head
vedhavyas 2019-08-20 14:29:13 -07:00
parent a7376c72a7
commit bf35d691af
No known key found for this signature in database
GPG Key ID: 317BF0923E3EB7E5
3 changed files with 23 additions and 9 deletions

View File

@ -18,12 +18,15 @@ var bridges = map[string]*Bridge{}
var mu sync.RWMutex // to protect bridges var mu sync.RWMutex // to protect bridges
type context struct{ n string } type context struct{ n string }
// TODO ensure it wont override the another context with same name. func getCtxData(b *Bridge) (unsafe.Pointer, error) {
func setBridge(b *Bridge) unsafe.Pointer {
mu.Lock() mu.Lock()
defer mu.Unlock() defer mu.Unlock()
if _, ok := bridges[b.name]; ok {
return nil, fmt.Errorf("bridge with name %s already exists", b.name)
}
bridges[b.name] = b bridges[b.name] = b
return unsafe.Pointer(&context{n: b.name}) return unsafe.Pointer(&context{n: b.name}), nil
} }
func getBridge(ctx unsafe.Pointer) *Bridge { func getBridge(ctx unsafe.Pointer) *Bridge {
@ -42,6 +45,7 @@ type Bridge struct {
values []interface{} values []interface{}
refs map[interface{}]int refs map[interface{}]int
memory []byte memory []byte
exited bool
} }
func BridgeFromBytes(name string, bytes []byte, imports *wasmer.Imports) (*Bridge, error) { func BridgeFromBytes(name string, bytes []byte, imports *wasmer.Imports) (*Bridge, error) {
@ -61,8 +65,13 @@ func BridgeFromBytes(name string, bytes []byte, imports *wasmer.Imports) (*Bridg
return nil, err return nil, err
} }
ctx, err := getCtxData(b)
if err != nil {
return nil, err
}
b.instance = inst b.instance = inst
inst.SetContextData(setBridge(b)) inst.SetContextData(ctx)
b.addValues() b.addValues()
b.refs = make(map[interface{}]int) b.refs = make(map[interface{}]int)
return b, nil return b, nil
@ -159,8 +168,15 @@ func (b *Bridge) addValues() {
} }
} }
func (b *Bridge) check() {
if b.exited {
panic("WASM instance already exited")
}
}
// Run start the wasm instance. // Run start the wasm instance.
func (b *Bridge) Run(init chan error, done chan bool) { func (b *Bridge) Run(init chan error, done chan bool) {
b.check()
defer b.instance.Close() defer b.instance.Close()
b.done = done b.done = done
@ -358,7 +374,7 @@ func (b *Bridge) storeValue(addr int32, v interface{}) {
} }
type object struct { type object struct {
name string // TODO for debugging name string // for debugging
props map[string]interface{} props map[string]interface{}
new func(args []interface{}) interface{} new func(args []interface{}) interface{}
} }
@ -422,8 +438,8 @@ func (b *Bridge) makeFuncWrapper(id, this interface{}, args *[]interface{}) (int
return event.props["result"], nil return event.props["result"], nil
} }
// TODO cheeck if the wasm is still running
func (b *Bridge) CallFunc(fn string, args []interface{}) (interface{}, error) { func (b *Bridge) CallFunc(fn string, args []interface{}) (interface{}, error) {
b.check()
fw, ok := b.values[5].(*object).props[fn] fw, ok := b.values[5].(*object).props[fn]
if !ok { if !ok {
return nil, fmt.Errorf("missing function: %v", fn) return nil, fmt.Errorf("missing function: %v", fn)
@ -432,7 +448,6 @@ func (b *Bridge) CallFunc(fn string, args []interface{}) (interface{}, error) {
return b.makeFuncWrapper(fw.(*funcWrapper).id, b.values[7], &args) return b.makeFuncWrapper(fw.(*funcWrapper).id, b.values[7], &args)
} }
// TODO check if wasm is still running
func (b *Bridge) SetFunc(fname string, fn Func) error { func (b *Bridge) SetFunc(fname string, fn Func) error {
b.values[5].(*object).props[fname] = &fn b.values[5].(*object).props[fname] = &fn
return nil return nil

View File

@ -46,6 +46,7 @@ func debug(ctx unsafe.Pointer, sp int32) {
func wexit(ctx unsafe.Pointer, sp int32) { func wexit(ctx unsafe.Pointer, sp int32) {
b := getBridge(ctx) b := getBridge(ctx)
b.exitCode = int(b.getUint32(sp + 8)) b.exitCode = int(b.getUint32(sp + 8))
b.exited = true
close(b.done) close(b.done)
} }
@ -115,7 +116,6 @@ func valueGet(ctx unsafe.Pointer, sp int32) {
res, ok := obj.props[str] res, ok := obj.props[str]
if !ok { if !ok {
// TODO
panic(fmt.Sprintln("missing property", str, val)) panic(fmt.Sprintln("missing property", str, val))
} }
b.storeValue(sp+32, res) b.storeValue(sp+32, res)

View File

@ -7,7 +7,6 @@ import (
"syscall/js" "syscall/js"
) )
// TODO: log seems to cause an issue
func addition(this js.Value, args []js.Value) interface{} { func addition(this js.Value, args []js.Value) interface{} {
log.Println("In WASM", args) log.Println("In WASM", args)
a, b := args[0].Int(), args[1].Int() a, b := args[0].Int(), args[1].Int()