diff options
Diffstat (limited to 'jsre/pretty.go')
-rw-r--r-- | jsre/pretty.go | 28 |
1 files changed, 19 insertions, 9 deletions
diff --git a/jsre/pretty.go b/jsre/pretty.go index 64f0a95c9..d0b42cd01 100644 --- a/jsre/pretty.go +++ b/jsre/pretty.go @@ -187,20 +187,30 @@ func (ctx ppctx) fields(obj *otto.Object) []string { vals = append(vals, k) } } - // add own properties - ctx.doOwnProperties(obj.Value(), add) - // add properties of the constructor - if cp := constructorPrototype(obj); cp != nil { - ctx.doOwnProperties(cp.Value(), add) - } + iterOwnAndConstructorKeys(ctx.vm, obj, add) sort.Strings(vals) sort.Strings(methods) return append(vals, methods...) } -func (ctx ppctx) doOwnProperties(v otto.Value, f func(string)) { - Object, _ := ctx.vm.Object("Object") - rv, _ := Object.Call("getOwnPropertyNames", v) +func iterOwnAndConstructorKeys(vm *otto.Otto, obj *otto.Object, f func(string)) { + seen := make(map[string]bool) + iterOwnKeys(vm, obj, func(prop string) { + seen[prop] = true + f(prop) + }) + if cp := constructorPrototype(obj); cp != nil { + iterOwnKeys(vm, cp, func(prop string) { + if !seen[prop] { + f(prop) + } + }) + } +} + +func iterOwnKeys(vm *otto.Otto, obj *otto.Object, f func(string)) { + Object, _ := vm.Object("Object") + rv, _ := Object.Call("getOwnPropertyNames", obj.Value()) gv, _ := rv.Export() switch gv := gv.(type) { case []interface{}: |