()
| 135 | } |
| 136 | |
| 137 | func (v *Value) Array() []*Value { |
| 138 | obj := v.object() |
| 139 | if obj == nil { |
| 140 | return nil |
| 141 | } |
| 142 | // Read elements directly from the underlying otto object to avoid the |
| 143 | // per-index *Value allocation and object re-resolution that v.Get would |
| 144 | // incur. otto has no indexed accessor, so string keys are unavoidable, but |
| 145 | // this keeps the hot path (listing large directories) as lean as possible. |
| 146 | lengthVal, e := obj.Get("length") |
| 147 | if e != nil { |
| 148 | v.vm.ThrowTypeError("not a valid array") |
| 149 | } |
| 150 | length, e := lengthVal.ToInteger() |
| 151 | if e != nil || length < 0 { |
| 152 | v.vm.ThrowTypeError("not a valid array") |
| 153 | } |
| 154 | n := int(length) |
| 155 | r := make([]*Value, n) |
| 156 | for i := range n { |
| 157 | ev, e := obj.Get(strconv.Itoa(i)) |
| 158 | if e != nil { |
| 159 | v.vm.ThrowTypeError(e.Error()) |
| 160 | } |
| 161 | r[i] = newValue(v.vm, ev) |
| 162 | } |
| 163 | return r |
| 164 | } |
| 165 | |
| 166 | func (v *Value) M() types.M { |
| 167 | if v.IsNil() { |
no test coverage detected