Repository navigation
Commit d86588d
refactor!: rename Number.toU64Exact to toSafeInteger (#81)
Follow-up to #80 / v4.1.0.
`toU64Exact` never returns a value above 2^53−1 — everything from 2^53
to 2^64−1 throws. The doc comment said so, but the name promised
otherwise, and it promised it *specifically* because `toU32Exact` sits
right above it and **is** exact across its entire nominal range. Two
parallel names, two different contracts.
### The 2^53 cap is correct and is kept
It isn't a shortcut — it's the only way to write this against an `f64`:
```
u53max as f64 = 9007199254740991 exact round-trip = true
u64max as f64 = 18446744073709552000 == 2^64, rounds_up = true
```
`@floatFromInt(std.math.maxInt(u64))` rounds **up** to exactly 2^64, so
a naive full-range `value > max` check would admit 2^64 itself and hand
it to `@intFromFloat`, which cannot represent it. So the behaviour
stays; only the name changes.
### What changed
- New private `toUnsignedExact(comptime T, comptime max_int)`, shared by
both public conversions — the ten-line body is no longer duplicated.
Each caller states its own bound, and a `comptime` assert makes a bound
above `maxInt(u53)` a compile error rather than one `@intFromFloat`
cannot represent. *(Updated from an earlier `@min`-derived bound after
review — see the thread on `src/js/number.zig`.)*
- `toU32Exact` — behaviour unchanged, now a one-liner over the helper.
- `toU64Exact` → `toSafeInteger() !u64`, with a doc comment that says
plainly that no exact `u64` conversion exists for a JS number and points
at `BigInt.toU64`.
- Adds DSL coverage for `toU32Exact`, which has had none since it landed
in #71, plus a case pinning `2**32` as the exact point where the two
conversions diverge.
### Breaking change
Renamed with no deprecation alias, and committed as `refactor!:` with a
`BREAKING CHANGE:` footer so release-please cuts a major. Keeping a
misleadingly-named alias for a whole major cycle would defeat the point
of the change.
**Migration is a rename at the call site** — behaviour is identical, the
same inputs are accepted and the same `error.InvalidUnsignedInteger` is
returned. lodestar-z is the only consumer, with 30 call sites:
| File | Sites |
|---|---|
| `bindings/napi/BeaconStateView.zig` | 29 |
| `bindings/napi/BeaconConfig.zig` | 1 |
`chainConfigU64` in `BeaconConfig.zig` already wraps the conversion to
map `+Infinity` to `maxInt(u64)` for disabled fork epochs; that wrapper
is unaffected.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-authored-by: bing <spiralladder@fastmail.com>1 parent ff6e70e commit d86588d
3 files changed
Lines changed: 59 additions & 29 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
66 | 66 | | |
67 | 67 | | |
68 | | - | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
69 | 81 | | |
70 | | - | |
| 82 | + | |
71 | 83 | | |
72 | 84 | | |
73 | 85 | | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
74 | 92 | | |
75 | 93 | | |
76 | 94 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
82 | | - | |
83 | | - | |
84 | | - | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
85 | 90 | | |
86 | 91 | | |
87 | 92 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
53 | 57 | | |
54 | 58 | | |
55 | 59 | | |
| |||
61 | 65 | | |
62 | 66 | | |
63 | 67 | | |
64 | | - | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
65 | 80 | | |
66 | 81 | | |
67 | 82 | | |
68 | 83 | | |
69 | 84 | | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
83 | 90 | | |
84 | 91 | | |
85 | 92 | | |
| |||
0 commit comments