File Coverage

lib/Object/Pad/SlotAttr/Trigger.xs
Criterion Covered Total %
statement 11 13 84.6
branch 4 6 66.6
condition n/a
subroutine n/a
pod n/a
total 15 19 78.9


line stmt bran cond sub pod time code
1             /* You may distribute under the terms of either the GNU General Public License
2             * or the Artistic License (the same terms as Perl itself)
3             *
4             * (C) Paul Evans, 2021 -- leonerd@leonerd.org.uk
5             */
6             #define PERL_NO_GET_CONTEXT
7              
8             #include "EXTERN.h"
9             #include "perl.h"
10             #include "XSUB.h"
11              
12             #include "object_pad.h"
13              
14             #include "perl-backcompat.c.inc"
15              
16 2           static void trigger_gen_accessor_ops(pTHX_ SlotMeta *slotmeta, SV *hookdata, void *_funcdata, enum AccessorType type, struct AccessorGenerationCtx *ctx)
17             {
18 2 100         if(type != ACCESSOR_WRITER)
19             return;
20              
21             OP *selfop;
22 1           OP *callop = newLISTOP(OP_LIST, 0,
23             selfop = newOP(OP_PADSV, 0), NULL);
24 1 50         callop = op_append_list(OP_LIST, callop,
25             newMETHOP_named(OP_METHOD_NAMED, 0, newSVpvn_share(SvPV_nolen(hookdata), SvCUR(hookdata), 0)));
26              
27 1           selfop->op_targ = PADIX_SELF;
28              
29 1           callop = op_convert_list(OP_ENTERSUB, OPf_STACKED, callop);
30              
31 1           ctx->post_bodyops = op_append_list(OP_LINESEQ, ctx->post_bodyops, callop);
32              
33 1           return;
34             }
35              
36 1           static void trigger_seal(pTHX_ SlotMeta *slotmeta, SV *hookdata, void *_funcdata)
37             {
38 1 50         if(mop_slot_get_attribute(slotmeta, "writer"))
39             return;
40              
41 0           warn("Applying :Trigger attribute to slot %" SVf " is not useful without a :writer",
42 0           SVfARG(mop_slot_get_name(slotmeta)));
43             }
44              
45             static const struct SlotHookFuncs trigger_hooks = {
46             .ver = OBJECTPAD_ABIVERSION,
47             .flags = OBJECTPAD_FLAG_ATTR_MUST_VALUE,
48             .permit_hintkey = "Object::Pad::SlotAttr::Trigger/Trigger",
49              
50             .seal_slot = &trigger_seal,
51             .gen_accessor_ops = &trigger_gen_accessor_ops,
52             };
53              
54             MODULE = Object::Pad::SlotAttr::Trigger PACKAGE = Object::Pad::SlotAttr::Trigger
55              
56             BOOT:
57 2           register_slot_attribute("Trigger", &trigger_hooks, NULL);