feat(js): export module/namespace consts and enums - #73
Open
nazarhussain wants to merge 3 commits into
Open
Conversation
Scalar/string `pub const` decls were silently skipped by exportModule's reflection; only class statics exported. Export them as enumerable properties at the module root and inside namespaces, and export `pub const` enums as frozen plain objects with verbatim tag names, mirroring napi-rs. Needed by lodestar-z's swap-or-not-shuffle binding to drop its JS-side constant assignments. Also document that TypedArray.fromExternal copies and panics outside DSL callbacks (use owned typed arrays for ownership transfer).
spiral-ladder
previously approved these changes
Aug 17, 2026
- `pub var` decls are never exported: the value would be a stale registration-time snapshot, and inside a DSL class the comptime `@field` read in analyzeClass broke compilation outright (pre-existing on main) - class-level enums export as frozen objects, matching namespaces - integer consts and enum tags outside i64 range now fail with a compile error naming the decl instead of a cryptic `@intCast` failure - pin skipped shapes (arrays, struct values) with tests
Co-authored-by: bing <spiralladder@fastmail.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
While porting
@chainsafe/swap-or-not-shuffleinto lodestar-z (ChainSafe/lodestar-z#559), scalarpub constdecls and enums inside exported modules were silently skipped byjs.exportModule's reflection — the binding had to assign constants likeSHUFFLE_ROUNDS_MAINNETandByteCounton the JS side. Classes already auto-export such consts as statics (applyStaticFields); this extends the same behavior to namespaces and the module root.Changes
pub constdecls (int, float, bool,[]const u8, string literals) export as enumerable value properties at the module root and inside namespaces, reusingwrap_class.isStaticValueType/createStaticFieldValue(nowpub). A namespace containing only consts now exports too.pub const X = enum {...}exports as a frozen plain object mapping each tag name (verbatim, no case conversion) to its integer value, mirroring napi-rs#[napi] pub enum. Signed tag values preserved.TypedArray.fromExternaldoc now states it copies (pointing atOwnedTypedArray.fromOwnedSlice/intoValuefor ownership transfer) and warns it panics outside DSL callbacks. New README "Constants and Enums" section.Decisions
setNamedProperty), matching namespace function properties. Verified class statics are also enumerable, so the two paths are consistent..registerhook stays the escape hatch.Testing
10 new vitest cases in
examples/js_dsl(root/namespace consts, const-only namespace, enumerability, verbatim enum tags, signed values, frozen-object semantics), written first and watched fail. Full suite:zig build test:zapi+ 130 vitest tests green.Consumer cleanup
Once released, lodestar-z deletes its
bindings.jsconstant assignments and declares the consts/enum inbindings/napi/shuffle.zig(guarded by its "should expose the reference constants" test).🤖 Generated with Claude Code