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

First pass at basic Node Addon API docs #268

Closed
wants to merge 3 commits into from

Conversation

digitalinfinity
Copy link
Contributor

Added docs for Basic Types, Strings and Symbols. Feedback welcome, I think this will take a couple more iterations to get right.

Copy link
Member

@NickNaso NickNaso left a comment

Choose a reason for hiding this comment

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

I tried to give you some suggestions.

It represents a JavaScript value of an unknown type. It is a thin wrapper around
the N-API datatype `napi_value`. Methods on this class can be used to check
the JavaScript type of the underlying `napi_value` and also convert to C++
Copy link
Member

Choose a reason for hiding this comment

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

to convert

- `[in] value` - The underlying JavaScript value that the `Value` instance represents

Used to create a Node addon API `Value` that represents the `napi_value` passed
Copy link
Member

Choose a reason for hiding this comment

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

Maybe here we need to use: Returns a Node addon API Value that represents the napi_value passed in.

Copy link
Member

Choose a reason for hiding this comment

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

Agreed we to follow the standard format we should have a Returns after the parameters.

- `[in] other` - The value to compare against

Tests if this value strictly equals another value.
Copy link
Member

Choose a reason for hiding this comment

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

Maybe it's better move Tests if this value strictly equals another value. on top and here something like this: Returns true if the value isn strictly equals another value, or false otherwise.

return type may return an empty value to indicate a pending exception. So when
not using C++ exceptions, callers should check whether the value is empty
before attempting to use it.
Copy link
Member

Choose a reason for hiding this comment

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

Maybe it's better to move this part on top and here: Returns true if the Value is an empty value.

About the error if the function that you are calling doesn't return value it' more appropriate use Env::IsExceptionPending() to retrieve if there is an active error.

```

Gets the underlying `napi_valuetype` of the value.
Copy link
Member

Choose a reason for hiding this comment

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

Maybe it's more appropriate use: Returns

`napi_coerce_to_object`. This will throw a JavaScript exception if the
coercion fails.

Copy link
Member

Choose a reason for hiding this comment

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

Move this part on top and here something like this: Returns a JavaScript Object.


Names are JavaScript values that can be used as a property name. There are two
specialized types of names supported in Node Addon API- `String` and `Symbol`.
Copy link
Member

Choose a reason for hiding this comment

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

Maybe you can link the doc of String and Symbol:
Names are JavaScript values that can be used as a property name. There are two
specialized types of names supported in Node Addon API- String and Symbol.

created from the JavaScript primitive. Note that the value is not coerced to a
string.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe it could be better to split this two cases


Returns an empy array. If an error occurs, a `Napi::Error` will get thrown.

Copy link
Member

Choose a reason for hiding this comment

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

empy -> empty

Wraps a `napi_value` as a `Napi::Array`. If an error occurs, a `Napi::Error`
will get thrown.

Copy link
Member

Choose a reason for hiding this comment

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

Move this on top and here something like this: Returns the primitive top wrap.

```

Returns the underlying N-API value primitive. If the instance is _empty_,
Copy link
Member

Choose a reason for hiding this comment

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

How about `Returns the underlying N-API napi_value'. If ...

```

Returns `true` if this value strictly equals another value, or `false` otherwise
Copy link
Member

Choose a reason for hiding this comment

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

I think we need full sentences so needs a period at the end of the sentence. Say goes throughout the rest of the doc.

- `[in] env` - The `napi_env` environment in which to construct the Value object.
- `[in] value` - The C++ type to represent in JavaScript.

Copy link
Member

Choose a reason for hiding this comment

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

Needs a Returns... before the explanation of how it works.

template <typename T> T As() const;
```

Copy link
Member

Choose a reason for hiding this comment

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

Needs Returns.... same for other places, won't mention it specifically in those other places.


This conversion does NOT coerce the type. Calling any methods inappropriate for
the actual value type will throw `Napi::Error`.
Copy link
Member

Choose a reason for hiding this comment

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

I think an example here would help.

```

Gets the environment the value is associated with. See `napi_env` for more
Copy link
Member

Choose a reason for hiding this comment

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

This should probably reference the Env class as opposed to napi_env.


When C++ exceptions are disabled at compile time, a method with a `Value`
return type may return an empty value to indicate a pending exception. So when
Copy link
Member

