N-API scope-bound value lifetimes (Locals + generation-tagged ids) - #41
Merged
Merged
Conversation
This was referenced Jul 24, 2026
Arshia001
force-pushed
the
napi-scope-bound-lifetimes
branch
from
July 28, 2026 14:11
a35176a to
bac2e66
Compare
…ership Replace the flat values map with a slot table whose u32 ids encode ((slot_index + 1) << 12) | generation. Stale or forged ids now fail the generation check in LoadValue and surface as napi_invalid_arg instead of resolving to an unrelated value. Still ref-backed: no lifetime change, and all mint/resolve sites are untouched since the id stays a u32. Also gate LookupEnvState on g_envs membership so a stale or forged SnapiEnvState pointer from the guest is rejected before the first dereference. Groundwork for the scope-bound value-lifetime rework. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Value slots now store raw napi_values owned by a per-env stack of scope frames instead of pinning every value with a persistent reference: - guest napi_open/close_handle_scope become real: they push/pop frames backed by heap napi handle scopes, with LIFO enforced as a safe napi_handle_scope_mismatch error. Closing a frame retires its ids (generation bump) before the backing Locals die with the napi scope. - generic_wasm_callback pushes an implicit escapable frame per callback, so argument/intermediate/return ids die when the callback returns; the return value is escaped into the enclosing scope for V8's trampoline. WasmInterruptCallback gets the same treatment with a plain frame. - escape_handle mints the escaped value into the parent frame of the escapable scope; get_reference_value keeps minting into the current frame. Cross-scope retention remains the napi_ref table. - Teardown drains frames instead of deleting per-value references. - The Rust layer prunes stale guest_data_ptrs mappings (amortized, only ids dead in every registered env) and gains a live-value-count debug getter for leak assertions. The count-cap remains as a backstop bound on live slots. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Arshia001
force-pushed
the
napi-scope-bound-lifetimes
branch
from
July 28, 2026 14:52
bac2e66 to
ef4b722
Compare
syrusakbary
approved these changes
Jul 30, 2026
syrusakbary
approved these changes
Aug 5, 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.
Stack 2/4 (napi). Base:
cross-vm-resource-limits. → next:napi-v8-imports-bridge-fixes.Reworks N-API value lifetimes from persist-everything to scope-bound Locals: