@@ -4120,8 +4120,8 @@ inline AsyncWorker::AsyncWorker(const Object& receiver,
41204120 _env, resource_name, NAPI_AUTO_LENGTH, &resource_id);
41214121 NAPI_THROW_IF_FAILED_VOID (_env, status);
41224122
4123- status = napi_create_async_work (_env, resource, resource_id, OnExecute ,
4124- OnWorkComplete , this , &_work);
4123+ status = napi_create_async_work (_env, resource, resource_id, OnAsyncWorkExecute ,
4124+ OnAsyncWorkComplete , this , &_work);
41254125 NAPI_THROW_IF_FAILED_VOID (_env, status);
41264126}
41274127
@@ -4146,8 +4146,8 @@ inline AsyncWorker::AsyncWorker(Napi::Env env,
41464146 _env, resource_name, NAPI_AUTO_LENGTH, &resource_id);
41474147 NAPI_THROW_IF_FAILED_VOID (_env, status);
41484148
4149- status = napi_create_async_work (_env, resource, resource_id, OnExecute ,
4150- OnWorkComplete , this , &_work);
4149+ status = napi_create_async_work (_env, resource, resource_id, OnAsyncWorkExecute ,
4150+ OnAsyncWorkComplete , this , &_work);
41514151 NAPI_THROW_IF_FAILED_VOID (_env, status);
41524152}
41534153
@@ -4234,40 +4234,51 @@ inline void AsyncWorker::SetError(const std::string& error) {
42344234inline std::vector<napi_value> AsyncWorker::GetResult (Napi::Env /* env*/ ) {
42354235 return {};
42364236}
4237+ // The OnAsyncWorkExecute method receives an napi_env argument. However, do NOT
4238+ // use it within this method, as it does not run on the JavaScript thread and
4239+ // must not run any method that would cause JavaScript to run. In practice,
4240+ // this means that almost any use of napi_env will be incorrect.
4241+ inline void AsyncWorker::OnAsyncWorkExecute (napi_env env, void * asyncworker) {
4242+ AsyncWorker* self = static_cast <AsyncWorker*>(asyncworker);
4243+ self->OnExecute (env);
4244+ }
42374245// The OnExecute method receives an napi_env argument. However, do NOT
4238- // use it within this method, as it does not run on the main thread and must
4239- // not run any method that would cause JavaScript to run. In practice, this
4240- // means that almost any use of napi_env will be incorrect.
4241- inline void AsyncWorker::OnExecute (napi_env /* DO_NOT_USE*/ , void * this_pointer) {
4242- AsyncWorker* self = static_cast <AsyncWorker*>(this_pointer);
4246+ // use it within this method, as it does not run on the JavaScript thread and
4247+ // must not run any method that would cause JavaScript to run. In practice,
4248+ // this means that almost any use of napi_env will be incorrect.
4249+ inline void AsyncWorker::OnExecute (Napi::Env /* DO_NOT_USE*/ ) {
42434250#ifdef NAPI_CPP_EXCEPTIONS
42444251 try {
4245- self-> Execute ();
4252+ Execute ();
42464253 } catch (const std::exception& e) {
4247- self-> SetError (e.what ());
4254+ SetError (e.what ());
42484255 }
42494256#else // NAPI_CPP_EXCEPTIONS
4250- self-> Execute ();
4257+ Execute ();
42514258#endif // NAPI_CPP_EXCEPTIONS
42524259}
42534260
4254- inline void AsyncWorker::OnWorkComplete (
4255- napi_env /* env*/ , napi_status status, void * this_pointer) {
4256- AsyncWorker* self = static_cast <AsyncWorker*>(this_pointer);
4261+ inline void AsyncWorker::OnAsyncWorkComplete (napi_env env,
4262+ napi_status status,
4263+ void * asyncworker) {
4264+ AsyncWorker* self = static_cast <AsyncWorker*>(asyncworker);
4265+ self->OnWorkComplete (env, status);
4266+ }
4267+ inline void AsyncWorker::OnWorkComplete (Napi::Env /* env*/ , napi_status status) {
42574268 if (status != napi_cancelled) {
4258- HandleScope scope (self-> _env );
4269+ HandleScope scope (_env);
42594270 details::WrapCallback ([&] {
4260- if (self-> _error .size () == 0 ) {
4261- self-> OnOK ();
4271+ if (_error.size () == 0 ) {
4272+ OnOK ();
42624273 }
42634274 else {
4264- self-> OnError (Error::New (self-> _env , self-> _error ));
4275+ OnError (Error::New (_env, _error));
42654276 }
42664277 return nullptr ;
42674278 });
42684279 }
4269- if (!self-> _suppress_destruct ) {
4270- self-> Destroy ();
4280+ if (!_suppress_destruct) {
4281+ Destroy ();
42714282 }
42724283}
42734284
@@ -4632,14 +4643,14 @@ inline AsyncProgressWorker<T>::AsyncProgressWorker(const Function& callback,
46324643
46334644template <class T >
46344645inline AsyncProgressWorker<T>::AsyncProgressWorker(const Object& receiver,
4635- const Function& callback)
4646+ const Function& callback)
46364647 : AsyncProgressWorker(receiver, callback, " generic" ) {
46374648}
46384649
46394650template <class T >
46404651inline AsyncProgressWorker<T>::AsyncProgressWorker(const Object& receiver,
4641- const Function& callback,
4642- const char * resource_name)
4652+ const Function& callback,
4653+ const char * resource_name)
46434654 : AsyncProgressWorker(receiver,
46444655 callback,
46454656 resource_name,
@@ -4665,14 +4676,14 @@ inline AsyncProgressWorker<T>::AsyncProgressWorker(Napi::Env env)
46654676
46664677template <class T >
46674678inline AsyncProgressWorker<T>::AsyncProgressWorker(Napi::Env env,
4668- const char * resource_name)
4679+ const char * resource_name)
46694680 : AsyncProgressWorker(env, resource_name, Object::New(env)) {
46704681}
46714682
46724683template <class T >
46734684inline AsyncProgressWorker<T>::AsyncProgressWorker(Napi::Env env,
4674- const char * resource_name,
4675- const Object& resource)
4685+ const char * resource_name,
4686+ const Object& resource)
46764687 : AsyncWorker(env, resource_name, resource),
46774688 _asyncdata (nullptr ),
46784689 _asyncsize(0 ) {
@@ -4751,7 +4762,6 @@ template<class T>
47514762inline void AsyncProgressWorker<T>::ExecutionProgress::Send(const T* data, size_t count) const {
47524763 _worker->SendProgress_ (data, count);
47534764}
4754-
47554765#endif
47564766
47574767// //////////////////////////////////////////////////////////////////////////////
0 commit comments