fix(ext/napi): prevent spurious TypeError from napi_coerce_to_object on null/undefined#32695
Merged
bartlomieju merged 2 commits intodenoland:mainfrom Mar 13, 2026
Conversation
…on null/undefined
When `napi_coerce_to_object` was called with null or undefined, V8's
`ToObject()` would throw a TypeError ("Cannot convert undefined or null
to object"). The `napi_wrap!` macro's try-catch caught this and stored
it in `env.last_exception`. When the NAPI callback returned through
`call_fn`, this pending exception was re-thrown into V8, producing a
spurious unhandled rejection.
This caused real-world issues with NAPI modules like rolldown, where
plugin errors were masked by the spurious TypeError instead of showing
the original error message.
The fix checks for null/undefined before calling `to_object()`,
returning `napi_invalid_arg` without generating a V8 exception.
Closes denoland#31306
Co-Authored-By: Claude Opus 4.6 <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.
Fixes #31306
Fixes #28682
napi_coerce_to_object(null/undefined)was letting V8'sToObject()throw a TypeError, which got stored inenv.last_exceptionby thenapi_wrap!try-catch, then re-thrown bycall_fnas a spurious unhandled rejectionto_object(), returningnapi_invalid_argwithout generating a V8 exceptionnapi_coerce_to_object(null/undefined)returns an error status without leaving a pending exception