(args []super.Value)
| 11 | } |
| 12 | |
| 13 | func (l *LenFn) Call(args []super.Value) super.Value { |
| 14 | val := args[0].Under() |
| 15 | var length int |
| 16 | switch typ := super.TypeUnder(val.Type()).(type) { |
| 17 | case *super.TypeOfNull: |
| 18 | case *super.TypeRecord: |
| 19 | length = len(typ.Fields) |
| 20 | case *super.TypeArray, *super.TypeSet, *super.TypeMap: |
| 21 | var err error |
| 22 | length, err = val.ContainerLength() |
| 23 | if err != nil { |
| 24 | panic(err) |
| 25 | } |
| 26 | case *super.TypeOfString: |
| 27 | length = utf8.RuneCount(val.Bytes()) |
| 28 | case *super.TypeOfBytes, *super.TypeOfIP, *super.TypeOfNet: |
| 29 | length = len(val.Bytes()) |
| 30 | case *super.TypeError: |
| 31 | return l.sctx.WrapError("len()", val) |
| 32 | case *super.TypeOfType: |
| 33 | t, err := l.sctx.LookupByValue(val.Bytes()) |
| 34 | if err != nil { |
| 35 | return l.sctx.NewError(err) |
| 36 | } |
| 37 | length = TypeLength(t) |
| 38 | default: |
| 39 | return l.sctx.WrapError("len: bad type", val) |
| 40 | } |
| 41 | return super.NewInt64(int64(length)) |
| 42 | } |
| 43 | |
| 44 | func TypeLength(typ super.Type) int { |
| 45 | switch typ := typ.(type) { |
nothing calls this directly
no test coverage detected