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
« xs/context.xs ↑ Summary include/context_vars/context_vars_scope.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 22 static int context_vars_obj_free(pTHX_ SV *sv, MAGIC *mg) {
21 22 context_vars_ctx *c = (context_vars_ctx *)mg->mg_ptr;
22 PERL_UNUSED_ARG(sv);
23 22 ▶ if (c) {
Branch: 1 branch
BranchTrueFalseTotal
if (c) { 22 0 22
24 22 mg->mg_ptr = NULL;
25 22 context_vars_release(aTHX_ c);
26 }
27 22 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 22 static SV *context_vars_obj_new(pTHX_ context_vars_ctx *c) {
43 22 SV *inner = newSV(0);
44 SV *rv;
45 22 MAGIC *mg = sv_magicext(inner, NULL, PERL_MAGIC_ext, &context_vars_obj_vtbl,
46 (const char *)c, 0);
47 22 mg->mg_flags |= MGf_DUP;
48 22 rv = sv_2mortal(newRV_noinc(inner));
49 22 sv_bless(rv, gv_stashpvs("Context::Vars::Context", GV_ADD));
50 22 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 73 static context_vars_ctx *context_vars_obj(pTHX_ SV *sv, const char *what) {
66 MAGIC *mg;
67 73 ▶ if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { 64 9 73
if (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG) { 64 0 64
68 64 ▶ for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) {
Branch: 1 branch
BranchTrueFalseTotal
for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) { 52 12 64
69 52 ▶ 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) 52 0 52
if (mg->mg_type != PERL_MAGIC_ext || mg->mg_virtual != &context_vars_obj_vtbl) 0 52 52
70 0 continue;
71 52 ▶ if (!mg->mg_ptr)
Branch: 1 branch
BranchTrueFalseTotal
if (!mg->mg_ptr) 0 52 52
72 0 croak("%s: this context belongs to another thread", what);
73 52 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 5064 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 5064 dSP;
91 5064 SV **argv = NULL;
92 I32 i, count;
93  
94 5064 ENTER;
95 5064 ▶ if (n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) { 5 5059 5064
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 5064 ▶ if (copy) context_vars_enter_copy(aTHX_ c);
Branch: 1 branch
BranchTrueFalseTotal
if (copy) context_vars_enter_copy(aTHX_ c); 5038 26 5064
102 26 else context_vars_enter(aTHX_ c);
103  
104 5064 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 5064 5064
105 5064 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 5064 0 5064
EXTEND(SP, n); 0 5064 5064
106 5073 ▶ for (i = 0; i < n; i++) PUSHs(argv[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) PUSHs(argv[i]); 9 5064 5073
107 5064 PUTBACK;
108  
109 5064 count = call_sv(code, gimme);
110  
111 5060 SPAGAIN;
112 10092 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 5032 5060 10092
113 5032 SV *r = SP[i - count + 1];
114 5032 ▶ 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 5032 5032
115 }
116 5060 PL_stack_sp = PL_stack_base + ax + count - 1;
117 5060 LEAVE;
118 5060 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 5038 static XS(context_vars_wrapped) {
147 5038 dXSARGS;
148 5038 context_vars_wrap *w = NULL;
149 MAGIC *mg;
150 I32 count;
151  
152 5038 ▶ for (mg = SvMAGIC((SV *)cv); mg; mg = mg->mg_moremagic)
Branch: 1 branch
BranchTrueFalseTotal
for (mg = SvMAGIC((SV *)cv); mg; mg = mg->mg_moremagic) 5038 0 5038
153 5038 ▶ 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) { 5038 0 5038
if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &context_vars_wrap_vtbl) { 5038 0 5038
154 5038 w = (context_vars_wrap *)mg->mg_ptr;
155 5038 break;
156 }
157 5038 ▶ 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 5038 5038
158  
159 5038 count = context_vars_call_in(aTHX_ w->ctx, 1, w->code, &ST(0), items, GIMME_V, ax);
160 5035 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 5037 static SV *context_vars_wrap_new(pTHX_ context_vars_ctx *c, SV *code) {
166 5037 CV *xcv = newXS(NULL, context_vars_wrapped, __FILE__);
167 context_vars_wrap *w;
168 MAGIC *mg;
169  
170 5037 Newx(w, 1, context_vars_wrap);
171 5037 w->ctx = c;
172 5037 w->code = SvREFCNT_inc_simple_NN(code);
173 5037 mg = sv_magicext((SV *)xcv, NULL, PERL_MAGIC_ext, &context_vars_wrap_vtbl,
174 (const char *)w, 0);
175 5037 mg->mg_flags |= MGf_DUP;
176 5037 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
« xs/context.xs ↑ Summary include/context_vars/context_vars_scope.h »