Skip to content

Commit

Permalink
fix compilation for Visual Studio 2022 (#1492)
Browse files Browse the repository at this point in the history
* fix compilation for Visual Studio 2022
  • Loading branch information
vmoroz authored May 7, 2024
1 parent 4a9d74b commit e011720
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/
5 changes: 4 additions & 1 deletion napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -4891,7 +4891,10 @@ inline napi_value ObjectWrap<T>::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;
});
}
Expand Down
12 changes: 6 additions & 6 deletions napi.h
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,7 @@ class TypedArrayOf : public TypedArray {
napi_typedarray_type type =
TypedArray::TypedArrayTypeForPrimitiveType<T>()
#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.
Expand All @@ -1291,7 +1291,7 @@ class TypedArrayOf : public TypedArray {
napi_typedarray_type type =
TypedArray::TypedArrayTypeForPrimitiveType<T>()
#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.
Expand Down Expand Up @@ -1381,8 +1381,8 @@ class DataView : public Object {
template <typename T>
void WriteData(size_t byteOffset, T value) const;

void* _data;
size_t _length;
void* _data{};
size_t _length{};
};

class Function : public Object {
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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;
};
Expand Down
22 changes: 12 additions & 10 deletions test/async_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,27 @@ 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<CustomAllocWorker>;

public:
CustomAllocWorker(Function& cb) : AsyncWorker(cb){};
static void DoWork(const CallbackInfo& info) {
Function cb = info[0].As<Function>();
std::allocator<CustomAllocWorker> create_alloc;
CustomAllocWorker* newWorker = create_alloc.allocate(1);
create_alloc.construct(newWorker, cb);
Allocator allocator;
CustomAllocWorker* newWorker = allocator.allocate(1);
std::allocator_traits<Allocator>::construct(allocator, newWorker, cb);
newWorker->Queue();
}

protected:
void Execute() override {}
void Destroy() override {
assert(this->_secretVal == 24);
std::allocator<CustomAllocWorker> deallocer;
deallocer.destroy(this);
deallocer.deallocate(this, 1);
Allocator allocator;
std::allocator_traits<Allocator>::destroy(allocator, this);
allocator.deallocate(this, 1);
}

private:
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit e011720

Please sign in to comment.