Skip to content

Commit

Permalink
fix compilation with exceptions disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
mmomtchev committed Sep 16, 2023
1 parent 25f3091 commit 2c4db7c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
8 changes: 6 additions & 2 deletions Examples/javascript/async/native_async.i
Expand Up @@ -54,7 +54,7 @@ static Napi::Value piAsync(const Napi::CallbackInfo &info) {
virtual void Execute() override {
Napi::Env env(nullptr);
if (ow) ow->lock();
#if defined(_CPPUNWIND) || defined(__EXCEPTIONS)
#ifdef NAPI_CPP_EXCEPTIONS
try {
result = arg1->approx();
} catch (const std::exception &e) {
Expand All @@ -64,9 +64,11 @@ static Napi::Value piAsync(const Napi::CallbackInfo &info) {
result = (int)arg1->approx();
#endif
if (ow) ow->unlock();
#ifndef NAPI_CPP_EXCEPTIONS
goto fail;
fail:
return;
returrn;
#endif
}

virtual void Init(const Napi::CallbackInfo &info) {
Expand Down Expand Up @@ -97,7 +99,9 @@ static Napi::Value piAsync(const Napi::CallbackInfo &info) {
self->Init(info);
self->Queue();
return self->deferred.Promise();
#ifndef NAPI_CPP_EXCEPTIONS
fail:
return Napi::Value();
#endif
}
%}
2 changes: 2 additions & 0 deletions Lib/javascript/napi/javascriptcode.swg
Expand Up @@ -934,12 +934,14 @@ fail:
return;
}

#ifdef NAPI_CPP_EXCEPTIONS
virtual void SWIG_NAPI_Rethrow(const std::exception_ptr &saved) override {
// Back in the main thread with V8 access, caller has HandleScope
$jstry
std::rethrow_exception(saved);
$jscatch
}
#endif

virtual void SWIG_NAPI_Resolve() override {
// Back in the main thread with V8 access, caller has HandleScope
Expand Down
10 changes: 9 additions & 1 deletion Lib/javascript/napi/javascriptrun.swg
Expand Up @@ -536,11 +536,14 @@ public:
virtual void SWIG_NAPI_Execute() = 0;
virtual void SWIG_NAPI_Resolve() = 0;
virtual bool SWIG_NAPI_Cleanup() = 0;
#ifdef NAPI_CPP_EXCEPTIONS
virtual void SWIG_NAPI_Fail(const std::exception_ptr &);
virtual void SWIG_NAPI_Rethrow(const std::exception_ptr &) = 0;
#endif
inline Napi::Promise SWIG_NAPI_Promise() { return SWIG_NAPI_deferred.Promise(); }
};

#ifdef NAPI_CPP_EXCEPTIONS
void SWIG_NAPI_AsyncContext::SWIG_NAPI_Fail(const std::exception_ptr &saved) {
Napi::Error error;
try {
Expand All @@ -557,6 +560,7 @@ void SWIG_NAPI_AsyncContext::SWIG_NAPI_Fail(const std::exception_ptr &saved) {
fail:
return;
}
#endif

// This is the async worker, this class deletes itself (NAPI does this)
// as well as its context when the async operation completes
Expand Down Expand Up @@ -588,11 +592,15 @@ public:

virtual void OnOK() override {
Napi::HandleScope scope(Env());
#ifdef NAPI_CPP_EXCEPTIONS
if (saved_exception) {
context->SWIG_NAPI_Fail(saved_exception);
} else {
context->SWIG_NAPI_Resolve();
}
#else
context->SWIG_NAPI_Resolve();
#endif
delete context;
}

Expand Down Expand Up @@ -632,7 +640,7 @@ public:
Napi::Value Run(const Napi::CallbackInfo &info) {
SWIG_NAPI_AsyncContext::Status rc;

#if defined(_CPPUNWIND) || defined(__EXCEPTIONS)
#ifdef NAPI_CPP_EXCEPTIONS
try {
rc = context->SWIG_NAPI_Init(info);
} catch (...) {
Expand Down

0 comments on commit 2c4db7c

Please sign in to comment.