Skip to content

Conversation

@tniessen
Copy link
Member

@tniessen tniessen commented May 4, 2025

This change refactors StringBytes::WriteUCS2() in multiple ways.

The flags argument being passed to WriteUCS2() is not useful: the only really relevant flag is NO_NULL_TERMINATION since V8 ignores REPLACE_INVALID_UTF8, HINT_MANY_WRITES_EXPECTED, and PRESERVE_ONE_BYTE_NULL for UTF-16 strings. However, WriteUCS2() might not null-terminate the result correctly regardless of whether NO_NULL_TERMINATION is set because it makes multiple calls to String::Write() internally. For these reasons, this patch removes the flags argument entirely and always assumes NO_NULL_TERMINATION.

Next, this patch replaces the calls to the deprecated function String::Write() with calls to the new function String::WriteV2(), which always succeeds and always writes a predictable number of characters, removing the need to deal with a return value here.

Lastly, this patch simplifies the implementation of WriteUCS2() and computes the exact number of characters nchars from the beginning, removing the need to later check again if the number of characters is zero.

This change refactors `StringBytes::WriteUCS2()` in multiple ways.

The `flags` argument being passed to `WriteUCS2()` is not useful: the
only really relevant flag is `NO_NULL_TERMINATION` since V8 ignores
`REPLACE_INVALID_UTF8`, `HINT_MANY_WRITES_EXPECTED`, and
`PRESERVE_ONE_BYTE_NULL` for UTF-16 strings. However, `WriteUCS2()`
might not null-terminate the result correctly regardless of whether
`NO_NULL_TERMINATION` is set because it makes multiple calls to
`String::Write()` internally. For these reasons, this patch removes the
`flags` argument entirely and always assumes `NO_NULL_TERMINATION`.

Next, this patch replaces the calls to the deprecated function
`String::Write()` with calls to the new function `String::WriteV2()`,
which always succeeds and always writes a predictable number of
characters, removing the need to deal with a return value here.

Lastly, this patch simplifies the implementation of `WriteUCS2()` and
computes the exact number of characters `nchars` from the beginning,
removing the need to later check again if the number of characters is
zero.
@tniessen tniessen added the c++ Issues and PRs that require attention from people who are familiar with C++. label May 4, 2025
@nodejs-github-bot nodejs-github-bot added buffer Issues and PRs related to the buffer subsystem. needs-ci PRs that need a full CI run. labels May 4, 2025
@codecov
Copy link

codecov bot commented May 4, 2025

Codecov Report

Attention: Patch coverage is 92.30769% with 1 line in your changes missing coverage. Please review.

Project coverage is 90.17%. Comparing base (92102c0) to head (ea0a7c6).
Report is 316 commits behind head on main.

Files with missing lines Patch % Lines
src/string_bytes.cc 92.30% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #58163      +/-   ##
==========================================
- Coverage   90.18%   90.17%   -0.02%     
==========================================
  Files         630      630              
  Lines      186473   186499      +26     
  Branches    36612    36614       +2     
==========================================
  Hits       168169   168169              
- Misses      11116    11120       +4     
- Partials     7188     7210      +22     
Files with missing lines Coverage Δ
src/string_bytes.h 80.00% <ø> (ø)
src/string_bytes.cc 70.41% <92.30%> (+0.89%) ⬆️

... and 32 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.

@tniessen tniessen added author ready PRs that have at least one approval, no pending requests for changes, and a CI started. request-ci Add this label to start a Jenkins CI on a PR. labels May 6, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label May 6, 2025
@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@tniessen tniessen added the commit-queue Add this label to land a pull request using GitHub Actions. label May 8, 2025
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label May 8, 2025
@nodejs-github-bot nodejs-github-bot merged commit e38ce27 into nodejs:main May 8, 2025
77 checks passed
@nodejs-github-bot
Copy link
Collaborator

Landed in e38ce27

targos pushed a commit that referenced this pull request May 16, 2025
This change refactors `StringBytes::WriteUCS2()` in multiple ways.

The `flags` argument being passed to `WriteUCS2()` is not useful: the
only really relevant flag is `NO_NULL_TERMINATION` since V8 ignores
`REPLACE_INVALID_UTF8`, `HINT_MANY_WRITES_EXPECTED`, and
`PRESERVE_ONE_BYTE_NULL` for UTF-16 strings. However, `WriteUCS2()`
might not null-terminate the result correctly regardless of whether
`NO_NULL_TERMINATION` is set because it makes multiple calls to
`String::Write()` internally. For these reasons, this patch removes the
`flags` argument entirely and always assumes `NO_NULL_TERMINATION`.

