diff --git a/builtin/builtin.go b/builtin/builtin.go index a6550acf9..4ec4e51bb 100644 --- a/builtin/builtin.go +++ b/builtin/builtin.go @@ -7,6 +7,7 @@ import ( "fmt" "reflect" "sort" + "strconv" "strings" "time" @@ -992,6 +993,9 @@ var Builtins = []*Function{ } size := v.Len() + if strconv.IntSize == 32 { + return uniq32(v, size) + } ret := []any{} // Scan small sets linearly and index them once they grow. var index *uniqIndex diff --git a/builtin/utils.go b/builtin/utils.go index 39f7206bd..8fc004717 100644 --- a/builtin/utils.go +++ b/builtin/utils.go @@ -103,6 +103,9 @@ func containsEqual(values []any, value any) bool { // strings and numbers. Strings only equal strings. Integers equal integers // via int(x) and floats via float64(x), so integers are also keyed as // float64 for float lookups. Everything else falls back to a scan. +// +// amd64 uses this index. On 386, signed int64 equality is wider than int, so +// uniq uses uniqIndex32 instead. type uniqIndex struct { strings map[string]struct{} ints map[int]struct{} @@ -159,7 +162,8 @@ func (u *uniqIndex) add(value any) { } } -// intKeys mirrors the conversions runtime.Equal applies to integers. +// intKeys mirrors the conversions runtime.Equal applies to integers when int +// is 64 bits. On 386, signed pairs that include an int64 compare as int64. func intKeys(value any) (int, float64, bool) { switch x := value.(type) { case int: @@ -186,6 +190,145 @@ func intKeys(value any) (int, float64, bool) { return 0, 0, false } +// uniqIndex32 matches runtime.Equal on GOARCH=386, where signed integers that +// include an int64 compare as int64 and mixed signedness still compares as int. +type uniqIndex32 struct { + strings map[string]struct{} + signed map[int64]struct{} + signedAsInt map[int]struct{} + unsigned map[int]struct{} + intFloats map[float64]struct{} + floats map[float64]struct{} + other []any +} + +func newUniqIndex32(values []any) *uniqIndex32 { + u := &uniqIndex32{ + strings: map[string]struct{}{}, + signed: map[int64]struct{}{}, + signedAsInt: map[int]struct{}{}, + unsigned: map[int]struct{}{}, + intFloats: map[float64]struct{}{}, + floats: map[float64]struct{}{}, + } + for _, v := range values { + u.add(v) + } + return u +} + +func (u *uniqIndex32) has(value any) bool { + if s, ok := value.(string); ok { + _, found := u.strings[s] + return found + } + if k, ok := numberKey32(value); ok { + if k.signed { + if _, found := u.signed[k.s]; found { + return true + } + if _, found := u.unsigned[k.trunc]; found { + return true + } + } else { + if _, found := u.unsigned[k.trunc]; found { + return true + } + if _, found := u.signedAsInt[k.trunc]; found { + return true + } + } + _, found := u.floats[k.float] + return found + } + if f, ok := floatKey(value); ok { + _, found := u.floats[f] + if !found { + _, found = u.intFloats[f] + } + return found + } + return containsEqual(u.other, value) +} + +func (u *uniqIndex32) add(value any) { + if s, ok := value.(string); ok { + u.strings[s] = struct{}{} + return + } + if k, ok := numberKey32(value); ok { + u.intFloats[k.float] = struct{}{} + if k.signed { + u.signed[k.s] = struct{}{} + u.signedAsInt[k.trunc] = struct{}{} + return + } + u.unsigned[k.trunc] = struct{}{} + return + } + if f, ok := floatKey(value); ok { + u.floats[f] = struct{}{} + return + } + u.other = append(u.other, value) +} + +type integerKey32 struct { + signed bool + s int64 + trunc int + float float64 +} + +func numberKey32(value any) (integerKey32, bool) { + switch x := value.(type) { + case int: + return integerKey32{signed: true, s: int64(x), trunc: x, float: float64(x)}, true + case int8: + return integerKey32{signed: true, s: int64(x), trunc: int(x), float: float64(x)}, true + case int16: + return integerKey32{signed: true, s: int64(x), trunc: int(x), float: float64(x)}, true + case int32: + return integerKey32{signed: true, s: int64(x), trunc: int(x), float: float64(x)}, true + case int64: + return integerKey32{signed: true, s: x, trunc: int(x), float: float64(x)}, true + case uint: + return integerKey32{trunc: int(x), float: float64(x)}, true + case uint8: + return integerKey32{trunc: int(x), float: float64(x)}, true + case uint16: + return integerKey32{trunc: int(x), float: float64(x)}, true + case uint32: + return integerKey32{trunc: int(x), float: float64(x)}, true + case uint64: + return integerKey32{trunc: int(x), float: float64(x)}, true + } + return integerKey32{}, false +} + +// uniq32 is uniq on GOARCH=386, where signed int64 values must not share an +// int key. strconv.IntSize == 32 is constant, so amd64 does not call this. +func uniq32(v reflect.Value, size int) (any, error) { + ret := []any{} + var index *uniqIndex32 + for i := 0; i < size; i++ { + value := v.Index(i).Interface() + if index == nil && len(ret) >= 16 { + index = newUniqIndex32(ret) + } + if index != nil { + if index.has(value) { + continue + } + index.add(value) + } else if containsEqual(ret, value) { + continue + } + ret = append(ret, value) + } + return ret, nil +} + func floatKey(value any) (float64, bool) { switch x := value.(type) { case float32: diff --git a/checker/checker.go b/checker/checker.go index f33b936b2..54d175008 100644 --- a/checker/checker.go +++ b/checker/checker.go @@ -4,6 +4,7 @@ import ( "fmt" "reflect" "regexp" + "strconv" "time" "github.com/expr-lang/expr/ast" @@ -454,9 +455,14 @@ func (v *Checker) binaryNode(node *ast.BinaryNode) Nature { case "%": if l.IsInteger && r.IsInteger { - return v.config.NtCache.FromType(intType) + return l.PromoteNumericNature(&v.config.NtCache, r) } if l.MaybeCompatible(&v.config.NtCache, r, IntegerCheck) { + // One operand is any. On 32-bit the runtime value may be int64, + // so claiming int would make `==` emit OpEqualInt and panic. + if strconv.IntSize == 32 { + return Nature{} + } return v.config.NtCache.FromType(intType) } @@ -741,7 +747,7 @@ func (v *Checker) callNode(node *ast.CallNode) Nature { } if nt.Kind == reflect.Func { - outType, err := v.checkArguments(fnName, nt, node.Arguments, node) + outType, err := v.checkArguments(fnName, nt, node.Arguments, node, true) if err != nil { if v.err == nil { v.err = err @@ -1071,7 +1077,7 @@ func (v *Checker) checkFunction(f *builtin.Function, node ast.Node, arguments [] } return v.config.NtCache.FromType(t) } else if len(f.Types) == 0 { - nt, err := v.checkArguments(f.Name, v.config.NtCache.FromType(f.Type()), arguments, node) + nt, err := v.checkArguments(f.Name, v.config.NtCache.FromType(f.Type()), arguments, node, false) if err != nil { if v.err == nil { v.err = err @@ -1087,7 +1093,7 @@ func (v *Checker) checkFunction(f *builtin.Function, node ast.Node, arguments [] } var lastErr *file.Error for _, t := range f.Types { - outNature, err := v.checkArguments(f.Name, v.config.NtCache.FromType(t), arguments, node) + outNature, err := v.checkArguments(f.Name, v.config.NtCache.FromType(t), arguments, node, false) if err != nil { lastErr = err continue @@ -1117,6 +1123,7 @@ func (v *Checker) checkArguments( fn Nature, arguments []ast.Node, node ast.Node, + reflectCall bool, ) (Nature, *file.Error) { if fn.IsUnknown(&v.config.NtCache) { return Nature{}, nil @@ -1200,6 +1207,16 @@ func (v *Checker) checkArguments( if in.IsInteger && argNature.IsInteger && argNature.Kind != in.Kind { traverseAndReplaceIntegerNodesWithIntegerNodes(&arguments[i], in) + // Only real Go funcs (reflect.Call or OpCallTyped) assert .(int). + // Builtins and expr.Function take ...any and convert with ToInt. + if reflectCall && strconv.IntSize == 32 { + if got := arguments[i].Type(); got != nil && in.Type != nil && got.Kind() == reflect.Int64 && !got.AssignableTo(in.Type) { + return Nature{}, &file.Error{ + Location: arguments[i].Location(), + Message: fmt.Sprintf("cannot use %s as argument (type %s) to call %v ", got, in.String(), name), + } + } + } continue } diff --git a/checker/nature/nature.go b/checker/nature/nature.go index c96f28c43..fd1b8c7c4 100644 --- a/checker/nature/nature.go +++ b/checker/nature/nature.go @@ -3,6 +3,7 @@ package nature import ( "fmt" "reflect" + "strconv" "time" "github.com/expr-lang/expr/builtin" @@ -11,6 +12,7 @@ import ( var ( intType = reflect.TypeOf(0) + int64Type = reflect.TypeOf(int64(0)) floatType = reflect.TypeOf(float64(0)) arrayType = reflect.TypeOf([]any{}) byteSliceType = reflect.TypeOf([]byte{}) @@ -484,7 +486,25 @@ func (n *Nature) PromoteNumericNature(c *Cache, rhs Nature) Nature { if n.IsFloat || rhs.IsFloat { return c.FromType(floatType) } - return c.FromType(intType) + return c.FromType(integerArithType(n.Kind, rhs.Kind)) +} + +// integerArithType matches the runtime helpers. int64 arithmetic stays int64 +// only when int is 32 bits; otherwise the result is int, as on amd64 master. +func integerArithType(a, b reflect.Kind) reflect.Type { + if strconv.IntSize == 32 && signedKind(a) && signedKind(b) && (a == reflect.Int64 || b == reflect.Int64) { + return int64Type + } + return intType +} + +func signedKind(k reflect.Kind) bool { + switch k { + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + return true + default: + return false + } } func (n *Nature) IsTime() bool { diff --git a/expr_test.go b/expr_test.go index 7e2b3eff1..2b62e5bf8 100644 --- a/expr_test.go +++ b/expr_test.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "reflect" + "strconv" "strings" "sync" "testing" @@ -712,6 +713,15 @@ func TestExpr_readme_example(t *testing.T) { require.Equal(t, "Hello, world!", output) } +// int32PlusInt64Result is int on 64-bit and int64 on 32-bit, matching +// signed arithmetic that includes an int64. +func int32PlusInt64Result() any { + if strconv.IntSize == 32 { + return int64(0) + } + return 0 +} + func TestExpr(t *testing.T) { date := time.Date(2017, time.October, 23, 18, 30, 0, 0, time.UTC) oneDay, _ := time.ParseDuration("24h") @@ -813,7 +823,7 @@ func TestExpr(t *testing.T) { }, { `Int32 + Int64`, - 0, + int32PlusInt64Result(), }, { `Float64 + 0`, diff --git a/test/issues/804/issue_test.go b/test/issues/804/issue_test.go new file mode 100644 index 000000000..e6fe4d7ba --- /dev/null +++ b/test/issues/804/issue_test.go @@ -0,0 +1,174 @@ +package issue_test + +import ( + "math" + "reflect" + "strconv" + "testing" + + "github.com/expr-lang/expr" + "github.com/expr-lang/expr/internal/testify/require" + "github.com/expr-lang/expr/vm/runtime" +) + +// int64 arithmetic is widened only where int is 32 bits. On amd64 the +// result stays int, matching master. +func int64Arith(v int64) any { + if strconv.IntSize == 32 { + return v + } + return int(v) +} + +type callEnv struct { + I64 int64 + Any any + I int + J int + FnInt func(int) int + MI map[int]string +} + +func TestIssue804(t *testing.T) { + env := callEnv{ + I64: int64(math.MaxInt32) + 1, + Any: int64(math.MaxInt32) + 1, + I: 20, + J: 22, + FnInt: func(x int) int { return x }, + MI: map[int]string{1: "one"}, + } + wide := int64(1) << 32 + + t.Run("values", func(t *testing.T) { + tests := []struct { + expr string + want any + env any + }{ + {"I64 + 0", int64Arith(int64(math.MaxInt32) + 1), env}, + {"I64 + 1", int64Arith(int64(math.MaxInt32) + 2), env}, + {"I64 * 2", int64Arith((int64(math.MaxInt32) + 1) * 2), env}, + {"I64 % 3", int64Arith((int64(math.MaxInt32) + 1) % 3), env}, + {"I64 > 0", true, env}, + {"I + J", 42, env}, + {"1 + 2", 3, env}, + {"8 % 3", 2, env}, + } + for _, tt := range tests { + t.Run(tt.expr, func(t *testing.T) { + got, err := expr.Eval(tt.expr, tt.env) + require.NoError(t, err) + require.Equal(t, tt.want, got) + }) + } + }) + + t.Run("wide equality", func(t *testing.T) { + got, err := expr.Eval("W == 0", map[string]any{"W": wide}) + require.NoError(t, err) + require.Equal(t, false, got) + }) + + t.Run("checker matches runtime", func(t *testing.T) { + for _, code := range []string{"I64 + 0", "I64 - 1", "I64 * 2", "I64 % 3", "I + J", "1 + 2", "I64 > 0"} { + program, err := expr.Compile(code, expr.Env(env)) + require.NoError(t, err) + got, err := expr.Run(program, env) + require.NoError(t, err) + require.Equal(t, program.Node().Type(), reflect.TypeOf(got)) + } + }) +} + +func TestIssue804_funcAndMap(t *testing.T) { + env := callEnv{ + I64: int64(math.MaxInt32) + 1, + FnInt: func(x int) int { return x }, + MI: map[int]string{1: "one"}, + } + + t.Run("FnInt(I64 + 1)", func(t *testing.T) { + program, err := expr.Compile("FnInt(I64 + 1)", expr.Env(env)) + if strconv.IntSize == 32 { + require.Error(t, err) + require.Contains(t, err.Error(), "int64") + return + } + require.NoError(t, err) + got, err := expr.Run(program, env) + require.NoError(t, err) + require.Equal(t, int(env.I64+1), got) + }) + + t.Run("MI[I64 + 1]", func(t *testing.T) { + _, err := expr.Compile(`MI[I64 + 1]`, expr.Env(env)) + if strconv.IntSize == 32 { + require.Error(t, err) + return + } + require.NoError(t, err) + }) +} + +func TestIssue804_moduloAny(t *testing.T) { + t.Run("Any % 2 == 1", func(t *testing.T) { + got, err := expr.Eval(`Any % 2 == 1`, callEnv{Any: int64(math.MaxInt32) + 2}) + require.NoError(t, err) + require.Equal(t, true, got) + }) + + t.Run("(I64 % Any) == 0", func(t *testing.T) { + got, err := expr.Eval(`(I64 % Any) == 0`, callEnv{ + I64: int64(1)<<32 - 1, + Any: int64(3), + }) + require.NoError(t, err) + require.Equal(t, true, got) + }) + + t.Run("modulo any type", func(t *testing.T) { + program, err := expr.Compile(`Any % 2`, expr.Env(callEnv{Any: int64(1)})) + require.NoError(t, err) + if strconv.IntSize == 32 { + require.Equal(t, reflect.TypeOf((*any)(nil)).Elem(), program.Node().Type()) + return + } + require.Equal(t, reflect.TypeOf(0), program.Node().Type()) + }) +} + +// Builtins and expr.Function receive ...any and convert with ToInt. They must +// accept an int64 on 386 instead of failing the typed-call check. +func TestIssue804_toIntCallees(t *testing.T) { + env := map[string]any{"I64": int64(3)} + double := expr.Function("double", + func(params ...any) (any, error) { + return runtime.ToInt(params[0]) * 2, nil + }, + new(func(int) int), + ) + + tests := []struct { + expr string + want any + opts []expr.Option + }{ + {`repeat("a", I64)`, "aaa", nil}, + {`bitand(I64, 1)`, 1, nil}, + {`bitshl(I64, 1)`, 6, nil}, + {`split("a,b,c", ",", I64)`, []string{"a", "b", "c"}, nil}, + {`double(I64)`, 6, []expr.Option{double}}, + {`double(I64 + 1)`, 8, []expr.Option{double}}, + } + for _, tt := range tests { + t.Run(tt.expr, func(t *testing.T) { + opts := append([]expr.Option{expr.Env(env)}, tt.opts...) + program, err := expr.Compile(tt.expr, opts...) + require.NoError(t, err) + got, err := expr.Run(program, env) + require.NoError(t, err) + require.Equal(t, tt.want, got) + }) + } +} diff --git a/vm/runtime/helpers/main.go b/vm/runtime/helpers/main.go index 54a4fc235..9d333bca2 100644 --- a/vm/runtime/helpers/main.go +++ b/vm/runtime/helpers/main.go @@ -14,10 +14,20 @@ func main() { err := template.Must( template.New("helpers"). Funcs(template.FuncMap{ - "cases": func(op string) string { return cases(op, uints, ints, floats) }, - "cases_int_only": func(op string) string { return cases(op, uints, ints) }, + "cases": func(op string) string { + mode := modePlain + switch op { + case "+", "-", "*": + mode = modeArith + case "==", "<", ">", "<=", ">=": + mode = modeCmp + } + return cases(op, mode, uints, ints, floats) + }, + "cases_int_only": func(op string) string { return cases(op, modePlain, uints, ints) }, + "cases_mod_wide": func() string { return modWideCases() }, "cases_with_duration": func(op string) string { - return cases(op, uints, ints, floats, []string{"time.Duration"}) + return cases(op, modeArith, uints, ints, floats, []string{"time.Duration"}) }, "array_equal_cases": func() string { return arrayEqualCases([]string{"string"}, uints, ints, floats) }, }). @@ -55,7 +65,15 @@ var floats = []string{ "float64", } -func cases(op string, xs ...[]string) string { +type genMode int + +const ( + modePlain genMode = iota + modeArith // int64 result only when int is 32 bits + modeCmp // compare as int64; bool result matches int on amd64 +) + +func cases(op string, mode genMode, xs ...[]string) string { var types []string for _, x := range xs { types = append(types, x...) @@ -79,9 +97,21 @@ func cases(op string, xs ...[]string) string { t = "float64" } echo(`case %v:`, b) - if op == "/" { + switch { + case op == "/": echo(`return float64(x) / float64(y)`) - } else { + case mode == modeArith && widenInt64(a, b): + // strconv.IntSize is a constant, so on amd64 the int64 arm + // compiles out and the result stays int. + echo(`if strconv.IntSize == 32 {`) + echo(`return int64(x) %s int64(y)`, op) + echo(`}`) + echo(`return int(x) %s int(y)`, op) + case mode == modeCmp && widenInt64(a, b): + // Signed int64 comparison matches int comparison when int is + // 64 bits, so this needs no arch branch. + echo(`return int64(x) %s int64(y)`, op) + default: echo(`return %v(x) %v %v(y)`, t, op, t) } } @@ -133,10 +163,59 @@ func isFloat(t string) bool { return strings.HasPrefix(t, "float") } +func isSigned(t string) bool { + switch t { + case "int", "int8", "int16", "int32", "int64": + return true + default: + return false + } +} + +// widenInt64 reports signed pairs that include an int64. uint64 stays on the +// int path: mixed signedness has no wider type, and unsigned-only arithmetic +// is left unchanged. +func widenInt64(a, b string) bool { + if isFloat(a) || isFloat(b) || isDuration(a) || isDuration(b) { + return false + } + if !isSigned(a) || !isSigned(b) { + return false + } + return a == "int64" || b == "int64" +} + func isDuration(t string) bool { return t == "time.Duration" } +// modWideCases emits only signed pairs that include an int64. Every other +// pair falls through to Modulo, which still returns int. +func modWideCases() string { + signed := []string{"int", "int8", "int16", "int32", "int64"} + _, _ = fmt.Fprintf(os.Stderr, "Generating wide %% cases for %v\n", signed) + var out string + echo := func(s string, xs ...any) { + out += fmt.Sprintf(s, xs...) + "\n" + } + for _, a := range signed { + echo(`case %s:`, a) + echo(`switch y := b.(type) {`) + for _, b := range signed { + if a != "int64" && b != "int64" { + continue + } + echo(`case %s:`, b) + echo(`if strconv.IntSize == 32 {`) + echo(`return int64(x) %s int64(y)`, "%") + echo(`}`) + echo(`return int(x) %s int(y)`, "%") + } + echo(`}`) + } + return strings.TrimRight(out, "\n") +} + const helpers = `// Code generated by vm/runtime/helpers/main.go. DO NOT EDIT. package runtime @@ -144,6 +223,7 @@ package runtime import ( "fmt" "reflect" + "strconv" "time" ) @@ -329,4 +409,13 @@ func Modulo(a, b interface{}) int { } panic(fmt.Sprintf("invalid operation: %T %% %T", a, b)) } + +// ModuloWide is the 32-bit path for signed pairs that include an int64. +// Modulo keeps its int result. The VM calls ModuloWide only when int is 32 bits. +func ModuloWide(a, b interface{}) interface{} { + switch x := a.(type) { + {{ cases_mod_wide }} + } + return Modulo(a, b) +} ` diff --git a/vm/runtime/helpers[generated].go b/vm/runtime/helpers[generated].go index d950f1111..1e60ef759 100644 --- a/vm/runtime/helpers[generated].go +++ b/vm/runtime/helpers[generated].go @@ -5,6 +5,7 @@ package runtime import ( "fmt" "reflect" + "strconv" "time" ) @@ -166,7 +167,7 @@ func Equal(a, b interface{}) bool { case int32: return int(x) == int(y) case int64: - return int(x) == int(y) + return int64(x) == int64(y) case float32: return float64(x) == float64(y) case float64: @@ -193,7 +194,7 @@ func Equal(a, b interface{}) bool { case int32: return int(x) == int(y) case int64: - return int(x) == int(y) + return int64(x) == int64(y) case float32: return float64(x) == float64(y) case float64: @@ -220,7 +221,7 @@ func Equal(a, b interface{}) bool { case int32: return int(x) == int(y) case int64: - return int(x) == int(y) + return int64(x) == int64(y) case float32: return float64(x) == float64(y) case float64: @@ -247,7 +248,7 @@ func Equal(a, b interface{}) bool { case int32: return int(x) == int(y) case int64: - return int(x) == int(y) + return int64(x) == int64(y) case float32: return float64(x) == float64(y) case float64: @@ -266,15 +267,15 @@ func Equal(a, b interface{}) bool { case uint64: return int(x) == int(y) case int: - return int(x) == int(y) + return int64(x) == int64(y) case int8: - return int(x) == int(y) + return int64(x) == int64(y) case int16: - return int(x) == int(y) + return int64(x) == int64(y) case int32: - return int(x) == int(y) + return int64(x) == int64(y) case int64: - return int(x) == int(y) + return int64(x) == int64(y) case float32: return float64(x) == float64(y) case float64: @@ -857,7 +858,7 @@ func Less(a, b interface{}) bool { case int32: return int(x) < int(y) case int64: - return int(x) < int(y) + return int64(x) < int64(y) case float32: return float64(x) < float64(y) case float64: @@ -884,7 +885,7 @@ func Less(a, b interface{}) bool { case int32: return int(x) < int(y) case int64: - return int(x) < int(y) + return int64(x) < int64(y) case float32: return float64(x) < float64(y) case float64: @@ -911,7 +912,7 @@ func Less(a, b interface{}) bool { case int32: return int(x) < int(y) case int64: - return int(x) < int(y) + return int64(x) < int64(y) case float32: return float64(x) < float64(y) case float64: @@ -938,7 +939,7 @@ func Less(a, b interface{}) bool { case int32: return int(x) < int(y) case int64: - return int(x) < int(y) + return int64(x) < int64(y) case float32: return float64(x) < float64(y) case float64: @@ -957,15 +958,15 @@ func Less(a, b interface{}) bool { case uint64: return int(x) < int(y) case int: - return int(x) < int(y) + return int64(x) < int64(y) case int8: - return int(x) < int(y) + return int64(x) < int64(y) case int16: - return int(x) < int(y) + return int64(x) < int64(y) case int32: - return int(x) < int(y) + return int64(x) < int64(y) case int64: - return int(x) < int(y) + return int64(x) < int64(y) case float32: return float64(x) < float64(y) case float64: @@ -1202,7 +1203,7 @@ func More(a, b interface{}) bool { case int32: return int(x) > int(y) case int64: - return int(x) > int(y) + return int64(x) > int64(y) case float32: return float64(x) > float64(y) case float64: @@ -1229,7 +1230,7 @@ func More(a, b interface{}) bool { case int32: return int(x) > int(y) case int64: - return int(x) > int(y) + return int64(x) > int64(y) case float32: return float64(x) > float64(y) case float64: @@ -1256,7 +1257,7 @@ func More(a, b interface{}) bool { case int32: return int(x) > int(y) case int64: - return int(x) > int(y) + return int64(x) > int64(y) case float32: return float64(x) > float64(y) case float64: @@ -1283,7 +1284,7 @@ func More(a, b interface{}) bool { case int32: return int(x) > int(y) case int64: - return int(x) > int(y) + return int64(x) > int64(y) case float32: return float64(x) > float64(y) case float64: @@ -1302,15 +1303,15 @@ func More(a, b interface{}) bool { case uint64: return int(x) > int(y) case int: - return int(x) > int(y) + return int64(x) > int64(y) case int8: - return int(x) > int(y) + return int64(x) > int64(y) case int16: - return int(x) > int(y) + return int64(x) > int64(y) case int32: - return int(x) > int(y) + return int64(x) > int64(y) case int64: - return int(x) > int(y) + return int64(x) > int64(y) case float32: return float64(x) > float64(y) case float64: @@ -1547,7 +1548,7 @@ func LessOrEqual(a, b interface{}) bool { case int32: return int(x) <= int(y) case int64: - return int(x) <= int(y) + return int64(x) <= int64(y) case float32: return float64(x) <= float64(y) case float64: @@ -1574,7 +1575,7 @@ func LessOrEqual(a, b interface{}) bool { case int32: return int(x) <= int(y) case int64: - return int(x) <= int(y) + return int64(x) <= int64(y) case float32: return float64(x) <= float64(y) case float64: @@ -1601,7 +1602,7 @@ func LessOrEqual(a, b interface{}) bool { case int32: return int(x) <= int(y) case int64: - return int(x) <= int(y) + return int64(x) <= int64(y) case float32: return float64(x) <= float64(y) case float64: @@ -1628,7 +1629,7 @@ func LessOrEqual(a, b interface{}) bool { case int32: return int(x) <= int(y) case int64: - return int(x) <= int(y) + return int64(x) <= int64(y) case float32: return float64(x) <= float64(y) case float64: @@ -1647,15 +1648,15 @@ func LessOrEqual(a, b interface{}) bool { case uint64: return int(x) <= int(y) case int: - return int(x) <= int(y) + return int64(x) <= int64(y) case int8: - return int(x) <= int(y) + return int64(x) <= int64(y) case int16: - return int(x) <= int(y) + return int64(x) <= int64(y) case int32: - return int(x) <= int(y) + return int64(x) <= int64(y) case int64: - return int(x) <= int(y) + return int64(x) <= int64(y) case float32: return float64(x) <= float64(y) case float64: @@ -1892,7 +1893,7 @@ func MoreOrEqual(a, b interface{}) bool { case int32: return int(x) >= int(y) case int64: - return int(x) >= int(y) + return int64(x) >= int64(y) case float32: return float64(x) >= float64(y) case float64: @@ -1919,7 +1920,7 @@ func MoreOrEqual(a, b interface{}) bool { case int32: return int(x) >= int(y) case int64: - return int(x) >= int(y) + return int64(x) >= int64(y) case float32: return float64(x) >= float64(y) case float64: @@ -1946,7 +1947,7 @@ func MoreOrEqual(a, b interface{}) bool { case int32: return int(x) >= int(y) case int64: - return int(x) >= int(y) + return int64(x) >= int64(y) case float32: return float64(x) >= float64(y) case float64: @@ -1973,7 +1974,7 @@ func MoreOrEqual(a, b interface{}) bool { case int32: return int(x) >= int(y) case int64: - return int(x) >= int(y) + return int64(x) >= int64(y) case float32: return float64(x) >= float64(y) case float64: @@ -1992,15 +1993,15 @@ func MoreOrEqual(a, b interface{}) bool { case uint64: return int(x) >= int(y) case int: - return int(x) >= int(y) + return int64(x) >= int64(y) case int8: - return int(x) >= int(y) + return int64(x) >= int64(y) case int16: - return int(x) >= int(y) + return int64(x) >= int64(y) case int32: - return int(x) >= int(y) + return int64(x) >= int64(y) case int64: - return int(x) >= int(y) + return int64(x) >= int64(y) case float32: return float64(x) >= float64(y) case float64: @@ -2237,6 +2238,9 @@ func Add(a, b interface{}) interface{} { case int32: return int(x) + int(y) case int64: + if strconv.IntSize == 32 { + return int64(x) + int64(y) + } return int(x) + int(y) case float32: return float64(x) + float64(y) @@ -2264,6 +2268,9 @@ func Add(a, b interface{}) interface{} { case int32: return int(x) + int(y) case int64: + if strconv.IntSize == 32 { + return int64(x) + int64(y) + } return int(x) + int(y) case float32: return float64(x) + float64(y) @@ -2291,6 +2298,9 @@ func Add(a, b interface{}) interface{} { case int32: return int(x) + int(y) case int64: + if strconv.IntSize == 32 { + return int64(x) + int64(y) + } return int(x) + int(y) case float32: return float64(x) + float64(y) @@ -2318,6 +2328,9 @@ func Add(a, b interface{}) interface{} { case int32: return int(x) + int(y) case int64: + if strconv.IntSize == 32 { + return int64(x) + int64(y) + } return int(x) + int(y) case float32: return float64(x) + float64(y) @@ -2337,14 +2350,29 @@ func Add(a, b interface{}) interface{} { case uint64: return int(x) + int(y) case int: + if strconv.IntSize == 32 { + return int64(x) + int64(y) + } return int(x) + int(y) case int8: + if strconv.IntSize == 32 { + return int64(x) + int64(y) + } return int(x) + int(y) case int16: + if strconv.IntSize == 32 { + return int64(x) + int64(y) + } return int(x) + int(y) case int32: + if strconv.IntSize == 32 { + return int64(x) + int64(y) + } return int(x) + int(y) case int64: + if strconv.IntSize == 32 { + return int64(x) + int64(y) + } return int(x) + int(y) case float32: return float64(x) + float64(y) @@ -2584,6 +2612,9 @@ func Subtract(a, b interface{}) interface{} { case int32: return int(x) - int(y) case int64: + if strconv.IntSize == 32 { + return int64(x) - int64(y) + } return int(x) - int(y) case float32: return float64(x) - float64(y) @@ -2611,6 +2642,9 @@ func Subtract(a, b interface{}) interface{} { case int32: return int(x) - int(y) case int64: + if strconv.IntSize == 32 { + return int64(x) - int64(y) + } return int(x) - int(y) case float32: return float64(x) - float64(y) @@ -2638,6 +2672,9 @@ func Subtract(a, b interface{}) interface{} { case int32: return int(x) - int(y) case int64: + if strconv.IntSize == 32 { + return int64(x) - int64(y) + } return int(x) - int(y) case float32: return float64(x) - float64(y) @@ -2665,6 +2702,9 @@ func Subtract(a, b interface{}) interface{} { case int32: return int(x) - int(y) case int64: + if strconv.IntSize == 32 { + return int64(x) - int64(y) + } return int(x) - int(y) case float32: return float64(x) - float64(y) @@ -2684,14 +2724,29 @@ func Subtract(a, b interface{}) interface{} { case uint64: return int(x) - int(y) case int: + if strconv.IntSize == 32 { + return int64(x) - int64(y) + } return int(x) - int(y) case int8: + if strconv.IntSize == 32 { + return int64(x) - int64(y) + } return int(x) - int(y) case int16: + if strconv.IntSize == 32 { + return int64(x) - int64(y) + } return int(x) - int(y) case int32: + if strconv.IntSize == 32 { + return int64(x) - int64(y) + } return int(x) - int(y) case int64: + if strconv.IntSize == 32 { + return int64(x) - int64(y) + } return int(x) - int(y) case float32: return float64(x) - float64(y) @@ -2936,6 +2991,9 @@ func Multiply(a, b interface{}) interface{} { case int32: return int(x) * int(y) case int64: + if strconv.IntSize == 32 { + return int64(x) * int64(y) + } return int(x) * int(y) case float32: return float64(x) * float64(y) @@ -2965,6 +3023,9 @@ func Multiply(a, b interface{}) interface{} { case int32: return int(x) * int(y) case int64: + if strconv.IntSize == 32 { + return int64(x) * int64(y) + } return int(x) * int(y) case float32: return float64(x) * float64(y) @@ -2994,6 +3055,9 @@ func Multiply(a, b interface{}) interface{} { case int32: return int(x) * int(y) case int64: + if strconv.IntSize == 32 { + return int64(x) * int64(y) + } return int(x) * int(y) case float32: return float64(x) * float64(y) @@ -3023,6 +3087,9 @@ func Multiply(a, b interface{}) interface{} { case int32: return int(x) * int(y) case int64: + if strconv.IntSize == 32 { + return int64(x) * int64(y) + } return int(x) * int(y) case float32: return float64(x) * float64(y) @@ -3044,14 +3111,29 @@ func Multiply(a, b interface{}) interface{} { case uint64: return int(x) * int(y) case int: + if strconv.IntSize == 32 { + return int64(x) * int64(y) + } return int(x) * int(y) case int8: + if strconv.IntSize == 32 { + return int64(x) * int64(y) + } return int(x) * int(y) case int16: + if strconv.IntSize == 32 { + return int64(x) * int64(y) + } return int(x) * int(y) case int32: + if strconv.IntSize == 32 { + return int64(x) * int64(y) + } return int(x) * int(y) case int64: + if strconv.IntSize == 32 { + return int64(x) * int64(y) + } return int(x) * int(y) case float32: return float64(x) * float64(y) @@ -3716,3 +3798,71 @@ func Modulo(a, b interface{}) int { } panic(fmt.Sprintf("invalid operation: %T %% %T", a, b)) } + +// ModuloWide is the 32-bit path for signed pairs that include an int64. +// Modulo keeps its int result. The VM calls ModuloWide only when int is 32 bits. +func ModuloWide(a, b interface{}) interface{} { + switch x := a.(type) { + case int: + switch y := b.(type) { + case int64: + if strconv.IntSize == 32 { + return int64(x) % int64(y) + } + return int(x) % int(y) + } + case int8: + switch y := b.(type) { + case int64: + if strconv.IntSize == 32 { + return int64(x) % int64(y) + } + return int(x) % int(y) + } + case int16: + switch y := b.(type) { + case int64: + if strconv.IntSize == 32 { + return int64(x) % int64(y) + } + return int(x) % int(y) + } + case int32: + switch y := b.(type) { + case int64: + if strconv.IntSize == 32 { + return int64(x) % int64(y) + } + return int(x) % int(y) + } + case int64: + switch y := b.(type) { + case int: + if strconv.IntSize == 32 { + return int64(x) % int64(y) + } + return int(x) % int(y) + case int8: + if strconv.IntSize == 32 { + return int64(x) % int64(y) + } + return int(x) % int(y) + case int16: + if strconv.IntSize == 32 { + return int64(x) % int64(y) + } + return int(x) % int(y) + case int32: + if strconv.IntSize == 32 { + return int64(x) % int64(y) + } + return int(x) % int(y) + case int64: + if strconv.IntSize == 32 { + return int64(x) % int64(y) + } + return int(x) % int(y) + } + } + return Modulo(a, b) +} diff --git a/vm/vm.go b/vm/vm.go index c06a7063e..78c74843b 100644 --- a/vm/vm.go +++ b/vm/vm.go @@ -7,6 +7,7 @@ import ( "reflect" "regexp" "sort" + "strconv" "strings" "time" @@ -301,7 +302,13 @@ func (vm *VM) Run(program *Program, env any) (_ any, err error) { case OpModulo: b := vm.pop() a := vm.pop() - vm.push(runtime.Modulo(a, b)) + // Modulo's exported result stays int. On 32-bit, int64 operands + // need a wider result, so that path is separate. + if strconv.IntSize == 32 { + vm.push(runtime.ModuloWide(a, b)) + } else { + vm.push(runtime.Modulo(a, b)) + } case OpExponent: b := vm.pop()