include/context_vars/context_vars_var.h

filter: Statementstmt 100% 131 / 131 Branchbran 90.0% 90 / 100 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 95.6% 221 / 231 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/context_vars/context_vars_deep.h ↑ Summary include/context_vars/context_vars_ctx.h »
1 #ifndef CONTEXT_VARS_VAR_H
2 #define CONTEXT_VARS_VAR_H
3  
4 /* The imported scalar.
5 *
6 * There is one scalar in the interpreter for each name, and every package
7 * that imports the name has it in its own glob. The scalar holds no value of
8 * its own. It carries magic, recognised by the address of the vtable below,
9 * with the name hanging off it: a read fetches the name from the current
10 * context into the scalar, and an assignment stores what was assigned under
11 * the name in the current context. The scalar's own buffer is where one
12 * read lands and is never the truth.
13 *
14 * A reference left in that buffer is let go at the end of the statement
15 * that read or assigned it. The buffer outlives that statement, and a
16 * reference kept there would hold an object alive after every context
17 * holding it had let it go, for as long as nothing read or assigned the
18 * variable again. A copy taken from the variable is an ordinary reference.
19 *
20 * It is NOT done by weakening the buffer's reference, which was the first
21 * way: when the object goes, perl clears every weak reference to it and
22 * calls set magic on each, which here would unset the variable in whatever
23 * context is current - the caller's, at the end of a sub that had replaced
24 * the reference.
25 *
26 * ---- local ---------------------------------------------------------------
27 *
28 * `local $name` works, and needs two things from here that are easy to get
29 * wrong. perl makes a NEW scalar for the glob, copies this magic to it, and
30 * calls set on it with PL_localizing at 1: that is the moment to remember
31 * what the context holds under the name. On the way out perl puts the OLD
32 * scalar back and calls set on it with PL_localizing at 2, and the old
33 * scalar holds whatever was last READ into it, which may be the value of a
34 * different context altogether. So that call ignores the scalar and puts
35 * back what was remembered. */
36  
37 static int context_vars_var_get(pTHX_ SV *sv, MAGIC *mg);
38 static int context_vars_var_set(pTHX_ SV *sv, MAGIC *mg);
39  
40 static MGVTBL context_vars_var_vtbl = {
41 context_vars_var_get, context_vars_var_set, 0, 0, 0
42 };
43  
44 /* The magic that makes `sv` one of ours, or NULL. By hand: mg_findext is
45 * 5.14. */
46 12283 static MAGIC *context_vars_var_mg(SV *sv) {
47 MAGIC *mg;
48 12283 ▶ if (!sv || SvTYPE(sv) < SVt_PVMG) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || SvTYPE(sv) < SVt_PVMG) return NULL; 11512 771 12283
if (!sv || SvTYPE(sv) < SVt_PVMG) return NULL; 10211 1301 11512
49 2417 ▶ for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic)
Branch: 1 branch
BranchTrueFalseTotal
for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) 1301 1116 2417
50 1301 ▶ if (mg->mg_type == PERL_MAGIC_ext
Branch: 1 branch
BranchTrueFalseTotal
if (mg->mg_type == PERL_MAGIC_ext 185 1116 1301
51 185 ▶ && mg->mg_virtual == &context_vars_var_vtbl)
Branch: 1 branch
BranchTrueFalseTotal
&& mg->mg_virtual == &context_vars_var_vtbl) 185 0 185
52 185 return mg;
53 1116 return NULL;
54 }
55  
56 /* A mortal that, when the statement's temporaries are freed, takes the
57 * reference out of the variable's buffer without calling its magic. Taking it
58 * out early is harmless: the buffer is never the truth, and the next read
59 * fetches again. */
60 77 static int context_vars_unhold_free(pTHX_ SV *sv, MAGIC *mg) {
61 77 SV *var = (SV *)mg->mg_ptr;
62 PERL_UNUSED_ARG(sv);
63 77 ▶ if (var) {
Branch: 1 branch
BranchTrueFalseTotal
if (var) { 77 0 77
64 77 mg->mg_ptr = NULL;
65 77 ▶ if (SvROK(var)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(var)) { 72 5 77
66 72 SV *target = SvRV(var);
67 72 SvRV_set(var, NULL);
68 72 SvROK_off(var);
69 72 SvREFCNT_dec(target);
70 }
71 77 SvREFCNT_dec(var);
72 }
73 77 return 0;
74 }
75  
76 static MGVTBL context_vars_unhold_vtbl = { 0, 0, 0, 0, context_vars_unhold_free };
77  
78 17173 static void context_vars_var_unhold(pTHX_ SV *sv) {
79 SV *later;
80 17173 ▶ if (!SvROK(sv)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(sv)) return; 17096 77 17173
81 77 later = sv_newmortal();
82 77 SvREFCNT_inc_simple_void_NN(sv);
83 77 sv_magicext(later, NULL, PERL_MAGIC_ext, &context_vars_unhold_vtbl, (const char *)sv, 0);
84 }
85  
86 10597 static int context_vars_var_get(pTHX_ SV *sv, MAGIC *mg) {
87 10597 SV *held = context_vars_fetch(aTHX_ mg->mg_ptr, (STRLEN)mg->mg_len);
88 10639 ▶ if (held && (context_vars_is_map(aTHX_ held) || context_vars_is_vec(aTHX_ held))) {
Branch: 3 branches
BranchTrueFalseTotal
if (held && (context_vars_is_map(aTHX_ held) || context_vars_is_vec(aTHX_ held))) { 10559 38 10597
if (held && (context_vars_is_map(aTHX_ held) || context_vars_is_vec(aTHX_ held))) { 10521 38 10559
if (held && (context_vars_is_map(aTHX_ held) || context_vars_is_vec(aTHX_ held))) { 4 10517 10521
89 42 AV *path = newAV();
90 42 av_push(path, newSVpvn(mg->mg_ptr, mg->mg_len));
91 42 sv_setsv(sv, context_vars_out(aTHX_ held, path));
92 }
93 10555 ▶ else if (held) sv_setsv(sv, held);
Branch: 1 branch
BranchTrueFalseTotal
else if (held) sv_setsv(sv, held); 10517 38 10555
94 38 else sv_setsv(sv, &PL_sv_undef);
95 10597 context_vars_var_unhold(aTHX_ sv);
96 10597 return 0;
97 }
98  
99 /* Assigning undef unsets the name. A name that is not set reads undef, so
100 * the two are the same to a reader and the context stays as small as what
101 * is actually in it. A plain hash or array, or a proxy, is stored as context
102 * data, at every depth. */
103 10770 static void context_vars_var_put(pTHX_ const char *name, STRLEN len, SV *value) {
104 21512 ▶ if (value && SvOK(value)) {
Branch: 2 branches
BranchTrueFalseTotal
if (value && SvOK(value)) { 10767 3 10770
if (value && SvOK(value)) { 10742 25 10767
105 10742 SV *conv = context_vars_in(aTHX_ value);
106 10742 context_vars_writable(aTHX);
107 10742 ▶ context_vars_store(aTHX_ name, len, conv ? conv : value);
Branch: 1 branch
BranchTrueFalseTotal
context_vars_store(aTHX_ name, len, conv ? conv : value); 16 10726 10742
108 10742 ▶ if (conv) SvREFCNT_dec(conv);
Branch: 1 branch
BranchTrueFalseTotal
if (conv) SvREFCNT_dec(conv); 16 10726 10742
109 }
110 28 ▶ else if (context_vars_fetch(aTHX_ name, len)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (context_vars_fetch(aTHX_ name, len)) { 22 6 28
111 22 context_vars_writable(aTHX);
112 22 context_vars_remove(aTHX_ name, len);
113 }
114 10770 }
115  
116 6590 static int context_vars_var_set(pTHX_ SV *sv, MAGIC *mg) {
117 dMY_CXT;
118 6590 const char *name = mg->mg_ptr;
119 6590 STRLEN len = (STRLEN)mg->mg_len;
120  
121 6590 ▶ if (PL_localizing == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (PL_localizing == 1) { 14 6576 6590
122 14 SV *held = context_vars_fetch(aTHX_ name, len);
123 14 ▶ av_push(MY_CXT.locals, held ? newSVsv(held) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
av_push(MY_CXT.locals, held ? newSVsv(held) : newSV(0)); 13 1 14
124 14 context_vars_var_put(aTHX_ name, len, sv);
125 14 context_vars_var_unhold(aTHX_ sv);
126 14 return 0;
127 }
128 6576 ▶ if (PL_localizing == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (PL_localizing == 2) { 14 6562 6576
129 14 SV *was = av_pop(MY_CXT.locals);
130 14 context_vars_var_put(aTHX_ name, len, was);
131 14 SvREFCNT_dec(was);
132 14 return 0;
133 }
134 6562 context_vars_var_put(aTHX_ name, len, sv);
135 6562 ▶ if (context_vars_is_fresh(aTHX_ sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (context_vars_is_fresh(aTHX_ sv)) { 3 6559 6562
136 /* perl 5.14 and older vivify $v->{k} by putting a fresh plain hash
137 * straight into $v and then writing into it: tie that hash in place,
138 * as a store through a proxy does (context_vars_tie_fresh). */
139 3 AV *path = (AV *)sv_2mortal((SV *)newAV());
140 3 av_push(path, newSVpvn(name, len));
141 3 context_vars_tie_fresh(aTHX_ sv, path);
142 }
143 6562 context_vars_var_unhold(aTHX_ sv);
144 6562 return 0;
145 }
146  
147 /* ---- import --------------------------------------------------------------- */
148  
149 /* A name is `$`, `@` or `%` and then an identifier, in ASCII. Returns the
150 * length of the identifier, or 0 when `arg` is anything else. */
151 116 static STRLEN context_vars_name_ok(const char *arg, STRLEN len) {
152 STRLEN i;
153 116 ▶ if (len < 2 || (arg[0] != '$' && arg[0] != '@' && arg[0] != '%')) return 0;
Branch: 4 branches
BranchTrueFalseTotal
if (len < 2 || (arg[0] != '$' && arg[0] != '@' && arg[0] != '%')) return 0; 112 4 116
if (len < 2 || (arg[0] != '$' && arg[0] != '@' && arg[0] != '%')) return 0; 25 87 112
if (len < 2 || (arg[0] != '$' && arg[0] != '@' && arg[0] != '%')) return 0; 14 11 25
if (len < 2 || (arg[0] != '$' && arg[0] != '@' && arg[0] != '%')) return 0; 3 11 14
154 109 ▶ if (!(isALPHA(arg[1]) || arg[1] == '_')) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!(isALPHA(arg[1]) || arg[1] == '_')) return 0; 8 101 109
if (!(isALPHA(arg[1]) || arg[1] == '_')) return 0; 4 4 8
155 397 ▶ for (i = 2; i < len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i < len; i++) 297 100 397
156 297 ▶ if (!(isALNUM(arg[i]))) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!(isALNUM(arg[i]))) return 0; 5 292 297
157 100 return len - 1;
158 }
159  
160 /* Names perl keeps in main:: whatever package they are written in. A glob
161 * of that name in the caller's package would be made and never looked at. */
162 100 static int context_vars_name_forced(const char *n, STRLEN len) {
163 static const char *const forced[] = {
164 "_", "ENV", "INC", "ARGV", "ARGVOUT", "STDIN", "STDOUT", "STDERR", "SIG", NULL
165 };
166 int i;
167 918 ▶ for (i = 0; forced[i]; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; forced[i]; i++) 832 86 918
168 832 ▶ if (strlen(forced[i]) == len && memEQ(forced[i], n, len)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (strlen(forced[i]) == len && memEQ(forced[i], n, len)) return 1; 95 737 832
if (strlen(forced[i]) == len && memEQ(forced[i], n, len)) return 1; 14 81 95
169 86 return 0;
170 }
171  
172 /* The one scalar for a name in this interpreter, made the first time the
173 * name is imported. They are kept in a package hash and not in C, so that a
174 * new thread gets its own, already pointing at its own scalars. */
175 72 static SV *context_vars_var_for(pTHX_ const char *name, STRLEN len) {
176 72 HV *reg = get_hv("Context::Vars::_VARS", GV_ADD);
177 72 SV **svp = hv_fetch(reg, name, (I32)len, 0);
178 SV *sv;
179  
180 72 ▶ if (svp && SvROK(*svp) && context_vars_var_mg(SvRV(*svp)))
Branch: 3 branches
BranchTrueFalseTotal
if (svp && SvROK(*svp) && context_vars_var_mg(SvRV(*svp))) 6 66 72
if (svp && SvROK(*svp) && context_vars_var_mg(SvRV(*svp))) 6 0 6
if (svp && SvROK(*svp) && context_vars_var_mg(SvRV(*svp))) 6 0 6
181 6 return SvRV(*svp);
182  
183 66 sv = newSV(0);
184 66 ▶ SvUPGRADE(sv, SVt_PVMG);
Branch: 1 branch
BranchTrueFalseTotal
SvUPGRADE(sv, SVt_PVMG); 66 0 66
185 66 sv_magicext(sv, NULL, PERL_MAGIC_ext, &context_vars_var_vtbl, name, (I32)len);
186 66 (void)hv_store(reg, name, (I32)len, newRV_noinc(sv), 0);
187 66 return sv;
188 }
189  
190 /* The one array or hash for a name in this interpreter: a container tied to a
191 * proxy for its context name, "@name" or "%name", and marked so that `local`
192 * on it keeps the context's value. Kept in the same package hash as the
193 * scalars. */
194 14 static SV *context_vars_container_for(pTHX_ const char *cname, STRLEN clen, int is_hash) {
195 14 HV *reg = get_hv("Context::Vars::_VARS", GV_ADD);
196 14 SV **svp = hv_fetch(reg, cname, (I32)clen, 0);
197 SV *c;
198 AV *path;
199  
200 14 ▶ if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == (is_hash ? SVt_PVHV : SVt_PVAV))
Branch: 4 branches
BranchTrueFalseTotal
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == (is_hash ? SVt_PVHV : SVt_PVAV)) 5 9 14
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == (is_hash ? SVt_PVHV : SVt_PVAV)) 5 0 5
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == (is_hash ? SVt_PVHV : SVt_PVAV)) 2 3 5
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == (is_hash ? SVt_PVHV : SVt_PVAV)) 5 0 5
201 5 return SvRV(*svp);
202  
203 9 ▶ c = is_hash ? (SV *)newHV() : (SV *)newAV();
Branch: 1 branch
BranchTrueFalseTotal
c = is_hash ? (SV *)newHV() : (SV *)newAV(); 5 4 9
204 9 path = newAV();
205 9 av_push(path, newSVpvn(cname, clen));
206 9 context_vars_tie(aTHX_ c, path);
207 9 context_vars_mark_container(aTHX_ c, cname, clen);
208 9 (void)hv_store(reg, cname, (I32)clen, newRV_noinc(c), 0);
209 9 return c;
210 }
211  
212 /* Puts the scalar, array or hash for `arg` into `stash` under its name,
213 * marked as imported so that `use strict` accepts the name there as it
214 * accepts one from `use vars`. Croaks, naming the argument, when it is not a
215 * name. */
216 116 static void context_vars_import_one(pTHX_ HV *stash, SV *argsv) {
217 STRLEN alen, len;
218 116 const char *arg = SvPV(argsv, alen);
219 const char *name;
220 SV *sv;
221 SV **gvp;
222 GV *gv;
223  
224 116 len = context_vars_name_ok(arg, alen);
225 116 ▶ if (!len)
Branch: 1 branch
BranchTrueFalseTotal
if (!len) 16 100 116
226 16 croak("Context::Vars: cannot import '%" SVf "': a variable is a "
227 "scalar, array or hash with a plain name, such as $request_id", SVfARG(argsv));
228 100 name = arg + 1;
229 100 ▶ if (context_vars_name_forced(name, len))
Branch: 1 branch
BranchTrueFalseTotal
if (context_vars_name_forced(name, len)) 14 86 100
230 14 croak("Context::Vars: cannot import '%" SVf "': perl keeps that name "
231 "in package main", SVfARG(argsv));
232  
233 86 gvp = hv_fetch(stash, name, (I32)len, 1);
234 86 ▶ if (!gvp) croak("Context::Vars: cannot make a symbol for '%" SVf "'", SVfARG(argsv));
Branch: 1 branch
BranchTrueFalseTotal
if (!gvp) croak("Context::Vars: cannot make a symbol for '%" SVf "'", SVfARG(argsv)); 0 86 86
235 86 gv = (GV *)*gvp;
236 86 ▶ if (SvTYPE(gv) != SVt_PVGV)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(gv) != SVt_PVGV) 83 3 86
237 83 gv_init(gv, stash, name, len, GV_ADDMULTI);
238  
239 86 ▶ if (arg[0] == '$') {
Branch: 1 branch
BranchTrueFalseTotal
if (arg[0] == '$') { 72 14 86
240 72 sv = context_vars_var_for(aTHX_ name, len);
241 72 ▶ if (GvSV(gv) != sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (GvSV(gv) != sv) { 71 1 72
242 71 SV *old = GvSV(gv);
243 71 GvSV(gv) = SvREFCNT_inc_simple_NN(sv);
244 71 SvREFCNT_dec(old);
245 }
246 72 GvIMPORTED_SV_on(gv);
247 }
248 14 ▶ else if (arg[0] == '%') {
Branch: 1 branch
BranchTrueFalseTotal
else if (arg[0] == '%') { 7 7 14
249 7 sv = context_vars_container_for(aTHX_ arg, alen, 1);
250 7 ▶ if ((SV *)GvHV(gv) != sv) {
Branch: 1 branch
BranchTrueFalseTotal
if ((SV *)GvHV(gv) != sv) { 7 0 7
251 7 HV *old = GvHV(gv);
252 7 GvHV(gv) = (HV *)SvREFCNT_inc_simple_NN(sv);
253 7 SvREFCNT_dec((SV *)old);
254 }
255 7 GvIMPORTED_HV_on(gv);
256 }
257 else {
258 7 sv = context_vars_container_for(aTHX_ arg, alen, 0);
259 7 ▶ if ((SV *)GvAV(gv) != sv) {
Branch: 1 branch
BranchTrueFalseTotal
if ((SV *)GvAV(gv) != sv) { 7 0 7
260 7 AV *old = GvAV(gv);
261 7 GvAV(gv) = (AV *)SvREFCNT_inc_simple_NN(sv);
262 7 SvREFCNT_dec((SV *)old);
263 }
264 7 GvIMPORTED_AV_on(gv);
265 }
266 86 GvMULTI_on(gv);
267 86 }
268  
269 #endif
« include/context_vars/context_vars_deep.h ↑ Summary include/context_vars/context_vars_ctx.h »