Skip to content

Commit e0bbe7c

Browse files
committed
Update Go version to 1.25 and refactor integer handling in number.go
1 parent 2564df6 commit e0bbe7c

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/Srlion/glua
22

3-
go 1.24
3+
go 1.25
44

55
require golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c
66

number.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,55 @@ import (
1010
"golang.org/x/exp/constraints"
1111
)
1212

13-
const LuaNumberMaxSafeInteger int64 = (1 << 53) - 1
13+
const LuaNumberMaxSafeInteger = (1 << 53) - 1
1414

1515
func pushNumber(L State, n LUA_NUMBER) {
1616
C.lua_pushnumber_wrap(L.c(), C.double(n))
1717
}
1818

19-
func pushInt[V constraints.Integer](L State, n V) {
20-
// max(-n, n) is a quick way to get the absolute value of n, since golang doesn't have a built-in abs function for integers :D
21-
if max(-n, n) <= V(LuaNumberMaxSafeInteger) {
22-
pushNumber(L, LUA_NUMBER(n))
23-
} else {
24-
if n < 0 {
25-
L.PushString(strconv.FormatInt(int64(n), 10))
26-
} else {
27-
L.PushString(strconv.FormatUint(uint64(n), 10))
28-
}
19+
func pushIntSigned[T constraints.Signed](L State, n T) {
20+
const max = int64(LuaNumberMaxSafeInteger)
21+
v := int64(n)
22+
if v >= -max && v <= max {
23+
pushNumber(L, LUA_NUMBER(v))
24+
return
25+
}
26+
L.PushString(strconv.FormatInt(v, 10))
27+
}
28+
29+
func pushIntUnsigned[T constraints.Unsigned](L State, n T) {
30+
const max = uint64(LuaNumberMaxSafeInteger)
31+
v := uint64(n)
32+
if v <= max {
33+
pushNumber(L, LUA_NUMBER(v))
34+
return
2935
}
36+
L.PushString(strconv.FormatUint(v, 10))
3037
}
3138

3239
func (L State) PushNumber(n any) {
3340
switch v := n.(type) {
3441
case int:
35-
pushInt(L, v)
42+
pushIntSigned(L, v)
3643
case int8:
3744
pushNumber(L, LUA_NUMBER(v))
3845
case int16:
3946
pushNumber(L, LUA_NUMBER(v))
4047
case int32:
4148
pushNumber(L, LUA_NUMBER(v))
4249
case int64:
43-
pushInt(L, v)
50+
pushIntSigned(L, v)
4451

4552
case uint:
46-
pushInt(L, v)
53+
pushIntUnsigned(L, v)
4754
case uint8:
4855
pushNumber(L, LUA_NUMBER(v))
4956
case uint16:
5057
pushNumber(L, LUA_NUMBER(v))
5158
case uint32:
5259
pushNumber(L, LUA_NUMBER(v))
5360
case uint64:
54-
pushInt(L, v)
61+
pushIntUnsigned(L, v)
5562

5663
case float32:
5764
pushNumber(L, LUA_NUMBER(v))

0 commit comments

Comments
 (0)