1+ #include < future>
12#include " napi.h"
23
34using namespace Napi ;
45
56namespace {
67
8+ std::promise<void > promise_for_child_process_;
9+ std::promise<void > promise_for_worker_thread_;
10+
11+ void ResetPromises (const CallbackInfo&) {
12+ promise_for_child_process_ = std::promise<void >();
13+ promise_for_worker_thread_ = std::promise<void >();
14+ }
15+
16+ void WaitForWorkerThread (const CallbackInfo&) {
17+ std::future<void > future = promise_for_worker_thread_.get_future ();
18+
19+ std::future_status status = future.wait_for (std::chrono::seconds (5 ));
20+
21+ if (status != std::future_status::ready) {
22+ Error::Fatal (" WaitForWorkerThread" , " status != std::future_status::ready" );
23+ }
24+ }
25+
26+ void ReleaseAndWaitForChildProcess (const CallbackInfo& info,
27+ const uint32_t index) {
28+ if (info.Length () < index + 1 ) {
29+ return ;
30+ }
31+
32+ if (!info[index].As <Boolean>().Value ()) {
33+ return ;
34+ }
35+
36+ promise_for_worker_thread_.set_value ();
37+
38+ std::future<void > future = promise_for_child_process_.get_future ();
39+
40+ std::future_status status = future.wait_for (std::chrono::seconds (5 ));
41+
42+ if (status != std::future_status::ready) {
43+ Error::Fatal (" ReleaseAndWaitForChildProcess" ,
44+ " status != std::future_status::ready" );
45+ }
46+ }
47+
48+ void ReleaseWorkerThread (const CallbackInfo&) {
49+ promise_for_child_process_.set_value ();
50+ }
51+
752void DoNotCatch (const CallbackInfo& info) {
853 Function thrower = info[0 ].As <Function>();
954 thrower ({});
@@ -18,16 +63,22 @@ void ThrowApiError(const CallbackInfo& info) {
1863
1964void ThrowJSError (const CallbackInfo& info) {
2065 std::string message = info[0 ].As <String>().Utf8Value ();
66+
67+ ReleaseAndWaitForChildProcess (info, 1 );
2168 throw Error::New (info.Env (), message);
2269}
2370
2471void ThrowTypeError (const CallbackInfo& info) {
2572 std::string message = info[0 ].As <String>().Utf8Value ();
73+
74+ ReleaseAndWaitForChildProcess (info, 1 );
2675 throw TypeError::New (info.Env (), message);
2776}
2877
2978void ThrowRangeError (const CallbackInfo& info) {
3079 std::string message = info[0 ].As <String>().Utf8Value ();
80+
81+ ReleaseAndWaitForChildProcess (info, 1 );
3182 throw RangeError::New (info.Env (), message);
3283}
3384
@@ -83,16 +134,22 @@ void CatchAndRethrowErrorThatEscapesScope(const CallbackInfo& info) {
83134
84135void ThrowJSError (const CallbackInfo& info) {
85136 std::string message = info[0 ].As <String>().Utf8Value ();
137+
138+ ReleaseAndWaitForChildProcess (info, 1 );
86139 Error::New (info.Env (), message).ThrowAsJavaScriptException ();
87140}
88141
89142void ThrowTypeError (const CallbackInfo& info) {
90143 std::string message = info[0 ].As <String>().Utf8Value ();
144+
145+ ReleaseAndWaitForChildProcess (info, 1 );
91146 TypeError::New (info.Env (), message).ThrowAsJavaScriptException ();
92147}
93148
94149void ThrowRangeError (const CallbackInfo& info) {
95150 std::string message = info[0 ].As <String>().Utf8Value ();
151+
152+ ReleaseAndWaitForChildProcess (info, 1 );
96153 RangeError::New (info.Env (), message).ThrowAsJavaScriptException ();
97154}
98155
@@ -187,6 +244,8 @@ void ThrowDefaultError(const CallbackInfo& info) {
187244 Error::Fatal (" ThrowDefaultError" , " napi_get_named_property" );
188245 }
189246
247+ ReleaseAndWaitForChildProcess (info, 1 );
248+
190249 // The macro creates a `Napi::Error` using the factory that takes only the
191250 // env, however, it heeds the exception mechanism to be used.
192251 NAPI_THROW_IF_FAILED_VOID (env, status);
@@ -209,5 +268,8 @@ Object InitError(Env env) {
209268 Function::New (env, CatchAndRethrowErrorThatEscapesScope);
210269 exports[" throwFatalError" ] = Function::New (env, ThrowFatalError);
211270 exports[" throwDefaultError" ] = Function::New (env, ThrowDefaultError);
271+ exports[" resetPromises" ] = Function::New (env, ResetPromises);
272+ exports[" waitForWorkerThread" ] = Function::New (env, WaitForWorkerThread);
273+ exports[" releaseWorkerThread" ] = Function::New (env, ReleaseWorkerThread);
212274 return exports;
213275}
0 commit comments