Skip to content

feat: add Object::New overload utilizing node_api_create_object_with_properties - #1735

Merged
legendecas merged 1 commit into
mainfrom
add-node_api_create_object_with_properties
Aug 28, 2026
Merged

feat: add Object::New overload utilizing node_api_create_object_with_properties#1735
legendecas merged 1 commit into
mainfrom
add-node_api_create_object_with_properties

Conversation

@KevinEady

Copy link
Copy Markdown
Contributor

Add Object::New overload utilizing node_api_create_object_with_properties

@KevinEady KevinEady moved this from Need Triage to In Progress in Node-API Team Project Jun 5, 2026
@KevinEady
KevinEady force-pushed the add-node_api_create_object_with_properties branch 4 times, most recently from bbf03d9 to ef2601b Compare June 5, 2026 13:24
@codecov-commenter

codecov-commenter commented Jun 5, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 66.66667% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 63.68%. Comparing base (7223518) to head (be1b480).
⚠️ Report is 7 commits behind head on main.

Files with missing lines Patch % Lines
napi-inl.h 66.66% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1735      +/-   ##
==========================================
- Coverage   63.69%   63.68%   -0.02%     
==========================================
  Files           3        3              
  Lines        2063     2065       +2     
  Branches      730      731       +1     
==========================================
+ Hits         1314     1315       +1     
  Misses        162      162              
- Partials      587      588       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 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.

@legendecas

legendecas commented Jun 12, 2026

Copy link
Copy Markdown
Member

The CI failure is nodejs/node-gyp#3327

@KevinEady
KevinEady force-pushed the add-node_api_create_object_with_properties branch 2 times, most recently from eb9b7ef to e725910 Compare July 31, 2026 13:02
@KevinEady
KevinEady marked this pull request as draft July 31, 2026 13:02
@KevinEady
KevinEady force-pushed the add-node_api_create_object_with_properties branch from e725910 to 23b62a9 Compare July 31, 2026 14:30
@KevinEady
KevinEady marked this pull request as ready for review July 31, 2026 14:30
@KevinEady
KevinEady requested a review from legendecas July 31, 2026 14:30
Comment thread napi-inl.h Outdated
std::vector<napi_value>& propertyNames,
std::vector<napi_value>& propertyValues) {
if (propertyNames.size() != propertyValues.size()) {
Napi::Error::New(env, "Mismatch in size of property names and values")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This should prefer NAPI_THROW to throw as a c++ exception when it is enabled:

Suggested change
Napi::Error::New(env, "Mismatch in size of property names and values")
NAPI_THROW(
Napi::Error::New(env, "Mismatch in size of property names and values"),
Object());

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed in push from 23b62a9 to be1b480 (diff)

Comment thread napi.h
Comment on lines +911 to +912
std::vector<napi_value>& propertyNames, ///< Property names
std::vector<napi_value>& propertyValues ///< Property values

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This could take const vectors:

Suggested change
std::vector<napi_value>& propertyNames, ///< Property names
std::vector<napi_value>& propertyValues ///< Property values
const std::vector<napi_value>& propertyNames, ///< Property names
const std::vector<napi_value>& propertyValues ///< Property values

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi @legendecas ,

Thanks for your patience :)

This cannot be const because using the .data() would then return const napi_value*, but node_api_create_object_with_properties does not accept const-qualified pointers:

../../../napi-inl.h:1651:7: error: no matching function for call to 'node_api_create_object_with_properties'
      node_api_create_object_with_properties(env,
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/kevineady/Library/Caches/node-gyp/24.14.0/include/node/js_native_api.h:56:1: note: candidate function not viable: 3rd argument ('const value_type *' (aka 'napi_value__ *const *')) would lose const qualifier
node_api_create_object_with_properties(napi_env env,
node_api_create_object_with_properties(napi_env env,
                                       napi_value prototype_or_null,
                                       napi_value* property_names,
                                       napi_value* property_values,
                                       size_t property_count,
                                       napi_value* result);

Might have been an oversight of the Node-API for node_api_create_object_with_properties...?

Eg. Function::New can accept an const std::vector<napi_value>& args, but that is because the underlying Node-API napi_new_instance accepts a const napi_value* argv.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You are right, I think this can be a follow-up. node_api_create_object_with_properties is actually read-only on property_names and property_values, so it should be safe to add the const qualifiers.

In the interim, I think we could also use const_cast because this does not write into the pointee so this does not incur any runtime behavior change. But we could do it after updating node_api_create_object_with_properties.

@legendecas legendecas linked an issue Aug 12, 2026 that may be closed by this pull request
@KevinEady
KevinEady force-pushed the add-node_api_create_object_with_properties branch from 23b62a9 to be1b480 Compare August 28, 2026 11:31
@legendecas
legendecas merged commit 06d56f7 into main Aug 28, 2026
111 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in Node-API Team Project Aug 28, 2026
@github-actions github-actions Bot mentioned this pull request Aug 28, 2026
@legendecas
legendecas deleted the add-node_api_create_object_with_properties branch August 28, 2026 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

Porting create_object_with_properties

3 participants