forked from grafana/k6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrozen_object.go
69 lines (60 loc) · 1.41 KB
/
frozen_object.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package common
import (
"fmt"
"github.com/dop251/goja"
)
// FreezeObject replicates the JavaScript Object.freeze function.
func FreezeObject(rt *goja.Runtime, obj goja.Value) error {
global := rt.GlobalObject().Get("Object").ToObject(rt)
freeze, ok := goja.AssertFunction(global.Get("freeze"))
if !ok {
panic("failed to get the Object.freeze function from the runtime")
}
isFrozen, ok := goja.AssertFunction(global.Get("isFrozen"))
if !ok {
panic("failed to get the Object.isFrozen function from the runtime")
}
fobj := &freezing{
global: global,
rt: rt,
freeze: freeze,
isFrozen: isFrozen,
}
return fobj.deepFreeze(obj)
}
type freezing struct {
rt *goja.Runtime
global goja.Value
freeze goja.Callable
isFrozen goja.Callable
}
func (f *freezing) deepFreeze(val goja.Value) error {
if val != nil && goja.IsNull(val) {
return nil
}
_, err := f.freeze(goja.Undefined(), val)
if err != nil {
return fmt.Errorf("object freeze failed: %w", err)
}
o := val.ToObject(f.rt)
if o == nil {
return nil
}
for _, key := range o.Keys() {
prop := o.Get(key)
if prop == nil {
continue
}
frozen, err := f.isFrozen(goja.Undefined(), prop)
if err != nil {
return err
}
if frozen.ToBoolean() { // prevent cycles
continue
}
if err = f.deepFreeze(prop); err != nil {
return fmt.Errorf("deep freezing the property %s failed: %w", key, err)
}
}
return nil
}