feat: add owned typed arrays - #68
Open
GrapeBaBa wants to merge 9 commits into
Open
Conversation
GrapeBaBa
force-pushed
the
fix/external-typed-array-lifecycle
branch
from
August 10, 2026 06:35
cf8a1fd to
f1b7b72
Compare
GrapeBaBa
marked this pull request as ready for review
August 10, 2026 10:01
GrapeBaBa
requested review from
nazarhussain,
spiral-ladder and
wemeetagain
and removed request for
spiral-ladder
August 10, 2026 10:03
spiral-ladder
left a comment
Member
There was a problem hiding this comment.
with this API, isn't this strictly better than fromExternal? Is there any reason to keep the original API?
nazarhussain
requested changes
Aug 11, 2026
nazarhussain
left a comment
Contributor
There was a problem hiding this comment.
Over all looks good. One API asymmetry.
We have OwnedBuffer.intoValue(self: OwnedBuffer, env) vs OwnedTypedArray.intoValue(self: *Self, env) .
The later pattern introduced in this PR seems better approach. Suggest to update the the existing OwnedBuffer to follow the same.
nazarhussain
previously approved these changes
Aug 13, 2026
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.
Summary
js.OwnedTypedArray(Element, array_type)and concreteOwned*ArrayaliasesOwnedBuffer.intoValuewith the same pointer-based transactional ownership model while retaining its existing unsupported-runtime copy fallbackElementagainst its N-API TypedArray kind at compile timeTypedArray.fromExternalunchanged frommainNoExternalBuffersAllowedunchanged for owned typed arrays; no copy fallbackAPI
fromOwnedSlicetakes an existing allocator-owned mutable slice without copying.fromSlicecreates an owned copy when that is what the caller needs.intoValuetakes*OwnedTypedArray. Once ownership transfers, it empties the source, so a normal deferreddeinitis safe. The allocator must remain valid until the ArrayBuffer finalizer runs.OwnedBuffer.intoValuenow follows the same pointer-based transfer contract. It retains its existingNoExternalBuffersAllowedcopy fallback and consumes the source only after that copy succeeds.Lodestar-Z use case
In the Lodestar-Z BLS bindings,
compressandserializecurrently produce fixed-size stack arrays and pass them toTypedArray.fromExternal, which copies them into allocator-owned native storage before creating the external ArrayBuffer. With this API, the bindings can allocate the output slice on the native heap, letblstwrite into it directly, and return it withOwnedUint8Array.fromOwnedSlice. This removes one stack-to-native-heap copy per result while preserving the external ArrayBuffer GC lifecycle.Ownership boundary
NoExternalBuffersAllowed,PendingException, orCannotRunJS: Node returns before finalizer registration, so restore ownership to the caller and propagate the original errordeiniton the local owner, which frees restored/untransferred data and is a no-op after transferThe heap finalizer context is the owning value itself, matching the boxed-owner model rather than duplicating ownership fields in a second context type.
Node implementation reference for the ambiguous failure boundary:
Verification
zig build test:zapi— 37/37zig buildpnpm test:js— 121/121pnpm lint:jszig fmt --check src/OwnedBuffer.zig src/to_from_value.zig src/js/typed_arrays.zig src/js/wrap_function.ziggit diff --checkSpec tests were intentionally not run.
The repository-wide
zig fmt --check srcstill reports the pre-existing formatting issue insrc/create_callback.zig; this PR does not modify that file.Coverage limitation
The JS tests exercise non-empty and zero-length
OwnedUint8Arrayvalues and a non-emptyOwnedBufferthrough the real Node addon boundary. They do not deterministically force GC to count typed-array finalizer invocations or injectNoExternalBuffersAllowed, pending/cannot-run, generic external-creation, typed-array-view creation, or fallback-copy failures. No production test seam or compatibility branch was added for those cases.