From ca1db22c95f3fff2ff9498cde967b279d20eac66 Mon Sep 17 00:00:00 2001 From: John French Date: Mon, 2 Apr 2018 20:13:41 -0400 Subject: [PATCH] don't try to escape null If an exception is pending from Call Or MakeCallback on FunctionReference the return value may be nullptr. Check for a pending exception and don't try to escape in these cases. PR-URL: https://github.com/nodejs/node-addon-api/pull/245 Refs: https://github.com/nodejs/node-addon-api/issues/233 Reviewed-By: Hitesh Kanwathirtha --- napi-inl.h | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/napi-inl.h b/napi-inl.h index b3ac0ec..884f8cb 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -2285,36 +2285,60 @@ inline Napi::Value FunctionReference::operator ()( inline Napi::Value FunctionReference::Call(const std::initializer_list& args) const { EscapableHandleScope scope(_env); - return scope.Escape(Value().Call(args)); + Napi::Value result = Value().Call(args); + if (scope.Env().IsExceptionPending()) { + return Value(); + } + return scope.Escape(result); } inline Napi::Value FunctionReference::Call(const std::vector& args) const { EscapableHandleScope scope(_env); - return scope.Escape(Value().Call(args)); + Napi::Value result = Value().Call(args); + if (scope.Env().IsExceptionPending()) { + return Value(); + } + return scope.Escape(result); } inline Napi::Value FunctionReference::Call( napi_value recv, const std::initializer_list& args) const { EscapableHandleScope scope(_env); - return scope.Escape(Value().Call(recv, args)); + Napi::Value result = Value().Call(recv, args); + if (scope.Env().IsExceptionPending()) { + return Value(); + } + return scope.Escape(result); } inline Napi::Value FunctionReference::Call( napi_value recv, const std::vector& args) const { EscapableHandleScope scope(_env); - return scope.Escape(Value().Call(recv, args)); + Napi::Value result = Value().Call(recv, 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); - return scope.Escape(Value().MakeCallback(recv, args)); + Napi::Value result = Value().MakeCallback(recv, args); + if (scope.Env().IsExceptionPending()) { + return Value(); + } + return scope.Escape(result); } inline Napi::Value FunctionReference::MakeCallback( napi_value recv, const std::vector& args) const { EscapableHandleScope scope(_env); - return scope.Escape(Value().MakeCallback(recv, args)); + Napi::Value result = Value().MakeCallback(recv, args); + if (scope.Env().IsExceptionPending()) { + return Value(); + } + return scope.Escape(result); } inline Object FunctionReference::New(const std::initializer_list& args) const {