feat: add owned typed arrays - #68
Conversation
cf8a1fd to
f1b7b72
Compare
spiral-ladder
left a comment
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
left a comment
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.
Yes, if it is tested in lodestar-z, we can do follow up PR to deprecate the original API. |
Yes, made the |
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: