feat: type-tag-aware unwrap and removeWrap#7
Merged
wemeetagain merged 3 commits intomainfrom Mar 23, 2026
Merged
Conversation
wemeetagain
reviewed
Mar 19, 2026
src/Env.zig
Outdated
| /// https://nodejs.org/api/n-api.html#napi_unwrap | ||
| pub fn unwrap(self: Env, comptime Data: type, object: Value) NapiError!*Data { | ||
| fn dataTypeTagFor(comptime Data: type) ?c.napi_type_tag { | ||
| if (!@hasDecl(Data, "type_tag")) return null; |
Member
There was a problem hiding this comment.
this is quite frameworky (as in -- not part of napi, something you're adding on top) and needs to be documented somewhere
Contributor
Author
There was a problem hiding this comment.
I removed the implicit feature and added an explicit API with an example.
3916a3d to
e1bd71b
Compare
lodekeeper-z
approved these changes
Mar 23, 2026
lodekeeper-z
left a comment
There was a problem hiding this comment.
Clean implementation. Type-tagged unwrap is exactly what we need for production NAPI bindings — prevents silent memory reinterpretation when JS passes wrong this. Good example coverage with the Cat/Dog test.
wemeetagain
approved these changes
Mar 23, 2026
Member
wemeetagain
left a comment
There was a problem hiding this comment.
This is a really nice addition :)
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
unwrapChecked/removeWrapCheckedtoEnv— thin wrappers that verify an object's type tag vianapi_check_object_type_tagbefore unwrapping, returningerror.InvalidArgon mismatchunwrap/removeWrapremain unchanged (plain N-API wrappers)examples/type_tag/with a Cat/Dog example demonstrating how type tags prevent silently reinterpreting memory when the wrongthisis passed to a native methodMotivation
When multiple classes use
napi_wrap, passing the wrong JS object to a method silently casts the native pointer to the wrong type. Type tags (napi_type_tag_object+napi_check_object_type_tag) are the N-API solution for this, but the check-then-unwrap pattern is boilerplateunwrapCheckedmakes it a single explicit call.Test plan
zig buildcompiles all examplesvitest run examples/type_tag/mod.test.ts— 3 tests pass (Cat, Dog, cross-type mismatch throws)