Skip to content

Commit

Permalink
feat: 字典类型增加 len() 方法
Browse files Browse the repository at this point in the history
  • Loading branch information
JustAnotherID committed Oct 21, 2023
1 parent d841825 commit 5153493
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions types_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ func funcDictItems(ctx *Context, this *VMValue, params []*VMValue) *VMValue {
return VMValueNewArrayRaw(arr)
}

func funcDictLen(ctx *Context, this *VMValue, params []*VMValue) *VMValue {
d := this.MustReadDictData()
var size int64
d.Dict.Range(func(key string, value *VMValue) bool {
size++
return true
})
return VMValueNewInt(size)
}

var builtinProto = map[VMValueType]*VMDictValue{
VMTypeArray: VMValueMustNewDictWithArray(
VMValueNewStr("kh"), nnf(&ndf{"Array.kh", []string{"num"}, []*VMValue{VMValueNewInt(1)}, nil, funcArrayKeepHigh}),
Expand All @@ -154,6 +164,7 @@ var builtinProto = map[VMValueType]*VMDictValue{
VMValueNewStr("keys"), nnf(&ndf{"Dict.keys", []string{}, nil, nil, funcDictKeys}),
VMValueNewStr("values"), nnf(&ndf{"Dict.values", []string{}, nil, nil, funcDictValues}),
VMValueNewStr("items"), nnf(&ndf{"Dict.items", []string{}, nil, nil, funcDictItems}),
VMValueNewStr("len"), nnf(&ndf{"Dict.len", []string{}, nil, nil, funcDictLen}),
),
}

Expand Down

0 comments on commit 5153493

Please sign in to comment.