fix(napi): add test coverage and fix escape_handle/throw bugs#32960
Merged
bartlomieju merged 3 commits intomainfrom Mar 24, 2026
Merged
fix(napi): add test coverage and fix escape_handle/throw bugs#32960bartlomieju merged 3 commits intomainfrom
bartlomieju merged 3 commits intomainfrom
Conversation
… exceptions Add NAPI test coverage for three fundamental areas that were previously untested: Handle scopes (handle_scope.rs + handle_scope_test.js): - napi_open/close_handle_scope - napi_open/close_escapable_handle_scope + napi_escape_handle - Nested handle scopes - Double escape (ignored: Deno panics instead of returning error) References (reference.rs + reference_test.js): - napi_create_reference with strong refcount - napi_get_reference_value - napi_delete_reference - napi_reference_ref / napi_reference_unref counting - napi_create_external / napi_get_value_external roundtrip - External values accessed through references Exceptions (exception.rs + exception_test.js): - napi_is_exception_pending - napi_get_and_clear_last_exception - Exception propagation through napi_call_function - Throw + clear cycle (ignored: napi_throw with raw string value) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Two fixes for NAPI bugs found by the new test coverage: 1. napi_escape_handle: Track whether escape has been called via a heap-allocated bool in napi_open_escapable_handle_scope. Return napi_escape_called_twice on second call instead of silently succeeding. Previously the escapable handle scope was entirely a no-op with no state tracking. 2. napi_throw: Only store the exception in env.last_exception, do not also call scope.throw_exception(). The NAPI callback wrapper (call_fn) already checks last_exception after each callback and throws via V8 then. Throwing in both places made it impossible to clear the exception with napi_get_and_clear_last_exception since V8 would still have a pending exception after clearing last_exception. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This was referenced Mar 24, 2026
Co-Authored-By: Claude Opus 4.6 (1M context) <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.
Summary
New tests
Handle scopes (
handle_scope.rs+handle_scope_test.js):napi_open/close_handle_scopenapi_open/close_escapable_handle_scope+napi_escape_handleReferences (
reference.rs+reference_test.js):napi_create_referencewith strong refcountnapi_get_reference_value/napi_delete_referencenapi_reference_ref/napi_reference_unrefcountingnapi_create_external/napi_get_value_externalroundtripExceptions (
exception.rs+exception_test.js):napi_is_exception_pendingnapi_get_and_clear_last_exceptionnapi_call_functionBug fixes
napi_escape_handledouble-call panic: The escapable handle scope wasa complete no-op with no state tracking. Calling
napi_escape_handletwice would silently succeed instead of returning
napi_escape_called_twice. Fixed by tracking escape state via aheap-allocated bool created in
napi_open_escapable_handle_scope.napi_throw+napi_get_and_clear_last_exceptioncycle broken:napi_throwwas both storing the exception inenv.last_exceptionANDcalling
scope.throw_exception(). This made it impossible to clear theexception with
napi_get_and_clear_last_exceptionsince V8 still had apending exception. Fixed by only storing in
env.last_exception; thecallback wrapper (
call_fn) already handles the V8 throw after thecallback returns.
Test plan
./x test-napipasses: 72 passed, 0 failed🤖 Generated with Claude Code