feat(tinygo): make 53-bit limits portable across platforms

This commit is contained in:
Steve Moyer
2025-02-21 10:55:24 -05:00
parent 879c0ab03b
commit 9589cc8b44
3 changed files with 4 additions and 4 deletions

View File

@@ -9,9 +9,9 @@ import (
const ( const (
// MaxInt53 represents the maximum safe integer in JavaScript (2^53 - 1) // MaxInt53 represents the maximum safe integer in JavaScript (2^53 - 1)
MaxInt53 = 9007199254740991 MaxInt53 int64 = 9007199254740991
// MinInt53 represents the minimum safe integer in JavaScript (-2^53 + 1) // MinInt53 represents the minimum safe integer in JavaScript (-2^53 + 1)
MinInt53 = -9007199254740991 MinInt53 int64 = -9007199254740991
) )
func ValidateIntegerBoundsIPLD(node ipld.Node) error { func ValidateIntegerBoundsIPLD(node ipld.Node) error {

View File

@@ -185,7 +185,7 @@ func anyAssemble(val any) qp.Assemble {
return qp.Int(i) return qp.Int(i)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
u := rv.Uint() u := rv.Uint()
if u > limits.MaxInt53 { if u > uint64(limits.MaxInt53) {
panic(fmt.Sprintf("unsigned integer %d exceeds safe bounds", u)) panic(fmt.Sprintf("unsigned integer %d exceeds safe bounds", u))
} }
return qp.Int(int64(u)) return qp.Int(int64(u))

View File

@@ -68,7 +68,7 @@ func Parse(str string) (Selector, error) {
if err != nil { if err != nil {
return nil, newParseError("invalid index", str, col, tok) return nil, newParseError("invalid index", str, col, tok)
} }
if idx > limits.MaxInt53 || idx < limits.MinInt53 { if int64(idx) > limits.MaxInt53 || int64(idx) < limits.MinInt53 {
return nil, newParseError(fmt.Sprintf("index %d exceeds safe integer bounds", idx), str, col, tok) return nil, newParseError(fmt.Sprintf("index %d exceeds safe integer bounds", idx), str, col, tok)
} }
sel = append(sel, segment{str: tok, optional: opt, index: idx}) sel = append(sel, segment{str: tok, optional: opt, index: idx})