From e011720010af26ed66638ceac822e5f1c5e43cde Mon Sep 17 00:00:00 2001 From: Vladimir Morozov Date: Tue, 7 May 2024 12:01:13 -0700 Subject: [PATCH] fix compilation for Visual Studio 2022 (#1492) * fix compilation for Visual Studio 2022 --- .gitignore | 11 +++++++++++ napi-inl.h | 5 ++++- napi.h | 12 ++++++------ test/async_worker.cc | 22 ++++++++++++---------- 4 files changed, 33 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index bcded4821..f6b80ae92 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,14 @@ # ignore node-gyp generated files outside its build directory /test/*.Makefile /test/*.mk + +# ignore node-gyp generated Visual Studio files +*.vcxproj +*.vcxproj.filters +*.vcxproj.user +*.vsidx +*.sln +*.suo +/test/.vs/ +/test/Release/ +/test/Debug/ diff --git a/napi-inl.h b/napi-inl.h index a5ae7af72..40e7d4208 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -4891,7 +4891,10 @@ inline napi_value ObjectWrap::WrappedMethod( napi_env env, napi_callback_info info) NAPI_NOEXCEPT { return details::WrapCallback([&] { const CallbackInfo cbInfo(env, info); - method(cbInfo, cbInfo[0]); + // MSVC requires to copy 'method' function pointer to a local variable + // before invoking it. + auto m = method; + m(cbInfo, cbInfo[0]); return nullptr; }); } diff --git a/napi.h b/napi.h index 9f20cb884..0b1fe170a 100644 --- a/napi.h +++ b/napi.h @@ -1268,7 +1268,7 @@ class TypedArrayOf : public TypedArray { napi_typedarray_type type = TypedArray::TypedArrayTypeForPrimitiveType() #else - napi_typedarray_type type + napi_typedarray_type type #endif ///< Type of array, if different from the default array type for the ///< template parameter T. @@ -1291,7 +1291,7 @@ class TypedArrayOf : public TypedArray { napi_typedarray_type type = TypedArray::TypedArrayTypeForPrimitiveType() #else - napi_typedarray_type type + napi_typedarray_type type #endif ///< Type of array, if different from the default array type for the ///< template parameter T. @@ -1381,8 +1381,8 @@ class DataView : public Object { template void WriteData(size_t byteOffset, T value) const; - void* _data; - size_t _length; + void* _data{}; + size_t _length{}; }; class Function : public Object { @@ -1715,7 +1715,7 @@ FunctionReference Persistent(Function value); /// /// Following C++ statements will not be executed. The exception will bubble /// up as a C++ exception of type `Napi::Error`, until it is either caught -/// while still in C++, or else automatically propataged as a JavaScript +/// while still in C++, or else automatically propagated as a JavaScript /// exception when the callback returns to JavaScript. /// /// #### Example 2A - Propagating a Node-API C++ exception: @@ -1888,7 +1888,7 @@ class CallbackInfo { napi_value _this; size_t _argc; napi_value* _argv; - napi_value _staticArgs[6]; + napi_value _staticArgs[6]{}; napi_value* _dynamicArgs; void* _data; }; diff --git a/test/async_worker.cc b/test/async_worker.cc index 972b7b2eb..c0a20cb5c 100644 --- a/test/async_worker.cc +++ b/test/async_worker.cc @@ -51,15 +51,17 @@ class TestWorkerWithUserDefRecv : public AsyncWorker { }; // Using default std::allocator impl, but assuming user can define their own -// alloc/dealloc methods +// allocate/deallocate methods class CustomAllocWorker : public AsyncWorker { + using Allocator = std::allocator; + public: CustomAllocWorker(Function& cb) : AsyncWorker(cb){}; static void DoWork(const CallbackInfo& info) { Function cb = info[0].As(); - std::allocator create_alloc; - CustomAllocWorker* newWorker = create_alloc.allocate(1); - create_alloc.construct(newWorker, cb); + Allocator allocator; + CustomAllocWorker* newWorker = allocator.allocate(1); + std::allocator_traits::construct(allocator, newWorker, cb); newWorker->Queue(); } @@ -67,9 +69,9 @@ class CustomAllocWorker : public AsyncWorker { void Execute() override {} void Destroy() override { assert(this->_secretVal == 24); - std::allocator deallocer; - deallocer.destroy(this); - deallocer.deallocate(this, 1); + Allocator allocator; + std::allocator_traits::destroy(allocator, this); + allocator.deallocate(this, 1); } private: @@ -108,7 +110,7 @@ class TestWorker : public AsyncWorker { : AsyncWorker(cb, resource_name, resource) {} TestWorker(Function cb, const char* resource_name) : AsyncWorker(cb, resource_name) {} - bool _succeed; + bool _succeed{}; }; class TestWorkerWithResult : public AsyncWorker { @@ -143,7 +145,7 @@ class TestWorkerWithResult : public AsyncWorker { const char* resource_name, const Object& resource) : AsyncWorker(cb, resource_name, resource) {} - bool _succeed; + bool _succeed{}; }; class TestWorkerNoCallback : public AsyncWorker { @@ -194,7 +196,7 @@ class TestWorkerNoCallback : public AsyncWorker { : AsyncWorker(env, resource_name, resource), _deferred(Napi::Promise::Deferred::New(env)) {} Promise::Deferred _deferred; - bool _succeed; + bool _succeed{}; }; class EchoWorker : public AsyncWorker {