xs/mcp.xs

filter: Statementstmt 96.4% 189 / 196 Branchbran 66.8% 143 / 214 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.9% 332 / 410 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/pmcp/pmcp_auth.h ↑ Summary include/pmcp/pmcp_stdio.h »
1 MODULE = Punk::Plugin::MCP PACKAGE = Punk::Plugin::MCP
2  
3 PROTOTYPES: DISABLE
4  
5 # The plugin object. It carries no state: everything lives on the application.
6  
7 SV *
8 new(class)
9 SV *class
10 CODE:
11 {
12 199 const char *pkg = PMCP_WHO;
13 199 STRLEN pkgl = sizeof(PMCP_WHO) - 1;
14 398 ▶ if (SvOK(class) && !SvROK(class)) pkg = SvPV(class, pkgl);
Branch: 2 branches
BranchTrueFalseTotal
if (SvOK(class) && !SvROK(class)) pkg = SvPV(class, pkgl); 199 0 199
if (SvOK(class) && !SvROK(class)) pkg = SvPV(class, pkgl); 199 0 199
15 199 RETVAL = sv_bless(newRV_noinc((SV *)newHV()),
16 gv_stashpvn(pkg, (I32)pkgl, GV_ADD));
17 }
18 OUTPUT:
19 RETVAL
20  
21 # Installs the `mcp` keyword at compile time, the only moment that makes the
22 # bareword form parse. `plugin 'MCP'` runs when the package body runs, after
23 # perl has already parsed (or refused) every `mcp tool => ...` line. Silent in a
24 # package that is not a Punk application.
25  
26 void
27 import(class, ...)
28 SV *class
29 CODE:
30 {
31 171 ▶ const char *pkg = CopSTASHPV(PL_curcop);
Branch: 7 branches
BranchTrueFalseTotal
const char *pkg = CopSTASHPV(PL_curcop); 171 0 171
const char *pkg = CopSTASHPV(PL_curcop); 171 0 171
const char *pkg = CopSTASHPV(PL_curcop); 171 0 171
const char *pkg = CopSTASHPV(PL_curcop); 0 171 171
const char *pkg = CopSTASHPV(PL_curcop); 0 0 0
const char *pkg = CopSTASHPV(PL_curcop); 171 0 171
const char *pkg = CopSTASHPV(PL_curcop); 0 171 171
32 SV *pkgsv, *app;
33 PERL_UNUSED_VAR(class);
34 {
35 /* `use Punk::Plugin::MCP ':command'`, from Punk/Command/Mcp.pm:
36 * the command and the doctor row, and nothing installed (13) */
37 I32 a;
38 171 ▶ for (a = 1; a < items; a++)
Branch: 1 branch
BranchTrueFalseTotal
for (a = 1; a < items; a++) 6 165 171
39 12 ▶ if (SvOK(ST(a)) && strEQ(SvPV_nolen(ST(a)), ":command")) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvOK(ST(a)) && strEQ(SvPV_nolen(ST(a)), ":command")) { 6 0 6
if (SvOK(ST(a)) && strEQ(SvPV_nolen(ST(a)), ":command")) { 6 0 6
40 6 pmcp_command_register(aTHX);
41 6 XSRETURN_EMPTY;
42 }
43 }
44 165 ▶ if (!pkg) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (!pkg) XSRETURN_EMPTY; 0 165 165
45 165 pkgsv = sv_2mortal(newSVpv(pkg, 0));
46 165 ▶ if (!pmcp_can(aTHX_ pkgsv, "punk_app")) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_can(aTHX_ pkgsv, "punk_app")) XSRETURN_EMPTY; 1 164 165
47 164 app = pmcp_call_method(aTHX_ pkgsv, "punk_app", NULL, 0, G_SCALAR, NULL);
48 164 sv_2mortal(app);
49 164 ▶ if (pmcp_is_hash(app)) pmcp_install(aTHX_ app);
Branch: 1 branch
BranchTrueFalseTotal
if (pmcp_is_hash(app)) pmcp_install(aTHX_ app); 164 0 164
50 164 XSRETURN_EMPTY;
51 }
52  
53 void
54 register(self, app, opts = &PL_sv_undef)
55 SV *self
56 SV *app
57 SV *opts
58 CODE:
59 PERL_UNUSED_VAR(self);
60 199 ▶ if (!pmcp_is_hash(app))
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_is_hash(app)) 0 199 199
61 0 croak("%s: register needs the Punk::App", PMCP_WHO);
62 199 pmcp_register(aTHX_ app, opts);
63  
64 # state_for($class): what the application declared and froze, or undef.
65  
66 SV *
67 state_for(self, class)
68 SV *self
69 SV *class
70 CODE:
71 {
72 16 SV *app = NULL;
73 PERL_UNUSED_VAR(self);
74 16 RETVAL = &PL_sv_undef;
75 16 ▶ if (pmcp_can(aTHX_ class, "punk_app")) {
Branch: 1 branch
BranchTrueFalseTotal
if (pmcp_can(aTHX_ class, "punk_app")) { 15 1 16
76 15 app = sv_2mortal(pmcp_call_method(aTHX_ class, "punk_app", NULL, 0,
77 G_SCALAR, NULL));
78 15 ▶ if (pmcp_is_hash(app)
Branch: 1 branch
BranchTrueFalseTotal
if (pmcp_is_hash(app) 15 0 15
79 15 ▶ && pmcp_hget(aTHX_ (HV *)SvRV(app), "mcp"))
Branch: 1 branch
BranchTrueFalseTotal
&& pmcp_hget(aTHX_ (HV *)SvRV(app), "mcp")) 14 1 15
80 14 RETVAL = pmcp_state_view(aTHX_ app);
81 }
82 16 ▶ if (RETVAL == &PL_sv_undef) SvREFCNT_inc_simple_void_NN(RETVAL);
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL == &PL_sv_undef) SvREFCNT_inc_simple_void_NN(RETVAL); 2 14 16
83 }
84 OUTPUT:
85 RETVAL
86  
87 # Test only: a message dispatched against an application's frozen server.
88  
89 void
90 _dispatch_app(class, bytes, protocol = &PL_sv_undef)
91 SV *class
92 SV *bytes
93 SV *protocol
94 PPCODE:
95 {
96 1 SV *app = sv_2mortal(pmcp_call_method(aTHX_ class, "punk_app", NULL, 0,
97 G_SCALAR, NULL));
98 1 HV *st = pmcp_state(aTHX_ app);
99 1 pmcp_server *ps = pmcp_server_of(aTHX_ pmcp_hget(aTHX_ st, "server"));
100 pmcp_req r;
101 SV *reply;
102 1 int status = 0;
103 1 enum pmcp_ver v = PMCP_V_2025_03_26;
104 1 ▶ if (!ps) croak("%s: %" SVf " is not compiled", PMCP_WHO, SVfARG(class));
Branch: 1 branch
BranchTrueFalseTotal
if (!ps) croak("%s: %" SVf " is not compiled", PMCP_WHO, SVfARG(class)); 0 1 1
105 1 ▶ if (SvOK(protocol)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(protocol)) { 0 1 1
106 STRLEN l;
107 0 const char *p = SvPV_const(protocol, l);
108 0 v = pmcp_version_of(p, l);
109 }
110 1 pmcp_req_init(&r, v, 0);
111 1 reply = pmcp_dispatch(aTHX_ &ps->srv, &r, bytes, &status);
112 1 SP = PL_stack_base + ax - 1;
113 1 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 1 1
114 1 mPUSHs(newSViv(status));
115 1 ▶ if (reply) mPUSHs(reply); else PUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
if (reply) mPUSHs(reply); else PUSHs(&PL_sv_undef); 1 0 1
116 1 pmcp_req_free(aTHX_ &r);
117 }
118  
119 const char *
120 _dist()
121 CODE:
122 1 RETVAL = PMCP_DIST;
123 OUTPUT:
124 RETVAL
125  
126 SV *
127 _rand_id()
128 CODE:
129 1000 RETVAL = pmcp_rand_id(aTHX);
130 OUTPUT:
131 RETVAL
132  
133 SV *
134 _abi_versions()
135 CODE:
136 {
137 2 HV *h = newHV();
138 2 ▶ (void)hv_stores(h, "frj", newSViv(PMCP_FRJ ? PMCP_FRJ->abi_version : 0));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(h, "frj", newSViv(PMCP_FRJ ? PMCP_FRJ->abi_version : 0)); 2 0 2
139 2 ▶ (void)hv_stores(h, "jsf", newSViv(PMCP_JSF ? PMCP_JSF->abi_version : 0));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(h, "jsf", newSViv(PMCP_JSF ? PMCP_JSF->abi_version : 0)); 2 0 2
140 2 RETVAL = newRV_noinc((SV *)h);
141 }
142 OUTPUT:
143 RETVAL
144  
145 int
146 _abi_check(module, version)
147 const char *module
148 SV *version
149 CODE:
150 {
151 5 int need = strEQ(module, "JSON::Schema::Fast") ? JSF_ABI_VERSION
152 : FRJ_ABI_VERSION;
153 5 ▶ const char *since = strEQ(module, "JSON::Schema::Fast")
Branch: 1 branch
BranchTrueFalseTotal
const char *since = strEQ(module, "JSON::Schema::Fast") 1 4 5
154 ? PMCP_JSF_SINCE : PMCP_FRJ_SINCE;
155 5 UV found = SvOK(version) ? 1 : 0;
156 6 ▶ pmcp_abi_check(aTHX_ module, found, found ? (int)SvIV(version) : 0,
Branch: 2 branches
BranchTrueFalseTotal
pmcp_abi_check(aTHX_ module, found, found ? (int)SvIV(version) : 0, 1 4 5
pmcp_abi_check(aTHX_ module, found, found ? (int)SvIV(version) : 0, 4 1 5
157 need, since,
158 1 found ? NULL : sv_2mortal(newSVpvs("faked by t/02")));
159 2 ▶ RETVAL = 1;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = 1; 0 2 2
160 }
161 OUTPUT:
162 RETVAL
163  
164 SV *
165 _abi_selftest()
166 CODE:
167 {
168 static const char doc[] = "{\"b\":[1,2,3],\"a\":\"x\"}";
169 static const char schema[] = "{\"type\":\"object\",\"required\":[\"a\"],"
170 "\"properties\":{\"a\":{\"type\":\"string\"}}}";
171 1 HV *out = newHV();
172 frj_opts o;
173 SV *data, *json, *compiled, *bad;
174 1 AV *errors = (AV *)sv_2mortal((SV *)newAV());
175  
176 1 PMCP_FRJ->opts_init(&o);
177 1 o.canonical = 1;
178 1 data = sv_2mortal(PMCP_FRJ->decode(aTHX_ doc, sizeof doc - 1, &o));
179 1 json = PMCP_FRJ->encode(aTHX_ data, &o);
180 1 (void)hv_stores(out, "decoded_hash", newSViv(pmcp_is_hash(data)));
181 1 (void)hv_stores(out, "json", json);
182  
183 1 compiled = sv_2mortal(PMCP_JSF->compile(aTHX_
184 sv_2mortal(newSVpvn(schema, sizeof schema - 1)),
185 NULL, 0, 0));
186 1 bad = sv_2mortal(newRV_noinc((SV *)newHV()));
187 1 (void)hv_stores(out, "valid",
188 newSViv(PMCP_JSF->is_valid(aTHX_ compiled, data)));
189 1 (void)hv_stores(out, "invalid",
190 newSViv(PMCP_JSF->validate(aTHX_ compiled, bad, errors)));
191 1 (void)hv_stores(out, "errors", newSViv(av_len(errors) + 1));
192 1 RETVAL = newRV_noinc((SV *)out);
193 }
194 OUTPUT:
195 RETVAL
196  
197 void
198 _call(target, inv, want_list, ...)
199 SV *target
200 SV *inv
201 int want_list
202 PPCODE:
203 {
204 12 SV *err = NULL;
205 SV *ret;
206 12 ▶ SV **argv = items > 3 ? &ST(3) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **argv = items > 3 ? &ST(3) : NULL; 5 7 12
207 12 int argc = items - 3;
208 12 ▶ I32 flags = want_list ? G_LIST : G_SCALAR;
Branch: 1 branch
BranchTrueFalseTotal
I32 flags = want_list ? G_LIST : G_SCALAR; 3 9 12
209  
210 12 ▶ if (pmcp_is_code(target))
Branch: 1 branch
BranchTrueFalseTotal
if (pmcp_is_code(target)) 8 4 12
211 8 ▶ ret = pmcp_call(aTHX_ SvOK(inv) ? inv : NULL, NULL, target,
Branch: 1 branch
BranchTrueFalseTotal
ret = pmcp_call(aTHX_ SvOK(inv) ? inv : NULL, NULL, target, 0 8 8
212 argv, argc, flags, &err);
213 else
214 4 ret = pmcp_call_method(aTHX_ inv, SvPV_nolen(target),
215 argv, argc, flags, &err);
216 /* the call may have reallocated the stack */
217 12 SP = PL_stack_base + ax - 1;
218 12 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 12 12
219 12 ▶ if (ret) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret) { 10 2 12
220 10 mPUSHs(newSViv(1));
221 10 mPUSHs(ret);
222 }
223 else {
224 2 mPUSHs(newSViv(0));
225 2 mPUSHs(err);
226 }
227 }
228  
229 SV *
230 _call_plain(code, ...)
231 SV *code
232 CODE:
233 2 ▶ RETVAL = pmcp_call_sv(aTHX_ code, items > 1 ? &ST(1) : NULL,
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = pmcp_call_sv(aTHX_ code, items > 1 ? &ST(1) : NULL, 1 1 2
234 items - 1, G_SCALAR, NULL);
235 OUTPUT:
236 RETVAL
237  
238 void
239 _require(module)
240 const char *module
241 PPCODE:
242 {
243 2 SV *err = NULL;
244 2 int ok = pmcp_require(aTHX_ module, &err);
245 2 SP = PL_stack_base + ax - 1;
246 2 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 2 2
247 2 mPUSHs(newSViv(ok));
248 2 ▶ if (err) mPUSHs(err);
Branch: 1 branch
BranchTrueFalseTotal
if (err) mPUSHs(err); 1 1 2
249 1 else PUSHs(&PL_sv_undef);
250 }
251  
252 SV *
253 _closure(value)
254 SV *value
255 CODE:
256 {
257 2 AV *cap = newAV();
258 2 av_push(cap, newSVsv(value));
259 2 RETVAL = pmcp_closure(aTHX_ pmcp_clos_echo, cap);
260 }
261 OUTPUT:
262 RETVAL
263  
264 void
265 _croak_sv(sv)
266 SV *sv
267 CODE:
268 3 pmcp_croak_sv(aTHX_ sv);
269  
270 int
271 _is_array(sv)
272 SV *sv
273 CODE:
274 3 RETVAL = pmcp_is_array(sv);
275 OUTPUT:
276 RETVAL
277  
278 void
279 _dispatch(state, bytes)
280 SV *state
281 SV *bytes
282 PPCODE:
283 {
284 84 ▶ HV *h = pmcp_is_hash(state) ? (HV *)SvRV(state) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *h = pmcp_is_hash(state) ? (HV *)SvRV(state) : NULL; 84 0 84
285 pmcp_srv s;
286 pmcp_req r;
287 SV **e, *reply;
288 84 int status = 0;
289 84 enum pmcp_ver v = PMCP_V_2025_03_26;
290  
291 84 ▶ if (!h) croak(PMCP_DIST ": _dispatch needs a state hash");
Branch: 1 branch
BranchTrueFalseTotal
if (!h) croak(PMCP_DIST ": _dispatch needs a state hash"); 0 84 84
292 84 Zero(&s, 1, pmcp_srv);
293 #define PMCP_T_SV(k, f) \
294 if ((e = hv_fetchs(h, k, 0)) && SvOK(*e)) s.f = *e;
295 #define PMCP_T_BIT(k, f) \
296 if ((e = hv_fetchs(h, k, 0)) && SvTRUE(*e)) s.f = 1;
297 84 ▶ PMCP_T_SV("name", name)
Branch: 2 branches
BranchTrueFalseTotal
PMCP_T_SV("name", name) 4 80 84
PMCP_T_SV("name", name) 4 0 4
298 84 ▶ PMCP_T_SV("title", title)
Branch: 2 branches
BranchTrueFalseTotal
PMCP_T_SV("title", title) 3 81 84
PMCP_T_SV("title", title) 3 0 3
299 84 ▶ PMCP_T_SV("version", version)
Branch: 2 branches
BranchTrueFalseTotal
PMCP_T_SV("version", version) 4 80 84
PMCP_T_SV("version", version) 4 0 4
300 84 ▶ PMCP_T_SV("description", description)
Branch: 2 branches
BranchTrueFalseTotal
PMCP_T_SV("description", description) 3 81 84
PMCP_T_SV("description", description) 3 0 3
301 84 ▶ PMCP_T_SV("website", website)
Branch: 2 branches
BranchTrueFalseTotal
PMCP_T_SV("website", website) 3 81 84
PMCP_T_SV("website", website) 3 0 3
302 84 ▶ PMCP_T_SV("icons", icons)
Branch: 2 branches
BranchTrueFalseTotal
PMCP_T_SV("icons", icons) 3 81 84
PMCP_T_SV("icons", icons) 3 0 3
303 84 ▶ PMCP_T_SV("instructions", instructions)
Branch: 2 branches
BranchTrueFalseTotal
PMCP_T_SV("instructions", instructions) 1 83 84
PMCP_T_SV("instructions", instructions) 1 0 1
304 84 ▶ PMCP_T_BIT("tools", has_tools)
Branch: 2 branches
BranchTrueFalseTotal
PMCP_T_BIT("tools", has_tools) 2 82 84
PMCP_T_BIT("tools", has_tools) 2 0 2
305 84 ▶ PMCP_T_BIT("resources", has_resources)
Branch: 2 branches
BranchTrueFalseTotal
PMCP_T_BIT("resources", has_resources) 3 81 84
PMCP_T_BIT("resources", has_resources) 3 0 3
306 84 ▶ PMCP_T_BIT("prompts", has_prompts)
Branch: 2 branches
BranchTrueFalseTotal
PMCP_T_BIT("prompts", has_prompts) 2 82 84
PMCP_T_BIT("prompts", has_prompts) 2 0 2
307 84 ▶ PMCP_T_BIT("logging", logging)
Branch: 2 branches
BranchTrueFalseTotal
PMCP_T_BIT("logging", logging) 3 81 84
PMCP_T_BIT("logging", logging) 3 0 3
308 84 ▶ PMCP_T_BIT("notify", can_notify)
Branch: 2 branches
BranchTrueFalseTotal
PMCP_T_BIT("notify", can_notify) 3 81 84
PMCP_T_BIT("notify", can_notify) 3 0 3
309 #undef PMCP_T_SV
310 #undef PMCP_T_BIT
311 84 ▶ if ((e = hv_fetchs(h, "protocol", 0)) && SvOK(*e)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((e = hv_fetchs(h, "protocol", 0)) && SvOK(*e)) { 0 84 84
if ((e = hv_fetchs(h, "protocol", 0)) && SvOK(*e)) { 0 0 0
312 STRLEN l;
313 0 const char *p = SvPV_const(*e, l);
314 0 v = pmcp_version_of(p, l);
315 }
316 84 e = hv_fetchs(h, "in_session", 0);
317 84 ▶ pmcp_req_init(&r, v, e && SvTRUE(*e));
Branch: 2 branches
BranchTrueFalseTotal
pmcp_req_init(&r, v, e && SvTRUE(*e)); 2 82 84
pmcp_req_init(&r, v, e && SvTRUE(*e)); 2 0 2
318  
319 84 reply = pmcp_dispatch(aTHX_ &s, &r, bytes, &status);
320  
321 /* the dispatch ran Perl, which may have moved the stack */
322 84 SP = PL_stack_base + ax - 1;
323 84 ▶ EXTEND(SP, 4);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 4); 0 84 84
324 84 mPUSHs(newSViv(status));
325 84 ▶ if (reply) mPUSHs(reply); else PUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
if (reply) mPUSHs(reply); else PUSHs(&PL_sv_undef); 72 12 84
326 84 ▶ if (r.negotiated) mPUSHs(newSVpv(PMCP_VER_NAME[r.negotiated], 0));
Branch: 1 branch
BranchTrueFalseTotal
if (r.negotiated) mPUSHs(newSVpv(PMCP_VER_NAME[r.negotiated], 0)); 24 60 84
327 60 else PUSHs(&PL_sv_undef);
328 84 ▶ if (r.cancelled) mPUSHs(newSVsv(r.cancelled)); else PUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
if (r.cancelled) mPUSHs(newSVsv(r.cancelled)); else PUSHs(&PL_sv_undef); 2 82 84
329 84 pmcp_req_free(aTHX_ &r);
330 }
331  
332 int
333 _method_found(name)
334 SV *name
335 CODE:
336 {
337 STRLEN l;
338 17 const char *p = SvPV_const(name, l);
339 17 ▶ RETVAL = pmcp_method_find(p, l) != NULL;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = pmcp_method_find(p, l) != NULL; 15 2 17
340 }
341 OUTPUT:
342 RETVAL
343  
344 void
345 _methods()
346 PPCODE:
347 {
348 size_t i;
349 1 ▶ EXTEND(SP, (SSize_t)PMCP_N_METHODS);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, (SSize_t)PMCP_N_METHODS); 0 1 1
350 15 ▶ for (i = 0; i < PMCP_N_METHODS; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < PMCP_N_METHODS; i++) 14 1 15
351 14 mPUSHs(newSVpvn(PMCP_METHODS[i].name, PMCP_METHODS[i].len));
352 }
353  
354 MODULE = Punk::Plugin::MCP PACKAGE = Punk::Plugin::MCP::Server
355  
356 PROTOTYPES: DISABLE
357  
358 void
359 DESTROY(self)
360 SV *self
361 CODE:
362 {
363 139 pmcp_server *ps = pmcp_server_of(aTHX_ self);
364 139 ▶ if (ps) {
Branch: 1 branch
BranchTrueFalseTotal
if (ps) { 139 0 139
365 139 sv_setiv(SvRV(self), 0);
366 139 pmcp_server_free(aTHX_ ps);
367 }
368 }
369  
370 # A pointer object must not be copied into a new thread: two copies would free
371 # one struct twice.
372  
373 int
374 CLONE_SKIP(...)
375 CODE:
376 PERL_UNUSED_VAR(items);
377 0 ▶ RETVAL = 1;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = 1; 0 0 0
378 OUTPUT:
379 RETVAL
380  
381 MODULE = Punk::Plugin::MCP PACKAGE = Punk::Plugin::MCP
382  
383 # Test only, until 07 routes it: one message dispatched against the frozen
384 # server of $c's application, with $c as the request.
385  
386 void
387 _rpc(c, bytes, protocol = &PL_sv_undef, session = &PL_sv_undef)
388 SV *c
389 SV *bytes
390 SV *protocol
391 SV *session
392 PPCODE:
393 {
394 146 SV *app = sv_2mortal(pmcp_call_method(aTHX_ c, "app", NULL, 0,
395 G_SCALAR, NULL));
396 146 HV *st = pmcp_state(aTHX_ app);
397 146 pmcp_server *ps = pmcp_server_of(aTHX_ pmcp_hget(aTHX_ st, "server"));
398 pmcp_req r;
399 SV *reply;
400 146 int status = 0;
401 146 enum pmcp_ver v = PMCP_V_2025_11_25;
402 146 ▶ if (!ps) croak("%s: the application is not compiled", PMCP_WHO);
Branch: 1 branch
BranchTrueFalseTotal
if (!ps) croak("%s: the application is not compiled", PMCP_WHO); 0 146 146
403 146 ▶ if (SvOK(protocol)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(protocol)) { 138 8 146
404 STRLEN l;
405 138 const char *p = SvPV_const(protocol, l);
406 138 v = pmcp_version_of(p, l);
407 }
408 146 pmcp_req_init(&r, v, 0);
409 146 r.c = c;
410 146 ▶ if (SvOK(session)) r.session = session;
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(session)) r.session = session; 2 144 146
411 146 reply = pmcp_dispatch(aTHX_ &ps->srv, &r, bytes, &status);
412 146 SP = PL_stack_base + ax - 1;
413 146 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 146 146
414 146 mPUSHs(newSViv(status));
415 146 ▶ if (reply) mPUSHs(reply); else PUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
if (reply) mPUSHs(reply); else PUSHs(&PL_sv_undef); 138 8 146
416 146 pmcp_req_free(aTHX_ &r);
417 }
418  
419 MODULE = Punk::Plugin::MCP PACKAGE = Punk::MCP::Call
420  
421 PROTOTYPES: DISABLE
422  
423 # The call object $c->mcp returns. Read-only accessors over its keys.
424  
425 SV *
426 method(self)
427 SV *self
428 ALIAS:
429 name = 1
430 arguments = 2
431 protocol = 3
432 progress_token = 4
433 cancelled = 5
434 session = 6
435 client = 7
436 CODE:
437 {
438 static const char *const keys[] = { "method", "name", "arguments",
439 "protocol", "progress_token", "cancelled", "session", "client" };
440 22 SV **e = pmcp_is_hash(self)
441 22 ? hv_fetch((HV *)SvRV(self), keys[ix], (I32)strlen(keys[ix]), 0)
442 22 ▶ : NULL;
Branch: 1 branch
BranchTrueFalseTotal
: NULL; 22 0 22
443 22 ▶ RETVAL = (e && *e) ? newSVsv(*e) : newSV(0);
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = (e && *e) ? newSVsv(*e) : newSV(0); 21 1 22
RETVAL = (e && *e) ? newSVsv(*e) : newSV(0); 21 0 21
444 }
445 OUTPUT:
446 RETVAL
447  
448 MODULE = Punk::Plugin::MCP PACKAGE = Punk::Plugin::MCP
449  
450 # Test only: how many calls are registered as streaming on this worker.
451  
452 IV
453 _live_count()
454 CODE:
455 4 ▶ RETVAL = PMCP_LIVE ? (IV)HvUSEDKEYS(PMCP_LIVE) : 0;
Branch: 3 branches
BranchTrueFalseTotal
RETVAL = PMCP_LIVE ? (IV)HvUSEDKEYS(PMCP_LIVE) : 0; 4 0 4
RETVAL = PMCP_LIVE ? (IV)HvUSEDKEYS(PMCP_LIVE) : 0; 0 4 4
RETVAL = PMCP_LIVE ? (IV)HvUSEDKEYS(PMCP_LIVE) : 0; 2 2 4
456 OUTPUT:
457 RETVAL
458  
459 MODULE = Punk::Plugin::MCP PACKAGE = Punk::Plugin::MCP
460  
461 # Punk::Plugin::MCP->resource_updated($class, $uri): tell the sessions that
462 # subscribe to $uri, from anywhere (a queue job, a cron), with no request.
463 # Returns what publish said: 1 on the ring, 0 this worker only, -1 too big.
464  
465 int
466 resource_updated(self, class, uri)
467 SV *self
468 SV *class
469 SV *uri
470 CODE:
471 {
472 6 SV *app = NULL;
473 HV *st;
474 pmcp_server *ps;
475 PERL_UNUSED_VAR(self);
476 6 ▶ if (!SvOK(uri) || SvROK(uri))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(uri) || SvROK(uri)) 6 0 6
if (!SvOK(uri) || SvROK(uri)) 0 6 6
477 0 croak("%s: resource_updated takes the application class and a URI", PMCP_WHO);
478 6 ▶ if (pmcp_can(aTHX_ class, "punk_app"))
Branch: 1 branch
BranchTrueFalseTotal
if (pmcp_can(aTHX_ class, "punk_app")) 6 0 6
479 6 app = sv_2mortal(pmcp_call_method(aTHX_ class, "punk_app", NULL, 0, G_SCALAR, NULL));
480 6 ▶ st = pmcp_is_hash(app) ? pmcp_state(aTHX_ app) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
st = pmcp_is_hash(app) ? pmcp_state(aTHX_ app) : NULL; 6 0 6
481 6 ▶ ps = st ? pmcp_server_of(aTHX_ pmcp_hget(aTHX_ st, "server")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
ps = st ? pmcp_server_of(aTHX_ pmcp_hget(aTHX_ st, "server")) : NULL; 6 0 6
482 6 ▶ if (!ps) croak("%s: %" SVf " has no compiled MCP server", PMCP_WHO, SVfARG(class));
Branch: 1 branch
BranchTrueFalseTotal
if (!ps) croak("%s: %" SVf " has no compiled MCP server", PMCP_WHO, SVfARG(class)); 0 6 6
483 12 ▶ RETVAL = (pmcp_sessions_on(aTHX_ ps->opts) || PMCP_STDIO.active)
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = (pmcp_sessions_on(aTHX_ ps->opts) || PMCP_STDIO.active) 0 0 0
RETVAL = (pmcp_sessions_on(aTHX_ ps->opts) || PMCP_STDIO.active) 0 6 6
484 6 ▶ ? pmcp_bus_send(aTHX_ ps, app, PMCP_BUS_UPDATED, NULL, uri) : 0;
Branch: 1 branch
BranchTrueFalseTotal
? pmcp_bus_send(aTHX_ ps, app, PMCP_BUS_UPDATED, NULL, uri) : 0; 0 6 6
485 }
486 OUTPUT:
487 RETVAL
488  
489 # ---- test only: the bus and the stream table -----------------------------------
490  
491 SV *
492 _bus_encode(kind, pid, sid, uri)
493 int kind
494 UV pid
495 SV *sid
496 SV *uri
497 CODE:
498 6 ▶ RETVAL = pmcp_bus_encode(aTHX_ kind, (U32)pid, SvOK(sid) ? sid : NULL,
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = pmcp_bus_encode(aTHX_ kind, (U32)pid, SvOK(sid) ? sid : NULL, 6 0 6
499 6 ▶ SvOK(uri) ? uri : NULL);
Branch: 1 branch
BranchTrueFalseTotal
SvOK(uri) ? uri : NULL); 4 2 6
500 6 ▶ if (!RETVAL) RETVAL = newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
if (!RETVAL) RETVAL = newSV(0); 1 5 6
501 OUTPUT:
502 RETVAL
503  
504 void
505 _bus_decode(payload)
506 SV *payload
507 PPCODE:
508 {
509 int kind;
510 U32 pid;
511 SV *sid, *uri;
512 3 ▶ if (pmcp_bus_decode(aTHX_ payload, &kind, &pid, &sid, &uri)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pmcp_bus_decode(aTHX_ payload, &kind, &pid, &sid, &uri)) { 1 2 3
513 1 ▶ EXTEND(SP, 4);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 4); 0 1 1
514 1 mPUSHs(newSViv(kind));
515 1 mPUSHs(newSVuv(pid));
516 1 PUSHs(sid);
517 1 PUSHs(uri);
518 }
519 }
520  
521 void
522 _bus_receive(class, payload)
523 SV *class
524 SV *payload
525 CODE:
526 {
527 3 SV *app = sv_2mortal(pmcp_call_method(aTHX_ class, "punk_app", NULL, 0, G_SCALAR, NULL));
528 3 SV *server = pmcp_hget(aTHX_ pmcp_state(aTHX_ app), "server");
529 3 AV *cap = newAV();
530 SV *cb, *argv[2], *r;
531 3 av_push(cap, newSVsv(server));
532 3 cb = sv_2mortal(pmcp_closure(aTHX_ pmcp_bus_cb, cap));
533 3 argv[0] = sv_2mortal(newSVpvs("pmcp"));
534 3 argv[1] = payload;
535 3 r = pmcp_call_sv(aTHX_ cb, argv, 2, G_SCALAR, NULL);
536 3 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 3 0 3
537 }
538  
539 void
540 _stream_register(class, c, sid, stream)
541 SV *class
542 SV *c
543 SV *sid
544 SV *stream
545 CODE:
546 {
547 7 SV *app = sv_2mortal(pmcp_call_method(aTHX_ class, "punk_app", NULL, 0, G_SCALAR, NULL));
548 7 SV *server = pmcp_hget(aTHX_ pmcp_state(aTHX_ app), "server");
549 7 pmcp_stream_register(aTHX_ server, c, sid, stream, c);
550 }
551  
552 IV
553 _stream_count()
554 CODE:
555 6 ▶ RETVAL = PMCP_STREAMS ? (IV)HvUSEDKEYS(PMCP_STREAMS) : 0;
Branch: 3 branches
BranchTrueFalseTotal
RETVAL = PMCP_STREAMS ? (IV)HvUSEDKEYS(PMCP_STREAMS) : 0; 6 0 6
RETVAL = PMCP_STREAMS ? (IV)HvUSEDKEYS(PMCP_STREAMS) : 0; 0 6 6
RETVAL = PMCP_STREAMS ? (IV)HvUSEDKEYS(PMCP_STREAMS) : 0; 0 6 6
556 OUTPUT:
557 RETVAL
558  
559 MODULE = Punk::Plugin::MCP PACKAGE = Punk::Plugin::MCP
560  
561 # Test only: the audience check wrapped round an introspection checker, given
562 # any checker coderef and the audiences it must name.
563  
564 SV *
565 _aud_checker(checker, auds)
566 SV *checker
567 SV *auds
568 CODE:
569 {
570 1 AV *cap = newAV();
571 1 av_push(cap, newSVsv(checker));
572 1 av_push(cap, newSVsv(auds));
573 1 RETVAL = pmcp_closure(aTHX_ pmcp_aud_checker, cap);
574 }
575 OUTPUT:
576 RETVAL
« include/pmcp/pmcp_auth.h ↑ Summary include/pmcp/pmcp_stdio.h »