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

Set up doc-comments and doxygen #34

Merged
merged 3 commits into from
May 9, 2017
Merged

Set up doc-comments and doxygen #34

merged 3 commits into from
May 9, 2017

Conversation

jasongin
Copy link
Member

@jasongin jasongin commented May 3, 2017

  • Add doc-comments to a few of the classes in napi.h: Value, String, Error
  • Add a doxygen config file

To generate documents:

  1. Download and install doxygen from http://www.stack.nl/~dimitri/doxygen/download.html
  2. Run the "doxywizard" (GUI) or doxygen.exe (command-line) using the Doxyfile configuration file added in this change.

Doxygen supports multiple different doc-comment styles and text formatting methods. Here, I have chosen:

  • Triple-slash style doc-comments, because they make it easier to add end-of-line comments for method parameters, and for simple property-style methods that are common in these APIs.
  • Markdown for text formatting, because we're all familiar with markdown and it's nicer to read compared to the @command or \command formatting instructions that are also supported. We can still mix in any of Doxygen's supported commands.

Doxygen also supports Javadoc style comments (/** ... */) but those would not be as nice here IMO.

@jasongin
Copy link
Member Author

jasongin commented May 3, 2017

While someone has created a doxygen npm package, it apparently isn't widely used. I tried it out and it doesn't work very well: there's no way to download the doxygen binaries only when needed (so it's very slow when running repeatedly), and it doesn't show progress while downloading or running.

@jasongin
Copy link
Member Author

jasongin commented May 3, 2017

Related: #6 Write documentation for C++ APIs

Copy link

@boingoing boingoing left a comment

Choose a reason for hiding this comment

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

Looks good to me, comments are mostly just nitpicky grammar and such.

Only glanced over the Doxyfile. This is a huge file.

👍

napi.h Outdated
/// Converts to another type of `Napi::Value`, when the actual type is known or assumed.
///
/// This conversion does NOT coerce the type. Calling any methods inappropriate for the actual
/// value type will cause `Napi::Error`s to be thrown.

Choose a reason for hiding this comment

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

Just nitpicky, should we write this as such?

Calling any methods inappropriate for the actual value type will throw Napi::Error.

napi.h Outdated
/// Creates a new String value from a UTF-8 encoded C string.
static String New(
napi_env env, ///< N-API environment
const char* value ///< UTF-8 encoded C string

Choose a reason for hiding this comment

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

C string seems to imply "null-terminated" but should we explicitly mention that here since there is an overload which takes a length parameter?

napi.h Outdated
static String New(
napi_env env, ///< N-API environment
const char* value, ///< UTF-8 encoded C string
size_t length ///< length of the string in bytes

Choose a reason for hiding this comment

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

length is not capitalized, seems like it should be to be consistent with other comments.

napi.h Outdated
/// may be changed to/from a strong reference by adjusting the refcount.
///
/// The referenced value is not immediately destroyed when the reference count is zero; it is
/// merely then eligible for if there are no other references to the value.

Choose a reason for hiding this comment

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

Eligible for destruction?

napi.h Outdated
/// Napi::Value GetSomething(const Napi::CallbackInfo& info);
/// void SetSomething(const Napi::CallbackInfo& info, const Napi::Value& value);
/// Napi::Value DoSomething(const Napi::CallbackInfo& info);
/// }

Choose a reason for hiding this comment

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

Nitpicky: This brace is not aligned with the class statement - looks like it's 2 spaces further right.

@jasongin
Copy link
Member Author

jasongin commented May 3, 2017

Thanks @boingoing. I pushed an update that fixed all the issues you commented on.

@jasongin
Copy link
Member Author

jasongin commented May 3, 2017

I was thinking an easy way to publish the generated API docs would be to use GitHub Pages. Based on the GitHub help, I created a gh-pages branch in this repo and pushed a commit with all the generated docs.

@mhdawson Can you go to the settings for this repo and enable GitHub Pages, using the gh-pages branch? Then I think the docs should be visible at https://nodejs.github.io/node-api/

@jasongin
Copy link
Member Author

jasongin commented May 4, 2017

Docs are visible now at https://nodejs.github.io/node-api/

jasongin added 2 commits May 4, 2017 17:04
 - Add doc-comments to a few of the classes in napi.h
 - Add a doxygen config file
@jasongin
Copy link
Member Author

jasongin commented May 5, 2017

@digitalinfinity @gabrielschulhof @mhdawson @addaleax Any feedback on the documentation format?

README.md Outdated
### API Documentation

- [ABI-Stable C APIs in Node.js](https://nodejs.org/dist/latest-v7.x/docs/api/n-api.html)
- [C++ APIs in this package](https://nodejs.github.io/node-api/namespace_napi.html)
Copy link
Member

Choose a reason for hiding this comment

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

Aaaah this is so cool! Thank you for setting it up!

README.md Outdated
@@ -5,6 +5,13 @@ Node.js API (N-API), along with library code that enables
backward-compatibility with use with older versions of Node.js that do
not have N-API built-in.

### API Documentation

- [ABI-Stable C APIs in Node.js](https://nodejs.org/dist/latest-v7.x/docs/api/n-api.html)
Copy link
Member

Choose a reason for hiding this comment

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

latest-v7.x only works by accident (and it might/should get deleted) – I would just stick with linking to https://nodejs.org/api/n-api.html and accept that it only works once the 8.0.0 release is out

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. I wanted to link to https://github.com/nodejs/node/blob/master/doc/api/n-api.md for now, but links to external markdown files confuse the Doxygen generator.

napi.h Outdated
/// wrapper automatically converts and throws it as a JavaScript exception.
///
/// Catching a C++ exception of type `Napi::Error` also clears the JavaScript exception. Of
/// course it may be then re-thrown, which restores the JavaScript exception.
Copy link
Member

Choose a reason for hiding this comment

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

This paragraph sounds a bit confusing (it sounds like C++ exception management affects the JS VM’s state), I would be okay with just deleting it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Reworded and combined with the previous paragraph.

napi.h Outdated
/// a C++ exception of type `Napi::Error`.
///
/// If a C++ exception of type `Napi::Error` escapes from a N-API C++ callback, then the N-API
/// wrapper automatically converts and throws it as a JavaScript exception.
Copy link
Member

Choose a reason for hiding this comment

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

By the way, should we catch all exceptions coming from C++ callbacks and convert them to JS errors? I don’t think there’s a way to do anything more meaningful (or at least I would assume JS engines don’t like it if the call stack unwinds outside of their control)

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure. I suppose we could catch any kind of std::exception and then copy the message into a JS error. But would that be safe, or could some exceptions leave the module in an inconsistent state?

The alternative is to just let any other kinds of uncaught C++ exceptions result in a crash, which is what happens now.

napi.h Outdated
/// exception of type `Napi::Error`, until it is either caught while still in C++, or else
/// automatically re-thrown as a JavaScript exception when the callback returns to JavaScript.
///
/// #### Example 2 - Ignoring a NAPI exception:
Copy link
Member

Choose a reason for hiding this comment

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

s/Ignoring/not catching/?

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.

napi.h Outdated
/// exception of type `Napi::Error`, until it is either caught while still in C++, or else
/// automatically re-thrown as a JavaScript exception when the callback returns to JavaScript.
///
/// #### Example 3 - Handling a NAPI exception:
Copy link
Member

Choose a reason for hiding this comment

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

N-API?

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.

@mhdawson
Copy link
Member

mhdawson commented May 8, 2017 via email

@jasongin jasongin merged commit b62dc80 into nodejs:master May 9, 2017
@jasongin jasongin deleted the doc branch May 9, 2017 19:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants