From 92d27b8b2dda2ee55aba9937be6e28c871336460 Mon Sep 17 00:00:00 2001 From: toyobayashi Date: Wed, 8 Feb 2023 22:09:07 +0800 Subject: [PATCH 1/5] src: define `NAPI_HAS_THREADS` --- napi-inl.h | 14 +++++++++----- napi.h | 18 +++++++++++++++--- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/napi-inl.h b/napi-inl.h index 3eab2f32e..0227c28ef 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -201,7 +201,7 @@ struct FinalizeData { Hint* hint; }; -#if (NAPI_VERSION > 3 && !defined(__wasm32__)) +#if (NAPI_VERSION > 3 && NAPI_HAS_THREADS) template , typename FinalizerDataType = void> @@ -295,7 +295,7 @@ napi_value DefaultCallbackWrapper(napi_env env, Napi::Function cb) { return cb; } #endif // NAPI_VERSION > 4 -#endif // NAPI_VERSION > 3 && !defined(__wasm32__) +#endif // NAPI_VERSION > 3 && NAPI_HAS_THREADS template struct AccessorCallbackData { @@ -4649,7 +4649,7 @@ inline Value EscapableHandleScope::Escape(napi_value escapee) { return Value(_env, result); } -#if (NAPI_VERSION > 2) +#if (NAPI_VERSION > 2 && !defined(__wasm__)) //////////////////////////////////////////////////////////////////////////////// // CallbackScope class //////////////////////////////////////////////////////////////////////////////// @@ -4733,6 +4733,8 @@ inline Napi::Env AsyncContext::Env() const { // AsyncWorker class //////////////////////////////////////////////////////////////////////////////// +#if NAPI_HAS_THREADS + inline AsyncWorker::AsyncWorker(const Function& callback) : AsyncWorker(callback, "generic") {} @@ -4911,7 +4913,9 @@ inline void AsyncWorker::OnWorkComplete(Napi::Env /*env*/, napi_status status) { } } -#if (NAPI_VERSION > 3 && !defined(__wasm32__)) +#endif // NAPI_HAS_THREADS + +#if (NAPI_VERSION > 3 && NAPI_HAS_THREADS) //////////////////////////////////////////////////////////////////////////////// // TypedThreadSafeFunction class //////////////////////////////////////////////////////////////////////////////// @@ -6160,7 +6164,7 @@ inline void AsyncProgressQueueWorker::ExecutionProgress::Send( const T* data, size_t count) const { _worker->SendProgress_(data, count); } -#endif // NAPI_VERSION > 3 && !defined(__wasm32__) +#endif // NAPI_VERSION > 3 && NAPI_HAS_THREADS //////////////////////////////////////////////////////////////////////////////// // Memory Management class diff --git a/napi.h b/napi.h index b5f2a508a..06448b481 100644 --- a/napi.h +++ b/napi.h @@ -1,11 +1,21 @@ #ifndef SRC_NAPI_H_ #define SRC_NAPI_H_ +#ifndef NAPI_HAS_THREADS +#if !defined(__wasm__) || (defined(__EMSCRIPTEN_PTHREADS__) || (defined(__wasi__) && defined(_REENTRANT))) +#define NAPI_HAS_THREADS 1 +#else +#define NAPI_HAS_THREADS 0 +#endif +#endif + #include #include #include #include +#if NAPI_HAS_THREADS #include +#endif #include #include @@ -2394,7 +2404,7 @@ class EscapableHandleScope { napi_escapable_handle_scope _scope; }; -#if (NAPI_VERSION > 2) +#if (NAPI_VERSION > 2 && !defined(__wasm__)) class CallbackScope { public: CallbackScope(napi_env env, napi_callback_scope scope); @@ -2435,6 +2445,7 @@ class AsyncContext { napi_async_context _context; }; +#if NAPI_HAS_THREADS class AsyncWorker { public: virtual ~AsyncWorker(); @@ -2497,8 +2508,9 @@ class AsyncWorker { std::string _error; bool _suppress_destruct; }; +#endif // NAPI_HAS_THREADS -#if (NAPI_VERSION > 3 && !defined(__wasm32__)) +#if (NAPI_VERSION > 3 && NAPI_HAS_THREADS) class ThreadSafeFunction { public: // This API may only be called from the main thread. @@ -3068,7 +3080,7 @@ class AsyncProgressQueueWorker void Signal() const; void SendProgress_(const T* data, size_t count); }; -#endif // NAPI_VERSION > 3 && !defined(__wasm32__) +#endif // NAPI_VERSION > 3 && NAPI_HAS_THREADS // Memory management. class MemoryManagement { From 106abfad867b5434037dfebb0cbc59e141d4c336 Mon Sep 17 00:00:00 2001 From: toyobayashi Date: Thu, 9 Feb 2023 00:48:20 +0800 Subject: [PATCH 2/5] src: run clang-format --- napi-inl.h | 2 +- napi.h | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/napi-inl.h b/napi-inl.h index 0227c28ef..81f0faaba 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -4913,7 +4913,7 @@ inline void AsyncWorker::OnWorkComplete(Napi::Env /*env*/, napi_status status) { } } -#endif // NAPI_HAS_THREADS +#endif // NAPI_HAS_THREADS #if (NAPI_VERSION > 3 && NAPI_HAS_THREADS) //////////////////////////////////////////////////////////////////////////////// diff --git a/napi.h b/napi.h index 06448b481..c1dc47a74 100644 --- a/napi.h +++ b/napi.h @@ -2,7 +2,8 @@ #define SRC_NAPI_H_ #ifndef NAPI_HAS_THREADS -#if !defined(__wasm__) || (defined(__EMSCRIPTEN_PTHREADS__) || (defined(__wasi__) && defined(_REENTRANT))) +#if !defined(__wasm__) || (defined(__EMSCRIPTEN_PTHREADS__) || \ + (defined(__wasi__) && defined(_REENTRANT))) #define NAPI_HAS_THREADS 1 #else #define NAPI_HAS_THREADS 0 @@ -2508,7 +2509,7 @@ class AsyncWorker { std::string _error; bool _suppress_destruct; }; -#endif // NAPI_HAS_THREADS +#endif // NAPI_HAS_THREADS #if (NAPI_VERSION > 3 && NAPI_HAS_THREADS) class ThreadSafeFunction { From 69300c32deb8833a43ff6b89cc69d67c1118b18f Mon Sep 17 00:00:00 2001 From: toyobayashi Date: Sat, 11 Feb 2023 23:04:15 +0800 Subject: [PATCH 3/5] src: scope `` in `NAPI_HAS_THREADS` --- napi-inl.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/napi-inl.h b/napi-inl.h index 81f0faaba..69265591b 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -12,7 +12,9 @@ #include #include +#if NAPI_HAS_THREADS #include +#endif #include #include From fd8e9f7546cbade2322ffd088919ff80c1a28c96 Mon Sep 17 00:00:00 2001 From: toyobayashi Date: Mon, 13 Feb 2023 16:25:58 +0800 Subject: [PATCH 4/5] src: do not scope --- napi-inl.h | 2 +- napi.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/napi-inl.h b/napi-inl.h index 69265591b..28a150ab9 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -4651,7 +4651,7 @@ inline Value EscapableHandleScope::Escape(napi_value escapee) { return Value(_env, result); } -#if (NAPI_VERSION > 2 && !defined(__wasm__)) +#if (NAPI_VERSION > 2) //////////////////////////////////////////////////////////////////////////////// // CallbackScope class //////////////////////////////////////////////////////////////////////////////// diff --git a/napi.h b/napi.h index c1dc47a74..370260176 100644 --- a/napi.h +++ b/napi.h @@ -2405,7 +2405,7 @@ class EscapableHandleScope { napi_escapable_handle_scope _scope; }; -#if (NAPI_VERSION > 2 && !defined(__wasm__)) +#if (NAPI_VERSION > 2) class CallbackScope { public: CallbackScope(napi_env env, napi_callback_scope scope); From f5ad5acb233c7b64cdaa2b33121a35d1c10ead61 Mon Sep 17 00:00:00 2001 From: toyobayashi Date: Mon, 13 Feb 2023 16:52:47 +0800 Subject: [PATCH 5/5] src: add comment after endif --- napi-inl.h | 2 +- napi.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/napi-inl.h b/napi-inl.h index 28a150ab9..499698769 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -14,7 +14,7 @@ #include #if NAPI_HAS_THREADS #include -#endif +#endif // NAPI_HAS_THREADS #include #include diff --git a/napi.h b/napi.h index 370260176..6243acb55 100644 --- a/napi.h +++ b/napi.h @@ -16,7 +16,7 @@ #include #if NAPI_HAS_THREADS #include -#endif +#endif // NAPI_HAS_THREADS #include #include