src/xs/catch.cc

filter: Statementstmt 65.4% 36 / 55 Branchbran 24.3% 18 / 74 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 41.8% 54 / 129 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/xs/Backref.h ↑ Summary src/xs/Stash.h »
1 #include <xs/catch.h>
2 #include <xs/Simple.h>
3 #include <string>
4 #include <vector>
5 #include <cxxabi.h>
6 #include <exception>
7  
8 using panda::string_view;
9  
10 namespace xs {
11  
12 static std::vector<CatchHandler> catch_handlers;
13 static std::vector<ExceptionProcessor> exception_processors;
14  
15 96 static std::string get_type_name (const std::type_info& ti) {
16 int status;
17 96 ▶ char* class_name = abi::__cxa_demangle(ti.name(), NULL, NULL, &status);
Branch: 1 branch
BranchTrueFalseTotal
char* class_name = abi::__cxa_demangle(ti.name(), NULL, NULL, &status); 96 0 96
18 96 ▶ std::string ret = "[";
Branch: 1 branch
BranchTrueFalseTotal
std::string ret = "["; 96 0 96
19 96 ▶ if (status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status == 0) { 96 0 96
20 96 ▶ ret += class_name;
Branch: 1 branch
BranchTrueFalseTotal
ret += class_name; 96 0 96
21 96 free(class_name);
22 }
23 0 ▶ else ret = "<unknown type>";
Branch: 1 branch
BranchTrueFalseTotal
else ret = "<unknown type>"; 0 0 0
24 96 ▶ ret += "]";
Branch: 1 branch
BranchTrueFalseTotal
ret += "]"; 96 0 96
25 192 return ret;
26 }
27  
28 243 static Sv _exc2sv_default (const Sub&) {
29 243 try { throw; }
30 243 catch (SV* err) { return err; }
31 7 ▶ catch (Sv& err) { return err; }
Branch: 1 branch
BranchTrueFalseTotal
catch (Sv& err) { return err; } 7 0 7
32 114 ▶ catch (const char* err) { return Simple(string_view(err)); }
Branch: 2 branches
BranchTrueFalseTotal
catch (const char* err) { return Simple(string_view(err)); } 114 0 114
catch (const char* err) { return Simple(string_view(err)); } 0 0 0
33 catch (const string_view& err) { return Simple(err); }
34 24 ▶ catch (const panda::string& err) { return Simple(err); }
Branch: 2 branches
BranchTrueFalseTotal
catch (const panda::string& err) { return Simple(err); } 24 0 24
catch (const panda::string& err) { return Simple(err); } 0 0 0
35 catch (const std::string& err) { return Simple(string_view(err.data(), err.length())); }
36 2 catch (const PerlRuntimeException& err) {
37 2 ▶ return err.sv;
Branch: 1 branch
BranchTrueFalseTotal
return err.sv; 2 0 2
38 2 }
39 88 catch (std::exception& err) {
40 dTHX;
41 88 ▶ auto tn = get_type_name(typeid(err));
Branch: 1 branch
BranchTrueFalseTotal
auto tn = get_type_name(typeid(err)); 88 0 88
42 88 ▶ SV* errsv = newSVpv(tn.data(), 0);
Branch: 1 branch
BranchTrueFalseTotal
SV* errsv = newSVpv(tn.data(), 0); 88 0 88
43 88 ▶ sv_catpv(errsv, " ");
Branch: 1 branch
BranchTrueFalseTotal
sv_catpv(errsv, " "); 88 0 88
44 88 ▶ sv_catpv(errsv, err.what());
Branch: 1 branch
BranchTrueFalseTotal
sv_catpv(errsv, err.what()); 88 0 88
45 88 return Sv::noinc(errsv);
46 88 }
47 catch (std::exception* err) {
48 ▶ dTHX;
Branch: 5 branches
BranchTrueFalseTotal
dTHX; 0 0 0
dTHX; 0 0 0
dTHX; 0 0 0
dTHX; 0 0 0
dTHX; 0 0 0
49 auto tn = get_type_name(typeid(*err));
50 SV* errsv = newSVpv(tn.data(), 0);
51 sv_catpv(errsv, " ");
52 sv_catpv(errsv, err->what());
53 return Sv::noinc(errsv);
54 }
55 8 catch (...) {
56 dTHX;
57 8 ▶ auto tn = get_type_name(*abi::__cxa_current_exception_type());
Branch: 1 branch
BranchTrueFalseTotal
auto tn = get_type_name(*abi::__cxa_current_exception_type()); 8 0 8
58 8 ▶ SV* errsv = newSVpv(tn.data(), 0);
Branch: 1 branch
BranchTrueFalseTotal
SV* errsv = newSVpv(tn.data(), 0); 8 0 8
59 8 ▶ sv_catpv(errsv, " exception");
Branch: 1 branch
BranchTrueFalseTotal
sv_catpv(errsv, " exception"); 8 0 8
60 8 return Sv::noinc(errsv);
61 8 }
62 return Sv();
63 }
64  
65 243 static Sv _exc2sv_impl (const Sub& context, int i) {
66 243 ▶ if (i < 0) return _exc2sv_default(context); // no 1 has catched the exception, apply defaults
Branch: 1 branch
BranchTrueFalseTotal
if (i < 0) return _exc2sv_default(context); // no 1 has catched the exception, apply defaults 243 0 243
67 0 ▶ if (i >= (int)catch_handlers.size()) i = catch_handlers.size() - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= (int)catch_handlers.size()) i = catch_handlers.size() - 1; 0 0 0
68 try {
69 0 ▶ auto ret = catch_handlers[i](context);
Branch: 1 branch
BranchTrueFalseTotal
auto ret = catch_handlers[i](context); 0 0 0
70 0 ▶ if (ret) return ret;
Branch: 1 branch
BranchTrueFalseTotal
if (ret) return ret; 0 0 0
71 0 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 0 0 0
72 catch (...) {}
73 0 return _exc2sv_impl(context, i-1);
74 }
75  
76 243 Sv _exc2sv (const Sub& context) {
77 243 auto ex_sv = _exc2sv_impl(context, catch_handlers.size() - 1);
78 243 ▶ for (auto& processor: exception_processors) {
Branch: 1 branch
BranchTrueFalseTotal
for (auto& processor: exception_processors) { 0 243 243
79 0 ▶ ex_sv = processor(ex_sv, context);
Branch: 1 branch
BranchTrueFalseTotal
ex_sv = processor(ex_sv, context); 0 0 0
80 }
81 243 return ex_sv;
82 }
83  
84 0 void add_catch_handler (CatchHandler h) {
85 0 catch_handlers.push_back(h);
86 0 }
87  
88 0 void add_catch_handler (CatchHandlerSimple h) {
89 0 ▶ catch_handlers.push_back([h](const Sub&) -> Sv { return h(); });
Branch: 3 branches
BranchTrueFalseTotal
catch_handlers.push_back([h](const Sub&) -> Sv { return h(); }); 0 0 0
catch_handlers.push_back([h](const Sub&) -> Sv { return h(); }); 0 0 0
catch_handlers.push_back([h](const Sub&) -> Sv { return h(); }); 0 0 0
90 0 }
91  
92 0 void add_exception_processor(ExceptionProcessor f) {
93 0 exception_processors.push_back(f);
94 0 }
95  
96 0 void add_exception_processor(ExceptionProcessorSimple f) {
97 0 ▶ exception_processors.push_back([f](Sv& ex, const Sub&) -> Sv { return f(ex); });
Branch: 3 branches
BranchTrueFalseTotal
exception_processors.push_back([f](Sv& ex, const Sub&) -> Sv { return f(ex); }); 0 0 0
exception_processors.push_back([f](Sv& ex, const Sub&) -> Sv { return f(ex); }); 0 0 0
exception_processors.push_back([f](Sv& ex, const Sub&) -> Sv { return f(ex); }); 0 0 0
98 0 }
99  
100  
101 }
« src/xs/Backref.h ↑ Summary src/xs/Stash.h »