value semantics for bools

This commit is contained in:
E Sequeira 2022-07-20 16:20:02 +01:00
parent 9515d2f2c0
commit fe792a9369
10 changed files with 24 additions and 24 deletions

View file

@ -173,7 +173,7 @@ func builtinIsBool(args ...Object) (Object, error) {
if len(args) != 1 {
return nil, ErrWrongNumArguments
}
if _, ok := args[0].(*Bool); ok {
if _, ok := args[0].(Bool); ok {
return TrueValue, nil
}
return FalseValue, nil
@ -481,7 +481,7 @@ func builtinBool(args ...Object) (Object, error) {
if len(args) != 1 {
return nil, ErrWrongNumArguments
}
if _, ok := args[0].(*Bool); ok {
if _, ok := args[0].(Bool); ok {
return args[0], nil
}
v, ok := ToBool(args[0])

View file

@ -187,7 +187,7 @@ func fixDecodedObject(
modules *ModuleMap,
) (Object, error) {
switch o := o.(type) {
case *Bool:
case Bool:
if o.IsFalsy() {
return FalseValue, nil
}
@ -280,7 +280,7 @@ func init() {
gob.Register(&parser.SourceFileSet{})
gob.Register(&parser.SourceFile{})
gob.Register(&Array{})
gob.Register(&Bool{})
gob.Register(Bool{})
gob.Register(&Bytes{})
gob.Register(Char{})
gob.Register(&CompiledFunction{})

View file

@ -951,7 +951,7 @@ func (p *pp) printArg(arg Object, verb rune) {
// Some types can be done without reflection.
switch f := arg.(type) {
case *Bool:
case Bool:
p.fmtBool(!f.IsFalsy(), verb)
case Float:
p.fmtFloat(f.Value, 64, verb)

View file

@ -14,10 +14,10 @@ import (
var (
// TrueValue represents a true value.
TrueValue Object = &Bool{value: true}
TrueValue Object = Bool{value: true}
// FalseValue represents a false value.
FalseValue Object = &Bool{value: false}
FalseValue Object = Bool{value: false}
// UndefinedValue represents an undefined value.
UndefinedValue Object = &Undefined{}
@ -334,14 +334,14 @@ func (o *Array) CanIterate() bool {
// Bool represents a boolean value.
type Bool struct {
PtrObjectImpl
ObjectImpl
// this is intentionally non-public to force using objects.TrueValue and
// FalseValue always
value bool
}
func (o *Bool) String() string {
func (o Bool) String() string {
if o.value {
return "true"
}
@ -350,23 +350,23 @@ func (o *Bool) String() string {
}
// TypeName returns the name of the type.
func (o *Bool) TypeName() string {
func (o Bool) TypeName() string {
return "bool"
}
// Copy returns a copy of the type.
func (o *Bool) Copy() Object {
func (o Bool) Copy() Object {
return o
}
// IsFalsy returns true if the value of the type is falsy.
func (o *Bool) IsFalsy() bool {
func (o Bool) IsFalsy() bool {
return !o.value
}
// Equals returns true if the value of the type is equal to the value of
// another object.
func (o *Bool) Equals(x Object) bool {
func (o Bool) Equals(x Object) bool {
return o == x
}
@ -377,7 +377,7 @@ func (o *Bool) GobDecode(b []byte) (err error) {
}
// GobEncode encodes bool values into bytes.
func (o *Bool) GobEncode() (b []byte, err error) {
func (o Bool) GobEncode() (b []byte, err error) {
if o.value {
b = []byte{1}
} else {

View file

@ -17,7 +17,7 @@ func TestObject_TypeName(t *testing.T) {
require.Equal(t, "char", o.TypeName())
o = &tengo.String{}
require.Equal(t, "string", o.TypeName())
o = &tengo.Bool{}
o = tengo.Bool{}
require.Equal(t, "bool", o.TypeName())
o = &tengo.Array{}
require.Equal(t, "array", o.TypeName())
@ -131,7 +131,7 @@ func TestObject_BinaryOp(t *testing.T) {
var o tengo.Object = tengo.Char{}
_, err := o.BinaryOp(token.Add, tengo.UndefinedValue)
require.Error(t, err)
o = &tengo.Bool{}
o = tengo.Bool{}
_, err = o.BinaryOp(token.Add, tengo.UndefinedValue)
require.Error(t, err)
o = &tengo.Map{}

View file

@ -142,7 +142,7 @@ func Equal(
Equal(t, expected.Value, actual.(*tengo.String).Value, msg...)
case tengo.Char:
Equal(t, expected.Value, actual.(tengo.Char).Value, msg...)
case *tengo.Bool:
case tengo.Bool:
if expected != actual {
failExpectedActual(t, expected, actual, msg...)
}

View file

@ -193,7 +193,7 @@ func Encode(o tengo.Object) ([]byte, error) {
idx++
}
b = append(b, '}')
case *tengo.Bool:
case tengo.Bool:
if o.IsFalsy() {
b = strconv.AppendBool(b, false)
} else {

View file

@ -806,7 +806,7 @@ func textFormatBool(args ...tengo.Object) (ret tengo.Object, err error) {
return
}
b1, ok := args[0].(*tengo.Bool)
b1, ok := args[0].(tengo.Bool)
if !ok {
err = tengo.ErrInvalidArgumentType{
Name: "first",

View file

@ -88,7 +88,7 @@ func ToInt(o Object) (v int, ok bool) {
case Char:
v = int(o.Value)
ok = true
case *Bool:
case Bool:
if o == TrueValue {
v = 1
}
@ -115,7 +115,7 @@ func ToInt64(o Object) (v int64, ok bool) {
case Char:
v = int64(o.Value)
ok = true
case *Bool:
case Bool:
if o == TrueValue {
v = 1
}
@ -204,7 +204,7 @@ func ToInterface(o Object) (res interface{}) {
res = o.Value
case Float:
res = o.Value
case *Bool:
case Bool:
res = o == TrueValue
case Char:
res = o.Value

View file

@ -3953,8 +3953,8 @@ func objectZeroCopy(o tengo.Object) tengo.Object {
return tengo.Int{}
case tengo.Float:
return tengo.Float{}
case *tengo.Bool:
return &tengo.Bool{}
case tengo.Bool:
return tengo.Bool{}
case tengo.Char:
return tengo.Char{}
case *tengo.String: