Skip to content

node-api: use WriteV2 in napi_get_value_string_utf16 #58165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tniessen
Copy link
Member

@tniessen tniessen commented May 4, 2025

Since String::Write() is deprecated, use String::Write2() instead. That requires us to compute the correct number of characters ahead of time but removes the need for dealing with the return value.

Since `String::Write()` is deprecated, use `String::Write2()` instead.
That requires us to compute the correct number of characters ahead of
time but removes the need for dealing with the return value.
@tniessen tniessen added c++ Issues and PRs that require attention from people who are familiar with C++. v8 engine Issues and PRs related to the V8 dependency. node-api Issues and PRs related to the Node-API. labels May 4, 2025
@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/node-api

@nodejs-github-bot nodejs-github-bot added the needs-ci PRs that need a full CI run. label May 4, 2025
Copy link

codecov bot commented May 4, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.16%. Comparing base (6de55f7) to head (86f6bee).
Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #58165      +/-   ##
==========================================
- Coverage   90.17%   90.16%   -0.02%     
==========================================
  Files         630      630              
  Lines      186503   186504       +1     
  Branches    36614    36615       +1     
==========================================
- Hits       168187   168163      -24     
- Misses      11124    11128       +4     
- Partials     7192     7213      +21     
Files with missing lines Coverage Δ
src/js_native_api_v8.cc 76.54% <100.00%> (+0.01%) ⬆️

... and 24 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@vmoroz
Copy link
Member

vmoroz commented May 4, 2025

Could you also replace the other deprecated Write functions in this file - the WriteOneByte and WriteUtf8?

0,
bufsize - 1,
v8::String::NO_NULL_TERMINATION);
size_t n_chars = std::min(
Copy link
Member

@vmoroz vmoroz May 4, 2025

Choose a reason for hiding this comment

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

nit: the n_chars is a bit cryptic and unusual in this context. Could you use something like length, str_length, str_len, or str_size. The WriteV2 function parameter name is length.

bufsize - 1, static_cast<size_t>(val.As<v8::String>()->Length()));
val.As<v8::String>()->WriteV2(env->isolate,
0,
n_chars,
Copy link
Member

Choose a reason for hiding this comment

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

The WriteV2 function parameter length is uint32_t. It is different from the size_t on 64-bit CPUs. Please use the uint32_t type to avoid compiler warnings.

bufsize - 1,
v8::String::NO_NULL_TERMINATION);
size_t n_chars = std::min(
bufsize - 1, static_cast<size_t>(val.As<v8::String>()->Length()));
Copy link
Member

Choose a reason for hiding this comment

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

It would be nice to avoid calling val.As<v8::String>() twice.
In case if it does additional checks then it may cause unnecessary overhead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. node-api Issues and PRs related to the Node-API. v8 engine Issues and PRs related to the V8 dependency.
Projects
Status: Need Triage
Development

Successfully merging this pull request may close these issues.

4 participants