Skip to content

Commit e011720

Browse files
authored
fix compilation for Visual Studio 2022 (#1492)
* fix compilation for Visual Studio 2022
1 parent 4a9d74b commit e011720

File tree

4 files changed

+33
-17
lines changed

4 files changed

+33
-17
lines changed

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,14 @@
88
# ignore node-gyp generated files outside its build directory
99
/test/*.Makefile
1010
/test/*.mk
11+
12+
# ignore node-gyp generated Visual Studio files
13+
*.vcxproj
14+
*.vcxproj.filters
15+
*.vcxproj.user
16+
*.vsidx
17+
*.sln
18+
*.suo
19+
/test/.vs/
20+
/test/Release/
21+
/test/Debug/

napi-inl.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4891,7 +4891,10 @@ inline napi_value ObjectWrap<T>::WrappedMethod(
48914891
napi_env env, napi_callback_info info) NAPI_NOEXCEPT {
48924892
return details::WrapCallback([&] {
48934893
const CallbackInfo cbInfo(env, info);
4894-
method(cbInfo, cbInfo[0]);
4894+
// MSVC requires to copy 'method' function pointer to a local variable
4895+
// before invoking it.
4896+
auto m = method;
4897+
m(cbInfo, cbInfo[0]);
48954898
return nullptr;
48964899
});
48974900
}

napi.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,7 +1268,7 @@ class TypedArrayOf : public TypedArray {
12681268
napi_typedarray_type type =
12691269
TypedArray::TypedArrayTypeForPrimitiveType<T>()
12701270
#else
1271-
napi_typedarray_type type
1271+
napi_typedarray_type type
12721272
#endif
12731273
///< Type of array, if different from the default array type for the
12741274
///< template parameter T.
@@ -1291,7 +1291,7 @@ class TypedArrayOf : public TypedArray {
12911291
napi_typedarray_type type =
12921292
TypedArray::TypedArrayTypeForPrimitiveType<T>()
12931293
#else
1294-
napi_typedarray_type type
1294+
napi_typedarray_type type
12951295
#endif
12961296
///< Type of array, if different from the default array type for the
12971297
///< template parameter T.
@@ -1381,8 +1381,8 @@ class DataView : public Object {
13811381
template <typename T>
13821382
void WriteData(size_t byteOffset, T value) const;
13831383

1384-
void* _data;
1385-
size_t _length;
1384+
void* _data{};
1385+
size_t _length{};
13861386
};
13871387

13881388
class Function : public Object {
@@ -1715,7 +1715,7 @@ FunctionReference Persistent(Function value);
17151715
///
17161716
/// Following C++ statements will not be executed. The exception will bubble
17171717
/// up as a C++ exception of type `Napi::Error`, until it is either caught
1718-
/// while still in C++, or else automatically propataged as a JavaScript
1718+
/// while still in C++, or else automatically propagated as a JavaScript
17191719
/// exception when the callback returns to JavaScript.
17201720
///
17211721
/// #### Example 2A - Propagating a Node-API C++ exception:
@@ -1888,7 +1888,7 @@ class CallbackInfo {
18881888
napi_value _this;
18891889
size_t _argc;
18901890
napi_value* _argv;
1891-
napi_value _staticArgs[6];
1891+
napi_value _staticArgs[6]{};
18921892
napi_value* _dynamicArgs;
18931893
void* _data;
18941894
};

test/async_worker.cc

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,27 @@ class TestWorkerWithUserDefRecv : public AsyncWorker {
5151
};
5252

5353
// Using default std::allocator impl, but assuming user can define their own
54-
// alloc/dealloc methods
54+
// allocate/deallocate methods
5555
class CustomAllocWorker : public AsyncWorker {
56+
using Allocator = std::allocator<CustomAllocWorker>;
57+
5658
public:
5759
CustomAllocWorker(Function& cb) : AsyncWorker(cb){};
5860
static void DoWork(const CallbackInfo& info) {
5961
Function cb = info[0].As<Function>();
60-
std::allocator<CustomAllocWorker> create_alloc;
61-
CustomAllocWorker* newWorker = create_alloc.allocate(1);
62-
create_alloc.construct(newWorker, cb);
62+
Allocator allocator;
63+
CustomAllocWorker* newWorker = allocator.allocate(1);
64+
std::allocator_traits<Allocator>::construct(allocator, newWorker, cb);
6365
newWorker->Queue();
6466
}
6567

6668
protected:
6769
void Execute() override {}
6870
void Destroy() override {
6971
assert(this->_secretVal == 24);
70-
std::allocator<CustomAllocWorker> deallocer;
71-
deallocer.destroy(this);
72-
deallocer.deallocate(this, 1);
72+
Allocator allocator;
73+
std::allocator_traits<Allocator>::destroy(allocator, this);
74+
allocator.deallocate(this, 1);
7375
}
7476

7577
private:
@@ -108,7 +110,7 @@ class TestWorker : public AsyncWorker {
108110
: AsyncWorker(cb, resource_name, resource) {}
109111
TestWorker(Function cb, const char* resource_name)
110112
: AsyncWorker(cb, resource_name) {}
111-
bool _succeed;
113+
bool _succeed{};
112114
};
113115

114116
class TestWorkerWithResult : public AsyncWorker {
@@ -143,7 +145,7 @@ class TestWorkerWithResult : public AsyncWorker {
143145
const char* resource_name,
144146
const Object& resource)
145147
: AsyncWorker(cb, resource_name, resource) {}
146-
bool _succeed;
148+
bool _succeed{};
147149
};
148150

149151
class TestWorkerNoCallback : public AsyncWorker {
@@ -194,7 +196,7 @@ class TestWorkerNoCallback : public AsyncWorker {
194196
: AsyncWorker(env, resource_name, resource),
195197
_deferred(Napi::Promise::Deferred::New(env)) {}
196198
Promise::Deferred _deferred;
197-
bool _succeed;
199+
bool _succeed{};
198200
};
199201

200202
class EchoWorker : public AsyncWorker {

0 commit comments

Comments
 (0)