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

Added Call and MakeCallback that accept cargs as agruments #344

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions doc/function_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,23 @@ arguments of the referenced function.
Returns a `Napi::Value` representing the JavaScript object returned by the referenced
function.
### Call
Calls a referenced JavaScript function from a native add-on.
```cpp
Napi::Value Napi::FunctionReference::Call(napi_value recv, size_t argc, const napi_value* args) const;
```

- `[in] recv`: The `this` object passed to the referenced function when it's called.
- `[in] argc`: The number of arguments passed to the referenced function.
- `[in] args`: Array of JavaScript values as `napi_value` representing the
arguments of the referenced function.

Returns a `Napi::Value` representing the JavaScript object returned by the referenced
function.


### MakeCallback

Calls a referenced Javascript function from a native add-on after an asynchronous
Expand Down Expand Up @@ -180,6 +197,23 @@ arguments of the referenced function.
Returns a `Napi::Value` representing the JavaScript object returned by the referenced
function.

### MakeCallback

Calls a referenced JavaScript function from a native add-on after an asynchronous
operation.

```cpp
Napi::Value Napi::FunctionReference::MakeCallback(napi_value recv, size_t argc, const napi_value* args) const;
```
- `[in] recv`: The `this` object passed to the referenced function when it's called.
- `[in] argc`: The number of arguments passed to the referenced function.
- `[in] args`: Array of JavaScript values as `napi_value` representing the
arguments of the referenced function.
Returns a `Napi::Value` representing the JavaScript object returned by the referenced
function.
## Operator
```cpp
Expand Down
20 changes: 20 additions & 0 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2336,6 +2336,16 @@ inline Napi::Value FunctionReference::Call(
return scope.Escape(result);
}

inline Napi::Value FunctionReference::Call(
napi_value recv, size_t argc, const napi_value* args) const {
EscapableHandleScope scope(_env);
Napi::Value result = Value().Call(recv, argc, args);
if (scope.Env().IsExceptionPending()) {
return Value();
}
return scope.Escape(result);
}

inline Napi::Value FunctionReference::MakeCallback(
napi_value recv, const std::initializer_list<napi_value>& args) const {
EscapableHandleScope scope(_env);
Expand All @@ -2356,6 +2366,16 @@ inline Napi::Value FunctionReference::MakeCallback(
return scope.Escape(result);
}

inline Napi::Value FunctionReference::MakeCallback(
napi_value recv, size_t argc, const napi_value* args) const {
EscapableHandleScope scope(_env);
Napi::Value result = Value().MakeCallback(recv, argc, args);
if (scope.Env().IsExceptionPending()) {
return Value();
}
return scope.Escape(result);
}

inline Object FunctionReference::New(const std::initializer_list<napi_value>& args) const {
EscapableHandleScope scope(_env);
return scope.Escape(Value().New(args)).As<Object>();
Expand Down
2 changes: 2 additions & 0 deletions napi.h
Original file line number Diff line number Diff line change
Expand Up @@ -1036,9 +1036,11 @@ namespace Napi {
Napi::Value Call(const std::vector<napi_value>& args) const;
Napi::Value Call(napi_value recv, const std::initializer_list<napi_value>& args) const;
Napi::Value Call(napi_value recv, const std::vector<napi_value>& args) const;
Napi::Value Call(napi_value recv, size_t argc, const napi_value* args) const;

Napi::Value MakeCallback(napi_value recv, const std::initializer_list<napi_value>& args) const;
Napi::Value MakeCallback(napi_value recv, const std::vector<napi_value>& args) const;
Napi::Value MakeCallback(napi_value recv, size_t argc, const napi_value* args) const;

Object New(const std::initializer_list<napi_value>& args) const;
Object New(const std::vector<napi_value>& args) const;
Expand Down