-
Notifications
You must be signed in to change notification settings - Fork 28.6k
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
napi: add support for napi_set_named_property_len function #52984
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer adding node_api_set_named_property(napi_env env, napi_value object, const char* utf8name, size_t name_length, napi_value value)
(which is the new naming pattern) and deprecating napi_set_named_property
once the new one promoted to stable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for working on this. Request for change on implementation as comments above.
ab3f4c8
to
9c1a0bb
Compare
@mertcanaltin we discussed this PR at today's Node-API team meeting. We have come to the conclusion that, although this new API adds convenience for developers, it actually encourages a pattern of runtime inefficiency by making it easy to create properties with keys for which the engine is unaware that they are not just regular strings but property keys, and thus it doesn't optimize them. We believe it would be better to pivot this PR to creating additional variants of In addition, it would be advisable for add-on maintainers to create a property key once, and then reuse the resulting This is certainly less convenient for those writing the code, however, it increases the performance of the resulting add-ons. |
First of all, thank you very much for your review and support, do you think I can continue to develop this pr I would be very happy if you have any suggestions ❤️🙏 @gabrielschulhof |
I created a node_api_create_property_key_utf8 based on what you said node_api_create_proper- I wonder if we can get a performance as you say by making different string encodings? |
src/js_native_api.h
Outdated
@@ -118,6 +118,22 @@ NAPI_EXTERN napi_status NAPI_CDECL node_api_create_property_key_utf16( | |||
napi_env env, const char16_t* str, size_t length, napi_value* result); | |||
#endif // NAPI_EXPERIMENTAL | |||
|
|||
#ifdef NAPI_EXPERIMENTAL | |||
#define NODE_API_EXPERIMENTAL_HAS_PROPERTY_KEYS |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Defining the same macro twice will cause compiler warnings or errors. Please group the new node_api_create_property_key_utf8
API with the previous node_api_create_property_key_utf16
.
@mertcanaltin , there are a few Node-API benchmarks here: https://github.com/nodejs/node/tree/main/benchmark/napi. A new one can be created to benchmark different ways to set and get property values. |
I am hesitant of adding these APIs, as the functionality to do what you want already exists using both Adding new APIs has more overreaching effects than just Node.js itself, as other JavaScript engines must implement the new APIs as well. Since this functionality already exists and can be performed in a trivial manner, I 👎 this addition. |
Hi @KevinEady, |
thank you very much for reviewing me I was at a conference this week so I could not give a quick answer but I will give an update today thank you all very much |
We discussed in the Node-api team meeting again today, and consensus seems to be to implement the suggestion in #52984 (comment), and only add the new property key creation functions, and no others. |
greetings I'm very sorry I'm late getting back here, I sent a few edits today |
@mertcanaltin , thank you for making the progress with the PR.
Please let me know if I can help with the implementation. |
I did these steps @vmoroz
|
I wonder if you have a suggestion about test failures @vmoroz |
The error says that the module is not found. It is possible that it is failed to build. I will check it out and see how it can be fixed. |
@mertcanaltin , are you sure that the latest commit in this PR has all the latest code?
|
greetings, I tried to verify the structure, but I got a Cannot find package error @vmoroz ➜ node git:(dev-52979) ✗ make test
ninja -C out/Release
ninja: Entering directory `out/Release'
ninja: no work to do.
if [ ! -r node ] || [ ! -L node ]; then ln -fs out/Release/node node; fi
/Applications/Xcode.app/Contents/Developer/usr/bin/make -s tooltest
.....
----------------------------------------------------------------------
Ran 5 tests in 0.002s
OK
/Applications/Xcode.app/Contents/Developer/usr/bin/make -s test-doc
node:internal/modules/esm/resolve:210
const resolvedOption = FSLegacyMainResolve(packageJsonUrlString, packageConfig.main, baseStringified);
^
Error: Cannot find package '/Users/mert/Desktop/openSource/node/tools/doc/node_modules/rehype-raw/package.json' imported from /Users/mert/Desktop/openSource/node/tools/doc/generate.mjs
at legacyMainResolve (node:internal/modules/esm/resolve:210:26)
at packageResolve (node:internal/modules/esm/resolve:829:14)
at moduleResolve (node:internal/modules/esm/resolve:915:18)
at defaultResolve (node:internal/modules/esm/resolve:1123:11)
at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:557:12)
at ModuleLoader.resolve (node:internal/modules/esm/loader:526:25)
at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:249:38)
at ModuleJob._link (node:internal/modules/esm/module_job:126:49) {
code: 'ERR_MODULE_NOT_FOUND'
}
Node.js v23.0.0-pre
make[2]: *** [out/doc/api/addons.html] Error 1
make[1]: *** [doc-only] Error 2
make: *** [test] Error 2
➜ node git:(dev-52979) ✗ |
9216d2e
to
3aedaf5
Compare
in addition, markdown formatter was getting error, I corrected it by using |
This pull request addresses issue #52979, where developers encounter issues with the napi_set_named_property function treating '\0' characters as terminators rather than values. To solve this problem, the napi_set_named_property_len function has been introduced as a new variant. This function accepts a string and its length, allowing developers to use '\0' characters as values when setting named properties on JavaScript objects.
in addition notes:
This change includes updates to the documentation to reflect the addition of the new function.
Tests have been added to ensure the correctness of the implementation.
The stability of this feature is marked as experimental and may change in future releases.
Test Plan:
Run existing unit tests to ensure no regressions occur.
Add new unit tests specifically targeting the behavior of the napi_set_named_property_len function.
Manually test the functionality by integrating it into existing code where '\0' characters are used as property values.