use maps for the values

pull/2/head
vedhavyas 2019-09-23 22:36:03 +02:00
parent ff4ee892fe
commit 0072b5c1a3
No known key found for this signature in database
GPG Key ID: 317BF0923E3EB7E5
1 changed files with 21 additions and 18 deletions

View File

@ -46,9 +46,10 @@ type Bridge struct {
name string name string
instance wasmer.Instance instance wasmer.Instance
exitCode int exitCode int
values []interface{} valueIDX int
valuesMu sync.RWMutex valueMap map[int]interface{}
refs map[interface{}]int refs map[interface{}]int
valuesMu sync.RWMutex
memory []byte memory []byte
exited bool exited bool
cancF context.CancelFunc cancF context.CancelFunc
@ -80,6 +81,7 @@ func BridgeFromBytes(name string, bytes []byte, imports *wasmer.Imports) (*Bridg
inst.SetContextData(ctx) inst.SetContextData(ctx)
b.addValues() b.addValues()
b.refs = make(map[interface{}]int) b.refs = make(map[interface{}]int)
b.valueIDX = 8
return b, nil return b, nil
} }
@ -100,13 +102,13 @@ func (b *Bridge) addValues() {
}), }),
"_pendingEvent": nil, "_pendingEvent": nil,
}) })
b.values = []interface{}{ b.valueMap = map[int]interface{}{
math.NaN(), 0: math.NaN(),
float64(0), 1: float64(0),
nil, 2: nil,
true, 3: true,
false, 4: false,
&object{ 5: &object{
props: map[string]interface{}{ props: map[string]interface{}{
"Object": &object{name: "Object", new: func(args []interface{}) interface{} { "Object": &object{name: "Object", new: func(args []interface{}) interface{} {
return &object{name: "ObjectInner", props: map[string]interface{}{}} return &object{name: "ObjectInner", props: map[string]interface{}{}}
@ -197,10 +199,10 @@ func (b *Bridge) addValues() {
}), }),
}, },
}, //global }, //global
propObject("mem", map[string]interface{}{ 6: propObject("mem", map[string]interface{}{
"buffer": &buffer{data: b.mem()}}, "buffer": &buffer{data: b.mem()}},
), ),
goObj, // jsGo 7: goObj, // jsGo
} }
} }
@ -341,7 +343,7 @@ func (b *Bridge) loadValue(addr int32) interface{} {
b.valuesMu.RLock() b.valuesMu.RLock()
defer b.valuesMu.RUnlock() defer b.valuesMu.RUnlock()
return b.values[b.getUint32(addr)] return b.valueMap[int(b.getUint32(addr))]
} }
func (b *Bridge) storeValue(addr int32, v interface{}) { func (b *Bridge) storeValue(addr int32, v interface{}) {
@ -402,9 +404,10 @@ func (b *Bridge) storeValue(addr int32, v interface{}) {
ref, ok := b.refs[v] ref, ok := b.refs[v]
if !ok { if !ok {
b.valuesMu.RLock() b.valuesMu.RLock()
ref = len(b.values) b.valueMap[b.valueIDX] = v
b.values = append(b.values, v) ref = b.valueIDX
b.refs[v] = ref b.refs[v] = ref
b.valueIDX++
b.valuesMu.RUnlock() b.valuesMu.RUnlock()
} }
@ -472,7 +475,7 @@ type funcWrapper struct {
func (b *Bridge) makeFuncWrapper(id, this interface{}, args *[]interface{}) (interface{}, error) { func (b *Bridge) makeFuncWrapper(id, this interface{}, args *[]interface{}) (interface{}, error) {
b.valuesMu.RLock() b.valuesMu.RLock()
goObj := b.values[7].(*object) goObj := b.valueMap[7].(*object)
b.valuesMu.RUnlock() b.valuesMu.RUnlock()
event := propObject("_pendingEvent", map[string]interface{}{ event := propObject("_pendingEvent", map[string]interface{}{
"id": id, "id": id,
@ -492,12 +495,12 @@ func (b *Bridge) makeFuncWrapper(id, this interface{}, args *[]interface{}) (int
func (b *Bridge) CallFunc(fn string, args []interface{}) (interface{}, error) { func (b *Bridge) CallFunc(fn string, args []interface{}) (interface{}, error) {
b.check() b.check()
b.valuesMu.RLock() b.valuesMu.RLock()
fw, ok := b.values[5].(*object).props[fn] fw, ok := b.valueMap[5].(*object).props[fn]
if !ok { if !ok {
return nil, fmt.Errorf("missing function: %v", fn) return nil, fmt.Errorf("missing function: %v", fn)
} }
this := b.values[7] this := b.valueMap[7]
b.valuesMu.RUnlock() b.valuesMu.RUnlock()
return b.makeFuncWrapper(fw.(*funcWrapper).id, this, &args) return b.makeFuncWrapper(fw.(*funcWrapper).id, this, &args)
@ -506,7 +509,7 @@ func (b *Bridge) CallFunc(fn string, args []interface{}) (interface{}, error) {
func (b *Bridge) SetFunc(fname string, fn Func) error { func (b *Bridge) SetFunc(fname string, fn Func) error {
b.valuesMu.RLock() b.valuesMu.RLock()
defer b.valuesMu.RUnlock() defer b.valuesMu.RUnlock()
b.values[5].(*object).props[fname] = &fn b.valueMap[5].(*object).props[fname] = &fn
return nil return nil
} }