Skip to content

feat(js): add exact u32 conversion - #71

Merged
spiral-ladder merged 1 commit into
mainfrom
bing/to-u32-exact
Aug 4, 2026
Merged

spiral-ladder merged 1 commit into
mainfrom
bing/to-u32-exact

Conversation

@spiral-ladder

Copy link
Copy Markdown
Member

In a few places in the new pubkey cache in lodestar-z/bls, we use toU32() but that does not guarantee that the converted number is indeed a valid u32. We add this as an alternative to toU32() for additional safety

@spiral-ladder
spiral-ladder merged commit d6b21e1 into main Aug 4, 2026
5 checks passed
@spiral-ladder
spiral-ladder deleted the bing/to-u32-exact branch August 4, 2026 14:49
nazarhussain pushed a commit that referenced this pull request Aug 18, 2026
🤖 I have created a release *beep* *boop*
---


##
[4.0.0](zapi-v3.1.0...zapi-v4.0.0)
(2026-08-18)


### ⚠ BREAKING CHANGES

* isolate DSL class tags across addons
([#67](#67))
* manage external buffer lifetime
([#66](#66))
* require writable external buffer storage
([#58](#58))

### Features

* add owned typed arrays
([#68](#68))
([b92c2de](b92c2de))
* **js:** add exact u32 conversion
([#71](#71))
([d6b21e1](d6b21e1))
* **js:** add typed array toArray
([#70](#70))
([ab42ab0](ab42ab0))
* **js:** export module/namespace consts and enums
([#73](#73))
([76dc0db](76dc0db))


### Bug Fixes

* **dsl:** support class pointer arguments
([#50](#50))
([9dd2111](9dd2111))
* harden N-API boundary against JS-triggerable memory bugs
([#60](#60))
([fff76f3](fff76f3))
* isolate DSL class tags across addons
([#67](#67))
([3ab8c11](3ab8c11))
* manage external buffer lifetime
([#66](#66))
([d134a6a](d134a6a))
* **napi:** receive raw pointer out parameters
([#57](#57))
([fde4a9a](fde4a9a))
* remove redundant platform check from musl detection
([#64](#64))
([0d4829c](0d4829c))
* require writable external buffer storage
([#58](#58))
([8fd898f](8fd898f))
* restore registerDecls on Zig 0.16
([#59](#59))
([7f3af3e](7f3af3e))


### Code Refactoring

* **js:** unify env lifecycle refcounting
([#53](#53))
([d3d5056](d3d5056))


### Miscellaneous Chores

* define changelog sections for release-please
([#54](#54))
([1c5e7b4](1c5e7b4))
* update dev deps ([#74](#74))
([5121590](5121590))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
wemeetagain pushed a commit that referenced this pull request Sep 30, 2026
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants