From 97736c93f47d2271fb91255d633d7031b533a964 Mon Sep 17 00:00:00 2001 From: Vladimir Morozov Date: Sat, 3 Dec 2022 23:21:59 -0800 Subject: [PATCH] src: fix errors and warnings in VS 2017 - fix errors and warnings in VS 2017 - remove incorrect use of static keyword PR-URL: https://github.com/nodejs/node-addon-api/pull/1245 Reviewed-By: Kevin Eady Reviewed-By: Michael Dawson -static inline napi_status AttachData(napi_env env, - napi_value obj, - FreeType* data, - napi_finalize finalizer = nullptr, - void* hint = nullptr) { +inline napi_status AttachData(napi_env env, + napi_value obj, + FreeType* data, + napi_finalize finalizer = nullptr, + void* hint = nullptr) { napi_status status; if (finalizer == nullptr) { finalizer = [](napi_env /*env*/, void* data, void* /*hint*/) { @@ -134,8 +134,8 @@ struct CallbackData { }; template -static napi_value TemplatedVoidCallback(napi_env env, - napi_callback_info info) NAPI_NOEXCEPT { +napi_value TemplatedVoidCallback(napi_env env, + napi_callback_info info) NAPI_NOEXCEPT { return details::WrapCallback([&] { CallbackInfo cbInfo(env, info); Callback(cbInfo); @@ -144,8 +144,8 @@ static napi_value TemplatedVoidCallback(napi_env env, } template -static napi_value TemplatedCallback(napi_env env, - napi_callback_info info) NAPI_NOEXCEPT { +napi_value TemplatedCallback(napi_env env, + napi_callback_info info) NAPI_NOEXCEPT { return details::WrapCallback([&] { CallbackInfo cbInfo(env, info); return Callback(cbInfo); @@ -154,8 +154,8 @@ static napi_value TemplatedCallback(napi_env env, template -static napi_value TemplatedInstanceCallback( - napi_env env, napi_callback_info info) NAPI_NOEXCEPT { +napi_value TemplatedInstanceCallback(napi_env env, + napi_callback_info info) NAPI_NOEXCEPT { return details::WrapCallback([&] { CallbackInfo cbInfo(env, info); T* instance = T::Unwrap(cbInfo.This().As()); @@ -164,8 +164,8 @@ static napi_value TemplatedInstanceCallback( } template -static napi_value TemplatedInstanceVoidCallback( - napi_env env, napi_callback_info info) NAPI_NOEXCEPT { +napi_value TemplatedInstanceVoidCallback(napi_env env, napi_callback_info info) + NAPI_NOEXCEPT { return details::WrapCallback([&] { CallbackInfo cbInfo(env, info); T* instance = T::Unwrap(cbInfo.This().As()); @@ -2177,11 +2177,11 @@ inline const T* TypedArrayOf::Data() const { //////////////////////////////////////////////////////////////////////////////// template -static inline napi_status CreateFunction(napi_env env, - const char* utf8name, - napi_callback cb, - CbData* data, - napi_value* result) { +inline napi_status CreateFunction(napi_env env, + const char* utf8name, + napi_callback cb, + CbData* data, + napi_value* result) { napi_status status = napi_create_function(env, utf8name, NAPI_AUTO_LENGTH, cb, data, result); if (status == napi_ok) { diff --git a/test/addon_data.cc b/test/addon_data.cc index c721aa608..bb650b5b2 100644 --- a/test/addon_data.cc +++ b/test/addon_data.cc @@ -59,7 +59,7 @@ class Addon { static void DeleteAddon(Napi::Env, Addon* addon, uint32_t* hint) { delete addon; - fprintf(stderr, "hint: %d\n", *hint); + fprintf(stderr, "hint: %u\n", *hint); delete hint; } diff --git a/test/async_worker.cc b/test/async_worker.cc index 426b4e8cf..520fc8b5e 100644 --- a/test/async_worker.cc +++ b/test/async_worker.cc @@ -152,7 +152,7 @@ class FailCancelWorker : public AsyncWorker { #ifdef NAPI_CPP_EXCEPTIONS try { cancelWorker->Cancel(); - } catch (Napi::Error& e) { + } catch (Napi::Error&) { Napi::Error::New(info.Env(), "Unable to cancel async worker tasks") .ThrowAsJavaScriptException(); } @@ -193,7 +193,7 @@ class CancelWorker : public AsyncWorker { #ifdef NAPI_CPP_EXCEPTIONS try { cancelWorker->Cancel(); - } catch (Napi::Error& e) { + } catch (Napi::Error&) { Napi::Error::New(info.Env(), "Unable to cancel async worker tasks") .ThrowAsJavaScriptException(); } diff --git a/test/binding.gyp b/test/binding.gyp index b6aca5710..9222fe014 100644 --- a/test/binding.gyp +++ b/test/binding.gyp @@ -50,7 +50,7 @@ 'object/subscript_operator.cc', 'promise.cc', 'run_script.cc', - "symbol.cc", + 'symbol.cc', 'threadsafe_function/threadsafe_function_ctx.cc', 'threadsafe_function/threadsafe_function_existing_tsfn.cc', 'threadsafe_function/threadsafe_function_ptr.cc', diff --git a/test/function_reference.cc b/test/function_reference.cc index a25119846..99f560f27 100644 --- a/test/function_reference.cc +++ b/test/function_reference.cc @@ -82,16 +82,16 @@ Value CallWithRecvVector(const CallbackInfo& info) { Value CallWithRecvArgc(const CallbackInfo& info) { HandleScope scope(info.Env()); FunctionReference ref; - int argLength = info.Length() - 2; - napi_value* args = new napi_value[argLength]; ref.Reset(info[0].As()); - int argIdx = 0; - for (int i = 2; i < (int)info.Length(); i++, argIdx++) { - args[argIdx] = info[i]; + size_t argLength = info.Length() > 2 ? info.Length() - 2 : 0; + std::unique_ptr args{argLength > 0 ? new napi_value[argLength] + : nullptr}; + for (size_t i = 0; i < argLength; ++i) { + args[i] = info[i + 2]; } - return MaybeUnwrap(ref.Call(info[1], argLength, args)); + return MaybeUnwrap(ref.Call(info[1], argLength, args.get())); } Value MakeAsyncCallbackWithInitList(const Napi::CallbackInfo& info) { @@ -121,17 +121,19 @@ Value MakeAsyncCallbackWithVector(const Napi::CallbackInfo& info) { Value MakeAsyncCallbackWithArgv(const Napi::CallbackInfo& info) { Napi::FunctionReference ref; ref.Reset(info[0].As()); - int argLength = info.Length() - 1; - napi_value* args = new napi_value[argLength]; - int argIdx = 0; - for (int i = 1; i < (int)info.Length(); i++, argIdx++) { - args[argIdx] = info[i]; + size_t argLength = info.Length() > 1 ? info.Length() - 1 : 0; + std::unique_ptr args{argLength > 0 ? new napi_value[argLength] + : nullptr}; + for (size_t i = 0; i < argLength; ++i) { + args[i] = info[i + 1]; } Napi::AsyncContext context(info.Env(), "func_ref_resources", {}); - return MaybeUnwrap(ref.MakeCallback( - Napi::Object::New(info.Env()), argLength, args, context)); + return MaybeUnwrap(ref.MakeCallback(Napi::Object::New(info.Env()), + argLength, + argLength > 0 ? args.get() : nullptr, + context)); } Value CreateFunctionReferenceUsingNew(const Napi::CallbackInfo& info) { diff --git a/test/threadsafe_function/threadsafe_function.cc b/test/threadsafe_function/threadsafe_function.cc index c6eddc4de..701f71fe1 100644 --- a/test/threadsafe_function/threadsafe_function.cc +++ b/test/threadsafe_function/threadsafe_function.cc @@ -12,7 +12,7 @@ constexpr size_t ARRAY_LENGTH = 10; constexpr size_t MAX_QUEUE_SIZE = 2; static std::thread threads[2]; -static ThreadSafeFunction tsfn; +static ThreadSafeFunction s_tsfn; struct ThreadSafeFunctionInfo { enum CallType { DEFAULT, BLOCKING, NON_BLOCKING } type; @@ -29,17 +29,17 @@ struct ThreadSafeFunctionInfo { static int ints[ARRAY_LENGTH]; static void SecondaryThread() { - if (tsfn.Release() != napi_ok) { + if (s_tsfn.Release() != napi_ok) { Error::Fatal("SecondaryThread", "ThreadSafeFunction.Release() failed"); } } // Source thread producing the data static void DataSourceThread() { - ThreadSafeFunctionInfo* info = tsfn.GetContext(); + ThreadSafeFunctionInfo* info = s_tsfn.GetContext(); if (info->startSecondary) { - if (tsfn.Acquire() != napi_ok) { + if (s_tsfn.Acquire() != napi_ok) { Error::Fatal("DataSourceThread", "ThreadSafeFunction.Acquire() failed"); } @@ -56,13 +56,13 @@ static void DataSourceThread() { switch (info->type) { case ThreadSafeFunctionInfo::DEFAULT: - status = tsfn.BlockingCall(); + status = s_tsfn.BlockingCall(); break; case ThreadSafeFunctionInfo::BLOCKING: - status = tsfn.BlockingCall(&ints[index], callback); + status = s_tsfn.BlockingCall(&ints[index], callback); break; case ThreadSafeFunctionInfo::NON_BLOCKING: - status = tsfn.NonBlockingCall(&ints[index], callback); + status = s_tsfn.NonBlockingCall(&ints[index], callback); break; } @@ -101,7 +101,7 @@ static void DataSourceThread() { Error::Fatal("DataSourceThread", "Queue was never closing"); } - if (!queueWasClosing && tsfn.Release() != napi_ok) { + if (!queueWasClosing && s_tsfn.Release() != napi_ok) { Error::Fatal("DataSourceThread", "ThreadSafeFunction.Release() failed"); } } @@ -110,9 +110,9 @@ static Value StopThread(const CallbackInfo& info) { tsfnInfo.jsFinalizeCallback = Napi::Persistent(info[0].As()); bool abort = info[1].As(); if (abort) { - tsfn.Abort(); + s_tsfn.Abort(); } else { - tsfn.Release(); + s_tsfn.Release(); } { std::lock_guard _(tsfnInfo.protect); @@ -143,14 +143,14 @@ static Value StartThreadInternal(const CallbackInfo& info, tsfnInfo.maxQueueSize = info[3].As().Uint32Value(); tsfnInfo.closeCalledFromJs = false; - tsfn = ThreadSafeFunction::New(info.Env(), - info[0].As(), - "Test", - tsfnInfo.maxQueueSize, - 2, - &tsfnInfo, - JoinTheThreads, - threads); + s_tsfn = ThreadSafeFunction::New(info.Env(), + info[0].As(), + "Test", + tsfnInfo.maxQueueSize, + 2, + &tsfnInfo, + JoinTheThreads, + threads); threads[0] = std::thread(DataSourceThread); @@ -158,7 +158,7 @@ static Value StartThreadInternal(const CallbackInfo& info, } static Value Release(const CallbackInfo& /* info */) { - if (tsfn.Release() != napi_ok) { + if (s_tsfn.Release() != napi_ok) { Error::Fatal("Release", "ThreadSafeFunction.Release() failed"); } return Value(); diff --git a/test/typed_threadsafe_function/typed_threadsafe_function.cc b/test/typed_threadsafe_function/typed_threadsafe_function.cc index c25268aaf..ce345b8f0 100644 --- a/test/typed_threadsafe_function/typed_threadsafe_function.cc +++ b/test/typed_threadsafe_function/typed_threadsafe_function.cc @@ -40,23 +40,23 @@ static void TSFNCallJS(Env env, } using TSFN = TypedThreadSafeFunction; -static TSFN tsfn; +static TSFN s_tsfn; // Thread data to transmit to JS static int ints[ARRAY_LENGTH]; static void SecondaryThread() { - if (tsfn.Release() != napi_ok) { + if (s_tsfn.Release() != napi_ok) { Error::Fatal("TypedSecondaryThread", "ThreadSafeFunction.Release() failed"); } } // Source thread producing the data static void DataSourceThread() { - ThreadSafeFunctionInfo* info = tsfn.GetContext(); + ThreadSafeFunctionInfo* info = s_tsfn.GetContext(); if (info->startSecondary) { - if (tsfn.Acquire() != napi_ok) { + if (s_tsfn.Acquire() != napi_ok) { Error::Fatal("TypedDataSourceThread", "ThreadSafeFunction.Acquire() failed"); } @@ -71,13 +71,13 @@ static void DataSourceThread() { switch (info->type) { case ThreadSafeFunctionInfo::DEFAULT: - status = tsfn.BlockingCall(); + status = s_tsfn.BlockingCall(); break; case ThreadSafeFunctionInfo::BLOCKING: - status = tsfn.BlockingCall(&ints[index]); + status = s_tsfn.BlockingCall(&ints[index]); break; case ThreadSafeFunctionInfo::NON_BLOCKING: - status = tsfn.NonBlockingCall(&ints[index]); + status = s_tsfn.NonBlockingCall(&ints[index]); break; } @@ -117,7 +117,7 @@ static void DataSourceThread() { Error::Fatal("TypedDataSourceThread", "Queue was never closing"); } - if (!queueWasClosing && tsfn.Release() != napi_ok) { + if (!queueWasClosing && s_tsfn.Release() != napi_ok) { Error::Fatal("TypedDataSourceThread", "ThreadSafeFunction.Release() failed"); } @@ -127,9 +127,9 @@ static Value StopThread(const CallbackInfo& info) { tsfnInfo.jsFinalizeCallback = Napi::Persistent(info[0].As()); bool abort = info[1].As(); if (abort) { - tsfn.Abort(); + s_tsfn.Abort(); } else { - tsfn.Release(); + s_tsfn.Release(); } { std::lock_guard _(tsfnInfo.protect); @@ -160,15 +160,15 @@ static Value StartThreadInternal(const CallbackInfo& info, tsfnInfo.maxQueueSize = info[3].As().Uint32Value(); tsfnInfo.closeCalledFromJs = false; - tsfn = TSFN::New(info.Env(), - info[0].As(), - Object::New(info.Env()), - "Test", - tsfnInfo.maxQueueSize, - 2, - &tsfnInfo, - JoinTheThreads, - threads); + s_tsfn = TSFN::New(info.Env(), + info[0].As(), + Object::New(info.Env()), + "Test", + tsfnInfo.maxQueueSize, + 2, + &tsfnInfo, + JoinTheThreads, + threads); threads[0] = std::thread(DataSourceThread); @@ -176,7 +176,7 @@ static Value StartThreadInternal(const CallbackInfo& info, } static Value Release(const CallbackInfo& /* info */) { - if (tsfn.Release() != napi_ok) { + if (s_tsfn.Release() != napi_ok) { Error::Fatal("Release", "TypedThreadSafeFunction.Release() failed"); } return Value();