Skip to content

Commit

Permalink
fix %exception and restore swig_exception.i to its former glory
Browse files Browse the repository at this point in the history
  • Loading branch information
mmomtchev committed Sep 16, 2023
1 parent 2c4db7c commit 15ea779
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Examples/test-suite/swig_exception.i
Expand Up @@ -8,7 +8,7 @@
%exception {
try {
$action
} catch (std::logic_error& e) {
} catch (const std::exception& e) {
SWIG_exception(SWIG_SystemError, e.what());
}
}
Expand Down
11 changes: 10 additions & 1 deletion Lib/javascript/napi/javascriptcode.swg
Expand Up @@ -891,6 +891,15 @@ fail:
SWIG_guard.lock();
%}

/* -----------------------------------------------------------------------------
* js_rethrow_exception: default rethrow statement
* -----------------------------------------------------------------------------
*/
%fragment("js_rethrow_exception", "templates")
%{
std::rethrow_exception(saved);
%}


/* -----------------------------------------------------------------------------
* js_async_worker_local_class: template for the local async worker class in async wrappers
Expand Down Expand Up @@ -938,7 +947,7 @@ fail:
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);
$jsrethrow
$jscatch
}
#endif
Expand Down
14 changes: 14 additions & 0 deletions Source/Modules/javascript.cxx
Expand Up @@ -3044,6 +3044,19 @@ int NAPIEmitter::emitFunctionDefinition(Node *n, bool is_member, bool is_static,

Hash *action = emit_action_hash(n);

String *rethrow = NewStringEmpty();
if (is_async) {
// In async mode %exception wraps around the rethrow statement
String *rethrow_templ = NewStringEmpty();
Template(getTemplate("js_rethrow_exception")).print(rethrow_templ);
emit_action_code(n, rethrow, rethrow_templ);
} else {
// In sync mode %exception wraps around the action itself and becomes the action
emit_action_code(n, rethrow, Getattr(action, "action"));
Delete(Getattr(action, "action"));
Setattr(action, "action", rethrow);
}

wrapper->code = NewString("");
marshalOutput(n, params, wrapper, NewString(""));
String *output = wrapper->code;
Expand All @@ -3069,6 +3082,7 @@ int NAPIEmitter::emitFunctionDefinition(Node *n, bool is_member, bool is_static,
.replace("$jspreaction", Getattr(action, "preaction"))
.replace("$jstry", Getattr(action, "try"))
.replace("$jsaction", Getattr(action, "action"))
.replace("$jsrethrow", rethrow)
.replace("$jscatch", Getattr(action, "catch"))
.replace("$jspostaction", Getattr(action, "postaction"))
.replace("$jsoutput", output)
Expand Down

0 comments on commit 15ea779

Please sign in to comment.