Skip to content

Commit 67a5a4a

Browse files
committed
fix(javascript): fix concurrent map access in shared values
1 parent 5a1019a commit 67a5a4a

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

js/mokapi/shared.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"mokapi/engine/common"
66
"reflect"
77
"slices"
8+
"sync"
89

910
"github.com/dop251/goja"
1011
)
@@ -117,6 +118,7 @@ func Export(v any) any {
117118
type SharedValue struct {
118119
vm *goja.Runtime
119120
source goja.Value
121+
m sync.RWMutex
120122
}
121123

122124
func NewSharedValue(v goja.Value, vm *goja.Runtime) *SharedValue {
@@ -131,6 +133,9 @@ func (p *SharedValue) Use(vm *goja.Runtime) *SharedValue {
131133
}
132134

133135
func (p *SharedValue) Get(key string) goja.Value {
136+
p.m.RLock()
137+
defer p.m.RUnlock()
138+
134139
switch v := p.source.(type) {
135140
case *goja.Object:
136141
f := v.Get(key)
@@ -159,6 +164,9 @@ func (p *SharedValue) Has(key string) bool {
159164
}
160165

161166
func (p *SharedValue) Set(key string, value goja.Value) bool {
167+
p.m.Lock()
168+
defer p.m.Unlock()
169+
162170
switch v := p.source.(type) {
163171
case *goja.Object:
164172
sv := useValue(value, p.vm)
@@ -172,6 +180,9 @@ func (p *SharedValue) Set(key string, value goja.Value) bool {
172180
}
173181

174182
func (p *SharedValue) Delete(key string) bool {
183+
p.m.Lock()
184+
defer p.m.Unlock()
185+
175186
switch v := p.source.(type) {
176187
case *goja.Object:
177188
err := v.Delete(key)
@@ -185,6 +196,9 @@ func (p *SharedValue) Delete(key string) bool {
185196
}
186197

187198
func (p *SharedValue) Keys() []string {
199+
p.m.RLock()
200+
defer p.m.RUnlock()
201+
188202
switch v := p.source.(type) {
189203
case *goja.Object:
190204
return v.Keys()

0 commit comments

Comments
 (0)