From 4ea4019a3bed62ef54a6e6a404a4bb479bf30201 Mon Sep 17 00:00:00 2001 From: NickNaso Date: Tue, 18 Sep 2018 01:04:29 +0200 Subject: [PATCH 1/3] Added Call and MakeCallback that accept cargs as agruments --- napi-inl.h | 20 ++++++++++++++++++++ napi.h | 2 ++ 2 files changed, 22 insertions(+) diff --git a/napi-inl.h b/napi-inl.h index 59362e704..6e397aa5c 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -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& args) const { EscapableHandleScope scope(_env); @@ -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& args) const { EscapableHandleScope scope(_env); return scope.Escape(Value().New(args)).As(); diff --git a/napi.h b/napi.h index 531d1c358..e510a8273 100644 --- a/napi.h +++ b/napi.h @@ -1036,9 +1036,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; From 7f74b680296976e737c56897716ab9f3cbe07a1d Mon Sep 17 00:00:00 2001 From: NickNaso Date: Tue, 18 Sep 2018 23:30:24 +0200 Subject: [PATCH 2/3] Updated documentation for FunctionReference --- doc/function_reference.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/doc/function_reference.md b/doc/function_reference.md index e555f8b04..44487b512 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 the 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 the 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 From 280a05b71732534c1c5c4345cc754ba4498ea4b4 Mon Sep 17 00:00:00 2001 From: NickNaso Date: Wed, 19 Sep 2018 13:16:46 +0200 Subject: [PATCH 3/3] Fixed typos --- doc/function_reference.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/function_reference.md b/doc/function_reference.md index 44487b512..356fe1d93 100644 --- a/doc/function_reference.md +++ b/doc/function_reference.md @@ -150,14 +150,14 @@ function. ### Call -Calls a referenced Javascript function from a native add-on. +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 the arguments passed to the referenced function. +- `[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. @@ -199,7 +199,7 @@ function. ### MakeCallback -Calls a referenced Javascript function from a native add-on after an asynchronous +Calls a referenced JavaScript function from a native add-on after an asynchronous operation. ```cpp @@ -207,7 +207,7 @@ Napi::Value Napi::FunctionReference::MakeCallback(napi_value recv, size_t argc, ``` - `[in] recv`: The `this` object passed to the referenced function when it's called. -- `[in] argc`: The number of the arguments passed to the referenced function. +- `[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.