jsre: include constructor properties in auto-completion

This commit is contained in:
Felix Lange
2016-02-15 16:42:39 +01:00
parent ae5bc89cad
commit 6ba7bbbe29
3 changed files with 107 additions and 14 deletions

View File

@ -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{}: