Skip to content

Commit b7124e1

Browse files
committed
Fix compile error for functions with int64 parameters
1 parent b45ee4f commit b7124e1

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

compiler/compiler.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,9 @@ func (c *compiler) IntegerNode(node *ast.IntegerNode) {
345345
}
346346
c.emitPush(int32(node.Value))
347347
case reflect.Int64:
348-
panic(fmt.Sprintf("constant %d overflows int64", node.Value))
348+
if node.Value > math.MaxInt64 || node.Value < math.MinInt64 {
349+
panic(fmt.Sprintf("constant %d overflows int64", node.Value))
350+
}
349351
c.emitPush(int64(node.Value))
350352
case reflect.Uint:
351353
if node.Value < 0 {

compiler/compiler_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -628,3 +628,23 @@ func TestCompile_optimizes_jumps(t *testing.T) {
628628
})
629629
}
630630
}
631+
632+
func TestCompile_IntegerArgsFunc(t *testing.T) {
633+
env := mock.Env{}
634+
codes := []string{
635+
"FuncInt(0)",
636+
"FuncInt8(0)",
637+
"FuncInt16(0)",
638+
"FuncInt32(0)",
639+
"FuncInt64(0)",
640+
"FuncUint(0)",
641+
"FuncUint8(0)",
642+
"FuncUint16(0)",
643+
"FuncUint32(0)",
644+
"FuncUint64(0)",
645+
}
646+
for _, code := range codes {
647+
_, err := expr.Compile(code, expr.Env(env))
648+
require.NoError(t, err)
649+
}
650+
}

test/mock/mock.go

+40
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,46 @@ func (p Env) FuncTyped(_ string) int {
6565
return 2023
6666
}
6767

68+
func (p Env) FuncInt(_ int) int {
69+
return 0
70+
}
71+
72+
func (p Env) FuncUint(_ uint) int {
73+
return 0
74+
}
75+
76+
func (p Env) FuncInt8(_ float64) int {
77+
return 0
78+
}
79+
80+
func (p Env) FuncInt16(_ int16) int {
81+
return 0
82+
}
83+
84+
func (p Env) FuncInt32(_ int32) int {
85+
return 0
86+
}
87+
88+
func (p Env) FuncInt64(_ int64) int {
89+
return 0
90+
}
91+
92+
func (p Env) FuncUint8(_ uint8) int {
93+
return 0
94+
}
95+
96+
func (p Env) FuncUint16(_ uint16) int {
97+
return 0
98+
}
99+
100+
func (p Env) FuncUint32(_ uint32) int {
101+
return 0
102+
}
103+
104+
func (p Env) FuncUint64(_ uint64) int {
105+
return 0
106+
}
107+
68108
func (p Env) TimeEqualString(a time.Time, s string) bool {
69109
return a.Format("2006-01-02") == s
70110
}

0 commit comments

Comments
 (0)