From e56a2c5a858c713a866415bb005ed9e19fb565b0 Mon Sep 17 00:00:00 2001 From: vedhavyas Date: Mon, 23 Sep 2019 23:23:27 +0200 Subject: [PATCH] add relase call --- bridge.go | 50 +++++++++++++++++++++++++++++++++++++--- examples/wasm/main.wasm | Bin 2934596 -> 2934596 bytes go-converts/wasm.go | 8 ++++++- imports.go | 16 +++++++++---- 4 files changed, 65 insertions(+), 9 deletions(-) diff --git a/bridge.go b/bridge.go index 81a8400..7da6b40 100644 --- a/bridge.go +++ b/bridge.go @@ -18,9 +18,14 @@ import ( "github.com/wasmerio/go-ext-wasm/wasmer" ) -var undefined = &struct{}{} -var bridges = map[string]*Bridge{} -var mu sync.RWMutex // to protect bridges +const release_call = "_release_" + +var ( + undefined = &struct{}{} + bridges = map[string]*Bridge{} + mu sync.RWMutex // to protect bridges +) + type bctx struct{ n string } func getCtxData(b *Bridge) (unsafe.Pointer, error) { @@ -55,6 +60,30 @@ type Bridge struct { cancF context.CancelFunc } +// releaseRef removes the ref from the refs. +// Returns the idx and true if remove was successful. +func (b *Bridge) releaseRef(v interface{}) (int, bool) { + idx, ok := b.refs[v] + if !ok { + return 0, false + } + + delete(b.refs, v) + return idx, true +} + +// releaseVal removes val from the valueMap +// Returns the value and true if remove was successful +func (b *Bridge) releaseVal(idx int) (interface{}, bool) { + v, ok := b.valueMap[idx] + if !ok { + return nil, false + } + + delete(b.valueMap, idx) + return v, true +} + func BridgeFromBytes(name string, bytes []byte, imports *wasmer.Imports) (*Bridge, error) { b := new(Bridge) if imports == nil { @@ -513,6 +542,21 @@ func (b *Bridge) SetFunc(fname string, fn Func) error { return nil } +func (b *Bridge) releaseFunc(v interface{}) Func { + return Func(func(args []interface{}) (interface{}, error) { + b.valuesMu.Lock() + defer b.valuesMu.Unlock() + + idx, ok := b.releaseRef(v) + if !ok { + return nil, nil + } + + b.releaseVal(idx) + return nil, nil + }) +} + func Bytes(v interface{}) ([]byte, error) { arr, ok := v.(*array) if !ok { diff --git a/examples/wasm/main.wasm b/examples/wasm/main.wasm index 51d65fcb75eadc0988914af93521fd5ac2bad744..46875cce909fb5939e2aa5d0a8ab3d252b56eb35 100755 GIT binary patch delta 269 zcmWN=IZML;7zSXHc(!W1k9yzMgFs4&j;TkBNJSE>oy1lJi%K+x+VUI9 z(M`mNIMGA&5`BbD^b-TbATdM?6C=bZF-D9N6Q4+2+e*CAvJ-MrPRVIGBWLBDoRicAa#BvoX*nZj n<(!q3Vr+>bazpC=5CTdFJt6JAq