include/context_vars/context_vars_var.h

filter: Statementstmt 100% 95 / 95 Branchbran 89.3% 59 / 66 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 95.6% 154 / 161 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/context_vars/context_vars_hamt.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 8056 static MAGIC *context_vars_var_mg(SV *sv) {
47 MAGIC *mg;
48 8056 ▶ if (!sv || SvTYPE(sv) < SVt_PVMG) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || SvTYPE(sv) < SVt_PVMG) return NULL; 8056 0 8056
if (!sv || SvTYPE(sv) < SVt_PVMG) return NULL; 6772 1284 8056
49 2390 ▶ for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic)
Branch: 1 branch
BranchTrueFalseTotal
for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) 1284 1106 2390
50 1284 ▶ if (mg->mg_type == PERL_MAGIC_ext
Branch: 1 branch
BranchTrueFalseTotal
if (mg->mg_type == PERL_MAGIC_ext 178 1106 1284
51 178 ▶ && mg->mg_virtual == &context_vars_var_vtbl)
Branch: 1 branch
BranchTrueFalseTotal
&& mg->mg_virtual == &context_vars_var_vtbl) 178 0 178
52 178 return mg;
53 1106 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 61 static int context_vars_unhold_free(pTHX_ SV *sv, MAGIC *mg) {
61 61 SV *var = (SV *)mg->mg_ptr;
62 PERL_UNUSED_ARG(sv);
63 61 ▶ if (var) {
Branch: 1 branch
BranchTrueFalseTotal
if (var) { 61 0 61
64 61 mg->mg_ptr = NULL;
65 61 ▶ if (SvROK(var)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(var)) { 59 2 61
66 59 SV *target = SvRV(var);
67 59 SvRV_set(var, NULL);
68 59 SvROK_off(var);
69 59 SvREFCNT_dec(target);
70 }
71 61 SvREFCNT_dec(var);
72 }
73 61 return 0;
74 }
75  
76 static MGVTBL context_vars_unhold_vtbl = { 0, 0, 0, 0, context_vars_unhold_free };
77  
78 17155 static void context_vars_var_unhold(pTHX_ SV *sv) {
79 SV *later;
80 17155 ▶ if (!SvROK(sv)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(sv)) return; 17094 61 17155
81 61 later = sv_newmortal();
82 61 SvREFCNT_inc_simple_void_NN(sv);
83 61 sv_magicext(later, NULL, PERL_MAGIC_ext, &context_vars_unhold_vtbl, (const char *)sv, 0);
84 }
85  
86 10583 static int context_vars_var_get(pTHX_ SV *sv, MAGIC *mg) {
87 10583 SV *held = context_vars_fetch(aTHX_ mg->mg_ptr, (STRLEN)mg->mg_len);
88 10583 ▶ if (held) sv_setsv(sv, held);
Branch: 1 branch
BranchTrueFalseTotal
if (held) sv_setsv(sv, held); 10547 36 10583
89 36 else sv_setsv(sv, &PL_sv_undef);
90 10583 context_vars_var_unhold(aTHX_ sv);
91 10583 return 0;
92 }
93  
94 /* Assigning undef unsets the name. A name that is not set reads undef, so
95 * the two are the same to a reader and the context stays as small as what
96 * is actually in it. */
97 6610 static void context_vars_var_put(pTHX_ const char *name, STRLEN len, SV *value) {
98 6610 ▶ if (value && SvOK(value)) {
Branch: 2 branches
BranchTrueFalseTotal
if (value && SvOK(value)) { 6607 3 6610
if (value && SvOK(value)) { 6582 25 6607
99 6582 context_vars_writable(aTHX);
100 6582 context_vars_store(aTHX_ name, len, value);
101 }
102 28 ▶ else if (context_vars_fetch(aTHX_ name, len)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (context_vars_fetch(aTHX_ name, len)) { 22 6 28
103 22 context_vars_writable(aTHX);
104 22 context_vars_remove(aTHX_ name, len);
105 }
106 6610 }
107  
108 6586 static int context_vars_var_set(pTHX_ SV *sv, MAGIC *mg) {
109 dMY_CXT;
110 6586 const char *name = mg->mg_ptr;
111 6586 STRLEN len = (STRLEN)mg->mg_len;
112  
113 6586 ▶ if (PL_localizing == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (PL_localizing == 1) { 14 6572 6586
114 14 SV *held = context_vars_fetch(aTHX_ name, len);
115 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
116 14 context_vars_var_put(aTHX_ name, len, sv);
117 14 context_vars_var_unhold(aTHX_ sv);
118 14 return 0;
119 }
120 6572 ▶ if (PL_localizing == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (PL_localizing == 2) { 14 6558 6572
121 14 SV *was = av_pop(MY_CXT.locals);
122 14 context_vars_var_put(aTHX_ name, len, was);
123 14 SvREFCNT_dec(was);
124 14 return 0;
125 }
126 6558 context_vars_var_put(aTHX_ name, len, sv);
127 6558 context_vars_var_unhold(aTHX_ sv);
128 6558 return 0;
129 }
130  
131 /* ---- import --------------------------------------------------------------- */
132  
133 /* A name is `$` and then an identifier, in ASCII. Returns the length of the
134 * identifier, or 0 when `arg` is anything else. */
135 92 static STRLEN context_vars_name_ok(const char *arg, STRLEN len) {
136 STRLEN i;
137 92 ▶ if (len < 2 || arg[0] != '$') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (len < 2 || arg[0] != '$') return 0; 90 2 92
if (len < 2 || arg[0] != '$') return 0; 5 85 90
138 85 ▶ if (!(isALPHA(arg[1]) || arg[1] == '_')) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!(isALPHA(arg[1]) || arg[1] == '_')) return 0; 5 80 85
if (!(isALPHA(arg[1]) || arg[1] == '_')) return 0; 2 3 5
139 301 ▶ for (i = 2; i < len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i < len; i++) 223 78 301
140 223 ▶ if (!(isALNUM(arg[i]))) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!(isALNUM(arg[i]))) return 0; 5 218 223
141 78 return len - 1;
142 }
143  
144 /* Names perl keeps in main:: whatever package they are written in. A glob
145 * of that name in the caller's package would be made and never looked at. */
146 78 static int context_vars_name_forced(const char *n, STRLEN len) {
147 static const char *const forced[] = {
148 "_", "ENV", "INC", "ARGV", "ARGVOUT", "STDIN", "STDOUT", "STDERR", NULL
149 };
150 int i;
151 666 ▶ for (i = 0; forced[i]; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; forced[i]; i++) 596 70 666
152 596 ▶ 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; 65 531 596
if (strlen(forced[i]) == len && memEQ(forced[i], n, len)) return 1; 8 57 65
153 70 return 0;
154 }
155  
156 /* The one scalar for a name in this interpreter, made the first time the
157 * name is imported. They are kept in a package hash and not in C, so that a
158 * new thread gets its own, already pointing at its own scalars. */
159 70 static SV *context_vars_var_for(pTHX_ const char *name, STRLEN len) {
160 70 HV *reg = get_hv("Context::Vars::_VARS", GV_ADD);
161 70 SV **svp = hv_fetch(reg, name, (I32)len, 0);
162 SV *sv;
163  
164 70 ▶ 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 64 70
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
165 6 return SvRV(*svp);
166  
167 64 sv = newSV(0);
168 64 ▶ SvUPGRADE(sv, SVt_PVMG);
Branch: 1 branch
BranchTrueFalseTotal
SvUPGRADE(sv, SVt_PVMG); 64 0 64
169 64 sv_magicext(sv, NULL, PERL_MAGIC_ext, &context_vars_var_vtbl, name, (I32)len);
170 64 (void)hv_store(reg, name, (I32)len, newRV_noinc(sv), 0);
171 64 return sv;
172 }
173  
174 /* Puts the scalar for `arg` into `stash` under its name, marked as imported
175 * so that `use strict` accepts the name there as it accepts one from
176 * `use vars`. Croaks, naming the argument, when it is not a name. */
177 92 static void context_vars_import_one(pTHX_ HV *stash, SV *argsv) {
178 STRLEN alen, len;
179 92 const char *arg = SvPV(argsv, alen);
180 const char *name;
181 SV *sv;
182 SV **gvp;
183 GV *gv;
184  
185 92 len = context_vars_name_ok(arg, alen);
186 92 ▶ if (!len)
Branch: 1 branch
BranchTrueFalseTotal
if (!len) 14 78 92
187 14 croak("Context::Vars: cannot import '%" SVf "': a variable is a "
188 "scalar with a plain name, such as $request_id", SVfARG(argsv));
189 78 name = arg + 1;
190 78 ▶ if (context_vars_name_forced(name, len))
Branch: 1 branch
BranchTrueFalseTotal
if (context_vars_name_forced(name, len)) 8 70 78
191 8 croak("Context::Vars: cannot import '%" SVf "': perl keeps that name "
192 "in package main", SVfARG(argsv));
193  
194 70 sv = context_vars_var_for(aTHX_ name, len);
195 70 gvp = hv_fetch(stash, name, (I32)len, 1);
196 70 ▶ 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 70 70
197 70 gv = (GV *)*gvp;
198 70 ▶ if (SvTYPE(gv) != SVt_PVGV)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(gv) != SVt_PVGV) 67 3 70
199 67 gv_init(gv, stash, name, len, GV_ADDMULTI);
200  
201 70 ▶ if (GvSV(gv) != sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (GvSV(gv) != sv) { 69 1 70
202 69 SV *old = GvSV(gv);
203 69 GvSV(gv) = SvREFCNT_inc_simple_NN(sv);
204 69 SvREFCNT_dec(old);
205 }
206 70 GvIMPORTED_SV_on(gv);
207 70 GvMULTI_on(gv);
208 70 }
209  
210 #endif
« include/context_vars/context_vars_hamt.h ↑ Summary include/context_vars/context_vars_ctx.h »