Skip to content

Add String::New overload for string_view#1706

Open
cacharle wants to merge 3 commits intonodejs:mainfrom
cacharle:add-string-new-from-string-view
Open

Add String::New overload for string_view#1706
cacharle wants to merge 3 commits intonodejs:mainfrom
cacharle:add-string-new-from-string-view

Conversation

@cacharle
Copy link

Fixes #1703

@cacharle
Copy link
Author

I added the New overload for Symbol aswell. I looked at other functions that take const char* but there is way too many of them and I don't want to unnecessarily bloat the code / break something so let me know if there is other place which could benefit from this overload

@cacharle cacharle marked this pull request as ready for review February 1, 2026 17:11
@legendecas
Copy link
Member

Would you mind adding a test case in https://github.com/nodejs/node-addon-api/blob/main/test/name.cc? Thank you!

@legendecas
Copy link
Member

I added the New overload for Symbol aswell. I looked at other functions that take const char* but there is way too many of them and I don't want to unnecessarily bloat the code / break something so let me know if there is other place which could benefit from this overload

Sounds good! we can start with these two sites.

@cacharle
Copy link
Author

cacharle commented Feb 3, 2026

Same as #1705, the test take too long to compile, not sure how to run them

@legendecas legendecas moved this from Need Triage to In Progress in Node-API Team Project Feb 13, 2026
@legendecas
Copy link
Member

@cacharle I added a doc about building with ninja to speed up local builds: #1709

Function::New(env, NullString16ShouldThrow);
exports["checkString"] = Function::New(env, CheckString);
exports["createSymbol"] = Function::New(env, CreateSymbol);
exports["CreateSymbolFromStringView"] = Function::New(env, CreateSymbolFromStringView);
Copy link
Member

Choose a reason for hiding this comment

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

This seems to be the reason that the test failed.

Suggested change
exports["CreateSymbolFromStringView"] = Function::New(env, CreateSymbolFromStringView);
exports["createSymbolFromStringView"] = Function::New(env, CreateSymbolFromStringView);

Copy link
Contributor

@KevinEady KevinEady left a comment

Choose a reason for hiding this comment

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

Guards for c++17 can be removed.

Comment on lines +3 to +6
#if __cplusplus >= 201703L
#include <string_view>
#endif

Copy link
Contributor

Choose a reason for hiding this comment

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

We discussed in the Node API meeting on 20 Feb that this guard can be removed.
std::string_view was added in c++17, which is a minimum requirement starting with Node.js v20 (ref). Building addons with node-addon-api have the same build requirements as those of Node.js, so a minimum of c++17.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

Add String::New for std::string_view

3 participants