Skip to content
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

N-API documentation needs review (misleading / wrong code samples, etc) #20421

Closed
josephg opened this issue Apr 30, 2018 · 7 comments
Closed
Labels
doc Issues and PRs related to the documentations. node-api Issues and PRs related to the Node-API.

Comments

@josephg
Copy link
Contributor

josephg commented Apr 30, 2018

I've been reading the N-API docs to understand it and it needs some cleanup. I'm not sure if all of these notes are still an issue in master.

I also somewhere saw a line which was missing its ;, and now I can't find it.

N-API

These wrappers are not part of N-API, nor will they be maintained as part of Node.js. One such example is: node-api.

  • node-api has been renamed node-addon-api, and its repo has been renamed.

napi_status

  • The typedef described in the documentation doesn't match the definition in the 10.0.0 header file.

napi_create_error

  • Weird formatting of text 'be associated with the error':

image

Making handle lifespan shorter ...

  napi_status status = napi_get_element(e, object, i, &result);
  • e -> env. Likewise in the second example in this block, where the environment is referred to as env in calls to some methods but not napi_get_element.

Module registration:

To add the method hello as a function ...

napi_value Init(napi_env env, napi_value exports) {
  napi_status status;
  napi_property_descriptor desc =
    {"hello", Method, 0, 0, 0, napi_default, 0};
  if (status != napi_ok) return NULL;
  status = napi_define_properties(env, exports, 1, &desc);
  if (status != napi_ok) return NULL;
  return exports;
}
  • status is checked before it is assigned
  • napi_property_descriptor fields are (utf8name, name, method, getter, setter, value, attr, data). There are 8 of them, not 7. Unless I'm missing something, the code should be {"hello", 0, Method, 0, 0, 0, napi_default, 0}. But imho it should use NULL instead of 0. In modern C I would simply write this as {.utf8name="hello", .method=Method}, although I'm not sure if the VC++ compiler can handle struct property initializers yet. The internet says yes
  • To make it more obvious how to extend the example, it might be better to make desc an array of napi_property_descriptor objects. Although the class example does that... so maybe its not super important.

To define a class ...

napi_value Init(napi_env env, napi_value exports) {
  napi_status status;
  napi_property_descriptor properties[] = {
    { "value", NULL, GetValue, SetValue, 0, napi_default, 0 },
    DECLARE_NAPI_METHOD("plusOne", PlusOne),
    DECLARE_NAPI_METHOD("multiply", Multiply),
  };
  // ...
  • Again, the property descriptor is invalid. It should be { "value", NULL, 0, GetValue, SetValue, 0, napi_default, 0 },.
  • DECLARE_NAPI_METHOD is not a real thing - it does not exist anywhere else in the documentation or header files. This example should either define it locally or not use it.

napi_property_descriptor

  • The order of the documentation for the data and attributes fields should be swapped
@vsemozhetbyt
Copy link
Contributor

Thank you.

I will fix 'be associated with the error' formatting issue soon.

cc @nodejs/n-api @nodejs/documentation

@vsemozhetbyt vsemozhetbyt added doc Issues and PRs related to the documentations. node-api Issues and PRs related to the Node-API. labels Apr 30, 2018
@mhdawson mhdawson mentioned this issue Apr 30, 2018
3 tasks
@mhdawson
Copy link
Member

PR to cleanup all by the modules section and the 'be associated with the error' which @vsemozhetbyt mentioned he is looking at: #20430

Will look at the modules section next.

@mhdawson
Copy link
Member

@josephg thanks for the comments/suggestions for cleanup.

@gabrielschulhof
Copy link
Contributor

#20433 fixes the napi_property_descriptor-related parts.

@vsemozhetbyt
Copy link
Contributor

vsemozhetbyt commented Apr 30, 2018

'be associated with the error' formatting issues were fixed during the broader sweep in #20438 (the cause was wrongly parsed indentation).

mhdawson added a commit to mhdawson/io.js that referenced this issue May 3, 2018
Partial doc cleanup as per
nodejs#20421
mhdawson added a commit that referenced this issue May 3, 2018
Partial doc cleanup as per
#20421

PR-URL: #20430
Reviewed-By: Trivikram Kamat <[email protected]>
Reviewed-By: Gabriel Schulhof <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
MylesBorins pushed a commit that referenced this issue May 4, 2018
Partial doc cleanup as per
#20421

PR-URL: #20430
Reviewed-By: Trivikram Kamat <[email protected]>
Reviewed-By: Gabriel Schulhof <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
MylesBorins pushed a commit that referenced this issue May 8, 2018
Partial doc cleanup as per
#20421

PR-URL: #20430
Reviewed-By: Trivikram Kamat <[email protected]>
Reviewed-By: Gabriel Schulhof <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
MylesBorins pushed a commit that referenced this issue Jul 9, 2018
Partial doc cleanup as per
#20421

PR-URL: #20430
Reviewed-By: Trivikram Kamat <[email protected]>
Reviewed-By: Gabriel Schulhof <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
MylesBorins pushed a commit that referenced this issue Jul 10, 2018
Partial doc cleanup as per
#20421

PR-URL: #20430
Reviewed-By: Trivikram Kamat <[email protected]>
Reviewed-By: Gabriel Schulhof <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
rvagg pushed a commit that referenced this issue Aug 16, 2018
Partial doc cleanup as per
#20421

PR-URL: #20430
Reviewed-By: Trivikram Kamat <[email protected]>
Reviewed-By: Gabriel Schulhof <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
@jasnell
Copy link
Member

jasnell commented Oct 24, 2018

has this been resolved?

@josephg
Copy link
Contributor Author

josephg commented Oct 24, 2018

Yes! Thanks everyone

@josephg josephg closed this as completed Oct 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. node-api Issues and PRs related to the Node-API.
Projects
None yet
Development

No branches or pull requests

5 participants