Choose a reason for hiding this comment

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

So when --> Therefore when

occurs, a `Napi::Error` will get thrown.

#### New
Copy link
Member

Choose a reason for hiding this comment

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

This should be #### Length

@@ -39,13 +33,41 @@ operator std::u16string() const;
```
Copy link
Member

Choose a reason for hiding this comment

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

I think an example or more description might be useful here.

String::New(napi_env env, const std::string& value);
String::New(napi_env env, const std::u16string& value);
String::New(napi_env env, const char* value);
String::New(napi_env env, const char16_t* value);
Copy link
Member

Choose a reason for hiding this comment

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

We should talk about this. Its not consistent with anything I've seen elsewhere in the node documentation

Copy link
Member

Choose a reason for hiding this comment

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

But with overrides it does make sense to me

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that was kind of what I was thinking. Anyone else have thoughts?

@mhdawson
Copy link
Member

Left a few comments but a really good first cut.

@mhdawson
Copy link
Member

@digitalinfinity are you going to get a chance to update for the comments?

@digitalinfinity
Copy link
Contributor Author

@mhdawson yes- so sorry, I've been sidetracked with other things for the past couple weeks but I should be able to find some cycles this week to incorporate all the great feedback provided so far.

@digitalinfinity
Copy link
Contributor Author

Ok, finally got a chance to incorporate the feedback. @NickNaso @mhdawson great feedback btw. @mhdawson I still need to add a couple examples but I think all of the other comments have been addressed.

Copy link
Contributor

@gabrielschulhof gabrielschulhof left a comment

Choose a reason for hiding this comment

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

Just a few nits.

[C++ wrapper classes for the ABI-stable C APIs for Node.js](https://nodejs.github.io/node-addon-api/)
Node Addon API consists of a few fundamental data types. These allow a user of
the API to create, convert and introspect fundamental JavaScript types, and
interop with their C++ counterparts.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you should expand this to "interoperate".

Returns `true` if the value is uninitialized.

An empty value is invalid, and most attempts to perform an operation on an
empty value will result in an exception. Note an empty value is distinct from
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think you need "Note" here.

bool IsBuffer() const;
```

Returns `true` if the underlying value is a Node `Buffer` or `false`
Copy link
Contributor

Choose a reason for hiding this comment

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

We should write "Node.js" instead of "Node".

Copy link
Member

Choose a reason for hiding this comment

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

+1


## Array

Arrays are native representations of JavaScript Arrays. `Napi::Array` is sugar
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should say "a wrapper" instead of "sugar".


Returns an empty array.

If an error occurs, a `Napi::Error` will get thrown. If C++ exceptions are not
Copy link
Contributor

Choose a reason for hiding this comment

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

"will be thrown" might be better – here, and subsequently.

Copy link
Member

Choose a reason for hiding this comment

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

+1

Copy link
Member

@mhdawson mhdawson left a comment

Choose a reason for hiding this comment

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

LGTM. After the few minor comments I think it is good to land and we can iterate after that if necessary.

@mhdawson
Copy link
Member

I believe all comments are now addressed will land.

mhdawson pushed a commit that referenced this pull request Jun 14, 2018
PR-URL: #268
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
@mhdawson
Copy link
Member

Landed as e029a07

@mhdawson mhdawson closed this Jun 14, 2018
kevindavies8 added a commit to kevindavies8/node-addon-api-Develop that referenced this pull request Aug 24, 2022
PR-URL: nodejs/node-addon-api#268
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Marlyfleitas added a commit to Marlyfleitas/node-api-addon-Development that referenced this pull request Aug 26, 2022
PR-URL: nodejs/node-addon-api#268
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
wroy7860 added a commit to wroy7860/addon-api-benchmark-node that referenced this pull request Sep 19, 2022
PR-URL: nodejs/node-addon-api#268
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
austinli64 added a commit to austinli64/node-addon-api that referenced this pull request May 9, 2023
PR-URL: nodejs/node-addon-api#268
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
johnfrench3 pushed a commit to johnfrench3/node-addon-api-git that referenced this pull request Aug 11, 2023
PR-URL: nodejs/node-addon-api#268
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
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.

None yet

4 participants