diff --git a/doc/function_reference.md b/doc/function_reference.md index e555f8b04..356fe1d93 100644 --- a/doc/function_reference.md +++ b/doc/function_reference.md @@ -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 @@ -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 diff --git a/napi-inl.h b/napi-inl.h index 5846cfb7b..a4b1d426b 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -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& args) const { EscapableHandleScope scope(_env); @@ -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& args) const { EscapableHandleScope scope(_env); return scope.Escape(Value().New(args)).As(); diff --git a/napi.h b/napi.h index 8085617f2..2e3753091 100644 --- a/napi.h +++ b/napi.h @@ -1097,9 +1097,11 @@ namespace Napi { Napi::Value Call(const std::vector& args) const; Napi::Value Call(napi_value recv, const std::initializer_list& args) const; Napi::Value Call(napi_value recv, const std::vector& 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& args) const; Napi::Value MakeCallback(napi_value recv, const std::vector& args) const; + Napi::Value MakeCallback(napi_value recv, size_t argc, const napi_value* args) const; Object New(const std::initializer_list& args) const; Object New(const std::vector& args) const;