From 515349304c8346a112501d9bc2c5b101de86f503 Mon Sep 17 00:00:00 2001 From: JustAnotherID Date: Sun, 22 Oct 2023 03:02:14 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AD=97=E5=85=B8=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20len()=20=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- types_methods.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/types_methods.go b/types_methods.go index 80468d24..9bf5ae0b 100644 --- a/types_methods.go +++ b/types_methods.go @@ -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}), @@ -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}), ), }