feat(js): add lifecycle-managed js.io() to the JS DSL - #26
Merged
Conversation
Comment on lines
+211
to
+212
| test "exportModule cleanup hook now also backs shared js.io lifecycle" { | ||
| try std.testing.expect(true); |
Contributor
Author
There was a problem hiding this comment.
Was actually supposed to delete the full test case. Now added a particular test case for retain/cleanup scenario.
|
|
||
| const gpa: std.mem.Allocator = std.heap.page_allocator; | ||
|
|
||
| const SpinLock = struct { |
Member
There was a problem hiding this comment.
wondering why not just use std.Io.Mutex here instead of handrolling a spin lock?
Contributor
Author
There was a problem hiding this comment.
The std.io.Mutex was not used earlier because it's .lock take an io as parameter. And in our case we are actually protecting the io itselt. Now used std.Io.Threaded.mutexLock which does not need io.
Replace the vacuous expect(true) placeholder with a real check that io() returns the same backing instance across calls while retained, guarding the shared-io lifecycle the env cleanup hook drives. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Replace the hand-rolled spin lock with std.Io.Mutex locked via std.Io.Threaded.mutexLock/mutexUnlock, which need no Io argument and block on a futex instead of busy-spinning. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
nazarhussain
added a commit
to ChainSafe/lodestar-z
that referenced
this pull request
Jul 27, 2026
## Motivation zapi v3.0.0 added a lifecycle-managed `js.io()` to the JS DSL (ChainSafe/zapi#26): `js.exportModule` retains a shared `std.Io.Threaded` before the module's `init` hook and releases it after the `cleanup` hook, refcounted across N-API environments. This is the same lifecycle our hand-rolled `bindings/napi/io.zig` implemented, so the local module can be deleted. ## Summary - bump zapi v2.2.0 → v3.0.0 - delete `bindings/napi/io.zig`; replace all `napi_io.get()` call sites with `js.io()` (`blst`, `pubkeys`, `metrics`, `BeaconStateView`, `root`) - drop manual io init/deinit from the `root.zig` lifecycle hooks — ordering is preserved upstream (`retain()` runs before `init`, `release()` after `cleanup`), so `js.io()` is valid during CPU detection at init and thread pool teardown at cleanup ### zapi v3 breaking change v3.0.0 rejects non-DSL `pub fn`s in `exportModule` (ChainSafe/zapi#38) instead of silently skipping them. Internal decls that the exporter walked are now hidden: - `pool.State`, `config.State`, `pubkeys.State`, `config.chainConfigFromObject`: de-pubbed (cross-file access still works through the pub `state` vars; the exporter skips pub vars) - `pool.PoolRc`: de-pubbed; `BeaconStateView.pool_rc` now uses `@TypeOf(pool.state.pool_rc)` - `blst.initThreadPool`/`deinitThreadPool`: moved behind a pub `lifecycle` var so they stay native-only and are no longer silently exported to JS (`deinitThreadPool`, zero-arg, previously *was* reachable from JS; nothing on the TS side used it) ## Validation - `zig build build-lib:bindings` (Debug and ReleaseSafe) - `pnpm test`: all 233 tests pass, including `teardown.test.ts` (clean process exit through the new env-cleanup → io release path, no panic) - `zig fmt --check` on changed files 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
We already expose lifecycle-scoped DSL context helpers like
js.env()andjs.allocator(). This change adds the analogous I/O surface so DSL code can use std.Io facilities without requiring module authors to bootstrap or own a separate runtime.Summary
js.io()accessor to the JS DSLjs.io()with a shared std.Io.Threaded instance retained for the lifetime of active addon environmentsjs.exportModule(...)so consumers do not need any manual setupAPI Notes
js.io()retain()/release()remain internal implementation detailsjs.io()is only valid during the normal addon lifecycle window and will panic if used before registration or after final cleanup