Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func Compile(tree *parser.Tree, config *conf.Config) (program *Program, err erro
c.emit(OpCast, 1)
case reflect.Float64:
c.emit(OpCast, 2)
case reflect.Bool:
c.emit(OpCast, 3)
}
if c.config.Optimize {
c.optimize()
Expand Down
48 changes: 48 additions & 0 deletions compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package compiler_test

import (
"math"
"reflect"
"testing"

"github.com/expr-lang/expr/internal/testify/assert"
Expand Down Expand Up @@ -675,3 +676,50 @@ func TestCompile_call_on_nil(t *testing.T) {
require.Error(t, err)
require.Contains(t, err.Error(), "foo is nil; cannot call nil as function")
}

func TestCompile_Expect(t *testing.T) {
tests := []struct {
input string
option expr.Option
op vm.Opcode
arg int
}{
{
input: "1",
option: expr.AsKind(reflect.Int),
op: vm.OpCast,
arg: 0,
},
{
input: "1",
option: expr.AsInt64(),
op: vm.OpCast,
arg: 1,
},
{
input: "1",
option: expr.AsFloat64(),
op: vm.OpCast,
arg: 2,
},
{
input: "true",
option: expr.AsBool(),
op: vm.OpCast,
arg: 3,
},
}

for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
program, err := expr.Compile(tt.input, tt.option)
require.NoError(t, err)

lastOp := program.Bytecode[len(program.Bytecode)-1]
lastArg := program.Arguments[len(program.Arguments)-1]

assert.Equal(t, tt.op, lastOp)
assert.Equal(t, tt.arg, lastArg)
})
}
}
20 changes: 20 additions & 0 deletions test/issues/830/issue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package issues

import (
"testing"

"github.com/expr-lang/expr"
"github.com/expr-lang/expr/internal/testify/assert"
"github.com/expr-lang/expr/internal/testify/require"
)

func TestIssue830(t *testing.T) {
program, err := expr.Compile("varNotExist", expr.AllowUndefinedVariables(), expr.AsBool())
require.NoError(t, err)

output, err := expr.Run(program, map[string]interface{}{})
require.NoError(t, err)

// The user expects output to be false (bool), but gets nil.
assert.Equal(t, false, output)
}
12 changes: 12 additions & 0 deletions vm/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,18 @@ func ToFloat64(a any) float64 {
}
}

func ToBool(a any) bool {
if a == nil {
return false
}
switch x := a.(type) {
case bool:
return x
default:
panic(fmt.Sprintf("invalid operation: bool(%T)", x))
}
}

func IsNil(v any) bool {
if v == nil {
return true
Expand Down
2 changes: 2 additions & 0 deletions vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,8 @@ func (vm *VM) Run(program *Program, env any) (_ any, err error) {
vm.push(runtime.ToInt64(vm.pop()))
case 2:
vm.push(runtime.ToFloat64(vm.pop()))
case 3:
vm.push(runtime.ToBool(vm.pop()))
}

case OpDeref:
Expand Down
83 changes: 75 additions & 8 deletions vm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,57 @@ func TestRun_ReuseVM_for_different_variables(t *testing.T) {
}

func TestRun_Cast(t *testing.T) {
input := `1`
tests := []struct {
input string
expect reflect.Kind
want any
}{
{
input: `1`,
expect: reflect.Float64,
want: float64(1),
},
{
input: `1`,
expect: reflect.Int,
want: int(1),
},
{
input: `1`,
expect: reflect.Int64,
want: int64(1),
},
{
input: `true`,
expect: reflect.Bool,
want: true,
},
{
input: `false`,
expect: reflect.Bool,
want: false,
},
{
input: `nil`,
expect: reflect.Bool,
want: false,
},
}

tree, err := parser.Parse(input)
require.NoError(t, err)
for _, tt := range tests {
t.Run(fmt.Sprintf("%v %v", tt.expect, tt.input), func(t *testing.T) {
tree, err := parser.Parse(tt.input)
require.NoError(t, err)

program, err := compiler.Compile(tree, &conf.Config{Expect: reflect.Float64})
require.NoError(t, err)
program, err := compiler.Compile(tree, &conf.Config{Expect: tt.expect})
require.NoError(t, err)

out, err := vm.Run(program, nil)
require.NoError(t, err)
out, err := vm.Run(program, nil)
require.NoError(t, err)

require.Equal(t, float64(1), out)
require.Equal(t, tt.want, out)
})
}
}

func TestRun_Helpers(t *testing.T) {
Expand Down Expand Up @@ -1063,6 +1102,34 @@ func TestVM_DirectBasicOpcodes(t *testing.T) {
consts: []any{int32(42)},
want: int64(42),
},
{
name: "OpCast bool to bool",
bytecode: []vm.Opcode{
vm.OpTrue, // Push true
vm.OpCast, // Cast to bool
},
args: []int{0, 3},
want: true,
},
{
name: "OpCast nil to bool",
bytecode: []vm.Opcode{
vm.OpNil, // Push nil
vm.OpCast, // Cast to bool
},
args: []int{0, 3},
want: false,
},
{
name: "OpCast int to bool",
bytecode: []vm.Opcode{
vm.OpPush, // Push int
vm.OpCast, // Cast to bool
},
args: []int{0, 3},
consts: []any{1},
wantErr: true,
},
{
name: "OpCast invalid type",
bytecode: []vm.Opcode{
Expand Down
Loading