src/xs/Sub.cc

filter: Statementstmt 97.3% 110 / 113 Branchbran 73.6% 84 / 114 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 85.4% 194 / 227 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/xs/HashEntry.h ↑ Summary t/typemap/error.xsi »
1 #include <xs/Sub.h>
2 #include <xs/Stash.h>
3 #include <xs/Object.h>
4 #include <panda/string.h>
5  
6 namespace xs {
7  
8 78 Sub Sub::create (panda::string_view sub_code) {
9 78 ▶ auto code = panda::string("sub { ") + sub_code + " }";
Branch: 2 branches
BranchTrueFalseTotal
auto code = panda::string("sub { ") + sub_code + " }"; 78 0 78
auto code = panda::string("sub { ") + sub_code + " }"; 78 0 78
10 156 ▶ return eval(code);
Branch: 2 branches
BranchTrueFalseTotal
return eval(code); 78 0 78
return eval(code); 78 0 78
11 78 }
12  
13 37 Sub Sub::create (XSUBADDR_t xsfunc) {
14 37 return newXS(nullptr, xsfunc, "<C++>");
15 }
16  
17 7 Stash Sub::stash () const { return CvSTASH((CV*)sv); }
18  
19 2 Glob Sub::glob () const { return CvGV((CV*)sv); }
20  
21 5 void Sub::_throw_super () const {
22 5 ▶ throw std::invalid_argument(panda::string("can't locate super method '") + name() + "' via package '" + stash().name() + "'");
Branch: 9 branches
BranchTrueFalseTotal
throw std::invalid_argument(panda::string("can't locate super method '") + name() + "' via package '" + stash().name() + "'"); 5 0 5
throw std::invalid_argument(panda::string("can't locate super method '") + name() + "' via package '" + stash().name() + "'"); 5 0 5
throw std::invalid_argument(panda::string("can't locate super method '") + name() + "' via package '" + stash().name() + "'"); 5 0 5
throw std::invalid_argument(panda::string("can't locate super method '") + name() + "' via package '" + stash().name() + "'"); 5 0 5
throw std::invalid_argument(panda::string("can't locate super method '") + name() + "' via package '" + stash().name() + "'"); 5 0 5
throw std::invalid_argument(panda::string("can't locate super method '") + name() + "' via package '" + stash().name() + "'"); 5 0 5
throw std::invalid_argument(panda::string("can't locate super method '") + name() + "' via package '" + stash().name() + "'"); 5 0 5
throw std::invalid_argument(panda::string("can't locate super method '") + name() + "' via package '" + stash().name() + "'"); 5 0 5
throw std::invalid_argument(panda::string("can't locate super method '") + name() + "' via package '" + stash().name() + "'"); 5 0 5
23 }
24  
25 354 size_t Sub::_call (CV* cv, I32 flags, const CallArgs& args, SV** ret, size_t maxret, AV** avr) {
26 354 dTHX; dSP; ENTER; SAVETMPS;
27 354 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 354 354
28  
29 354 ▶ if (args.self) XPUSHs(args.self);
Branch: 2 branches
BranchTrueFalseTotal
if (args.self) XPUSHs(args.self); 25 329 354
if (args.self) XPUSHs(args.self); 0 25 25
30 362 ▶ if (args.scalars) for (size_t i = 0; i < args.items; ++i) XPUSHs(args.scalars[i] ? args.scalars[i].get() : &PL_sv_undef);
Branch: 4 branches
BranchTrueFalseTotal
if (args.scalars) for (size_t i = 0; i < args.items; ++i) XPUSHs(args.scalars[i] ? args.scalars[i].get() : &PL_sv_undef); 4 350 354
if (args.scalars) for (size_t i = 0; i < args.items; ++i) XPUSHs(args.scalars[i] ? args.scalars[i].get() : &PL_sv_undef); 0 8 8
if (args.scalars) for (size_t i = 0; i < args.items; ++i) XPUSHs(args.scalars[i] ? args.scalars[i].get() : &PL_sv_undef); 8 0 8
if (args.scalars) for (size_t i = 0; i < args.items; ++i) XPUSHs(args.scalars[i] ? args.scalars[i].get() : &PL_sv_undef); 8 4 12
31 865 ▶ else for (size_t i = 0; i < args.items; ++i) XPUSHs(args.list[i] ? args.list[i] : &PL_sv_undef);
Branch: 3 branches
BranchTrueFalseTotal
else for (size_t i = 0; i < args.items; ++i) XPUSHs(args.list[i] ? args.list[i] : &PL_sv_undef); 0 515 515
else for (size_t i = 0; i < args.items; ++i) XPUSHs(args.list[i] ? args.list[i] : &PL_sv_undef); 511 4 515
else for (size_t i = 0; i < args.items; ++i) XPUSHs(args.list[i] ? args.list[i] : &PL_sv_undef); 515 350 865
32 354 PUTBACK;
33  
34 354 ▶ if (!maxret && !avr) flags |= G_DISCARD;
Branch: 2 branches
BranchTrueFalseTotal
if (!maxret && !avr) flags |= G_DISCARD; 194 160 354
if (!maxret && !avr) flags |= G_DISCARD; 192 2 194
35 354 size_t count = call_sv((SV*)cv, flags|G_EVAL);
36 354 SPAGAIN;
37  
38 354 auto errsv = GvSV(PL_errgv);
39 354 ▶ if (SvTRUE(errsv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(errsv)) { 2 352 354
40 4 ▶ while (count > 0) { (void)POPs; --count; }
Branch: 1 branch
BranchTrueFalseTotal
while (count > 0) { (void)POPs; --count; } 2 2 4
41 2 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 3 branches
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 2 0 2
PUTBACK; FREETMPS; LEAVE; 2 0 2
PUTBACK; FREETMPS; LEAVE; 2 0 2
42 2 auto exc = Sv::noinc(errsv);
43 2 ▶ GvSV(PL_errgv) = newSVpvs("");
Branch: 1 branch
BranchTrueFalseTotal
GvSV(PL_errgv) = newSVpvs(""); 2 0 2
44 2 ▶ throw PerlRuntimeException(exc);
Branch: 1 branch
BranchTrueFalseTotal
throw PerlRuntimeException(exc); 2 0 2
45 2 }
46  
47 352 ▶ auto nret = count > maxret ? maxret : count;
Branch: 1 branch
BranchTrueFalseTotal
auto nret = count > maxret ? maxret : count; 4 348 352
48  
49 352 ▶ if (!avr) {
Branch: 1 branch
BranchTrueFalseTotal
if (!avr) { 350 2 352
50 352 ▶ while (count > maxret) { (void)POPs; --count; }
Branch: 1 branch
BranchTrueFalseTotal
while (count > maxret) { (void)POPs; --count; } 2 350 352
51 518 ▶ while (count > 0) ret[--count] = SvREFCNT_inc_NN(POPs);
Branch: 1 branch
BranchTrueFalseTotal
while (count > 0) ret[--count] = SvREFCNT_inc_NN(POPs); 168 350 518
52 }
53 2 ▶ else if (count) {
Branch: 1 branch
BranchTrueFalseTotal
else if (count) { 2 0 2
54 2 nret = count;
55 2 AV* av = *avr = newAV();
56 2 av_extend(av, count-1);
57 2 AvFILLp(av) = count-1;
58 2 SV** svlist = AvARRAY(av);
59 8 ▶ while (count--) svlist[count] = SvREFCNT_inc_NN(POPs);
Branch: 1 branch
BranchTrueFalseTotal
while (count--) svlist[count] = SvREFCNT_inc_NN(POPs); 6 2 8
60 }
61 0 else *avr = NULL;
62  
63 352 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 131 221 352
64  
65 352 return nret;
66 }
67  
68 53 Sub Sub::clone_anon_xsub (const Sub& proto) {
69 dTHX;
70 53 CV* cv = MUTABLE_CV(newSV_type(SvTYPE(proto)));
71 53 CvFLAGS(cv) = CvFLAGS(proto) & ~(CVf_CLONE|CVf_WEAKOUTSIDE|CVf_CVGV_RC);
72 53 CvCLONED_on(cv);
73 53 CvFILE(cv) = CvFILE(proto);
74 53 CvGV_set(cv,CvGV(proto));
75 53 CvSTASH_set(cv, CvSTASH(proto));
76 53 CvISXSUB_on(cv);
77 53 CvXSUB(cv) = CvXSUB(proto);
78 #if PERL_VERSION >= 22
79 #ifndef PERL_IMPLICIT_CONTEXT
80 53 CvHSCXT(cv) = &PL_stack_sp;
81 #else
82 PoisonPADLIST(cv);
83 #endif
84 #endif
85 53 CvANON_on(cv);
86 53 return Sub::noinc(cv);
87 }
88  
89  
90 42 static inline OP* want_get_op_assign (OP* entersub) {
91 // for perls < 5.26, we don't have op_parent feature
92 #ifndef op_parent
93 auto cx = caller_cx(0, NULL);
94 auto cv = cx->blk_sub.cv;
95 OP* path[30];
96 size_t path_max = sizeof(path) / sizeof(path[0]);
97 size_t cur = 0;
98 for (size_t i = 0; i < path_max; ++i) path[i] = nullptr;
99  
100 auto op = CvSTART(cv);
101 while (1) {
102 if (op == entersub) break;
103 if (op->op_flags & OPf_KIDS) {
104 if (cur >= path_max) return nullptr;
105 path[cur++] = op;
106 op = cUNOPx(op)->op_first;
107 continue;
108 }
109 // get next sibling or parent's next sibling
110 while (!(op = OpSIBLING(op))) {
111 if (cur == 0) return nullptr; // we've searched the whole tree
112 op = path[--cur];
113 }
114 }
115  
116 if (op != entersub) return nullptr;
117  
118 while (cur--) {
119 OP* op = path[cur];
120 #else
121 74 ▶ for (OP* op = op_parent(entersub); op; op = op_parent(op)) {
Branch: 1 branch
BranchTrueFalseTotal
for (OP* op = op_parent(entersub); op; op = op_parent(op)) { 74 0 74
122 #endif
123 74 switch (op->op_type) {
124 31 case OP_AASSIGN: return op;
125 11 case OP_SASSIGN:
126 case OP_ANONHASH:
127 case OP_ANONLIST:
128 11 case OP_ENTERSUB: return nullptr;
129 }
130 }
131 0 return nullptr;
132 }
133  
134 174 static inline OP* want_unwrap (OP* op) {
135 174 ▶ if (op->op_type == OP_LIST) return cUNOPx(op)->op_first;
Branch: 1 branch
BranchTrueFalseTotal
if (op->op_type == OP_LIST) return cUNOPx(op)->op_first; 0 174 174
136 174 ▶ if (op->op_type == OP_NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (op->op_type == OP_NULL) { 60 114 174
137 60 ▶ if (op->op_targ == OP_LIST || (PL_opargs[op->op_targ] & OA_CLASS_MASK) & (OA_UNOP|OA_BINOP|OA_LISTOP)) {
Branch: 2 branches
BranchTrueFalseTotal
if (op->op_targ == OP_LIST || (PL_opargs[op->op_targ] & OA_CLASS_MASK) & (OA_UNOP|OA_BINOP|OA_LISTOP)) { 22 38 60
if (op->op_targ == OP_LIST || (PL_opargs[op->op_targ] & OA_CLASS_MASK) & (OA_UNOP|OA_BINOP|OA_LISTOP)) { 14 8 22
138 52 return cUNOPx(op)->op_first;
139 }
140 }
141 122 return nullptr;
142 }
143  
144 static double want_count_slice (OP* op_slice);
145  
146 80 static inline double want_count_list (OP* op) {
147 80 double count = 0;
148 OP* unwrapped;
149 237 ▶ for (; op; op = OpSIBLING(op)) {
Branch: 2 branches
BranchTrueFalseTotal
for (; op; op = OpSIBLING(op)) { 81 76 157
for (; op; op = OpSIBLING(op)) { 161 76 237
150 161 ▶ if ((unwrapped = want_unwrap(op))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((unwrapped = want_unwrap(op))) { 42 119 161
151 //warn("UNWRAP");
152 42 count += want_count_list(unwrapped);
153 42 continue;
154 }
155 //warn("INSPECT =============================="); op_dump(op);
156 119 switch (op->op_type) {
157 51 case OP_CONST:
158 case OP_PADSV:
159 case OP_GVSV:
160 case OP_UNDEF:
161 51 ++count;
162 51 break;
163 3 case OP_PADAV:
164 case OP_PADHV:
165 case OP_ENTERSUB:
166 3 return std::numeric_limits<double>::infinity();
167 13 case OP_ASLICE:
168 case OP_HSLICE:
169 13 count += want_count_slice(op);
170 13 break;
171 10 case OP_AELEM:
172 case OP_AELEMFAST:
173 case OP_AELEMFAST_LEX:
174 case OP_HELEM:
175 case OP_MULTIDEREF:
176 10 ++count;
177 10 break;
178 41 case OP_PADRANGE: // there should be optimized-out op_padsv/av/hv representing this padrange, so we skip it
179 case OP_PUSHMARK:
180 case OP_NULL: // non-list
181 41 break;
182 1 default:
183 1 return std::numeric_limits<double>::infinity();
184 }
185 }
186 76 return count;
187 }
188  
189 13 static double want_count_slice (OP* op_slice) {
190 13 auto op = cLISTOPx(op_slice)->op_first;
191 13 ▶ op = OpSIBLING(op);
Branch: 1 branch
BranchTrueFalseTotal
op = OpSIBLING(op); 13 0 13
192 assert(op);
193  
194 13 auto inner = want_unwrap(op);
195 13 ▶ if (inner) return want_count_list(inner); // @arr[1,2,3]
Branch: 1 branch
BranchTrueFalseTotal
if (inner) return want_count_list(inner); // @arr[1,2,3] 7 6 13
196  
197 6 ▶ switch (op->op_type) {
Branch: 1 branch
BranchTrueFalseTotal
switch (op->op_type) { 4 2 6
198 4 case OP_RV2AV: {
199 4 auto opf = cUNOPx(op)->op_first;
200 4 ▶ if (opf->op_type == OP_CONST && SvTYPE(cSVOPx_sv(opf)) == SVt_PVAV) { // @arr[1..3]
Branch: 2 branches
BranchTrueFalseTotal
if (opf->op_type == OP_CONST && SvTYPE(cSVOPx_sv(opf)) == SVt_PVAV) { // @arr[1..3] 4 0 4
if (opf->op_type == OP_CONST && SvTYPE(cSVOPx_sv(opf)) == SVt_PVAV) { // @arr[1..3] 4 0 4
201 4 return AvFILLp((AV*)cSVOPx_sv(opf)) + 1;
202 }
203 0 return std::numeric_limits<double>::infinity();
204 }
205 }
206  
207 2 return std::numeric_limits<double>::infinity();
208 }
209  
210 60 int Sub::want_count () {
211 60 auto type = want();
212 60 ▶ if (type == Want::Void) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (type == Want::Void) return 0; 9 51 60
213 51 ▶ else if (type == Want::Scalar) return 1;
Branch: 1 branch
BranchTrueFalseTotal
else if (type == Want::Scalar) return 1; 9 42 51
214  
215 42 auto entersub = PL_op;
216 42 ▶ if (entersub->op_type != OP_ENTERSUB) throw "want_count() must be called from XS sub";
Branch: 1 branch
BranchTrueFalseTotal
if (entersub->op_type != OP_ENTERSUB) throw "want_count() must be called from XS sub"; 0 42 42
217 42 OP* op_assign = want_get_op_assign(entersub);
218 42 ▶ if (!op_assign) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!op_assign) return -1; 11 31 42
219 //op_dump(op_assign);
220  
221 31 OP* lhs_root = cBINOPx(op_assign)->op_last;
222 31 auto ret = want_count_list(lhs_root);
223 31 ▶ return ret == std::numeric_limits<double>::infinity() ? -1 : (int)ret;
Branch: 1 branch
BranchTrueFalseTotal
return ret == std::numeric_limits<double>::infinity() ? -1 : (int)ret; 25 6 31
224 }
225  
226 }
« src/xs/HashEntry.h ↑ Summary t/typemap/error.xsi »