Skip to content

refactor(napi): clarify naming and comments in string conversions - #3706

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

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

Conversation

@overlookmotel

@overlookmotel overlookmotel commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Pure refactor, no behaviour change.

Adopt consistent var naming and code layout in code which produces strings.

  • Rename written_char_count to written_byte_count (UTF-8, Latin-1) or written_code_unit_count (UTF-16), which is what N-API reports.
  • Name the buffers bytes (UTF-8, Latin-1) or code_units (UTF-16) instead of ret / buf / result.
  • Add comments explaining the + 1 for the NUL terminator.
  • Add the missing SAFETY comment on from_utf8_unchecked in String::from_napi_value.
  • Give every buffer an explicit element type (same type as was previously inferred).
  • In owned_string_without_coercion, use len consistently and pass it as the buffer size instead of bytes.len() (the same value).

Also, remove a no-op pointer cast in JsString::into_utf16.

This just tidies things up in preparation for later PRs, so the substantive changes in those PRs have a clear diff.

Stack

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

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 →

No actionable comments were generated in the recent review.

Walkthrough

String conversion code now uses explicit buffer element types and encoding-specific written counts. The conversions truncate buffers to the reported count where applicable. UTF-16 conversion retains its vector as backing storage when constructing the returned slice.

Changes: String conversion buffers

Layer / File(s) Summary
JavaScript string conversion buffers
crates/napi/src/js_values/string/mod.rs
UTF-8 and Latin-1 conversions use explicitly typed buffers and reported byte counts. UTF-16 conversion uses its allocated vector as backing storage for the returned slice.
N-API string copies
crates/napi/src/bindgen_runtime/js_values/string.rs, crates/napi/src/error.rs
Bindgen runtime conversions truncate buffers to their reported written counts for UTF-8, UTF-16, and Latin-1. The error string-copy block explicitly types its UTF-8 buffer as bytes and truncates it to the written byte count.

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

Change: Refactor

Possibly related PRs

  • napi-rs/napi-rs#3708: Changes UTF-16 buffer allocation in JsString::into_utf16, a conversion also updated here.
  • napi-rs/napi-rs#3709: Changes the UTF-8 construction path in owned_string_without_coercion, which this PR also edits.
  • napi-rs/napi-rs#3707: Changes the same JsString conversion functions and overlaps with this PR’s count naming and buffer comments.

Suggested reviewers: brooooooklyn

Merge Risk: Minimal · up to 1d0aa

This refactor renames variables, adds comments and makes buffer types explicit in the string conversions. Behavior is unchanged, and no merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "refactor(napi): clarify naming and comments in string conversions" matches the changeset. The raw summary shows renamed count and buffer-length variables, added truncation to written length…
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.
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title "refactor(napi): clarify naming and comments in string conversions" matches the changeset. The raw summary shows renamed count and buffer-length variables, added truncation to written length, explicit u8/u16 buffer types, and clarified comments in the N-API string conversion code (string.rs, error.rs, js_values/string/mod.rs). The PR objectives describe the same pure refactor, so the title names the main change and is specific enough for a teammate scanning history.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • 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: 3f5c5196-a3bd-4122-95d9-de209c131189
Commits

Reviewing files that changed from the base of the PR and between 72b87f9 and 1d0aaa6.

Files selected for processing: 1

  • crates/napi/src/bindgen_runtime/js_values/string.rs
Files skipped from review as they are similar to previous changes: 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; 7 remain after this review.

@overlookmotel

Copy link
Copy Markdown
Contributor Author

I've done this as a stack, as it's the process I'm used to and I find it helpful for review. I built the stack using Graphite locally, but couldn't submit using Graphite.

PRs should merge cleanly on top of each other in this order:

@Brooooooklyn If this is a pain, please just say so and I'll squash it all to 1 PR.

For review, you can see the whole stack as 6 x commits in #3711.

I've deleted the CodeRabbit comments from the PR descriptions, as they were inaccurate - each one summarized the whole stack up to that commit, rather just the last commit that PR adds.

@Brooooooklyn
Brooooooklyn force-pushed the om/11-16-refactor_napi_clarify_naming_and_comments_in_string_conversions branch 2 times, most recently from 72b87f9 to 1d0aaa6 Compare November 19, 2026 06:47
@Brooooooklyn

Copy link
Copy Markdown
Member

Thanks for the stack. I rebased it onto current main and pushed a polish pass to each branch. Your commits are kept, each amended in place:

Checked on each commit: clippy -D warnings, the native and compat-mode suites, and the WASI lane (wasm32-wasip1-threads). The wasm loader files were regenerated with the CLI. If you'd rather not keep any of this, say so and I'll drop it.

Pure refactor, no behaviour change.

- Rename `written_char_count` (and `written` in
  `owned_string_without_coercion`) to `written_byte_count` (UTF-8,
  Latin-1) or `written_code_unit_count` (UTF-16), which is what N-API
  reports.
- Rename the buffers to `bytes` (UTF-8, Latin-1) or `code_units`
  (UTF-16), instead of a mix of `ret`, `buf` and `result`.
- Explain the `+ 1` for the NUL terminator.
- Give every buffer an explicit element type (the type that was
  previously inferred), and drop a no-op pointer cast in
  `JsString::into_utf16`.
- Add the missing SAFETY comment on `from_utf8_unchecked` in
  `String::from_napi_value`.
- In `owned_string_without_coercion`, rename `length` to `len` and pass
  it as the buffer size instead of `bytes.len()` (the same value).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Brooooooklyn
Brooooooklyn force-pushed the om/11-16-refactor_napi_clarify_naming_and_comments_in_string_conversions branch from 1d0aaa6 to cc0089f Compare November 19, 2026 15:03
@Brooooooklyn
Brooooooklyn merged commit 9d23918 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