Skip to content

First pass at basic Node Addon API docs#268

Closed
digitalinfinity wants to merge 3 commits intonodejs:masterfrom
digitalinfinity:doc_updates
Closed

First pass at basic Node Addon API docs#268
digitalinfinity wants to merge 3 commits intonodejs:masterfrom
digitalinfinity:doc_updates

Conversation

@digitalinfinity
Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread doc/basic_types.md Outdated
Value is the base class of Node Addon API's fundamental object type hierarchy.
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

to convert

Comment thread doc/basic_types.md Outdated
- `[in] env` - The `napi_env` environment in which to construct the Value object.
- `[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
Copy Markdown
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
Copy Markdown
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.

Comment thread doc/basic_types.md Outdated

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

Tests if this value strictly equals another value.
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.

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.

Comment thread doc/basic_types.md Outdated
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
not using C++ exceptions, callers should check whether the value is empty
before attempting to use it.
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.

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.

Comment thread doc/basic_types.md Outdated
napi_valuetype Type() const;
```

Gets the underlying `napi_valuetype` of the value.
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.

Maybe it's more appropriate use: Returns

Comment thread doc/basic_types.md
Converts a `Napi::Value` to a JavasScript Object. This is a wrapper around
`napi_coerce_to_object`. This will throw a JavaScript exception if the
coercion fails.

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.

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

Comment thread doc/basic_types.md Outdated
## Name

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
Copy Markdown
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.

Comment thread doc/basic_types.md
created. If the constructor is invoked passing in an N-API value, then a Name is
created from the JavaScript primitive. Note that the value is not coerced to a
string.

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.

Maybe it could be better to split this two cases

Comment thread doc/basic_types.md
```

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

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.

empy -> empty

Comment thread doc/basic_types.md

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

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.

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

Comment thread doc/basic_types.md Outdated
operator napi_value() const;
```

Returns the underlying N-API value primitive. If the instance is _empty_,
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.

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

Comment thread doc/basic_types.md Outdated
bool operator ==(const Value& other) const;
```

Returns `true` if this value strictly equals another value, or `false` otherwise
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.

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

Comment thread doc/basic_types.md

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

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.

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

Comment thread doc/basic_types.md
```cpp
template <typename T> T As() const;
```

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.

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

Comment thread doc/basic_types.md
Casts 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 throw `Napi::Error`.
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.

I think an example here would help.

Comment thread doc/basic_types.md Outdated
Napi::Env Env() const;
```

Gets the environment the value is associated with. See `napi_env` for more
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 probably reference the Env class as opposed to napi_env.

Comment thread doc/basic_types.md Outdated
JavaScript `null` or `undefined`, which are valid values.

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
Copy Markdown
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

Comment thread doc/basic_types.md
This method returns a native representation of a JavaScript array. If an error
occurs, a `Napi::Error` will get thrown.

#### New
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 be #### Length

Comment thread doc/string.md
@@ -39,13 +33,41 @@ operator std::u16string() const;
```
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.

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

Comment thread doc/string.md
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member

Left a few comments but a really good first cut.

@mhdawson
Copy link
Copy Markdown
Member

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

@digitalinfinity
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

Comment thread doc/basic_types.md Outdated
[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
Copy Markdown
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".

Comment thread doc/basic_types.md Outdated
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
Copy Markdown
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.

Comment thread doc/basic_types.md Outdated
bool IsBuffer() const;
```

Returns `true` if the underlying value is a Node `Buffer` or `false`
Copy link
Copy Markdown
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

+1

Comment thread doc/basic_types.md Outdated

## Array

Arrays are native representations of JavaScript Arrays. `Napi::Array` is sugar
Copy link
Copy Markdown
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".

Comment thread doc/basic_types.md Outdated

Returns an empty array.

If an error occurs, a `Napi::Error` will get thrown. If C++ exceptions are not
Copy link
Copy Markdown
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

+1

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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>
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.

4 participants