From 047f070d66c04ea1c3cf0a570743e25ef8803f7a Mon Sep 17 00:00:00 2001 From: fy Date: Sun, 16 Jun 2024 19:31:26 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=A1=A5=E5=85=A8AsBool=E7=9A=84?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- builtin_functions.go | 15 ++++++++++++--- types.go | 16 +++++++++++++--- types_test.go | 28 ++++++++++++++++++++++++++++ valuemap.go | 2 +- valuemap_test.go | 8 ++++---- 5 files changed, 58 insertions(+), 11 deletions(-) diff --git a/builtin_functions.go b/builtin_functions.go index 95d4bbee..16326908 100644 --- a/builtin_functions.go +++ b/builtin_functions.go @@ -57,6 +57,14 @@ func funcAbs(ctx *Context, this *VMValue, params []*VMValue) *VMValue { return nil } +func funcBool(ctx *Context, this *VMValue, params []*VMValue) *VMValue { + v := params[0] + if v.AsBool() { + return NewIntVal(1) + } + return NewIntVal(0) +} + func funcInt(ctx *Context, this *VMValue, params []*VMValue) *VMValue { switch params[0].TypeId { case VMTypeInt: @@ -123,10 +131,10 @@ func funcDir(ctx *Context, this *VMValue, params []*VMValue) *VMValue { } // -//func funcHelp(ctx *Context, this *VMValue, params []*VMValue) *VMValue { +// func funcHelp(ctx *Context, this *VMValue, params []*VMValue) *VMValue { // // 函数名,参数,说明 // return NewStrVal(params[0].ToString()) -//} +// } var nnf = NewNativeFunctionVal @@ -140,9 +148,10 @@ var builtinValues = map[string]*VMValue{ "float": nnf(&ndf{"float", []string{"value"}, nil, nil, funcFloat}), "str": nnf(&ndf{"str", []string{"value"}, nil, nil, funcStr}), "abs": nnf(&ndf{"abs", []string{"value"}, nil, nil, funcAbs}), + "bool": nnf(&ndf{"bool", []string{"value"}, nil, nil, funcBool}), // TODO: roll() // 要不要进行权限隔绝? "dir": nnf(&ndf{"dir", []string{"value"}, nil, nil, funcDir}), - //"help": nnf(&ndf{"help", []string{"value"}, nil, nil, funcHelp}), + // "help": nnf(&ndf{"help", []string{"value"}, nil, nil, funcHelp}), } diff --git a/types.go b/types.go index b651e199..75dc9d90 100644 --- a/types.go +++ b/types.go @@ -442,13 +442,23 @@ func (v *VMValue) AsBool() bool { switch v.TypeId { case VMTypeInt: return v.Value != IntType(0) + case VMTypeFloat: + return v.Value != 0.0 case VMTypeString: return v.Value != "" case VMTypeNull: return false - // case VMTypeComputedValue: - // vd := v.Value.(*VMComputedValueData) - // return vd.BaseValue.AsBool() + case VMTypeComputedValue: + vd := v.Value.(*ComputedData) + return vd.Expr != "" + case VMTypeArray: + ad := v.MustReadArray() + return len(ad.List) != 0 + case VMTypeDict: + dd := v.MustReadDictData() + return dd.Dict.Length() != 0 + case VMTypeFunction, VMTypeNativeFunction, VMTypeNativeObject: + return true default: return false } diff --git a/types_test.go b/types_test.go index 1aecdac3..1a5138ce 100644 --- a/types_test.go +++ b/types_test.go @@ -387,3 +387,31 @@ func TestNativeObject(t *testing.T) { ret := funcDir(vm, nil, []*VMValue{v}) assert.Equal(t, ret.ToString(), "['x']") } + +func TestAsBool(t *testing.T) { + assert.Equal(t, ni(1).AsBool(), true) + assert.Equal(t, ni(0).AsBool(), false) + + assert.Equal(t, nf(1.1).AsBool(), true) + assert.Equal(t, nf(0.0).AsBool(), false) + + assert.Equal(t, ns("1").AsBool(), true) + assert.Equal(t, ns("").AsBool(), false) + + assert.Equal(t, NewNullVal().AsBool(), false) + + assert.Equal(t, NewComputedVal("d10").AsBool(), true) + assert.Equal(t, NewComputedVal("").AsBool(), false) + + assert.Equal(t, na(ns("1")).AsBool(), true) + assert.Equal(t, na().AsBool(), false) + + assert.Equal(t, nd(ns("1"), ns("1")).V().AsBool(), true) + assert.Equal(t, nd().V().AsBool(), false) + + vm := NewVM() + _ = vm.Run("func a() {}") + assert.Equal(t, vm.Ret.AsBool(), true) + + assert.Equal(t, builtinValues["str"].AsBool(), true) +} diff --git a/valuemap.go b/valuemap.go index e1572ffc..e9937940 100644 --- a/valuemap.go +++ b/valuemap.go @@ -128,7 +128,7 @@ func (m *ValueMap) Load(key string) (value *VMValue, ok bool) { return e.load() } -func (m *ValueMap) Size() int { +func (m *ValueMap) Length() int { read, _ := m.read.Load().(readOnlyValueMap) if read.amended { m.mu.Lock() diff --git a/valuemap_test.go b/valuemap_test.go index 29ce4ce6..c7a47a43 100644 --- a/valuemap_test.go +++ b/valuemap_test.go @@ -18,7 +18,7 @@ func TestValueMapSize(t *testing.T) { v.Store("a", ni(1)) v.Store("b", ni(2)) v.Store("c", ni(3)) - assert.Equal(t, v.Size(), 3) + assert.Equal(t, v.Length(), 3) } func TestValueMapSize2(t *testing.T) { @@ -34,7 +34,7 @@ func TestValueMapSize2(t *testing.T) { v.Store("d", ni(4)) // fmt.Println(1, v.read) // fmt.Println(2, v.dirty) - assert.Equal(t, v.Size(), 4) + assert.Equal(t, v.Length(), 4) } func TestValueMapClear(t *testing.T) { @@ -48,8 +48,8 @@ func TestValueMapClear(t *testing.T) { return true }) assert.Equal(t, 0, len(v.dirty)) // 此时全在 read 表中 - assert.Equal(t, v.Size(), 4) + assert.Equal(t, v.Length(), 4) v.Clear() - assert.Equal(t, v.Size(), 0) + assert.Equal(t, v.Length(), 0) }