Next, this patch replaces the calls to the deprecated function
`String::Write()` with calls to the new function `String::WriteV2()`,
which always succeeds and always writes a predictable number of
characters, removing the need to deal with a return value here.

Lastly, this patch simplifies the implementation of `WriteUCS2()` and
computes the exact number of characters `nchars` from the beginning,
removing the need to later check again if the number of characters is
zero.

PR-URL: #58163
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
@aduh95 aduh95 added dont-land-on-v20.x PRs that should not land on the v20.x-staging branch and should not be released in v20.x. dont-land-on-v22.x PRs that should not land on the v22.x-staging branch and should not be released in v22.x. labels Jun 10, 2025
@aduh95
Copy link
Contributor

aduh95 commented Jun 10, 2025

Adding dont-land-on-v22.x PRs that should not land on the v22.x-staging branch and should not be released in v22.x. as it looks like WriteV2 is not available on 22.x.

codebytere added a commit to electron/electron that referenced this pull request Oct 21, 2025
codebytere added a commit to electron/electron that referenced this pull request Oct 28, 2025
codebytere added a commit to electron/electron that referenced this pull request Oct 30, 2025
codebytere added a commit to electron/electron that referenced this pull request Oct 30, 2025
* chore: upgrade Node.js to v24.10.0

* chore: fixup crypto patch

* chore: fixup crypto test patch

* src: prepare for v8 sandboxing

nodejs/node#58376

* esm: fix module.exports export on CJS modules

nodejs/node#57366

* chore: fixup lazyload fs patch

* esm: Source Phase Imports for WebAssembly

nodejs/node#56919

* module: remove --experimental-default-type

nodejs/node#56092

* lib,src: refactor assert to load error source from memory

nodejs/node#59751

* src: add source location to v8::TaskRunner

nodejs/node#54077

* src: remove dependency on wrapper-descriptor-based CppHeap

nodejs/node#54077

* src: do not use soon-to-be-deprecated V8 API

nodejs/node#53174

* src: stop using deprecated fields of v8::FastApiCallbackOptions

nodejs/node#54077

* test: update v8-stats test for V8 12.6

nodejs/node#54077

* esm: unflag --experimental-wasm-modules

nodejs/node#57038

* test: adapt assert tests to stack trace changes

nodejs/node#58070

* src,test: unregister the isolate after disposal and before freeing

nodejs/node#58070

* src: use cppgc to manage ContextifyContext

nodejs/node#56522

* src: replace uses of FastApiTypedArray

nodejs/node#58070

* module: integrate TypeScript into compile cache

nodejs/node#56629

* deps: update ada to 3.2.7

nodejs/node#59336

* src: make minor cleanups in encoding_binding.cc

nodejs/node#57448

* src: switch from `Get/SetPrototype` to `Get/SetPrototypeV2`

nodejs/node#55453

* src: use non-deprecated Get/SetPrototype methods

nodejs/node#59671

* src: simplify string_bytes with views

nodejs/node#54876

* src: improve utf8 string generation performance

nodejs/node#54873

* src: use non-deprecated Utf8LengthV2() method

nodejs/node#58070

* src: use non-deprecated WriteUtf8V2() method

nodejs/node#58070

* src: refactor WriteUCS2 and remove flags argument

nodejs/node#58163

* src: use String::WriteV2() in TwoByteValue

nodejs/node#58164

* node-api: use WriteV2 in napi_get_value_string_utf16

nodejs/node#58165

* node-api: use WriteOneByteV2 in napi_get_value_string_latin1

nodejs/node#58325

* src: migrate WriteOneByte to WriteOneByteV2

nodejs/node#59634

* fs: introduce dirent\.parentPath

nodejs/node#50976

* src: avoid copy by using std::views::keys

nodejs/node#56080

* chore: fixup patch indices

* fix: errant use of context->GetIsolate()

* fix: tweak BoringSSL compat patch for new changes

* fix: add back missing isolate dtor declaration

* fixup! esm: fix module.exports export on CJS modules

* cli: remove --no-experimental-fetch flag

https://github.com/nodejs/node/pull/52611/files

* esm: Source Phase Imports for WebAssembly

nodejs/node#56919

* fixup! src: prepare for v8 sandboxing

* chore: bump @types/node to v24

* chore: fix const assignment in crypto test

* fix: sandbox pointer patch issues

* chore: rework source phase import patch

* src: add percentage support to --max-old-space-size

nodejs/node#59082

* chore: fixup crypto tests

* chore: HostImportModuleWithPhaseDynamically todo

* fix: cjs esm failures

* fix: v8::Object::Wrappable issues

- v8/node@b72a615
- v8/node@490bac2
- v8/node@4896a0d

* chore: remove deleted specs

* src: use v8::ExternalMemoryAccounter

nodejs/node#58070

