Skip to content

refactor(napi): drop mem::forget round-trip in JsString::into_* - #3707

Merged
Brooooooklyn merged 1 commit into
napi-rs:mainfrom
overlookmotel:om/11-16-refactor_napi_drop_mem_forget_round-trip_in_jsstring_into__
Nov 19, 2026
Merged

Brooooooklyn merged 1 commit into
napi-rs:mainfrom
overlookmotel:om/11-16-refactor_napi_drop_mem_forget_round-trip_in_jsstring_into__

Conversation

@overlookmotel

@overlookmotel overlookmotel commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Pure refactor - no behaviour change.

into_utf8 and into_latin1 performed something of a dance - allocate a Vec<c_char>, then mem::forget it, and create a new Vec with Vec::from_raw_parts to turn it into a Vec<u8>.

Simplify this by allocating a Vec<u8> up front, casting the pointer at the N-API call, so truncate does the same job without unsafe and the Vec can be moved into the result directly.

Also drop the buf_ptr locals (including in into_utf16) and take pointers from the Vec where needed.

Based on top of #3706.

Summary by CodeRabbit

  • Refactor
    • Improved handling of JavaScript string conversions in UTF-8, UTF-16, and Latin-1, preserving the reported string length and avoiding unnecessary memory handling changes.
    • Updated string retrieval after N-API errors to retain its existing behavior, including clearing pending exceptions.

Copilot AI balanced review requested due to automatic review settings November 16, 2026 14:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Explain Change →

Caution

Review failed

The pull request is closed.

No actionable comments were generated in the recent review.

Walkthrough

String conversions now use unit-specific written-length variables and truncate buffers to the reported lengths. owned_string_without_coercion allocates space for a NUL terminator. JsString conversions retain their vectors directly instead of reconstructing them from raw parts.

Changes: String conversions

Layer / File(s) Summary
Conversion buffer sizing
crates/napi/src/bindgen_runtime/js_values/string.rs, crates/napi/src/error.rs
UTF-8, UTF-16, and Latin-1 conversions truncate buffers to reported lengths. owned_string_without_coercion explicitly allocates space for the NUL terminator.
JsString vector ownership
crates/napi/src/js_values/string/mod.rs
UTF-8 and Latin-1 vectors are truncated to the written byte count and retained directly. UTF-16 retains its vector as backing storage. The methods no longer reconstruct vectors from raw parts.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Refactor

Possibly related PRs

Suggested reviewers: brooooooklyn

Merge Risk: Minimal · previous review f3138

This is a behavior-preserving refactor of string conversion buffers that removes unsafe vector reconstruction. No concrete regression was identified, so merge risk is minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: removing a mem::forget round-trip in JsString::into_*. The raw summary confirms that into_utf8, into_utf16, and into_latin1 now use safe Vec ownership and …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: removing a mem::forget round-trip in JsString::into_*. The raw summary confirms that into_utf8, into_utf16, and into_latin1 now use safe Vec ownership and no longer call mem::forget or rebuild vectors from raw parts. The title is specific and matches the changeset and the PR objectives. The scope is a refactor and the change is behavior-preserving, which the title conveys.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 0f8ef3a9-7cd7-4600-9a84-06ce153c9d8c
Commits

Reviewing files that changed from the base of the PR and between ba233dc and f3138cd.

Files selected for processing: 1

  • crates/napi/src/bindgen_runtime/js_values/string.rs

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.

@Brooooooklyn
Brooooooklyn force-pushed the om/11-16-refactor_napi_drop_mem_forget_round-trip_in_jsstring_into__ branch 2 times, most recently from ba233dc to f3138cd Compare November 19, 2026 06:47
@Brooooooklyn Brooooooklyn changed the title refactor(napi): remove mem::forget round trip in JsString::into refactor(napi): drop mem::forget round-trip in JsString::into_* Nov 19, 2026
`into_utf8` and `into_latin1` allocated a `Vec<c_char>`, then used `mem::forget` + `Vec::from_raw_parts` to turn it into a `Vec<u8>` shortened to what N-API wrote. Allocate a `Vec<u8>` up front instead, casting the pointer at the N-API call, so `truncate` does the same job without `unsafe` and the `Vec` can be moved into the result directly. Also drop the `buf_ptr` locals (including in `into_utf16`) and take pointers from the `Vec` where needed.

No behaviour change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Brooooooklyn
Brooooooklyn force-pushed the om/11-16-refactor_napi_drop_mem_forget_round-trip_in_jsstring_into__ branch from f3138cd to 3a5d36e Compare November 19, 2026 15:04
@Brooooooklyn
Brooooooklyn merged commit ba10d14 into napi-rs:main Nov 19, 2026
This was referenced Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants