Skip to content

Commit

Permalink
src: add Call and MakeCallback that accept cargs
Browse files Browse the repository at this point in the history
PR-URL: #344
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
  • Loading branch information
NickNaso authored and mhdawson committed Sep 20, 2018
1 parent 73fed84 commit 97c4ab5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
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 @@ -2405,6 +2405,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 @@ -2425,6 +2435,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 @@ -1097,9 +1097,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

0 comments on commit 97c4ab5

Please sign in to comment.