Skip to content

Commit 97c4ab5

Browse files
NickNasomhdawson
authored andcommitted
src: add Call and MakeCallback that accept cargs
PR-URL: #344 Reviewed-By: Michael Dawson <[email protected]>
1 parent 73fed84 commit 97c4ab5

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

doc/function_reference.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,23 @@ arguments of the referenced function.
148148
Returns a `Napi::Value` representing the JavaScript object returned by the referenced
149149
function.
150150
151+
### Call
152+
153+
Calls a referenced JavaScript function from a native add-on.
154+
155+
```cpp
156+
Napi::Value Napi::FunctionReference::Call(napi_value recv, size_t argc, const napi_value* args) const;
157+
```
158+
159+
- `[in] recv`: The `this` object passed to the referenced function when it's called.
160+
- `[in] argc`: The number of arguments passed to the referenced function.
161+
- `[in] args`: Array of JavaScript values as `napi_value` representing the
162+
arguments of the referenced function.
163+
164+
Returns a `Napi::Value` representing the JavaScript object returned by the referenced
165+
function.
166+
167+
151168
### MakeCallback
152169

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

200+
### MakeCallback
201+
202+
Calls a referenced JavaScript function from a native add-on after an asynchronous
203+
operation.
204+
205+
```cpp
206+
Napi::Value Napi::FunctionReference::MakeCallback(napi_value recv, size_t argc, const napi_value* args) const;
207+
```
208+
209+
- `[in] recv`: The `this` object passed to the referenced function when it's called.
210+
- `[in] argc`: The number of arguments passed to the referenced function.
211+
- `[in] args`: Array of JavaScript values as `napi_value` representing the
212+
arguments of the referenced function.
213+
214+
Returns a `Napi::Value` representing the JavaScript object returned by the referenced
215+
function.
216+
183217
## Operator
184218
185219
```cpp

napi-inl.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2405,6 +2405,16 @@ inline Napi::Value FunctionReference::Call(
24052405
return scope.Escape(result);
24062406
}
24072407

2408+
inline Napi::Value FunctionReference::Call(
2409+
napi_value recv, size_t argc, const napi_value* args) const {
2410+
EscapableHandleScope scope(_env);
2411+
Napi::Value result = Value().Call(recv, argc, args);
2412+
if (scope.Env().IsExceptionPending()) {
2413+
return Value();
2414+
}
2415+
return scope.Escape(result);
2416+
}
2417+
24082418
inline Napi::Value FunctionReference::MakeCallback(
24092419
napi_value recv, const std::initializer_list<napi_value>& args) const {
24102420
EscapableHandleScope scope(_env);
@@ -2425,6 +2435,16 @@ inline Napi::Value FunctionReference::MakeCallback(
24252435
return scope.Escape(result);
24262436
}
24272437

2438+
inline Napi::Value FunctionReference::MakeCallback(
2439+
napi_value recv, size_t argc, const napi_value* args) const {
2440+
EscapableHandleScope scope(_env);
2441+
Napi::Value result = Value().MakeCallback(recv, argc, args);
2442+
if (scope.Env().IsExceptionPending()) {
2443+
return Value();
2444+
}
2445+
return scope.Escape(result);
2446+
}
2447+
24282448
inline Object FunctionReference::New(const std::initializer_list<napi_value>& args) const {
24292449
EscapableHandleScope scope(_env);
24302450
return scope.Escape(Value().New(args)).As<Object>();

napi.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,9 +1097,11 @@ namespace Napi {
10971097
Napi::Value Call(const std::vector<napi_value>& args) const;
10981098
Napi::Value Call(napi_value recv, const std::initializer_list<napi_value>& args) const;
10991099
Napi::Value Call(napi_value recv, const std::vector<napi_value>& args) const;
1100+
Napi::Value Call(napi_value recv, size_t argc, const napi_value* args) const;
11001101

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

11041106
Object New(const std::initializer_list<napi_value>& args) const;
11051107
Object New(const std::vector<napi_value>& args) const;

0 commit comments

Comments
 (0)