src/xs/Stash.cc

filter: Statementstmt 94.9% 75 / 79 Branchbran 52.4% 65 / 124 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 68.9% 140 / 203 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« regerror.cc ↑ Summary t/typemap/object/child.xsi »
1 #include <xs/Sub.h>
2 #include <xs/Stash.h>
3 #include <xs/Object.h>
4 #include <xs/HashEntry.h>
5  
6 #ifndef PERL_VERSION_DECIMAL
7 #define PERL_VERSION_DECIMAL(r,v,s) (r*1000000 + v*1000 + s)
8 #define PERL_DECIMAL_VERSION PERL_VERSION_DECIMAL(PERL_REVISION,PERL_VERSION,PERL_SUBVERSION)
9 #endif
10  
11 using panda::string_view;
12 using panda::string;
13  
14 namespace xs {
15  
16 13 Stash::op_proxy& Stash::op_proxy::operator= (SV* val) {
17 13 _throw();
18  
19 13 ▶ if (!val) {
Branch: 1 branch
BranchTrueFalseTotal
if (!val) { 1 12 13
20 1 ▶ slot(Scalar());
Branch: 1 branch
BranchTrueFalseTotal
slot(Scalar()); 1 0 1
21 1 return *this;
22 }
23  
24 12 ▶ if (SvROK(val)) val = SvRV(val);
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(val)) val = SvRV(val); 0 12 12
25 12 ▶ if (SvTYPE(val) == SVt_PVGV) return operator=((GV*)val);
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(val) == SVt_PVGV) return operator=((GV*)val); 1 11 12
26 11 slot(val);
27 11 return *this;
28 }
29  
30 7 Stash::op_proxy& Stash::op_proxy::operator= (GV* val) {
31 7 _throw();
32 7 ▶ if (val) {
Branch: 1 branch
BranchTrueFalseTotal
if (val) { 5 2 7
33 5 SvREFCNT_inc_simple_void_NN(val);
34 5 SvREFCNT_dec_NN(*ptr);
35 5 *ptr = (SV*)val;
36 5 Glob::operator=(val);
37 } else { // it is not allowed to set NULL to hash element, so nullify all slots
38 2 ▶ slot(Scalar());
Branch: 1 branch
BranchTrueFalseTotal
slot(Scalar()); 2 0 2
39 2 ▶ slot(Array());
Branch: 1 branch
BranchTrueFalseTotal
slot(Array()); 2 0 2
40 2 ▶ slot(Hash());
Branch: 1 branch
BranchTrueFalseTotal
slot(Hash()); 2 0 2
41 2 ▶ slot(Sub());
Branch: 1 branch
BranchTrueFalseTotal
slot(Sub()); 2 0 2
42 }
43 7 return *this;
44 }
45  
46 951 void Stash::_promote (GV* gv, const panda::string_view& key) const {
47 951 ▶ if (!gv || SvTYPE(gv) == SVt_PVGV) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!gv || SvTYPE(gv) == SVt_PVGV) return; 380 571 951
if (!gv || SvTYPE(gv) == SVt_PVGV) return; 344 36 380
48  
49 // perl [5.22.0 - 5.26.0] had a bug: core dump while promoting !CvNAMED subs. workaround it by adding name by hand
50 #if PERL_DECIMAL_VERSION < PERL_VERSION_DECIMAL(5,26,1) && PERL_DECIMAL_VERSION >= PERL_VERSION_DECIMAL(5,22,0)
51 if (SvROK(gv)) {
52 SV* val = SvRV(gv);
53 if (SvTYPE(val) == SVt_PVCV && !CvNAMED((CV*)val)) { // core-dump in gv_init_pvn with non-named CV
54 U32 hash;
55 PERL_HASH(hash, key.data(), key.length());
56 HEK* hek = share_hek(key.data(), key.length(), hash);
57 ((XPVCV*)MUTABLE_PTR(SvANY(val)))->xcv_gv_u.xcv_hek = hek;
58 CvNAMED_on((CV*)val);
59 CvCVGV_RC_off((CV*)val);
60 CvANON_off((CV*)val);
61 }
62 }
63 #endif
64  
65 // promote SV to GV. Note that prior to perl 5.22, not any SV could be promoted, otherwise gv_init_pvn croaks()
66 36 gv_init_pvn(gv, (HV*)sv, key.data(), key.length(), GV_ADDMULTI); // GV_ADDMULTI suppresses 'used only once' warning
67 }
68  
69 216 string Stash::path () const {
70 216 ▶ auto pkg = name();
Branch: 1 branch
BranchTrueFalseTotal
auto pkg = name(); 216 0 216
71 216 int len = pkg.length();
72 216 ▶ string ret(len+3);
Branch: 1 branch
BranchTrueFalseTotal
string ret(len+3); 216 0 216
73 216 ▶ char* dst = ret.buf();
Branch: 1 branch
BranchTrueFalseTotal
char* dst = ret.buf(); 216 0 216
74 216 const char* src = pkg.data();
75 4060 ▶ for (int i = 0; i < len; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < len; ++i) { 3844 216 4060
76 3844 ▶ if (*src == ':') {
Branch: 1 branch
BranchTrueFalseTotal
if (*src == ':') { 247 3597 3844
77 247 *dst = '/';
78 247 ++src;
79 247 ++i;
80 }
81 3597 else *dst = *src;
82 3844 ++dst;
83 3844 ++src;
84 }
85 216 *dst++ = '.';
86 216 *dst++ = 'p';
87 216 *dst++ = 'm';
88 216 ▶ ret.length(dst-ret.buf());
Branch: 1 branch
BranchTrueFalseTotal
ret.length(dst-ret.buf()); 216 0 216
89 432 return ret;
90 }
91  
92 108 void Stash::mark_as_loaded (const Stash& source) const {
93 108 ▶ if (!source) throw std::invalid_argument(string("can't register module '") + name() + "': source module doesn't exist");
Branch: 6 branches
BranchTrueFalseTotal
if (!source) throw std::invalid_argument(string("can't register module '") + name() + "': source module doesn't exist"); 1 107 108
if (!source) throw std::invalid_argument(string("can't register module '") + name() + "': source module doesn't exist"); 1 0 1
if (!source) throw std::invalid_argument(string("can't register module '") + name() + "': source module doesn't exist"); 1 0 1
if (!source) throw std::invalid_argument(string("can't register module '") + name() + "': source module doesn't exist"); 1 0 1
if (!source) throw std::invalid_argument(string("can't register module '") + name() + "': source module doesn't exist"); 1 0 1
if (!source) throw std::invalid_argument(string("can't register module '") + name() + "': source module doesn't exist"); 1 0 1
94 107 ▶ auto inc = Stash::root().hash("INC");
Branch: 2 branches
BranchTrueFalseTotal
auto inc = Stash::root().hash("INC"); 107 0 107
auto inc = Stash::root().hash("INC"); 107 0 107
95 107 ▶ auto realpath = inc.fetch(source.path());
Branch: 2 branches
BranchTrueFalseTotal
auto realpath = inc.fetch(source.path()); 107 0 107
auto realpath = inc.fetch(source.path()); 107 0 107
96 107 ▶ if (!realpath) throw std::invalid_argument(string("can't register module '") + name() + "': source module '" + source.name() + "' hasn't been registered");
Branch: 9 branches
BranchTrueFalseTotal
if (!realpath) throw std::invalid_argument(string("can't register module '") + name() + "': source module '" + source.name() + "' hasn't been registered"); 0 107 107
if (!realpath) throw std::invalid_argument(string("can't register module '") + name() + "': source module '" + source.name() + "' hasn't been registered"); 0 0 0
if (!realpath) throw std::invalid_argument(string("can't register module '") + name() + "': source module '" + source.name() + "' hasn't been registered"); 0 0 0
if (!realpath) throw std::invalid_argument(string("can't register module '") + name() + "': source module '" + source.name() + "' hasn't been registered"); 0 0 0
if (!realpath) throw std::invalid_argument(string("can't register module '") + name() + "': source module '" + source.name() + "' hasn't been registered"); 0 0 0
if (!realpath) throw std::invalid_argument(string("can't register module '") + name() + "': source module '" + source.name() + "' hasn't been registered"); 0 0 0
if (!realpath) throw std::invalid_argument(string("can't register module '") + name() + "': source module '" + source.name() + "' hasn't been registered"); 0 0 0
if (!realpath) throw std::invalid_argument(string("can't register module '") + name() + "': source module '" + source.name() + "' hasn't been registered"); 0 0 0
if (!realpath) throw std::invalid_argument(string("can't register module '") + name() + "': source module '" + source.name() + "' hasn't been registered"); 0 0 0
97 107 ▶ inc.store(path(), realpath);
Branch: 2 branches
BranchTrueFalseTotal
inc.store(path(), realpath); 107 0 107
inc.store(path(), realpath); 107 0 107
98 107 }
99  
100 631 void Stash::inherit (const Stash& parent) {
101 631 ▶ auto ISA = array("ISA");
Branch: 1 branch
BranchTrueFalseTotal
auto ISA = array("ISA"); 631 0 631
102 631 ▶ if (!ISA) { // we must create @ISA via gv_fetchpvn_flags, because perl is written like a monkey's shit
Branch: 1 branch
BranchTrueFalseTotal
if (!ISA) { // we must create @ISA via gv_fetchpvn_flags, because perl is written like a monkey's shit 561 70 631
103 561 ▶ auto fqn = string(name()) + "::ISA";
Branch: 3 branches
BranchTrueFalseTotal
auto fqn = string(name()) + "::ISA"; 561 0 561
auto fqn = string(name()) + "::ISA"; 561 0 561
auto fqn = string(name()) + "::ISA"; 561 0 561
104 561 ▶ ISA = GvAV(gv_fetchpvn_flags(fqn.data(), fqn.length(), GV_ADD, SVt_PVAV));
Branch: 2 branches
BranchTrueFalseTotal
ISA = GvAV(gv_fetchpvn_flags(fqn.data(), fqn.length(), GV_ADD, SVt_PVAV)); 561 0 561
ISA = GvAV(gv_fetchpvn_flags(fqn.data(), fqn.length(), GV_ADD, SVt_PVAV)); 561 0 561
105 561 }
106 631 ▶ av_push(ISA, Simple::shared(parent.name()).detach()); // can't use ISA.push() syntax, because @ISA is a magical array, otherwise MRO cache won't be cleared
Branch: 3 branches
BranchTrueFalseTotal
av_push(ISA, Simple::shared(parent.name()).detach()); // can't use ISA.push() syntax, because @ISA is a magical array, otherwise MRO cache won't be cleared 631 0 631
av_push(ISA, Simple::shared(parent.name()).detach()); // can't use ISA.push() syntax, because @ISA is a magical array, otherwise MRO cache won't be cleared 631 0 631
av_push(ISA, Simple::shared(parent.name()).detach()); // can't use ISA.push() syntax, because @ISA is a magical array, otherwise MRO cache won't be cleared 631 0 631
107 631 }
108  
109 2 void Stash::_throw_nomethod (const panda::string_view& name) const {
110 2 ▶ throw std::invalid_argument(panda::string("can't locate method '") + name + "' via package '" + this->name() + "'");
Branch: 7 branches
BranchTrueFalseTotal
throw std::invalid_argument(panda::string("can't locate method '") + name + "' via package '" + this->name() + "'"); 2 0 2
throw std::invalid_argument(panda::string("can't locate method '") + name + "' via package '" + this->name() + "'"); 2 0 2
throw std::invalid_argument(panda::string("can't locate method '") + name + "' via package '" + this->name() + "'"); 2 0 2
throw std::invalid_argument(panda::string("can't locate method '") + name + "' via package '" + this->name() + "'"); 2 0 2
throw std::invalid_argument(panda::string("can't locate method '") + name + "' via package '" + this->name() + "'"); 2 0 2
throw std::invalid_argument(panda::string("can't locate method '") + name + "' via package '" + this->name() + "'"); 2 0 2
throw std::invalid_argument(panda::string("can't locate method '") + name + "' via package '" + this->name() + "'"); 2 0 2
111 }
112  
113 4 bool Stash::isa (const panda::string_view& parent, U32 hash, int flags) const {
114 4 ▶ if (name() == parent) return TRUE;
Branch: 1 branch
BranchTrueFalseTotal
if (name() == parent) return TRUE; 0 4 4
115  
116 4 ▶ const struct mro_meta*const meta = HvMROMETA((HV*)sv);
Branch: 1 branch
BranchTrueFalseTotal
const struct mro_meta*const meta = HvMROMETA((HV*)sv); 4 0 4
117 4 HV *isa = meta->isa;
118  
119 4 ▶ if (!isa) {
Branch: 1 branch
BranchTrueFalseTotal
if (!isa) { 0 4 4
120 0 (void)mro_get_linear_isa((HV*)sv);
121 0 isa = meta->isa;
122 }
123  
124 4 ▶ if (hv_common(isa, NULL, parent.data(), parent.length(), flags, HV_FETCH_ISEXISTS, NULL, hash)) return TRUE;
Branch: 1 branch
BranchTrueFalseTotal
if (hv_common(isa, NULL, parent.data(), parent.length(), flags, HV_FETCH_ISEXISTS, NULL, hash)) return TRUE; 2 2 4
125 2 return FALSE;
126  
127 }
128  
129 0 Object Stash::bless () const {
130 0 return Object(sv_bless(newRV_noinc(newSV_type(SVt_PVMG)), (HV*)sv), NONE);
131 }
132  
133 2965 Object Stash::bless (const Sv& what) const {
134 2965 ▶ if (SvROK(what)) return sv_bless(what, (HV*)sv);
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(what)) return sv_bless(what, (HV*)sv); 3 2962 2965
135 2962 else return Object(sv_bless(newRV(what), (HV*)sv), NONE);
136 }
137  
138 6033 void Stash::add_const_sub (const panda::string_view& name, const Sv& _val) {
139 6033 ▶ auto val = _val;
Branch: 1 branch
BranchTrueFalseTotal
auto val = _val; 6033 0 6033
140 6033 val.readonly(true);
141 6033 ▶ newCONSTSUB_flags((HV*)sv, name.data(), name.length(), 0, val.detach()); // detach because newCONSTSUB doesn't increment refcnt
Branch: 1 branch
BranchTrueFalseTotal
newCONSTSUB_flags((HV*)sv, name.data(), name.length(), 0, val.detach()); // detach because newCONSTSUB doesn't increment refcnt 6033 0 6033
142 6033 }
143  
144 }
« regerror.cc ↑ Summary t/typemap/object/child.xsi »