* fs: port SonicBoom module to fs module as FastUtf8Stream

nodejs/node#58897

* chore: tweak sandboxed pr patch

* test: disable parallel/test-os-checked-function

* test: use WHATWG URL instead of url.parse

* fix: OPENSSL_secure_zalloc doesn't work in BoringSSL

* chore: fix accidental extra line

* 7017517: [defer-import-eval] Parse import defer syntax

https://chromium-review.googlesource.com/c/v8/v8/+/7017517
trop bot added a commit to electron/electron that referenced this pull request Oct 30, 2025
nodejs/node#58163

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
TheCommieAxolotl pushed a commit to TheCommieAxolotl/electron that referenced this pull request Nov 2, 2025
* chore: upgrade Node.js to v24.10.0

* chore: fixup crypto patch

* chore: fixup crypto test patch

* src: prepare for v8 sandboxing

nodejs/node#58376

* esm: fix module.exports export on CJS modules

nodejs/node#57366

* chore: fixup lazyload fs patch

* esm: Source Phase Imports for WebAssembly

nodejs/node#56919

* module: remove --experimental-default-type

nodejs/node#56092

* lib,src: refactor assert to load error source from memory

nodejs/node#59751

* src: add source location to v8::TaskRunner

nodejs/node#54077

* src: remove dependency on wrapper-descriptor-based CppHeap

nodejs/node#54077

* src: do not use soon-to-be-deprecated V8 API

nodejs/node#53174

* src: stop using deprecated fields of v8::FastApiCallbackOptions

nodejs/node#54077

* test: update v8-stats test for V8 12.6

nodejs/node#54077

* esm: unflag --experimental-wasm-modules

nodejs/node#57038

* test: adapt assert tests to stack trace changes

nodejs/node#58070

* src,test: unregister the isolate after disposal and before freeing

nodejs/node#58070

* src: use cppgc to manage ContextifyContext

nodejs/node#56522

* src: replace uses of FastApiTypedArray

nodejs/node#58070

* module: integrate TypeScript into compile cache

nodejs/node#56629

* deps: update ada to 3.2.7

nodejs/node#59336

* src: make minor cleanups in encoding_binding.cc

nodejs/node#57448

* src: switch from `Get/SetPrototype` to `Get/SetPrototypeV2`

nodejs/node#55453

* src: use non-deprecated Get/SetPrototype methods

nodejs/node#59671

* src: simplify string_bytes with views

nodejs/node#54876

* src: improve utf8 string generation performance

nodejs/node#54873

* src: use non-deprecated Utf8LengthV2() method

nodejs/node#58070

* src: use non-deprecated WriteUtf8V2() method

nodejs/node#58070

* src: refactor WriteUCS2 and remove flags argument

nodejs/node#58163

* src: use String::WriteV2() in TwoByteValue

nodejs/node#58164

* node-api: use WriteV2 in napi_get_value_string_utf16

nodejs/node#58165

* node-api: use WriteOneByteV2 in napi_get_value_string_latin1

nodejs/node#58325

* src: migrate WriteOneByte to WriteOneByteV2

nodejs/node#59634

* fs: introduce dirent\.parentPath

nodejs/node#50976

* src: avoid copy by using std::views::keys

nodejs/node#56080

* chore: fixup patch indices

* fix: errant use of context->GetIsolate()

* fix: tweak BoringSSL compat patch for new changes

* fix: add back missing isolate dtor declaration

* fixup! esm: fix module.exports export on CJS modules

* cli: remove --no-experimental-fetch flag

https://github.com/nodejs/node/pull/52611/files

* esm: Source Phase Imports for WebAssembly

nodejs/node#56919

* fixup! src: prepare for v8 sandboxing

* chore: bump @types/node to v24

* chore: fix const assignment in crypto test

* fix: sandbox pointer patch issues

* chore: rework source phase import patch

* src: add percentage support to --max-old-space-size

nodejs/node#59082

* chore: fixup crypto tests

* chore: HostImportModuleWithPhaseDynamically todo

* fix: cjs esm failures

* fix: v8::Object::Wrappable issues

- v8/node@b72a615
- v8/node@490bac2
- v8/node@4896a0d

* chore: remove deleted specs

* src: use v8::ExternalMemoryAccounter

nodejs/node#58070

* fs: port SonicBoom module to fs module as FastUtf8Stream

nodejs/node#58897

* chore: tweak sandboxed pr patch

* test: disable parallel/test-os-checked-function

* test: use WHATWG URL instead of url.parse

* fix: OPENSSL_secure_zalloc doesn't work in BoringSSL

* chore: fix accidental extra line

* 7017517: [defer-import-eval] Parse import defer syntax

