use channel and remove logs

pull/2/head
vedhavyas 2019-08-19 21:35:19 -04:00
parent 3351f3eb4b
commit 9a613261cb
No known key found for this signature in database
GPG Key ID: 317BF0923E3EB7E5
3 changed files with 15 additions and 21 deletions

View File

@ -36,7 +36,7 @@ func getBridge(ctx unsafe.Pointer) *Bridge {
type Bridge struct { type Bridge struct {
name string name string
instance wasmer.Instance instance wasmer.Instance
vmExit bool done chan bool
exitCode int exitCode int
values []interface{} values []interface{}
refs map[interface{}]int refs map[interface{}]int
@ -145,23 +145,20 @@ func (b *Bridge) addValues() {
} }
// Run start the wasm instance. // Run start the wasm instance.
func (b *Bridge) Run(init chan bool, done chan error) { func (b *Bridge) Run(init chan error, done chan bool) {
defer b.instance.Close() defer b.instance.Close()
b.done = done
run := b.instance.Exports["run"] run := b.instance.Exports["run"]
_, err := run(0, 0) _, err := run(0, 0)
if err != nil { if err != nil {
init <- false init <- err
done <- err
return return
} }
init <- true init <- nil
// use channel from wasm exit <-b.done
for !b.vmExit {
}
fmt.Printf("WASM exited with code: %v\n", b.exitCode) fmt.Printf("WASM exited with code: %v\n", b.exitCode)
done <- nil
} }
func (b *Bridge) mem() []byte { func (b *Bridge) mem() []byte {
@ -404,6 +401,7 @@ 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) {
fw, ok := b.values[5].(*object).props[fn] fw, ok := b.values[5].(*object).props[fn]
if !ok { if !ok {

View File

@ -45,8 +45,8 @@ func debug(ctx unsafe.Pointer, sp int32) {
//export wexit //export wexit
func wexit(ctx unsafe.Pointer, sp int32) { func wexit(ctx unsafe.Pointer, sp int32) {
b := getBridge(ctx) b := getBridge(ctx)
b.vmExit = true
b.exitCode = int(b.getUint32(sp + 8)) b.exitCode = int(b.getUint32(sp + 8))
close(b.done)
} }
//export wwrite //export wwrite
@ -56,7 +56,6 @@ func wwrite(ctx unsafe.Pointer, sp int32) {
p := int(b.getInt64(sp + 16)) p := int(b.getInt64(sp + 16))
l := int(b.getInt32(sp + 24)) l := int(b.getInt32(sp + 24))
syscall.Write(fd, b.mem()[p:p+l]) syscall.Write(fd, b.mem()[p:p+l])
fmt.Println("wasm write", fd, p, l)
} }
//export nanotime //export nanotime
@ -120,7 +119,6 @@ func valueGet(ctx unsafe.Pointer, sp int32) {
// TODO // TODO
panic(fmt.Sprintln("missing property", str, val)) panic(fmt.Sprintln("missing property", str, val))
} }
fmt.Println("valueGet", str, obj.name)
b.storeValue(sp+32, res) b.storeValue(sp+32, res)
} }
@ -132,7 +130,6 @@ func valueSet(ctx unsafe.Pointer, sp int32) {
prop := b.loadString(sp + 16) prop := b.loadString(sp + 16)
propVal := b.loadValue(sp + 32) propVal := b.loadValue(sp + 32)
obj.props[prop] = propVal obj.props[prop] = propVal
fmt.Println("valueSet", obj, prop, propVal)
} }
//export valueIndex //export valueIndex
@ -147,7 +144,6 @@ func valueIndex(ctx unsafe.Pointer, sp int32) {
iv := rv.Index(int(i)) iv := rv.Index(int(i))
b.storeValue(sp+24, iv.Interface()) b.storeValue(sp+24, iv.Interface())
fmt.Println("valueIndex:", iv)
} }
//export valueSetIndex //export valueSetIndex
@ -161,7 +157,6 @@ func valueCall(ctx unsafe.Pointer, sp int32) {
v := b.loadValue(sp + 8) v := b.loadValue(sp + 8)
str := b.loadString(sp + 16) str := b.loadString(sp + 16)
args := b.loadSliceOfValues(sp + 32) args := b.loadSliceOfValues(sp + 32)
fmt.Println("valueCall: ", v.(*object).name, str, args)
f, ok := v.(*object).props[str].(wasmFunc) f, ok := v.(*object).props[str].(wasmFunc)
if !ok { if !ok {
panic(fmt.Sprintln("valueCall: prop not found in ", v, str)) panic(fmt.Sprintln("valueCall: prop not found in ", v, str))
@ -193,7 +188,6 @@ func valueNew(ctx unsafe.Pointer, sp int32) {
sp = b.getSP() sp = b.getSP()
b.storeValue(sp+40, res) b.storeValue(sp+40, res)
b.setUint8(sp+48, 1) b.setUint8(sp+48, 1)
fmt.Println("valueNew ", val, args)
} }
//export valueLength //export valueLength
@ -205,7 +199,6 @@ func valueLength(ctx unsafe.Pointer, sp int32) {
rv = rv.Elem() rv = rv.Elem()
} }
b.setInt64(sp+16, int64(rv.Len())) b.setInt64(sp+16, int64(rv.Len()))
fmt.Println("valueLength:", rv.Len())
} }
//export valuePrepareString //export valuePrepareString
@ -219,7 +212,6 @@ func valuePrepareString(ctx unsafe.Pointer, sp int32) {
b.storeValue(sp+16, str) b.storeValue(sp+16, str)
b.setInt64(sp+24, int64(len(str))) b.setInt64(sp+24, int64(len(str)))
fmt.Println("valuePrepareString", val)
} }
//export valueLoadString //export valueLoadString

View File

@ -13,11 +13,15 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
init, done := make(chan bool), make(chan error) init, done := make(chan error), make(chan bool)
go b.Run(init, done) go b.Run(init, done)
<-init err = <-init
if err != nil {
panic(err)
}
res, err := b.CallFunc("printWasm", &[]interface{}{"Hello from Go"}) res, err := b.CallFunc("printWasm", &[]interface{}{"Hello from Go"})
fmt.Println(res, err) fmt.Println(res, err)
err = <-done <-done
fmt.Println("wasm exited", err) fmt.Println("wasm exited", err)
} }