include/context_vars/context_vars_deep.h

filter: Statementstmt 98.2% 230 / 234 Branchbran 72.5% 161 / 222 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 85.7% 391 / 456 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« Vars.xs ↑ Summary include/context_vars/context_vars_var.h »
1 #ifndef CONTEXT_VARS_DEEP_H
2 #define CONTEXT_VARS_DEEP_H
3  
4 /* Deep context data: arrays, hashes, and whatever plain hashes and arrays a
5 * variable holds at any depth.
6 *
7 * In a context, a plain hash is held as a HAMT map and a plain array as a
8 * HAMT vector, at every level, as objects of two classes of this dist's own
9 * so that a HAMT object a program stores is never mistaken for context data.
10 * Blessed objects and code are never converted: they are kept as references
11 * and shared, as their own classes expect.
12 *
13 * A change at any depth copies the nodes on the path from the variable down
14 * to what changed - a few small nodes for each level - and stores the new top
15 * under the variable's name through the variable's own rules, so that the
16 * change belongs to the current context and every other context keeps what
17 * it had. That is what makes "an assignment holds until the sub it is
18 * written in returns" true of $seen{a}{b} as of $scalar.
19 *
20 * What a program reads is a PROXY: an array or hash tied to a path - the
21 * variable's name and then each key or index - which it resolves against the
22 * current context at each access.
23 *
24 * Names in the context: a scalar under its bare name, an array under "@name",
25 * a hash under "%name". */
26  
27 static void context_vars_var_put(pTHX_ const char *name, STRLEN len, SV *value);
28  
29 #define CONTEXT_VARS_DEEP_MAX 4096 /* levels before a structure is called circular */
30  
31 /* ---- nodes ------------------------------------------------------------------- */
32  
33 33572 static int context_vars_is_map(pTHX_ SV *v) {
34 dMY_CXT;
35 33572 ▶ return v && SvROK(v) && SvOBJECT(SvRV(v)) && SvSTASH(SvRV(v)) == MY_CXT.map_stash;
Branch: 4 branches
BranchTrueFalseTotal
return v && SvROK(v) && SvOBJECT(SvRV(v)) && SvSTASH(SvRV(v)) == MY_CXT.map_stash; 33555 17 33572
return v && SvROK(v) && SvOBJECT(SvRV(v)) && SvSTASH(SvRV(v)) == MY_CXT.map_stash; 20843 12712 33555
return v && SvROK(v) && SvOBJECT(SvRV(v)) && SvSTASH(SvRV(v)) == MY_CXT.map_stash; 20840 3 20843
return v && SvROK(v) && SvOBJECT(SvRV(v)) && SvSTASH(SvRV(v)) == MY_CXT.map_stash; 20686 154 20840
36 }
37  
38 19239 static int context_vars_is_vec(pTHX_ SV *v) {
39 dMY_CXT;
40 19239 ▶ return v && SvROK(v) && SvOBJECT(SvRV(v)) && SvSTASH(SvRV(v)) == MY_CXT.vec_stash;
Branch: 4 branches
BranchTrueFalseTotal
return v && SvROK(v) && SvOBJECT(SvRV(v)) && SvSTASH(SvRV(v)) == MY_CXT.vec_stash; 19225 14 19239
return v && SvROK(v) && SvOBJECT(SvRV(v)) && SvSTASH(SvRV(v)) == MY_CXT.vec_stash; 6513 12712 19225
return v && SvROK(v) && SvOBJECT(SvRV(v)) && SvSTASH(SvRV(v)) == MY_CXT.vec_stash; 6510 3 6513
return v && SvROK(v) && SvOBJECT(SvRV(v)) && SvSTASH(SvRV(v)) == MY_CXT.vec_stash; 407 6103 6510
41 }
42  
43 /* Objects owning the struct, with one reference the caller owns. */
44 6143 static SV *context_vars_map_sv(pTHX_ hamt_map *m) {
45 6143 ▶ if (!m) croak("Context::Vars: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!m) croak("Context::Vars: out of memory"); 0 6143 6143
46 6143 return (CONTEXT_VARS_HAMT->map_to_sv)(aTHX_ m, "Context::Vars::Data::Map");
47 }
48  
49 68 static SV *context_vars_vec_sv(pTHX_ hamt_vec *v) {
50 68 ▶ if (!v) croak("Context::Vars: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!v) croak("Context::Vars: out of memory"); 0 68 68
51 68 return (CONTEXT_VARS_HAMT->vec_to_sv)(aTHX_ v, "Context::Vars::Data::Vector");
52 }
53  
54 6205 static void context_vars_ok(int status) {
55 6205 ▶ if (status != HAMT_ABI_OK) croak("Context::Vars: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (status != HAMT_ABI_OK) croak("Context::Vars: out of memory"); 0 6205 6205
56 6205 }
57  
58 /* ---- proxies -------------------------------------------------------------------- */
59  
60 /* A proxy's tie object is a blessed array: [ path, iteration state ], where the
61 * path is an array of the variable's context name and each key or index. */
62 8579 static AV *context_vars_proxy_path(pTHX_ SV *tieobj) {
63 SV **p;
64 8579 ▶ if (!tieobj || !SvROK(tieobj) || SvTYPE(SvRV(tieobj)) != SVt_PVAV) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!tieobj || !SvROK(tieobj) || SvTYPE(SvRV(tieobj)) != SVt_PVAV) return NULL; 8579 0 8579
if (!tieobj || !SvROK(tieobj) || SvTYPE(SvRV(tieobj)) != SVt_PVAV) return NULL; 8579 0 8579
if (!tieobj || !SvROK(tieobj) || SvTYPE(SvRV(tieobj)) != SVt_PVAV) return NULL; 0 8579 8579
65 8579 p = av_fetch((AV *)SvRV(tieobj), 0, 0);
66 8579 ▶ return (p && SvROK(*p) && SvTYPE(SvRV(*p)) == SVt_PVAV) ? (AV *)SvRV(*p) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (p && SvROK(*p) && SvTYPE(SvRV(*p)) == SVt_PVAV) ? (AV *)SvRV(*p) : NULL; 8579 0 8579
return (p && SvROK(*p) && SvTYPE(SvRV(*p)) == SVt_PVAV) ? (AV *)SvRV(*p) : NULL; 8579 0 8579
return (p && SvROK(*p) && SvTYPE(SvRV(*p)) == SVt_PVAV) ? (AV *)SvRV(*p) : NULL; 8579 0 8579
67 }
68  
69 /* The tie object of a container tied to one of our proxies, or NULL. */
70 37 static SV *context_vars_proxy_of_container(pTHX_ SV *container) {
71 dMY_CXT;
72 MAGIC *mg;
73 37 ▶ if (!container || !SvRMAGICAL(container)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!container || !SvRMAGICAL(container)) return NULL; 37 0 37
if (!container || !SvRMAGICAL(container)) return NULL; 36 1 37
74 1 mg = mg_find(container, PERL_MAGIC_tied);
75 1 ▶ if (!mg || !mg->mg_obj || !SvROK(mg->mg_obj) || !SvOBJECT(SvRV(mg->mg_obj))) return NULL;
Branch: 4 branches
BranchTrueFalseTotal
if (!mg || !mg->mg_obj || !SvROK(mg->mg_obj) || !SvOBJECT(SvRV(mg->mg_obj))) return NULL; 1 0 1
if (!mg || !mg->mg_obj || !SvROK(mg->mg_obj) || !SvOBJECT(SvRV(mg->mg_obj))) return NULL; 1 0 1
if (!mg || !mg->mg_obj || !SvROK(mg->mg_obj) || !SvOBJECT(SvRV(mg->mg_obj))) return NULL; 1 0 1
if (!mg || !mg->mg_obj || !SvROK(mg->mg_obj) || !SvOBJECT(SvRV(mg->mg_obj))) return NULL; 0 1 1
76 1 ▶ if (SvSTASH(SvRV(mg->mg_obj)) != MY_CXT.hash_stash
Branch: 1 branch
BranchTrueFalseTotal
if (SvSTASH(SvRV(mg->mg_obj)) != MY_CXT.hash_stash 1 0 1
77 1 ▶ && SvSTASH(SvRV(mg->mg_obj)) != MY_CXT.array_stash) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
&& SvSTASH(SvRV(mg->mg_obj)) != MY_CXT.array_stash) return NULL; 0 1 1
78 1 return mg->mg_obj;
79 }
80  
81 /* The path behind a reference to one of our proxies, or NULL. */
82 40 static AV *context_vars_proxy_ref_path(pTHX_ SV *v) {
83 SV *obj;
84 40 ▶ if (!v || !SvROK(v) || SvOBJECT(SvRV(v))) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!v || !SvROK(v) || SvOBJECT(SvRV(v))) return NULL; 40 0 40
if (!v || !SvROK(v) || SvOBJECT(SvRV(v))) return NULL; 40 0 40
if (!v || !SvROK(v) || SvOBJECT(SvRV(v))) return NULL; 0 40 40
85 40 ▶ if (SvTYPE(SvRV(v)) != SVt_PVHV && SvTYPE(SvRV(v)) != SVt_PVAV) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (SvTYPE(SvRV(v)) != SVt_PVHV && SvTYPE(SvRV(v)) != SVt_PVAV) return NULL; 14 26 40
if (SvTYPE(SvRV(v)) != SVt_PVHV && SvTYPE(SvRV(v)) != SVt_PVAV) return NULL; 3 11 14
86 37 obj = context_vars_proxy_of_container(aTHX_ SvRV(v));
87 37 ▶ return obj ? context_vars_proxy_path(aTHX_ obj) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return obj ? context_vars_proxy_path(aTHX_ obj) : NULL; 1 36 37
88 }
89  
90 /* Ties `container` to a proxy for `path`, which it takes over. */
91 2136 static void context_vars_tie(pTHX_ SV *container, AV *path) {
92 dMY_CXT;
93 2136 AV *state = newAV();
94 SV *obj;
95 2136 av_push(state, newRV_noinc((SV *)path));
96 2136 obj = newRV_noinc((SV *)state);
97 2136 ▶ sv_bless(obj, SvTYPE(container) == SVt_PVHV ? MY_CXT.hash_stash : MY_CXT.array_stash);
Branch: 1 branch
BranchTrueFalseTotal
sv_bless(obj, SvTYPE(container) == SVt_PVHV ? MY_CXT.hash_stash : MY_CXT.array_stash); 2095 41 2136
98 2136 sv_magic(container, obj, PERL_MAGIC_tied, NULL, 0);
99 2136 SvREFCNT_dec(obj);
100 2136 }
101  
102 /* A new path: `base` followed by `key`. */
103 8329 static AV *context_vars_path_plus(pTHX_ AV *base, SV *key) {
104 8329 AV *p = newAV();
105 8329 I32 i, n = av_len(base) + 1;
106 8329 av_extend(p, n);
107 18721 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 10392 8329 18721
108 10392 SV **e = av_fetch(base, i, 0);
109 10392 ▶ av_push(p, newSVsv(e ? *e : &PL_sv_undef));
Branch: 1 branch
BranchTrueFalseTotal
av_push(p, newSVsv(e ? *e : &PL_sv_undef)); 10392 0 10392
110 }
111 8329 ▶ if (key) av_push(p, newSVsv(key));
Branch: 1 branch
BranchTrueFalseTotal
if (key) av_push(p, newSVsv(key)); 8321 8 8329
112 8329 return p;
113 }
114  
115 /* ---- reading a path ---------------------------------------------------------- */
116  
117 6397 static SV *context_vars_node_get(pTHX_ SV *node, SV *key) {
118 6397 ▶ if (context_vars_is_map(aTHX_ node)) {
Branch: 1 branch
BranchTrueFalseTotal
if (context_vars_is_map(aTHX_ node)) { 6299 98 6397
119 STRLEN klen;
120 6299 const char *k = SvPV(key, klen);
121 6299 return (CONTEXT_VARS_HAMT->map_fetch)(aTHX_ (CONTEXT_VARS_HAMT->map_from_sv)(aTHX_ node),
122 6299 k, klen, SvUTF8(key) ? 1 : 0);
123 }
124 98 ▶ if (context_vars_is_vec(aTHX_ node)) {
Branch: 1 branch
BranchTrueFalseTotal
if (context_vars_is_vec(aTHX_ node)) { 98 0 98
125 98 IV i = SvIV(key);
126 98 ▶ if (i < 0) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (i < 0) return NULL; 0 98 98
127 98 return (CONTEXT_VARS_HAMT->vec_fetch)(aTHX_ (CONTEXT_VARS_HAMT->vec_from_sv)(aTHX_ node), (size_t)i);
128 }
129 0 return NULL;
130 }
131  
132 /* What is at a path in the current context, borrowed, or NULL. */
133 4477 static SV *context_vars_path_get(pTHX_ AV *path) {
134 4477 I32 i, n = av_len(path) + 1;
135 4477 SV **name = av_fetch(path, 0, 0);
136 STRLEN len;
137 const char *p;
138 SV *node;
139 4477 ▶ if (!name) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!name) return NULL; 0 4477 4477
140 4477 p = SvPV(*name, len);
141 4477 node = context_vars_fetch(aTHX_ p, len);
142 4606 ▶ for (i = 1; i < n && node; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 1; i < n && node; i++) { 129 4477 4606
for (i = 1; i < n && node; i++) { 129 0 129
143 129 SV **key = av_fetch(path, i, 0);
144 129 ▶ node = key ? context_vars_node_get(aTHX_ node, *key) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
node = key ? context_vars_node_get(aTHX_ node, *key) : NULL; 129 0 129
145 }
146 4477 return node;
147 }
148  
149 /* ---- conversion -------------------------------------------------------------- */
150  
151 static SV *context_vars_in_depth(pTHX_ SV *v, int depth);
152  
153 /* A plain hash or array, at any depth, as nodes; a proxy as the node it shows;
154 * anything else is NULL, meaning "store it as it is". The result has one
155 * reference the caller owns. */
156 14848 static SV *context_vars_in(pTHX_ SV *v) {
157 14848 return context_vars_in_depth(aTHX_ v, 0);
158 }
159  
160 14885 static SV *context_vars_in_depth(pTHX_ SV *v, int depth) {
161 SV *ref;
162 AV *ppath;
163 14885 ▶ if (!v || !SvROK(v)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!v || !SvROK(v)) return NULL; 14885 0 14885
if (!v || !SvROK(v)) return NULL; 10674 4211 14885
164 4211 ref = SvRV(v);
165 4211 ▶ if (SvOBJECT(ref)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (SvOBJECT(ref)) return NULL; 4171 40 4211
166 40 ▶ if (depth > CONTEXT_VARS_DEEP_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (depth > CONTEXT_VARS_DEEP_MAX) 0 40 40
167 0 croak("Context::Vars: a structure nested more than %d deep, or one that contains itself",
168 CONTEXT_VARS_DEEP_MAX);
169  
170 40 ▶ if ((ppath = context_vars_proxy_ref_path(aTHX_ v))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((ppath = context_vars_proxy_ref_path(aTHX_ v))) { 1 39 40
171 1 SV *node = context_vars_path_get(aTHX_ ppath);
172 1 ▶ return node ? newSVsv(node) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return node ? newSVsv(node) : NULL; 1 0 1
173 }
174 39 ▶ if (SvTYPE(ref) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(ref) == SVt_PVHV) { 26 13 39
175 26 HV *hv = (HV *)ref;
176 26 hamt_map *m = (CONTEXT_VARS_HAMT->map_new)(aTHX);
177 HE *he;
178 26 ▶ if (!m) croak("Context::Vars: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!m) croak("Context::Vars: out of memory"); 0 26 26
179 26 hv_iterinit(hv);
180 50 ▶ while ((he = hv_iternext(hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(hv))) { 24 26 50
181 STRLEN klen;
182 24 ▶ const char *k = HePV(he, klen);
Branch: 1 branch
BranchTrueFalseTotal
const char *k = HePV(he, klen); 0 24 24
183 24 SV *val = hv_iterval(hv, he);
184 24 SV *child = context_vars_in_depth(aTHX_ val, depth + 1);
185 24 ▶ context_vars_ok((CONTEXT_VARS_HAMT->map_store)(aTHX_ m, k, klen, HeUTF8(he) ? 1 : 0,
Branch: 2 branches
BranchTrueFalseTotal
context_vars_ok((CONTEXT_VARS_HAMT->map_store)(aTHX_ m, k, klen, HeUTF8(he) ? 1 : 0, 6 18 24
context_vars_ok((CONTEXT_VARS_HAMT->map_store)(aTHX_ m, k, klen, HeUTF8(he) ? 1 : 0, 0 24 24
186 child ? child : val));
187 24 ▶ if (child) SvREFCNT_dec(child);
Branch: 1 branch
BranchTrueFalseTotal
if (child) SvREFCNT_dec(child); 6 18 24
188 }
189 26 return context_vars_map_sv(aTHX_ m);
190 }
191 13 ▶ if (SvTYPE(ref) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(ref) == SVt_PVAV) { 10 3 13
192 10 AV *av = (AV *)ref;
193 10 I32 i, n = av_len(av) + 1;
194 10 hamt_vec *vv = (CONTEXT_VARS_HAMT->vec_new)(aTHX);
195 10 ▶ if (!vv) croak("Context::Vars: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!vv) croak("Context::Vars: out of memory"); 0 10 10
196 23 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 13 10 23
197 13 SV **e = av_fetch(av, i, 0);
198 13 ▶ SV *val = e ? *e : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *val = e ? *e : &PL_sv_undef; 13 0 13
199 13 SV *child = context_vars_in_depth(aTHX_ val, depth + 1);
200 13 ▶ context_vars_ok((CONTEXT_VARS_HAMT->vec_push)(aTHX_ vv, child ? child : val));
Branch: 1 branch
BranchTrueFalseTotal
context_vars_ok((CONTEXT_VARS_HAMT->vec_push)(aTHX_ vv, child ? child : val)); 0 13 13
201 13 ▶ if (child) SvREFCNT_dec(child);
Branch: 1 branch
BranchTrueFalseTotal
if (child) SvREFCNT_dec(child); 0 13 13
202 }
203 10 return context_vars_vec_sv(aTHX_ vv);
204 }
205 3 return NULL;
206 }
207  
208 /* What a program reads at `path`: a node as a proxy for that path (taking the
209 * path over), anything else as it is. Mortal, or the borrowed value itself. */
210 4278 static SV *context_vars_out(pTHX_ SV *val, AV *path) {
211 4278 ▶ if (context_vars_is_map(aTHX_ val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (context_vars_is_map(aTHX_ val)) { 2077 2201 4278
212 2077 HV *hv = newHV();
213 2077 context_vars_tie(aTHX_ (SV *)hv, path);
214 2077 return sv_2mortal(newRV_noinc((SV *)hv));
215 }
216 2201 ▶ if (context_vars_is_vec(aTHX_ val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (context_vars_is_vec(aTHX_ val)) { 33 2168 2201
217 33 AV *av = newAV();
218 33 context_vars_tie(aTHX_ (SV *)av, path);
219 33 return sv_2mortal(newRV_noinc((SV *)av));
220 }
221 2168 SvREFCNT_dec((SV *)path);
222 2168 ▶ return val ? val : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
return val ? val : &PL_sv_undef; 2168 0 2168
223 }
224  
225 static int context_vars_plain_cb(pTHX_ void *ud, const char *key, STRLEN klen, int utf8, SV *value);
226 static SV *context_vars_plain(pTHX_ SV *val);
227  
228 /* A node as a plain, unshared copy - for what is taken out of a structure,
229 * popped, shifted, spliced or deleted, which no longer has a path to proxy. */
230 54 static SV *context_vars_plain(pTHX_ SV *val) {
231 54 ▶ if (context_vars_is_map(aTHX_ val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (context_vars_is_map(aTHX_ val)) { 5 49 54
232 5 HV *hv = newHV();
233 5 (CONTEXT_VARS_HAMT->map_walk)(aTHX_ (CONTEXT_VARS_HAMT->map_from_sv)(aTHX_ val),
234 context_vars_plain_cb, hv);
235 5 return newRV_noinc((SV *)hv);
236 }
237 49 ▶ if (context_vars_is_vec(aTHX_ val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (context_vars_is_vec(aTHX_ val)) { 4 45 49
238 4 const hamt_vec *v = (CONTEXT_VARS_HAMT->vec_from_sv)(aTHX_ val);
239 4 size_t i, n = (CONTEXT_VARS_HAMT->vec_count)(aTHX_ v);
240 4 AV *av = newAV();
241 4 av_extend(av, (I32)n);
242 15 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 11 4 15
243 11 av_push(av, context_vars_plain(aTHX_ (CONTEXT_VARS_HAMT->vec_fetch)(aTHX_ v, i)));
244 4 return newRV_noinc((SV *)av);
245 }
246 45 ▶ return val ? newSVsv(val) : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
return val ? newSVsv(val) : newSV(0); 45 0 45
247 }
248  
249 6 static int context_vars_plain_cb(pTHX_ void *ud, const char *key, STRLEN klen, int utf8, SV *value) {
250 6 ▶ (void)hv_store((HV *)ud, key, utf8 ? -(I32)klen : (I32)klen, context_vars_plain(aTHX_ value), 0);
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_store((HV *)ud, key, utf8 ? -(I32)klen : (I32)klen, context_vars_plain(aTHX_ value), 0); 0 6 6
251 6 return 0;
252 }
253  
254 /* ---- small helpers for the proxies ----------------------------------------------- */
255  
256 84 static int context_vars_keys_cb(pTHX_ void *ud, const char *key, STRLEN klen, int utf8, SV *value) {
257 84 SV *k = newSVpvn(key, klen);
258 PERL_UNUSED_ARG(value);
259 84 ▶ if (utf8) SvUTF8_on(k);
Branch: 1 branch
BranchTrueFalseTotal
if (utf8) SvUTF8_on(k); 0 84 84
260 84 av_push((AV *)ud, k);
261 84 return 0;
262 }
263  
264 114 static size_t context_vars_vec_len(pTHX_ SV *node) {
265 114 return context_vars_is_vec(aTHX_ node)
266 114 ▶ ? (CONTEXT_VARS_HAMT->vec_count)(aTHX_ (CONTEXT_VARS_HAMT->vec_from_sv)(aTHX_ node)) : 0;
Branch: 1 branch
BranchTrueFalseTotal
? (CONTEXT_VARS_HAMT->vec_count)(aTHX_ (CONTEXT_VARS_HAMT->vec_from_sv)(aTHX_ node)) : 0; 114 0 114
267 }
268  
269 /* A struct of the caller's own onto the vector at `node`, or an empty one. */
270 19 static hamt_vec *context_vars_vec_dup(pTHX_ SV *node) {
271 19 hamt_vec *v = context_vars_is_vec(aTHX_ node)
272 18 ? (CONTEXT_VARS_HAMT->vec_copy)(aTHX_ (CONTEXT_VARS_HAMT->vec_from_sv)(aTHX_ node))
273 19 ▶ : (CONTEXT_VARS_HAMT->vec_new)(aTHX);
Branch: 1 branch
BranchTrueFalseTotal
: (CONTEXT_VARS_HAMT->vec_new)(aTHX); 18 1 19
274 19 ▶ if (!v) croak("Context::Vars: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!v) croak("Context::Vars: out of memory"); 0 19 19
275 19 return v;
276 }
277  
278 /* A new vector of the `n` values at `args`, converted as stored values are. */
279 4 static hamt_vec *context_vars_vec_of_args(pTHX_ SV **args, I32 n) {
280 4 hamt_vec *v = (CONTEXT_VARS_HAMT->vec_new)(aTHX);
281 I32 i;
282 4 ▶ if (!v) croak("Context::Vars: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!v) croak("Context::Vars: out of memory"); 0 4 4
283 9 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 5 4 9
284 5 SV *conv = context_vars_in(aTHX_ args[i]);
285 5 ▶ context_vars_ok((CONTEXT_VARS_HAMT->vec_push)(aTHX_ v, conv ? conv : args[i]));
Branch: 1 branch
BranchTrueFalseTotal
context_vars_ok((CONTEXT_VARS_HAMT->vec_push)(aTHX_ v, conv ? conv : args[i])); 5 0 5
286 5 ▶ if (conv) SvREFCNT_dec(conv);
Branch: 1 branch
BranchTrueFalseTotal
if (conv) SvREFCNT_dec(conv); 0 5 5
287 }
288 4 return v;
289 }
290  
291 static void context_vars_path_put(pTHX_ AV *path, SV *node);
292  
293 /* An array proxy below the top whose array is not there - cleared away, or
294 * replaced by something else - gets an empty one first, so that a store into
295 * it does not make a hash. */
296 23 static void context_vars_vec_ensure(pTHX_ AV *path) {
297 23 ▶ if (av_len(path) > 0 && !context_vars_is_vec(aTHX_ context_vars_path_get(aTHX_ path)))
Branch: 2 branches
BranchTrueFalseTotal
if (av_len(path) > 0 && !context_vars_is_vec(aTHX_ context_vars_path_get(aTHX_ path))) 0 23 23
if (av_len(path) > 0 && !context_vars_is_vec(aTHX_ context_vars_path_get(aTHX_ path))) 0 0 0
298 0 context_vars_path_put(aTHX_ path, context_vars_vec_sv(aTHX_ (CONTEXT_VARS_HAMT->vec_new)(aTHX)));
299 23 }
300  
301 /* ---- writing a path ---------------------------------------------------------- */
302  
303 /* Puts `node` - owned by the caller, or NULL to remove what is there - at
304 * `path` in the current context, copying every level above it. A level that
305 * is not there is made: the root by its sigil, anything deeper as a hash, or
306 * as an array when its key is the index of an array proxy (`kinds`). */
307 4147 static void context_vars_path_put(pTHX_ AV *path, SV *node) {
308 4147 I32 n = av_len(path) + 1, depth;
309 4147 SV **name = av_fetch(path, 0, 0);
310 SV **parents;
311 STRLEN len;
312 const char *p;
313 4147 SV *child = node;
314  
315 4147 ▶ if (!name) croak("Context::Vars: internal error: an empty path");
Branch: 1 branch
BranchTrueFalseTotal
if (!name) croak("Context::Vars: internal error: an empty path"); 0 4147 4147
316 4147 p = SvPV(*name, len);
317  
318 4147 Newx(parents, n, SV *);
319 4147 SAVEFREEPV(parents);
320 4147 parents[0] = context_vars_fetch(aTHX_ p, len);
321 6167 ▶ for (depth = 1; depth < n - 1; depth++) {
Branch: 1 branch
BranchTrueFalseTotal
for (depth = 1; depth < n - 1; depth++) { 2020 4147 6167
322 2020 SV **key = av_fetch(path, depth, 0);
323 4037 ▶ parents[depth] = parents[depth - 1] && key
Branch: 1 branch
BranchTrueFalseTotal
parents[depth] = parents[depth - 1] && key 2017 0 2017
324 4037 ▶ ? context_vars_node_get(aTHX_ parents[depth - 1], *key) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? context_vars_node_get(aTHX_ parents[depth - 1], *key) : NULL; 2017 3 2020
325 }
326  
327 10269 ▶ for (depth = n - 2; depth >= 0; depth--) {
Branch: 1 branch
BranchTrueFalseTotal
for (depth = n - 2; depth >= 0; depth--) { 6122 4147 10269
328 6122 SV *parent = parents[depth];
329 6122 SV **keyp = av_fetch(path, depth + 1, 0);
330 6122 ▶ SV *key = keyp ? *keyp : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *key = keyp ? *keyp : &PL_sv_undef; 6122 0 6122
331 SV *made;
332 6122 int as_vec = context_vars_is_vec(aTHX_ parent)
333 6122 ▶ || (!context_vars_is_map(aTHX_ parent) && depth == 0 && p[0] == '@');
Branch: 4 branches
BranchTrueFalseTotal
|| (!context_vars_is_map(aTHX_ parent) && depth == 0 && p[0] == '@'); 6098 24 6122
|| (!context_vars_is_map(aTHX_ parent) && depth == 0 && p[0] == '@'); 10 6088 6098
|| (!context_vars_is_map(aTHX_ parent) && depth == 0 && p[0] == '@'); 7 3 10
|| (!context_vars_is_map(aTHX_ parent) && depth == 0 && p[0] == '@'); 3 4 7
334  
335 6122 ▶ if (as_vec) {
Branch: 1 branch
BranchTrueFalseTotal
if (as_vec) { 27 6095 6122
336 27 hamt_vec *v = context_vars_is_vec(aTHX_ parent)
337 24 ? (CONTEXT_VARS_HAMT->vec_copy)(aTHX_ (CONTEXT_VARS_HAMT->vec_from_sv)(aTHX_ parent))
338 27 ▶ : (CONTEXT_VARS_HAMT->vec_new)(aTHX);
Branch: 1 branch
BranchTrueFalseTotal
: (CONTEXT_VARS_HAMT->vec_new)(aTHX); 24 3 27
339 27 IV i = SvIV(key);
340 27 ▶ if (!v) croak("Context::Vars: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!v) croak("Context::Vars: out of memory"); 0 27 27
341 27 ▶ if (i < 0) croak("Context::Vars: a negative index below the start of an array");
Branch: 1 branch
BranchTrueFalseTotal
if (i < 0) croak("Context::Vars: a negative index below the start of an array"); 0 27 27
342 53 ▶ while ((IV)(CONTEXT_VARS_HAMT->vec_count)(aTHX_ v) <= i)
Branch: 1 branch
BranchTrueFalseTotal
while ((IV)(CONTEXT_VARS_HAMT->vec_count)(aTHX_ v) <= i) 26 27 53
343 26 context_vars_ok((CONTEXT_VARS_HAMT->vec_push)(aTHX_ v, &PL_sv_undef));
344 27 ▶ context_vars_ok((CONTEXT_VARS_HAMT->vec_store)(aTHX_ v, (size_t)i,
Branch: 1 branch
BranchTrueFalseTotal
context_vars_ok((CONTEXT_VARS_HAMT->vec_store)(aTHX_ v, (size_t)i, 27 0 27
345 child ? child : &PL_sv_undef));
346 27 made = context_vars_vec_sv(aTHX_ v);
347 }
348 else {
349 STRLEN klen;
350 6095 const char *k = SvPV(key, klen);
351 6095 hamt_map *m = context_vars_is_map(aTHX_ parent)
352 6088 ? (CONTEXT_VARS_HAMT->map_copy)(aTHX_ (CONTEXT_VARS_HAMT->map_from_sv)(aTHX_ parent))
353 6095 ▶ : (CONTEXT_VARS_HAMT->map_new)(aTHX);
Branch: 1 branch
BranchTrueFalseTotal
: (CONTEXT_VARS_HAMT->map_new)(aTHX); 6088 7 6095
354 6095 ▶ if (!m) croak("Context::Vars: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!m) croak("Context::Vars: out of memory"); 0 6095 6095
355 6095 ▶ if (child)
Branch: 1 branch
BranchTrueFalseTotal
if (child) 6089 6 6095
356 6089 context_vars_ok((CONTEXT_VARS_HAMT->map_store)(aTHX_ m, k, klen, SvUTF8(key) ? 1 : 0, child));
357 else
358 6 context_vars_ok((CONTEXT_VARS_HAMT->map_remove)(aTHX_ m, k, klen, SvUTF8(key) ? 1 : 0));
359 6095 made = context_vars_map_sv(aTHX_ m);
360 }
361 6122 ▶ if (child) SvREFCNT_dec(child);
Branch: 1 branch
BranchTrueFalseTotal
if (child) SvREFCNT_dec(child); 6116 6 6122
362 6122 child = made;
363 }
364  
365 4147 context_vars_var_put(aTHX_ p, len, child);
366 4147 ▶ if (child) SvREFCNT_dec(child);
Branch: 1 branch
BranchTrueFalseTotal
if (child) SvREFCNT_dec(child); 4147 0 4147
367 4147 }
368  
369 /* ---- autovivification on old perls ------------------------------------------ */
370  
371 /* perl 5.14 and older vivify through a tied hash or array by making a plain
372 * container, storing it, and then writing into THAT container. So a fresh,
373 * empty, plain container stored at `path` - one nothing else holds - is tied
374 * in place to a proxy for the path, and the write that follows goes through
375 * it. A container something else holds is left alone. */
376 10647 static int context_vars_is_fresh(pTHX_ SV *v) {
377 SV *c;
378 10647 ▶ if (!v || !SvROK(v)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!v || !SvROK(v)) return 0; 10647 0 10647
if (!v || !SvROK(v)) return 0; 10601 46 10647
379 46 c = SvRV(v);
380 46 ▶ if (SvOBJECT(c) || SvREFCNT(c) != 1 || SvRMAGICAL(c)) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (SvOBJECT(c) || SvREFCNT(c) != 1 || SvRMAGICAL(c)) return 0; 36 10 46
if (SvOBJECT(c) || SvREFCNT(c) != 1 || SvRMAGICAL(c)) return 0; 21 15 36
if (SvOBJECT(c) || SvREFCNT(c) != 1 || SvRMAGICAL(c)) return 0; 0 21 21
381 21 ▶ if (SvTYPE(c) == SVt_PVHV) return HvUSEDKEYS((HV *)c) == 0;
Branch: 2 branches
BranchTrueFalseTotal
if (SvTYPE(c) == SVt_PVHV) return HvUSEDKEYS((HV *)c) == 0; 18 3 21
if (SvTYPE(c) == SVt_PVHV) return HvUSEDKEYS((HV *)c) == 0; 0 18 18
382 3 ▶ if (SvTYPE(c) == SVt_PVAV) return av_len((AV *)c) < 0;
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(c) == SVt_PVAV) return av_len((AV *)c) < 0; 3 0 3
383 0 return 0;
384 }
385  
386 4085 static void context_vars_tie_fresh(pTHX_ SV *v, AV *path) {
387 4085 ▶ if (context_vars_is_fresh(aTHX_ v))
Branch: 1 branch
BranchTrueFalseTotal
if (context_vars_is_fresh(aTHX_ v)) 8 4077 4085
388 8 context_vars_tie(aTHX_ SvRV(v), context_vars_path_plus(aTHX_ path, NULL));
389 4085 }
390  
391 /* ---- local on a whole container --------------------------------------------- */
392  
393 /* `local %name` and `local @name` make a new container for the glob, and
394 * perl does NOT carry a tie over to it: left alone, the local container would
395 * be a plain one, seen only through this package's glob and never by another
396 * package, a capture or a callback. So this ext magic's local hook ties the
397 * new container to the same path, copies the mark, and keeps the context's
398 * value of the variable to put back when the local ends. */
399 typedef struct { SV *name; SV *value; } context_vars_local_rec;
400  
401 9 static void context_vars_local_restore(pTHX_ void *p) {
402 9 context_vars_local_rec *r = (context_vars_local_rec *)p;
403 STRLEN len;
404 9 const char *n = SvPV(r->name, len);
405 9 context_vars_var_put(aTHX_ n, len, r->value);
406 9 SvREFCNT_dec(r->name);
407 9 ▶ if (r->value) SvREFCNT_dec(r->value);
Branch: 1 branch
BranchTrueFalseTotal
if (r->value) SvREFCNT_dec(r->value); 9 0 9
408 9 Safefree(r);
409 9 }
410  
411 static int context_vars_container_local(pTHX_ SV *nsv, MAGIC *mg);
412 static MGVTBL context_vars_container_vtbl = { 0, 0, 0, 0, 0, 0, 0, context_vars_container_local };
413  
414 18 static void context_vars_mark_container(pTHX_ SV *container, const char *name, STRLEN len) {
415 18 MAGIC *mg = sv_magicext(container, NULL, PERL_MAGIC_ext, &context_vars_container_vtbl, name, (I32)len);
416 18 mg->mg_flags |= MGf_LOCAL;
417 18 }
418  
419 9 static int context_vars_container_local(pTHX_ SV *nsv, MAGIC *mg) {
420 context_vars_local_rec *r;
421 9 SV *held = context_vars_fetch(aTHX_ mg->mg_ptr, (STRLEN)mg->mg_len);
422 9 Newx(r, 1, context_vars_local_rec);
423 9 r->name = newSVpvn(mg->mg_ptr, mg->mg_len);
424 9 ▶ r->value = held ? newSVsv(held) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
r->value = held ? newSVsv(held) : NULL; 9 0 9
425 9 SAVEDESTRUCTOR_X(context_vars_local_restore, r);
426 {
427 9 AV *path = newAV();
428 9 av_push(path, newSVpvn(mg->mg_ptr, mg->mg_len));
429 9 context_vars_tie(aTHX_ nsv, path);
430 }
431 9 context_vars_mark_container(aTHX_ nsv, mg->mg_ptr, (STRLEN)mg->mg_len);
432 9 return 0;
433 }
434  
435 #endif
« Vars.xs ↑ Summary include/context_vars/context_vars_var.h »