include/context_vars/context_vars_obj.h

filter: Statementstmt 93.9% 77 / 82 Branchbran 66.0% 33 / 50 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 83.3% 110 / 132 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/context_vars/context_vars_scope.h ↑ Summary include/context_vars/context_vars_hamt.h »
1 #ifndef CONTEXT_VARS_OBJ_H
2 #define CONTEXT_VARS_OBJ_H
3  
4 /* Context::Vars::Context, the object a loop author or a test holds, and the
5 * calls that run code in one.
6 *
7 * The object is a blessed reference to a scalar that carries magic,
8 * recognised by the address of the vtable below, with the holder as the
9 * magic's private data. The holder is released by the magic's free hook, so
10 * there is no DESTROY to call twice or to shadow in a subclass. A hash or a
11 * number blessed into the class, or one restored by a serialiser, has no such
12 * magic and is refused by every method, by name, instead of being
13 * dereferenced. That is HAMT's choice for its own objects, made for the same
14 * reason.
15 *
16 * A new thread does not get the object: CLONE_SKIP keeps it from being
17 * cloned, and if a clone is made some other way the dup hook empties it, so
18 * it is dead rather than a second owner of another interpreter's scalars. */
19  
20 20 static int context_vars_obj_free(pTHX_ SV *sv, MAGIC *mg) {
21 20 context_vars_ctx *c = (context_vars_ctx *)mg->mg_ptr;
22 PERL_UNUSED_ARG(sv);
23 20 ▶ if (c) {
Branch: 1 branch
BranchTrueFalseTotal
if (c) { 20 0 20
24 20 mg->mg_ptr = NULL;
25 20 context_vars_release(aTHX_ c);
26 }
27 20 return 0;
28 }
29  
30 0 static int context_vars_obj_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param) {
31 PERL_UNUSED_CONTEXT;
32 PERL_UNUSED_ARG(param);
33 0 mg->mg_ptr = NULL;
34 0 return 0;
35 }
36  
37 static MGVTBL context_vars_obj_vtbl = {
38 NULL, NULL, NULL, NULL, context_vars_obj_free, NULL, context_vars_obj_dup, NULL
39 };
40  
41 /* A new object, mortal, that TAKES OVER one reference to `c`. */
42 20 static SV *context_vars_obj_new(pTHX_ context_vars_ctx *c) {
43 20 SV *inner = newSV(0);
44 SV *rv;
45 20 MAGIC *mg = sv_magicext(inner, NULL, PERL_MAGIC_ext, &context_vars_obj_vtbl,
46 (const char *)c, 0);
47 20 mg->mg_flags |= MGf_DUP;
48 20 rv = sv_2mortal(newRV_noinc(inner));
49 20 sv_bless(rv, gv_stashpvs("Context::Vars::Context", GV_ADD));
50 20 return rv;
51 }
52  
53 /* The holder behind an object, borrowed, or NULL when `sv` is not one. */
54 15 static context_vars_ctx *context_vars_obj_try(pTHX_ SV *sv) {
55 MAGIC *mg;
56 PERL_UNUSED_CONTEXT;
57 15 ▶ if (!SvROK(sv) || SvTYPE(SvRV(sv)) < SVt_PVMG) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(sv) || SvTYPE(SvRV(sv)) < SVt_PVMG) return NULL; 9 6 15
if (!SvROK(sv) || SvTYPE(SvRV(sv)) < SVt_PVMG) return NULL; 0 9 9
58 9 ▶ for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic)
Branch: 1 branch
BranchTrueFalseTotal
for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) 3 6 9
59 3 ▶ if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &context_vars_obj_vtbl)
Branch: 2 branches
BranchTrueFalseTotal
if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &context_vars_obj_vtbl) 3 0 3
if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &context_vars_obj_vtbl) 3 0 3
60 3 return (context_vars_ctx *)mg->mg_ptr;
61 6 return NULL;
62 }
63  
64 /* The same, or a croak that names the method. */
65 71 static context_vars_ctx *context_vars_obj(pTHX_ SV *sv, const char *what) {
66 MAGIC *mg;
67 71 ▶ if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { 62 9 71
if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { 62 0 62
68 62 ▶ for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) {
Branch: 1 branch
BranchTrueFalseTotal
for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) { 50 12 62
69 50 ▶ if (mg->mg_type != PERL_MAGIC_ext || mg->mg_virtual != &context_vars_obj_vtbl)
Branch: 2 branches
BranchTrueFalseTotal
if (mg->mg_type != PERL_MAGIC_ext || mg->mg_virtual != &context_vars_obj_vtbl) 50 0 50
if (mg->mg_type != PERL_MAGIC_ext || mg->mg_virtual != &context_vars_obj_vtbl) 0 50 50
70 0 continue;
71 50 ▶ if (!mg->mg_ptr)
Branch: 1 branch
BranchTrueFalseTotal
if (!mg->mg_ptr) 0 50 50
72 0 croak("%s: this context belongs to another thread", what);
73 50 return (context_vars_ctx *)mg->mg_ptr;
74 }
75 }
76 21 croak("%s: not a Context::Vars::Context", what);
77 return NULL;
78 }
79  
80 /* ---- running code in a context --------------------------------------------- */
81  
82 /* Calls `code` with the `n` arguments at `args` in context `c` - `c` itself
83 * when `copy` is 0, a fresh copy of it when 1 - in the caller's `gimme`, and
84 * puts what it returned on the stack from position `ax` up. Returns how many.
85 *
86 * A die in the code is not caught: it goes up through here, and the save
87 * stack puts the previous context back on its way. */
88 5061 static I32 context_vars_call_in(pTHX_ context_vars_ctx *c, int copy, SV *code,
89 SV **args, I32 n, I32 gimme, I32 ax) {
90 5061 dSP;
91 5061 SV **argv = NULL;
92 I32 i, count;
93  
94 5061 ENTER;
95 5061 ▶ if (n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) { 5 5056 5061
96 /* The arguments live on the stack, which the call may move. */
97 5 Newx(argv, n, SV *);
98 5 SAVEFREEPV(argv);
99 5 Copy(args, argv, n, SV *);
100 }
101 5061 ▶ if (copy) context_vars_enter_copy(aTHX_ c);
Branch: 1 branch
BranchTrueFalseTotal
if (copy) context_vars_enter_copy(aTHX_ c); 5037 24 5061
102 24 else context_vars_enter(aTHX_ c);
103  
104 5061 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 5061 5061
105 5061 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 5061 0 5061
EXTEND(SP, n); 0 5061 5061
106 5070 ▶ for (i = 0; i < n; i++) PUSHs(argv[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) PUSHs(argv[i]); 9 5061 5070
107 5061 PUTBACK;
108  
109 5061 count = call_sv(code, gimme);
110  
111 5057 SPAGAIN;
112 10085 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 5028 5057 10085
113 5028 SV *r = SP[i - count + 1];
114 5028 ▶ PL_stack_base[ax + i] = SvTEMP(r) ? r : sv_mortalcopy(r);
Branch: 1 branch
BranchTrueFalseTotal
PL_stack_base[ax + i] = SvTEMP(r) ? r : sv_mortalcopy(r); 0 5028 5028
115 }
116 5057 PL_stack_sp = PL_stack_base + ax + count - 1;
117 5057 LEAVE;
118 5057 return count;
119 }
120  
121 /* ---- wrap ------------------------------------------------------------------- */
122  
123 /* A wrapped callback is an XSUB with no name, carrying the capture and the
124 * code as the private data of magic on itself, let go when it is freed. */
125 typedef struct {
126 context_vars_ctx *ctx;
127 SV *code;
128 } context_vars_wrap;
129  
130 5036 static int context_vars_wrap_free(pTHX_ SV *sv, MAGIC *mg) {
131 5036 context_vars_wrap *w = (context_vars_wrap *)mg->mg_ptr;
132 PERL_UNUSED_ARG(sv);
133 5036 ▶ if (w) {
Branch: 1 branch
BranchTrueFalseTotal
if (w) { 5036 0 5036
134 5036 mg->mg_ptr = NULL;
135 5036 context_vars_release(aTHX_ w->ctx);
136 5036 SvREFCNT_dec(w->code);
137 5036 Safefree(w);
138 }
139 5036 return 0;
140 }
141  
142 static MGVTBL context_vars_wrap_vtbl = {
143 NULL, NULL, NULL, NULL, context_vars_wrap_free, NULL, context_vars_obj_dup, NULL
144 };
145  
146 5037 static XS(context_vars_wrapped) {
147 5037 dXSARGS;
148 5037 context_vars_wrap *w = NULL;
149 MAGIC *mg;
150 I32 count;
151  
152 5037 ▶ for (mg = SvMAGIC((SV *)cv); mg; mg = mg->mg_moremagic)
Branch: 1 branch
BranchTrueFalseTotal
for (mg = SvMAGIC((SV *)cv); mg; mg = mg->mg_moremagic) 5037 0 5037
153 5037 ▶ if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &context_vars_wrap_vtbl) {
Branch: 2 branches
BranchTrueFalseTotal
if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &context_vars_wrap_vtbl) { 5037 0 5037
if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &context_vars_wrap_vtbl) { 5037 0 5037
154 5037 w = (context_vars_wrap *)mg->mg_ptr;
155 5037 break;
156 }
157 5037 ▶ if (!w) croak("Context::Vars: this wrapped callback belongs to another thread");
Branch: 1 branch
BranchTrueFalseTotal
if (!w) croak("Context::Vars: this wrapped callback belongs to another thread"); 0 5037 5037
158  
159 5037 count = context_vars_call_in(aTHX_ w->ctx, 1, w->code, &ST(0), items, GIMME_V, ax);
160 5034 XSRETURN(count);
161 }
162  
163 /* A code reference that runs `code` in a fresh copy of what is current now,
164 * every time it is called. TAKES OVER `c`. */
165 5036 static SV *context_vars_wrap_new(pTHX_ context_vars_ctx *c, SV *code) {
166 5036 CV *xcv = newXS(NULL, context_vars_wrapped, __FILE__);
167 context_vars_wrap *w;
168 MAGIC *mg;
169  
170 5036 Newx(w, 1, context_vars_wrap);
171 5036 w->ctx = c;
172 5036 w->code = SvREFCNT_inc_simple_NN(code);
173 5036 mg = sv_magicext((SV *)xcv, NULL, PERL_MAGIC_ext, &context_vars_wrap_vtbl,
174 (const char *)w, 0);
175 5036 mg->mg_flags |= MGf_DUP;
176 5036 return sv_2mortal(newRV_noinc((SV *)xcv));
177 }
178  
179 /* ---- names ------------------------------------------------------------------ */
180  
181 9 static int context_vars_names_cb(pTHX_ void *ud, const char *key, STRLEN klen,
182 int utf8, SV *value) {
183 PERL_UNUSED_ARG(utf8);
184 PERL_UNUSED_ARG(value);
185 9 av_push((AV *)ud, newSVpvn(key, klen));
186 9 return 0;
187 }
188  
189 #endif
« include/context_vars/context_vars_scope.h ↑ Summary include/context_vars/context_vars_hamt.h »