-
Notifications
You must be signed in to change notification settings - Fork 464
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
Function and FunctionReference documentation #299
Changes from 7 commits
17b299d
d2293c5
4a81a7d
ab4d8a6
a8ca2cf
9469a00
a09045b
8b3dbd8
6cf54c5
be41532
6bdfc56
fcc61a3
390ecd1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,282 @@ | ||
# Function | ||
|
||
You are reading a draft of the next documentation and it's in continuous update so | ||
if you don't find what you need please refer to: | ||
[C++ wrapper classes for the ABI-stable C APIs for Node.js](https://nodejs.github.io/node-addon-api/) | ||
The **Function** class provides a set of methods to create a function object in | ||
native code that can later be called from JavaScript. The created function is not | ||
automatically visible from JavaScript, instead it needs to be part of the add-on's | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a run-on sentance. Let's have "... from JavaScript. Instead it ..." |
||
module exports or be returned by one of the modules exported functions. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "module's" instead of "modules" |
||
|
||
In addition the `Function` class also provides methods that can be used to call | ||
functions that were created in JavaScript and passed to the native. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "... to the native add-on", perhaps? |
||
|
||
The `Function` class inherits its behavior from the `Object` class (for more info | ||
see: [Object](object.md)). | ||
|
||
## Example | ||
|
||
```cpp | ||
#include <napi.h> | ||
|
||
using namespace Napi; | ||
|
||
Value Fn(const CallbackInfo& info) { | ||
Env env = info.Env(); | ||
// ... | ||
return String::New(env, "Hello World"); | ||
} | ||
|
||
Object Init(Env env, Object exports) { | ||
exports.Set(String::New(env, "fn"), Function::New(env, Fn)); | ||
} | ||
|
||
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init) | ||
``` | ||
|
||
The above code can be used from JavaScript as follows: | ||
|
||
```js | ||
const addon = require('./addon'); | ||
addon.fn(); | ||
``` | ||
|
||
The `Function` class allows to call a JavaScript function object from a native | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should rephrase this as "With the |
||
add-on with two different methods: `Call` and `MackeCallback`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
The API of these two methods are very similar, but they are used in different context. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "is very similar, ..." |
||
`MakeCallback` method is used to call from native code back into JavaScript after | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "The |
||
returning from an [asynchronous operation](async_operations.md) and in general in | ||
situations which don't have an existing JavaScript function on the stack. | ||
`Call` method is used when there is already a JavaScript function on the stack | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "The |
||
(for example when running a native method called from JavaScript). | ||
|
||
## Methods | ||
|
||
### Constructor | ||
|
||
Creates a new empty instance of `Function`. | ||
|
||
```cpp | ||
Function(); | ||
``` | ||
|
||
### Constructor | ||
|
||
Creates a new instance of the `Function` object. | ||
|
||
```cpp | ||
Function(napi_env env, napi_value value); | ||
``` | ||
|
||
- `[in] env`: The `napi_env` environment in which to construct the Value object. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "... the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||
- `[in] value`: The `napi_value` which is handle for a JavaScript function. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "...which is a handle..." There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||
|
||
Returns a non-empty `Function` instance. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should really be consistent about whether we say There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok for me. Is it ok for you @mhdawson ? |
||
|
||
### New | ||
|
||
Creates an instance of a `Function` object. | ||
|
||
```cpp | ||
template <typename Callable> | ||
static Function New(napi_env env, Callable cb, const char* utf8name = nullptr, void* data = nullptr); | ||
``` | ||
|
||
- `[in] env`: The `napi_env` environment in which to construct the Function object. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please put backticks around "Function" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||
- `[in] cb`: Object that implements `Callable`. | ||
- `[in] utf8name`: Null-terminated string to be used as the name of the function. | ||
- `[in] data`: User-provided data context. This will be passed back into the | ||
function when invoked later. | ||
|
||
Returns an instance of a `Function` object. | ||
|
||
### New | ||
|
||
```cpp | ||
template <typename Callable> | ||
static Function New(napi_env env, Callable cb, const std::string& utf8name, void* data = nullptr); | ||
``` | ||
|
||
- `[in] env`: The `napi_env` environment in which to construct the Function object. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Backticks around "Function" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||
- `[in] cb`: Object that implements `Callable`. | ||
- `[in] utf8name`: String to be used as the name of the function. | ||
- `[in] data`: User-provided data context. This will be passed back into the | ||
function when invoked later. | ||
|
||
Returns an instance of a `Function` object. | ||
|
||
### New | ||
|
||
Creates a new JavaScript value from one that represents the constructor for the | ||
object. | ||
|
||
```cpp | ||
Object New(const std::initializer_list<napi_value>& args) const; | ||
``` | ||
|
||
- `[in] args`: Initializer list of JavaScript values as `napi_value` representing | ||
the arguments of the contructor function. | ||
|
||
Returns a new JavaScript object. | ||
|
||
### New | ||
|
||
Creates a new JavaScript value from one that represents the constructor for the | ||
object. | ||
|
||
```cpp | ||
Object New(const std::vector<napi_value>& args) const; | ||
``` | ||
|
||
- `[in] args`: Vector of JavaScript values as `napi_value` representing the | ||
arguments of the constructor function. | ||
|
||
Returns a new JavaScript object. | ||
|
||
### New | ||
|
||
Creates a new JavaScript value from one that represents the constructor for the | ||
object. | ||
|
||
```cpp | ||
Object New(size_t argc, const napi_value* args) const; | ||
``` | ||
|
||
- `[in] argc`: The number of the arguments passed to the contructor function. | ||
- `[in] args`: Array of JavaScript values as `napi_value` representing the | ||
arguments of the constructor function. | ||
|
||
Returns a new JavaScript object. | ||
|
||
### Call | ||
|
||
Calls a Javascript function from a native add-on. | ||
|
||
```cpp | ||
Value Call(const std::initializer_list<napi_value>& args) const; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs an example, because some people may less familiar with syntax for |
||
``` | ||
|
||
- `[in] args`: Initializer list of JavaScript values as `napi_value` representing | ||
the arguments of the function. | ||
|
||
Returns a Value representing the JavaScript object returned by the function. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Returns a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right. Sorry. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NP 🙂 Weird though ... github hasn't updated to show that 😕 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I see - you fixed the ones below, but you missed this one. NP, but please say "`Napi::Value`" rather than merely "`Value`" – that is, please use the name space with the type name. |
||
|
||
### Call | ||
|
||
Calls a Javascript function from a native add-on. | ||
|
||
```cpp | ||
Value Call(const std::vector<napi_value>& args) const; | ||
``` | ||
|
||
- `[in] args`: Vector of JavaScript values as `napi_value` representing the | ||
arguments of the function. | ||
|
||
Returns a `Value` representing the JavaScript value returned by the function. | ||
|
||
### Call | ||
|
||
Calls a Javascript function from a native add-on. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "JavaScript" |
||
|
||
```cpp | ||
Value Call(size_t argc, const napi_value* args) const; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmm, come to think of it, should the return type not also be given as |
||
``` | ||
|
||
- `[in] argc`: The number of the arguments passed to the function. | ||
- `[in] args`: Array of JavaScript values as `napi_value` representing the | ||
arguments of the function. | ||
|
||
Returns a `Value` representing the JavaScript value returned by the function. | ||
|
||
### Call | ||
|
||
Calls a Javascript function from a native add-on. | ||
|
||
```cpp | ||
Value Call(napi_value recv, const std::initializer_list<napi_value>& args) const; | ||
``` | ||
|
||
- `[in] recv`: The `this` object passed to the called function. | ||
- `[in] args`: Initializer list of JavaScript values as `napi_value` representing | ||
the arguments of the function. | ||
|
||
Returns a `Value` representing the JavaScript value returned by the function. | ||
|
||
### Call | ||
|
||
Calls a Javascript function from a native add-on. | ||
|
||
```cpp | ||
Value Call(napi_value recv, const std::vector<napi_value>& args) const; | ||
``` | ||
|
||
- `[in] recv`: The `this` object passed to the called function. | ||
- `[in] args`: Vector of JavaScript values as `napi_value` representing the | ||
arguments of the function. | ||
|
||
Returns a `Value` representing the JavaScript value returned by the function. | ||
|
||
### Call | ||
|
||
Calls a Javascript function from a native add-on. | ||
|
||
```cpp | ||
Value Call(napi_value recv, size_t argc, const napi_value* args) const; | ||
``` | ||
|
||
- `[in] recv`: The `this` object passed to the called function. | ||
- `[in] argc`: The number of the arguments passed to the function. | ||
- `[in] args`: Array of JavaScript values as `napi_value` representing the | ||
arguments of the function. | ||
|
||
Returns a `Value` representing the JavaScript value returned by the function. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm surprised we don't have overloads which accept an array of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So do I, what do you think if I open an issue about that? Just to remember this and maybe after the documentation I can try to work on that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From what I recall, these were my reasons for not adding such an overload:
But I can understand why the lack of such an overload could be unexpected, given the pattern of most other wrapper functions that take There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good points. Thanks for sharing this. |
||
### MakeCallback | ||
|
||
Calls a Javascript function from a native add-on after an asynchronous operation. | ||
|
||
```cpp | ||
Value MakeCallback(napi_value recv, const std::initializer_list<napi_value>& args) const; | ||
``` | ||
|
||
- `[in] recv`: The `this` object passed to the called function. | ||
- `[in] args`: Initializer list of JavaScript values as `napi_value` representing | ||
the arguments of the function. | ||
|
||
Returns a `Value` representing the JavaScript value returned by the function. | ||
|
||
### MakeCallback | ||
|
||
Calls a Javascript function from a native add-on after an asynchronous operation. | ||
|
||
```cpp | ||
Value MakeCallback(napi_value recv, const std::vector<napi_value>& args) const; | ||
``` | ||
|
||
- `[in] recv`: The `this` object passed to the called function. | ||
- `[in] args`: List of JavaScript values as `napi_value` representing the | ||
arguments of the function. | ||
|
||
Returns a `Value` representing the JavaScript value returned by the function. | ||
|
||
### MakeCallback | ||
|
||
Calls a Javascript function from a native add-on after an asynchronous operation. | ||
|
||
```cpp | ||
Value MakeCallback(napi_value recv, size_t argc, const napi_value* args) const; | ||
``` | ||
|
||
- `[in] recv`: The `this` object passed to the called function. | ||
- `[in] argc`: The number of the arguments passed to the function. | ||
- `[in] args`: Array of JavaScript values as `napi_value` representing the | ||
arguments of the function. | ||
|
||
Returns a `Value` representing the JavaScript value returned by the function. | ||
|
||
## Operator | ||
|
||
```cpp | ||
Value operator ()(const std::initializer_list<napi_value>& args) const; | ||
``` | ||
|
||
- `[in] args`: Initializer list of JavaScript values as `napi_value`. | ||
|
||
Returns a `Value` representing the JavaScript value returned by the function. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"... a set of methods
to createfor creating ..."