https://chromium-review.googlesource.com/c/v8/v8/+/7017517
codebytere added a commit to electron/electron that referenced this pull request Nov 4, 2025
* chore: upgrade Node.js to v24.10.0

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* chore: fixup crypto patch

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* chore: fixup crypto test patch

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* src: prepare for v8 sandboxing

nodejs/node#58376

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* esm: fix module.exports export on CJS modules

nodejs/node#57366

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* chore: fixup lazyload fs patch

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* esm: Source Phase Imports for WebAssembly

nodejs/node#56919

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* module: remove --experimental-default-type

nodejs/node#56092

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* lib,src: refactor assert to load error source from memory

nodejs/node#59751

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* src: add source location to v8::TaskRunner

nodejs/node#54077

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* src: remove dependency on wrapper-descriptor-based CppHeap

nodejs/node#54077

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* src: do not use soon-to-be-deprecated V8 API

nodejs/node#53174

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* src: stop using deprecated fields of v8::FastApiCallbackOptions

nodejs/node#54077

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* test: update v8-stats test for V8 12.6

nodejs/node#54077

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* esm: unflag --experimental-wasm-modules

nodejs/node#57038

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* test: adapt assert tests to stack trace changes

nodejs/node#58070

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* src,test: unregister the isolate after disposal and before freeing

nodejs/node#58070

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* src: use cppgc to manage ContextifyContext

nodejs/node#56522

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* src: replace uses of FastApiTypedArray

nodejs/node#58070

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* module: integrate TypeScript into compile cache

nodejs/node#56629

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* deps: update ada to 3.2.7

nodejs/node#59336

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* src: make minor cleanups in encoding_binding.cc

nodejs/node#57448

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* src: switch from `Get/SetPrototype` to `Get/SetPrototypeV2`

nodejs/node#55453

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* src: use non-deprecated Get/SetPrototype methods

nodejs/node#59671

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* src: simplify string_bytes with views

nodejs/node#54876

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* src: improve utf8 string generation performance

nodejs/node#54873

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* src: use non-deprecated Utf8LengthV2() method

nodejs/node#58070

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* src: use non-deprecated WriteUtf8V2() method

nodejs/node#58070

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* src: refactor WriteUCS2 and remove flags argument

nodejs/node#58163

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* src: use String::WriteV2() in TwoByteValue

nodejs/node#58164

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* node-api: use WriteV2 in napi_get_value_string_utf16

nodejs/node#58165

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* node-api: use WriteOneByteV2 in napi_get_value_string_latin1

nodejs/node#58325

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* src: migrate WriteOneByte to WriteOneByteV2

nodejs/node#59634

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* fs: introduce dirent\.parentPath

nodejs/node#50976

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* src: avoid copy by using std::views::keys

nodejs/node#56080

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* chore: fixup patch indices

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* fix: errant use of context->GetIsolate()

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* fix: tweak BoringSSL compat patch for new changes

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* fix: add back missing isolate dtor declaration

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* fixup! esm: fix module.exports export on CJS modules

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* cli: remove --no-experimental-fetch flag

https://github.com/nodejs/node/pull/52611/files

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* esm: Source Phase Imports for WebAssembly

nodejs/node#56919

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* fixup! src: prepare for v8 sandboxing

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* chore: bump @types/node to v24

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* chore: fix const assignment in crypto test

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* fix: sandbox pointer patch issues

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* chore: rework source phase import patch

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* src: add percentage support to --max-old-space-size

nodejs/node#59082

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* chore: fixup crypto tests

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* chore: HostImportModuleWithPhaseDynamically todo

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* fix: cjs esm failures

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* fix: v8::Object::Wrappable issues

- v8/node@b72a615
- v8/node@490bac2
- v8/node@4896a0d

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* chore: remove deleted specs

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* src: use v8::ExternalMemoryAccounter

nodejs/node#58070

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* fs: port SonicBoom module to fs module as FastUtf8Stream

nodejs/node#58897

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* chore: tweak sandboxed pr patch

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* test: disable parallel/test-os-checked-function

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* test: use WHATWG URL instead of url.parse

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* fix: OPENSSL_secure_zalloc doesn't work in BoringSSL

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* chore: fix accidental extra line

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* 7017517: [defer-import-eval] Parse import defer syntax

https://chromium-review.googlesource.com/c/v8/v8/+/7017517

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs that have at least one approval, no pending requests for changes, and a CI started. buffer Issues and PRs related to the buffer subsystem. c++ Issues and PRs that require attention from people who are familiar with C++. dont-land-on-v20.x PRs that should not land on the v20.x-staging branch and should not be released in v20.x. dont-land-on-v22.x PRs that should not land on the v22.x-staging branch and should not be released in v22.x. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants