Skip to content

Commit

Permalink
src: fix errors and warnings in VS 2017
Browse files Browse the repository at this point in the history
- fix errors and warnings in VS 2017
- remove incorrect use of static keyword

PR-URL: #1245
Reviewed-By: Kevin Eady <kevin.c.eady@gmail.com>
Reviewed-By: Michael Dawson <midawson@redhat.com
  • Loading branch information
vmoroz authored and mhdawson committed Dec 16, 2022
1 parent ad7ff92 commit 97736c9
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 74 deletions.
36 changes: 18 additions & 18 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ namespace details {
// TODO: Replace this code with `napi_add_finalizer()` whenever it becomes
// available on all supported versions of Node.js.
template <typename FreeType>
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*/) {
Expand Down Expand Up @@ -134,8 +134,8 @@ struct CallbackData<Callable, void> {
};

template <void (*Callback)(const CallbackInfo& info)>
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);
Expand All @@ -144,8 +144,8 @@ static napi_value TemplatedVoidCallback(napi_env env,
}

template <Napi::Value (*Callback)(const CallbackInfo& info)>
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);
Expand All @@ -154,8 +154,8 @@ static napi_value TemplatedCallback(napi_env env,

template <typename T,
Napi::Value (T::*UnwrapCallback)(const CallbackInfo& info)>
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<Object>());
Expand All @@ -164,8 +164,8 @@ static napi_value TemplatedInstanceCallback(
}

template <typename T, void (T::*UnwrapCallback)(const CallbackInfo& info)>
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<Object>());
Expand Down Expand Up @@ -2177,11 +2177,11 @@ inline const T* TypedArrayOf<T>::Data() const {
////////////////////////////////////////////////////////////////////////////////

template <typename CbData>
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) {
Expand Down
2 changes: 1 addition & 1 deletion test/addon_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions test/async_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion test/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
28 changes: 15 additions & 13 deletions test/function_reference.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<Function>());

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<napi_value[]> 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) {
Expand Down Expand Up @@ -121,17 +121,19 @@ Value MakeAsyncCallbackWithVector(const Napi::CallbackInfo& info) {
Value MakeAsyncCallbackWithArgv(const Napi::CallbackInfo& info) {
Napi::FunctionReference ref;
ref.Reset(info[0].As<Function>());
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<napi_value[]> 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) {
Expand Down
38 changes: 19 additions & 19 deletions test/threadsafe_function/threadsafe_function.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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");
}
}
Expand All @@ -110,9 +110,9 @@ static Value StopThread(const CallbackInfo& info) {
tsfnInfo.jsFinalizeCallback = Napi::Persistent(info[0].As<Function>());
bool abort = info[1].As<Boolean>();
if (abort) {
tsfn.Abort();
s_tsfn.Abort();
} else {
tsfn.Release();
s_tsfn.Release();
}
{
std::lock_guard<std::mutex> _(tsfnInfo.protect);
Expand Down Expand Up @@ -143,22 +143,22 @@ static Value StartThreadInternal(const CallbackInfo& info,
tsfnInfo.maxQueueSize = info[3].As<Number>().Uint32Value();
tsfnInfo.closeCalledFromJs = false;

tsfn = ThreadSafeFunction::New(info.Env(),
info[0].As<Function>(),
"Test",
tsfnInfo.maxQueueSize,
2,
&tsfnInfo,
JoinTheThreads,
threads);
s_tsfn = ThreadSafeFunction::New(info.Env(),
info[0].As<Function>(),
"Test",
tsfnInfo.maxQueueSize,
2,
&tsfnInfo,
JoinTheThreads,
threads);

threads[0] = std::thread(DataSourceThread);

return Value();
}

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();
Expand Down
40 changes: 20 additions & 20 deletions test/typed_threadsafe_function/typed_threadsafe_function.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,23 @@ static void TSFNCallJS(Env env,
}

using TSFN = TypedThreadSafeFunction<ThreadSafeFunctionInfo, int, TSFNCallJS>;
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");
}
Expand All @@ -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;
}

Expand Down Expand Up @@ -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");
}
Expand All @@ -127,9 +127,9 @@ static Value StopThread(const CallbackInfo& info) {
tsfnInfo.jsFinalizeCallback = Napi::Persistent(info[0].As<Function>());
bool abort = info[1].As<Boolean>();
if (abort) {
tsfn.Abort();
s_tsfn.Abort();
} else {
tsfn.Release();
s_tsfn.Release();
}
{
std::lock_guard<std::mutex> _(tsfnInfo.protect);
Expand Down Expand Up @@ -160,23 +160,23 @@ static Value StartThreadInternal(const CallbackInfo& info,
tsfnInfo.maxQueueSize = info[3].As<Number>().Uint32Value();
tsfnInfo.closeCalledFromJs = false;

tsfn = TSFN::New(info.Env(),
info[0].As<Function>(),
Object::New(info.Env()),
"Test",
tsfnInfo.maxQueueSize,
2,
&tsfnInfo,
JoinTheThreads,
threads);
s_tsfn = TSFN::New(info.Env(),
info[0].As<Function>(),
Object::New(info.Env()),
"Test",
tsfnInfo.maxQueueSize,
2,
&tsfnInfo,
JoinTheThreads,
threads);

threads[0] = std::thread(DataSourceThread);

return Value();
}

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();
Expand Down

0 comments on commit 97736c9

Please sign in to comment.