Skip to content

Commit

Permalink
feat: 补全AsBool的支持类型
Browse files Browse the repository at this point in the history
  • Loading branch information
fy0 committed Jun 16, 2024
1 parent 998e9f4 commit 047f070
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 11 deletions.
15 changes: 12 additions & 3 deletions builtin_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand All @@ -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}),
}
16 changes: 13 additions & 3 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
28 changes: 28 additions & 0 deletions types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion valuemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions valuemap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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)
}

0 comments on commit 047f070

Please sign in to comment.